Skip to content

Commit d20dffe

Browse files
committed
Allow to mock the same function with defferent arguments in the namespace
1 parent 7374355 commit d20dffe

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

classes/FunctionProphecy.php

+16-4
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,22 @@ public function __construct($namespace, Prophet $prophet)
5656
*/
5757
public function __call($functionName, array $arguments)
5858
{
59-
$delegateBuilder = new MockDelegateFunctionBuilder();
60-
$delegateBuilder->build($functionName);
61-
$prophecy = $this->prophet->prophesize($delegateBuilder->getFullyQualifiedClassName());
62-
$this->revelations[] = new Revelation($this->namespace, $functionName, $prophecy);
59+
foreach ($this->revelations as $revelation) {
60+
if ($revelation->namespace === $this->namespace
61+
&& $revelation->functionName === $functionName
62+
) {
63+
$prophecy = $revelation->prophecy;
64+
break;
65+
}
66+
}
67+
68+
if (! isset($prophecy)) {
69+
$delegateBuilder = new MockDelegateFunctionBuilder();
70+
$delegateBuilder->build($functionName);
71+
$prophecy = $this->prophet->prophesize($delegateBuilder->getFullyQualifiedClassName());
72+
$this->revelations[] = new Revelation($this->namespace, $functionName, $prophecy);
73+
}
74+
6375
return $prophecy->__call(MockDelegateFunctionBuilder::METHOD, $arguments);
6476
}
6577

classes/Revelation.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ final class Revelation implements ProphecyInterface
2020
/**
2121
* @var string The function namespace.
2222
*/
23-
private $namespace;
23+
public $namespace;
2424

2525
/**
2626
* @var string The function name.
2727
*/
28-
private $functionName;
28+
public $functionName;
2929

3030
/**
3131
* @var ProphecyInterface The prophecy.
3232
*/
33-
private $prophecy;
33+
public $prophecy;
3434

3535
/**
3636
* Builds the revelation.

tests/PHPProphetTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ protected function defineFunction($namespace, $functionName)
4747
PHPProphet::define($namespace, $functionName);
4848
}
4949

50+
public function testDoubleMockTheSameFunctionWithDifferentArguments()
51+
{
52+
$prophecy = $this->prophet->prophesize(__NAMESPACE__);
53+
$prophecy->min(1, 10)->willReturn(0);
54+
$prophecy->min(20, 30)->willReturn(1);
55+
$prophecy->reveal();
56+
57+
$this->assertSame(0, min(1, 10));
58+
$this->assertSame(1, min(20, 30));
59+
}
60+
61+
public function testTwoDifferentFunctionsMock()
62+
{
63+
$prophecy = $this->prophet->prophesize(__NAMESPACE__);
64+
$prophecy->min(1, 10)->willReturn(0);
65+
$prophecy->max(20, 30)->willReturn(1);
66+
$prophecy->reveal();
67+
68+
$this->assertSame(0, min(1, 10));
69+
$this->assertSame(1, max(20, 30));
70+
}
71+
5072
/**
5173
* This test is skipped until PHPUnit#2016 is resolved.
5274
*

0 commit comments

Comments
 (0)