Skip to content

Commit

Permalink
Merge pull request #6 from michalbundyra/hotfix/double-mock
Browse files Browse the repository at this point in the history
Allow to mock the same function with defferent arguments in the namespace
  • Loading branch information
michalbundyra authored Dec 11, 2020
2 parents 291569e + 748c3d7 commit 0dfd45c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
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

0 comments on commit 0dfd45c

Please sign in to comment.