Skip to content

Added support for multiple calls of the same function with different args #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions classes/ExpectationProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* @author Michał Bundyra (webimpress) <contact@webimpress.com>
* @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);
}
}
6 changes: 3 additions & 3 deletions classes/PHPMockery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -57,7 +57,7 @@ public static function mock($namespace, $name)
$disabler = new MockDisabler($mock);
Mockery::getContainer()->rememberMock($disabler);

return $expectation;
return new ExpectationProxy($mockeryMock);
}

/**
Expand Down
31 changes: 31 additions & 0 deletions tests/PHPMockeryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}