Skip to content

Commit

Permalink
Allow to mock the same function with defferent arguments in the names…
Browse files Browse the repository at this point in the history
…pace
  • Loading branch information
michalbundyra committed Mar 19, 2018
1 parent 7374355 commit d20dffe
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
20 changes: 16 additions & 4 deletions classes/FunctionProphecy.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,22 @@ 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
6 changes: 3 additions & 3 deletions classes/Revelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ final class Revelation implements ProphecyInterface
/**
* @var string The function namespace.
*/
private $namespace;
public $namespace;

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

/**
* @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 d20dffe

Please sign in to comment.