From 7b9b4f86d13374d26d5c718c77a955106e287989 Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 23 Mar 2018 14:44:26 +0000 Subject: [PATCH] Added support for multiple calls of the same function with different args Fixes #6 --- classes/ExpectationProxy.php | 44 ++++++++++++++++++++++++++++++++++++ classes/PHPMockery.php | 6 ++--- tests/PHPMockeryTest.php | 31 +++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 classes/ExpectationProxy.php diff --git a/classes/ExpectationProxy.php b/classes/ExpectationProxy.php new file mode 100644 index 0000000..2a2e4e0 --- /dev/null +++ b/classes/ExpectationProxy.php @@ -0,0 +1,44 @@ + + * @license http://www.wtfpl.net/txt/copying/ WTFPL + */ + +namespace phpmock\mockery; + +use Mockery\CompositeExpectation; +use Mockery\MockInterface; +use phpmock\integration\MockDelegateFunctionBuilder; + +/** + * Proxy to CompositeExpectation which clear all expectations created on mock. + */ +class ExpectationProxy extends CompositeExpectation +{ + private $isCleared = false; + + private $mock; + + public function __construct(MockInterface $mock) + { + $this->mock = $mock; + } + + public function __call($name, array $args) + { + if (! $this->isCleared) { + $callback = function () { + $this->_mockery_expectations = []; + }; + + $bind = $callback->bindTo($this->mock, get_class($this->mock)); + $bind(); + + $this->isCleared = true; + } + + $expectation = $this->mock->shouldReceive(MockDelegateFunctionBuilder::METHOD); + + return call_user_func_array([$expectation, $name], $args); + } +} diff --git a/classes/PHPMockery.php b/classes/PHPMockery.php index 18fd3d5..3b305ea 100644 --- a/classes/PHPMockery.php +++ b/classes/PHPMockery.php @@ -45,8 +45,8 @@ public static function mock($namespace, $name) $delegateBuilder->build($name); $mockeryMock = Mockery::mock($delegateBuilder->getFullyQualifiedClassName()); - $expectation = $mockeryMock->makePartial()->shouldReceive(MockDelegateFunctionBuilder::METHOD); - + $mockeryMock->makePartial()->shouldReceive(MockDelegateFunctionBuilder::METHOD); + $builder = new MockBuilder(); $builder->setNamespace($namespace) ->setName($name) @@ -57,7 +57,7 @@ public static function mock($namespace, $name) $disabler = new MockDisabler($mock); Mockery::getContainer()->rememberMock($disabler); - return $expectation; + return new ExpectationProxy($mockeryMock); } /** diff --git a/tests/PHPMockeryTest.php b/tests/PHPMockeryTest.php index 4ff6008..3f799a7 100644 --- a/tests/PHPMockeryTest.php +++ b/tests/PHPMockeryTest.php @@ -88,4 +88,35 @@ private function workaroundMockeryIssue268() } } } + + public function testMockDoubleCalls() + { + $mock = PHPMockery::mock(__NAMESPACE__, 'min'); + $mock->twice() + ->with(1, 10) + ->andReturnValues([0, 11]); + + $this->assertSame(0, min(1, 10)); + $this->assertSame(11, min(1, 10)); + } + + public function testMockDoubleCallsWithDifferentArgs() + { + $mock = PHPMockery::mock(__NAMESPACE__, 'max'); + $mock->with(0, 0)->andReturn(77); + $mock + ->once() + ->with(1, 10) + ->andReturn(0); + $mock + ->twice() + ->with(11, 20) + ->andReturn(10, 30); + + $this->assertSame(77, max(0, 0)); + $this->assertSame(0, max(1, 10)); + $this->assertSame(10, max(11, 20)); + $this->assertSame(30, max(11, 20)); + $this->assertSame(77, max(0, 0)); + } }