Skip to content

Allow to mock the same function with defferent arguments in the namespace #6

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 3 commits into from
Dec 11, 2020
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
21 changes: 17 additions & 4 deletions classes/FunctionProphecy.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,23 @@ public function __construct($namespace, Prophet $prophet)
*/
public function __call($functionName, array $arguments)
{
$delegateBuilder = new MockDelegateFunctionBuilder();
$delegateBuilder->build($functionName);
$prophecy = $this->prophet->prophesize($delegateBuilder->getFullyQualifiedClassName());
$this->revelations[] = new Revelation($this->namespace, $functionName, $prophecy);
foreach ($this->revelations as $revelation) {
if (
$revelation->namespace === $this->namespace
&& $revelation->functionName === $functionName
) {
$prophecy = $revelation->prophecy;
break;
}
}

if (! isset($prophecy)) {
$delegateBuilder = new MockDelegateFunctionBuilder();
$delegateBuilder->build($functionName);
$prophecy = $this->prophet->prophesize($delegateBuilder->getFullyQualifiedClassName());
$this->revelations[] = new Revelation($this->namespace, $functionName, $prophecy);
}

return $prophecy->__call(MockDelegateFunctionBuilder::METHOD, $arguments);
}

Expand Down
9 changes: 6 additions & 3 deletions classes/Revelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ final class Revelation implements ProphecyInterface
{

/**
* @internal
* @var string The function namespace.
*/
private $namespace;
public $namespace;

/**
* @internal
* @var string The function name.
*/
private $functionName;
public $functionName;

/**
* @internal
* @var ProphecyInterface The prophecy.
*/
private $prophecy;
public $prophecy;

/**
* Builds the revelation.
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPProphetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ protected function defineFunction($namespace, $functionName)
PHPProphet::define($namespace, $functionName);
}

public function testDoubleMockTheSameFunctionWithDifferentArguments()
{
$prophecy = $this->prophet->prophesize(__NAMESPACE__);
$prophecy->min(1, 10)->willReturn(0);
$prophecy->min(20, 30)->willReturn(1);
$prophecy->reveal();

$this->assertSame(0, min(1, 10));
$this->assertSame(1, min(20, 30));
}

public function testTwoDifferentFunctionsMock()
{
$prophecy = $this->prophet->prophesize(__NAMESPACE__);
$prophecy->min(1, 10)->willReturn(0);
$prophecy->max(20, 30)->willReturn(1);
$prophecy->reveal();

$this->assertSame(0, min(1, 10));
$this->assertSame(1, max(20, 30));
}

/**
* This test is skipped until PHPUnit#2016 is resolved.
*
Expand Down