Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0dfd45c

Browse files
Merge pull request #6 from michalbundyra/hotfix/double-mock
Allow to mock the same function with defferent arguments in the namespace
2 parents 291569e + 748c3d7 commit 0dfd45c

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

‎classes/FunctionProphecy.php‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,23 @@ 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 (
61+
$revelation->namespace === $this->namespace
62+
&& $revelation->functionName === $functionName
63+
) {
64+
$prophecy = $revelation->prophecy;
65+
break;
66+
}
67+
}
68+
69+
if (! isset($prophecy)) {
70+
$delegateBuilder = new MockDelegateFunctionBuilder();
71+
$delegateBuilder->build($functionName);
72+
$prophecy = $this->prophet->prophesize($delegateBuilder->getFullyQualifiedClassName());
73+
$this->revelations[] = new Revelation($this->namespace, $functionName, $prophecy);
74+
}
75+
6376
return $prophecy->__call(MockDelegateFunctionBuilder::METHOD, $arguments);
6477
}
6578

‎classes/Revelation.php‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,22 @@ final class Revelation implements ProphecyInterface
1818
{
1919

2020
/**
21+
* @internal
2122
* @var string The function namespace.
2223
*/
23-
private $namespace;
24+
public $namespace;
2425

2526
/**
27+
* @internal
2628
* @var string The function name.
2729
*/
28-
private $functionName;
30+
public $functionName;
2931

3032
/**
33+
* @internal
3134
* @var ProphecyInterface The prophecy.
3235
*/
33-
private $prophecy;
36+
public $prophecy;
3437

3538
/**
3639
* Builds the revelation.

‎tests/PHPProphetTest.php‎

Lines changed: 22 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /