File tree Expand file tree Collapse file tree 3 files changed +78
-3
lines changed Expand file tree Collapse file tree 3 files changed +78
-3
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 new ExpectationProxy ( $ mockeryMock ) ;
6161 }
6262
6363 /**
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments