How to use mock method of CompositeExpectation class

Best Mockery code snippet using CompositeExpectation.mock

UnitSeleniumTestCase.php

Source:UnitSeleniumTestCase.php Github

copy

Full Screen

1<?php2namespace Sepehr\PHPUnitSelenium\Tests\Unit;3use Mockery;4use Mockery\MockInterface;5use phpmock\mockery\PHPMockery;6use Mockery\CompositeExpectation;7use Sepehr\PHPUnitSelenium\Util\Locator;8use Sepehr\PHPUnitSelenium\Util\Filesystem;9use Sepehr\PHPUnitSelenium\SeleniumTestCase;10use Facebook\WebDriver\Remote\RemoteWebDriver;11use Sepehr\PHPUnitSelenium\WebDriver\WebDriverBy;12use Facebook\WebDriver\Remote\DesiredCapabilities;13use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;14/**15 * Base class for all unit tests that deal with test doubles.16 *17 * This may seem a little bit complicated, and in fact, it is! But in return18 * it provides an easy-to-use API to write unit tests as fast as possible. For19 * example, let's inject a dependency mock into the SUT:20 *21 * $this->inject($this->spy(RemoteWebDriver::class));22 *23 * Or better:24 *25 * $this->inject(26 * $this->mock('overload:' . DesiredCapabilities::class)27 * ->shouldReceive('create')28 * ->once()29 * );30 *31 * Or even better:32 *33 * $this->inject(WebDriverBy::class)34 * ->shouldReceive('create')35 * ->once()36 * ->withAnyArgs()37 * ->andReturn(Mockery::slef());38 *39 * How cool is that?!40 */41abstract class UnitSeleniumTestCase extends SeleniumTestCase42{43 use MockeryPHPUnitIntegration;44 /**45 * An array of reusable dependency test doubles.46 *47 * @var MockInterface[]48 */49 protected $doubles = [];50 /**51 * Test setup.52 */53 public function setUp()54 {55 // Mock all calls to sleep() during unit tests56 PHPMockery::mock('Sepehr\PHPUnitSelenium', 'sleep')57 ->zeroOrMoreTimes()58 ->andReturn(0);59 parent::setUp();60 }61 /**62 * Manages test doubles.63 *64 * @param string $doubleId65 * @param \Closure|null $closure66 * @param string $doubleType67 *68 * @return MockInterface69 * @throws \Exception70 */71 protected function double($doubleId, $closure = null, $doubleType = 'mock')72 {73 if (key_exists($doubleId, $this->doubles)) {74 return $closure75 ? $closure($this->doubles[$doubleId])76 : $this->doubles[$doubleId];77 }78 try {79 return $this->doubles[$doubleId] = $closure80 ? Mockery::$doubleType($doubleId, $closure)81 : Mockery::$doubleType($doubleId);82 } catch (\Exception $e) {83 throw new \Exception(84 "Could not find/create a $doubleType with identifier: $doubleId\nMessage: {$e->getMessage()}"85 );86 }87 }88 /**89 * Mock interface.90 *91 * @param string $mockId92 * @param \Closure|null $closure93 *94 * @return MockInterface95 * @throws \Exception96 */97 protected function mock($mockId, $closure = null)98 {99 return $this->double($mockId, $closure, 'mock');100 }101 /**102 * Spy interface.103 *104 * @param string $spyId105 * @param \Closure|null $closure106 *107 * @return MockInterface108 * @throws \Exception109 */110 protected function spy($spyId, $closure = null)111 {112 return $this->double($spyId, $closure, 'spy');113 }114 /**115 * Genius test double injector; she knows how to inject!116 *117 * @param MockInterface|CompositeExpectation|string $double118 * @param string $doubleType119 *120 * @return MockInterface121 */122 protected function inject($double, $doubleType = 'mock')123 {124 $double = $this->normalizeDouble($double, $doubleType);125 $injector = $this->getDependencyInjector($double);126 // Each dependency might have its own logic for127 // injection, so the separate methods...128 return $this->$injector($double);129 }130 /**131 * Injects a mock.132 *133 * @param MockInterface|CompositeExpectation|string $double134 *135 * @return MockInterface136 */137 protected function injectMock($double)138 {139 return $this->inject($double, 'mock');140 }141 /**142 * Injects a spy.143 *144 * @param MockInterface|CompositeExpectation|string $double145 *146 * @return MockInterface147 */148 protected function injectSpy($double)149 {150 return $this->inject($double, 'spy');151 }152 /**153 * Injects a RemoteWebDriver double into the SeleniumTestCase.154 *155 * @param MockInterface|CompositeExpectation|string $double156 *157 * @return RemoteWebDriver158 */159 protected function injectWebDriver($double = RemoteWebDriver::class)160 {161 // Default behavior for mock doubles162 $double = $this->normalizeDouble($double)->shouldReceive('quit')->byDefault();163 return $this->injectDependency($double, 'setWebDriver');164 }165 /**166 * Injects a DesiredCapabilities double into the SeleniumTestCase.167 *168 * @param MockInterface|CompositeExpectation|string $double169 *170 * @return DesiredCapabilities171 */172 protected function injectDesiredCapabilities($double = DesiredCapabilities::class)173 {174 return $this->injectDependency($double, 'setDesiredCapabilities');175 }176 /**177 * Injects a WebDriverBy double into the SeleniumTestCase.178 *179 * @param MockInterface|CompositeExpectation|string $double180 *181 * @return WebDriverBy182 */183 protected function injectWebDriverBy($double = WebDriverBy::class)184 {185 return $this->injectDependency($double, 'setWebDriverBy');186 }187 /**188 * Injects a Filesystem double into the SeleniumTestCase.189 *190 * @param MockInterface|CompositeExpectation|string $double191 *192 * @return Filesystem193 */194 protected function injectFilesystem($double = Filesystem::class)195 {196 return $this->injectDependency($double, 'setFilesystem');197 }198 /**199 * Injects a Locator double into the SeleniumTestCase.200 *201 * @param MockInterface|CompositeExpectation|string $double202 *203 * @return Filesystem204 */205 protected function injectLocator($double = Locator::class)206 {207 return $this->injectDependency($double, 'setLocator');208 }209 /**210 * Injects a dependency double into the SeleniumTestCase.211 *212 * @param MockInterface|CompositeExpectation|string $double213 * @param string|null $setter214 *215 * @return MockInterface216 */217 protected function injectDependency($double, $setter = null)218 {219 $double = $this->normalizeDouble($double);220 $setter = $setter ? $setter : $this->getDependencySetter($double);221 $this->$setter($double);222 return $double;223 }224 /**225 * Returns setter method name from a dependency mock object.226 *227 * @param MockInterface $double228 *229 * @return string230 */231 private function getDependencySetter($double)232 {233 return $this->getDependencyMethodName($double, 'set');234 }235 /**236 * Returns injector method name from a dependency mock object.237 *238 * @param MockInterface $double239 *240 * @return string241 */242 private function getDependencyInjector($double)243 {244 return $this->getDependencyMethodName($double, 'inject');245 }246 /**247 * Returns setter/injector method name for a dependency mock.248 *249 * @param MockInterface $double250 * @param string $type251 *252 * @return string253 * @throws \Exception254 */255 private function getDependencyMethodName(MockInterface $double, $type)256 {257 $fqn = $this->getDependencyFqn($double);258 $method = $type . $this->getDependencyName($this->getDependencyClass($fqn));259 if (method_exists($this, $method)) {260 return $method;261 }262 throw new \Exception("Could not find the \"$type\" method for: " . get_class($double));263 }264 /**265 * Returns FQN of the test double object.266 *267 * This is too much, I know :/268 *269 * @param MockInterface $double270 *271 * @return string272 * @throws \Exception273 */274 private function getDependencyFqn($double)275 {276 $fqn = preg_replace(277 '/^Mockery\\\(\d+)\\\/',278 '',279 str_replace('_', '\\', get_class($double))280 );281 if (class_exists($fqn)) {282 return $fqn;283 }284 throw new \Exception('Could not extract the FQN of original class from mock: ' . get_class($double));285 }286 /**287 * Returns the short class name for a FQN.288 *289 * @param string $fqn290 *291 * @return string292 */293 private function getDependencyClass($fqn)294 {295 return ltrim(strrchr($fqn, '\\'), '\\');296 }297 /**298 * Returns the dependency name that SUT uses.299 *300 * @param string $dependencyClass301 *302 * @return string303 */304 private function getDependencyName($dependencyClass)305 {306 $exceptions = [307 'RemoteWebDriver' => 'WebDriver',308 ];309 return key_exists($dependencyClass, $exceptions)310 ? $exceptions[$dependencyClass]311 : $dependencyClass;312 }313 /**314 * Normalizes a test double object.315 *316 * @param MockInterface|CompositeExpectation|string $double317 * @param string $doubleType318 *319 * @return MockInterface320 * @throws \Exception321 */322 private function normalizeDouble($double, $doubleType = 'mock')323 {324 if (is_string($double)) {325 $double = $this->$doubleType($double);326 } elseif ($double instanceof CompositeExpectation) {327 $double = $double->getMock();328 }329 if (! $double instanceof MockInterface) {330 throw new \Exception('Cannot inject an invalid test double, what the fuck?!');331 }332 return $double;333 }334}...

Full Screen

Full Screen

ExpectationProxy.php

Source:ExpectationProxy.php Github

copy

Full Screen

2/**3 * @author Michał Bundyra (webimpress) <contact@webimpress.com>4 * @license http://www.wtfpl.net/txt/copying/ WTFPL5 */6namespace phpmock\mockery;7use Mockery\CompositeExpectation;8use Mockery\MockInterface;9use phpmock\integration\MockDelegateFunctionBuilder;10/**11 * Proxy to CompositeExpectation which clear all expectations created on mock.12 */13class ExpectationProxy extends CompositeExpectation14{15 private $isCleared = false;16 private $mock;17 public function __construct(MockInterface $mock)18 {19 $this->mock = $mock;20 }21 public function __call($name, array $args)22 {23 if (! $this->isCleared) {24 $callback = function () {25 $this->_mockery_expectations = [];26 };27 $bind = $callback->bindTo($this->mock, get_class($this->mock));28 $bind();29 $this->isCleared = true;30 }31 $expectation = $this->mock->shouldReceive(MockDelegateFunctionBuilder::METHOD);32 return call_user_func_array([$expectation, $name], $args);33 }34}

Full Screen

Full Screen

mock

Using AI Code Generation

copy

Full Screen

1$mock = new MockCompositeExpectation();2$mock->setReturnValue('test', true);3$mock->expectOnce('test');4$mock->test();5$mock->tally();6$mock = new MockCompositeExpectation();7$mock->setReturnValue('test', true);8$mock->expectOnce('test');9$mock->test();10$mock->tally();11$mock = new MockCompositeExpectation();12$mock->setReturnValue('test', true);13$mock->expectOnce('test');14$mock->test();15$mock->tally();16$mock = new MockCompositeExpectation();17$mock->setReturnValue('test', true);18$mock->expectOnce('test');19$mock->test();20$mock->tally();21$mock = new MockCompositeExpectation();22$mock->setReturnValue('test', true);23$mock->expectOnce('test');24$mock->test();25$mock->tally();26$mock = new MockCompositeExpectation();27$mock->setReturnValue('test', true);28$mock->expectOnce('test');29$mock->test();30$mock->tally();31$mock = new MockCompositeExpectation();32$mock->setReturnValue('test', true);33$mock->expectOnce('test');34$mock->test();35$mock->tally();36$mock = new MockCompositeExpectation();37$mock->setReturnValue('test', true);38$mock->expectOnce('test');39$mock->test();40$mock->tally();41$mock = new MockCompositeExpectation();42$mock->setReturnValue('test', true);43$mock->expectOnce('test');44$mock->test();45$mock->tally();

Full Screen

Full Screen

mock

Using AI Code Generation

copy

Full Screen

1$mock = new MockCompositeExpectation();2$mock->expectOnce('addExpectation', array(new IsAExpectation('SimpleExpectation')));3$mock->expectOnce('test');4$mock->addExpectation(new SimpleExpectation());5$mock->test();6$mock = new MockCompositeExpectation();7$mock->expectOnce('addExpectation', array(new IsAExpectation('SimpleExpectation')));8$mock->expectOnce('test');9$mock->addExpectation(new SimpleExpectation());10$mock->test();11$mock = new MockCompositeExpectation();12$mock->expectOnce('addExpectation', array(new IsAExpectation('SimpleExpectation')));13$mock->expectOnce('test');14$mock->addExpectation(new SimpleExpectation());15$mock->test();16$mock = new MockCompositeExpectation();17$mock->expectOnce('addExpectation', array(new IsAExpectation('SimpleExpectation')));18$mock->expectOnce('test');19$mock->addExpectation(new SimpleExpectation());20$mock->test();21$mock = new MockCompositeExpectation();22$mock->expectOnce('addExpectation', array(new IsAExpectation('SimpleExpectation')));23$mock->expectOnce('test');24$mock->addExpectation(new SimpleExpectation());25$mock->test();26$mock = new MockCompositeExpectation();27$mock->expectOnce('addExpectation', array(new IsAExpectation('SimpleExpectation')));28$mock->expectOnce('test');29$mock->addExpectation(new SimpleExpectation());30$mock->test();31$mock = new MockCompositeExpectation();32$mock->expectOnce('addExpectation', array(new IsAExpectation('SimpleExpectation')));33$mock->expectOnce('test');34$mock->addExpectation(new SimpleExpectation());35$mock->test();

Full Screen

Full Screen

mock

Using AI Code Generation

copy

Full Screen

1$mockCompositeExpectation = $this->getMock('CompositeExpectation', array('test'));2$mockCompositeExpectation->expects($this->once())3 ->method('test')4 ->with($this->equalTo('test'))5 ->will($this->returnValue(true));6$mockCompositeExpectation->test('test');7$mockCompositeExpectation = $this->getMock('CompositeExpectation', array('test'));8$mockCompositeExpectation->expects($this->once())9 ->method('test')10 ->with($this->equalTo('test'))11 ->will($this->returnValue(true));12$mockCompositeExpectation->test('test');13$mockCompositeExpectation = $this->getMock('CompositeExpectation', array('test'));14$mockCompositeExpectation->expects($this->once())15 ->method('test')16 ->with($this->equalTo('test'))17 ->will($this->returnValue(true));18$mockCompositeExpectation->test('test');19$mockCompositeExpectation = $this->getMock('CompositeExpectation', array('test'));20$mockCompositeExpectation->expects($this->once())21 ->method('test')22 ->with($this->equalTo('test'))23 ->will($this->returnValue(true));24$mockCompositeExpectation->test('test');25$mockCompositeExpectation = $this->getMock('CompositeExpectation', array('test'));26$mockCompositeExpectation->expects($this->once())27 ->method('test')28 ->with($this->equalTo('test'))29 ->will($this->returnValue(true));30$mockCompositeExpectation->test('test');31$mockCompositeExpectation = $this->getMock('CompositeExpectation', array('test'));32$mockCompositeExpectation->expects($this->once())33 ->method('test')34 ->with($this->equalTo('test'))35 ->will($this->returnValue(true));36$mockCompositeExpectation->test('test');

Full Screen

Full Screen

mock

Using AI Code Generation

copy

Full Screen

1$mock = new MockCompositeExpectation();2$mock->expectOnce('mockMethod');3$mock->mockMethod();4$mock = new MockSimpleExpectation();5$mock->expectOnce('mockMethod');6$mock->mockMethod();7$mock = new MockCompositeExpectation();8$mock->expectOnce('mockMethod');9$mock->mockMethod();10$mock = new MockSimpleExpectation();11$mock->expectOnce('mockMethod');12$mock->mockMethod();13$mock = new MockCompositeExpectation();14$mock->expectOnce('mockMethod');15$mock->mockMethod();16$mock = new MockSimpleExpectation();17$mock->expectOnce('mockMethod');18$mock->mockMethod();

Full Screen

Full Screen

mock

Using AI Code Generation

copy

Full Screen

1$compositeExpectation = $this->getMock('CompositeExpectation');2$compositeExpectation->expects($this->once())3->method('addExpectation')4->with($this->anything());5$compositeExpectation = $this->getMock('CompositeExpectation');6$compositeExpectation->expects($this->once())7->method('addExpectation')8->with($this->anything());9$compositeExpectation = $this->getMock('CompositeExpectation');10$compositeExpectation->expects($this->once())11->method('addExpectation')12->with($this->anything());13$compositeExpectation = $this->getMock('CompositeExpectation');14$compositeExpectation->expects($this->once())15->method('addExpectation')16->with($this->anything());17$compositeExpectation = $this->getMock('CompositeExpectation');18$compositeExpectation->expects($this->once())19->method('addExpectation')20->with($this->anything());21$compositeExpectation = $this->getMock('CompositeExpectation');22$compositeExpectation->expects($this->once())23->method('addExpectation')24->with($this->anything());25$compositeExpectation = $this->getMock('CompositeExpectation');26$compositeExpectation->expects($this->once())27->method('addExpectation')28->with($this->anything());29$compositeExpectation = $this->getMock('CompositeExpectation');30$compositeExpectation->expects($this->once())31->method('addExpectation')32->with($this->anything());33$compositeExpectation = $this->getMock('CompositeExpectation');34$compositeExpectation->expects($this->once())35->method('addExpectation')

Full Screen

Full Screen

mock

Using AI Code Generation

copy

Full Screen

1require_once 'CompositeExpectation.php';2class MockCompositeExpectation extends CompositeExpectation {3 function MockCompositeExpectation() {4 $this->CompositeExpectation();5 }6}

Full Screen

Full Screen

mock

Using AI Code Generation

copy

Full Screen

1$mock = new MockCompositeExpectation();2$mock->expectOnce('addExpectation', array(new IsAExpectation('SimpleExpectation')));3$mock->expectOnce('test', array('a', 'b', 'c'));4$mock->test('a', 'b', 'c');5$mock = new MockCompositeExpectation();6$mock->expectOnce('addExpectation', array(new IsAExpectation('SimpleExpectation')));7$mock->expectOnce('test', array('a', 'b', 'c'));8$mock->test('a', 'b', 'c');9$mock = new MockCompositeExpectation();10$mock->expectOnce('addExpectation', array(new IsAExpectation('SimpleExpectation')));11$mock->expectOnce('test', array('a', 'b', 'c'));12$mock->test('a', 'b', 'c');13$mock = new MockCompositeExpectation();14$mock->expectOnce('addExpectation', array(new IsAExpectation('SimpleExpectation')));15$mock->expectOnce('test', array('a', 'b', 'c'));16$mock->test('a', 'b', 'c');17$mock = new MockCompositeExpectation();18$mock->expectOnce('addExpectation', array(new IsAExpectation('SimpleExpectation')));19$mock->expectOnce('test', array('a', 'b', 'c'));20$mock->test('a', 'b', 'c');21$mock = new MockCompositeExpectation();22$mock->expectOnce('addExpectation', array(new IsAExpectation('SimpleExpectation')));23$mock->expectOnce('test', array('a', 'b', 'c'));24$mock->test('a', 'b', 'c');25$mock = new MockCompositeExpectation();26$mock->expectOnce('addExpectation

Full Screen

Full Screen

mock

Using AI Code Generation

copy

Full Screen

1$composite = new CompositeExpectation();2$composite->addExpectation(new EqualExpectation("Hello World"));3$mock = new Mock("CompositeExpectation");4$mock->expectOnce("test", array($composite));5$mock->test("Hello World");6class CompositeExpectation {7 function test($value) {8 if ($this->expectations) {9 foreach ($this->expectations as $expectation) {10 if (! $expectation->test($value)) {11 return false;12 }13 }14 }15 return true;16 }17}18class EqualExpectation {19 function test($value) {20 if ($this->expected === $value) {21 return true;22 }23 return false;24 }25}26class Mock {27 function test($value) {28 return $this->expectations->test($value);29 }30}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mockery automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Trigger mock code on LambdaTest Cloud Grid

Execute automation tests with mock on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful