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 7b9b4f8

Browse files
Added support for multiple calls of the same function with different args
Fixes #6
1 parent 8fa9920 commit 7b9b4f8

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

‎classes/ExpectationProxy.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* @author Michał Bundyra (webimpress) <contact@webimpress.com>
4+
* @license http://www.wtfpl.net/txt/copying/ WTFPL
5+
*/
6+
7+
namespace phpmock\mockery;
8+
9+
use Mockery\CompositeExpectation;
10+
use Mockery\MockInterface;
11+
use phpmock\integration\MockDelegateFunctionBuilder;
12+
13+
/**
14+
* Proxy to CompositeExpectation which clear all expectations created on mock.
15+
*/
16+
class ExpectationProxy extends CompositeExpectation
17+
{
18+
private $isCleared = false;
19+
20+
private $mock;
21+
22+
public function __construct(MockInterface $mock)
23+
{
24+
$this->mock = $mock;
25+
}
26+
27+
public function __call($name, array $args)
28+
{
29+
if (! $this->isCleared) {
30+
$callback = function () {
31+
$this->_mockery_expectations = [];
32+
};
33+
34+
$bind = $callback->bindTo($this->mock, get_class($this->mock));
35+
$bind();
36+
37+
$this->isCleared = true;
38+
}
39+
40+
$expectation = $this->mock->shouldReceive(MockDelegateFunctionBuilder::METHOD);
41+
42+
return call_user_func_array([$expectation, $name], $args);
43+
}
44+
}

‎classes/PHPMockery.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public static function mock($namespace, $name)
4545
$delegateBuilder->build($name);
4646

4747
$mockeryMock = Mockery::mock($delegateBuilder->getFullyQualifiedClassName());
48-
$expectation = $mockeryMock->makePartial()->shouldReceive(MockDelegateFunctionBuilder::METHOD);
49-
48+
$mockeryMock->makePartial()->shouldReceive(MockDelegateFunctionBuilder::METHOD);
49+
5050
$builder = new MockBuilder();
5151
$builder->setNamespace($namespace)
5252
->setName($name)
@@ -57,7 +57,7 @@ public static function mock($namespace, $name)
5757
$disabler = new MockDisabler($mock);
5858
Mockery::getContainer()->rememberMock($disabler);
5959

60-
return $expectation;
60+
return newExpectationProxy($mockeryMock);
6161
}
6262

6363
/**

‎tests/PHPMockeryTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,35 @@ private function workaroundMockeryIssue268()
8888
}
8989
}
9090
}
91+
92+
public function testMockDoubleCalls()
93+
{
94+
$mock = PHPMockery::mock(__NAMESPACE__, 'min');
95+
$mock->twice()
96+
->with(1, 10)
97+
->andReturnValues([0, 11]);
98+
99+
$this->assertSame(0, min(1, 10));
100+
$this->assertSame(11, min(1, 10));
101+
}
102+
103+
public function testMockDoubleCallsWithDifferentArgs()
104+
{
105+
$mock = PHPMockery::mock(__NAMESPACE__, 'max');
106+
$mock->with(0, 0)->andReturn(77);
107+
$mock
108+
->once()
109+
->with(1, 10)
110+
->andReturn(0);
111+
$mock
112+
->twice()
113+
->with(11, 20)
114+
->andReturn(10, 30);
115+
116+
$this->assertSame(77, max(0, 0));
117+
$this->assertSame(0, max(1, 10));
118+
$this->assertSame(10, max(11, 20));
119+
$this->assertSame(30, max(11, 20));
120+
$this->assertSame(77, max(0, 0));
121+
}
91122
}

0 commit comments

Comments
(0)

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