Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.

Commit a1a2f93

Browse files
committed
Merge pull request #147 from fabian/willReturn
Simplified will return
2 parents 8ce9c35 + 457b943 commit a1a2f93

File tree

2 files changed

+212
-0
lines changed

2 files changed

+212
-0
lines changed

PHPUnit/Framework/MockObject/Builder/InvocationMocker.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,92 @@ public function will(PHPUnit_Framework_MockObject_Stub $stub)
114114
return $this;
115115
}
116116

117+
/**
118+
* @param mixed $value
119+
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
120+
*/
121+
public function willReturn($value)
122+
{
123+
$stub = new PHPUnit_Framework_MockObject_Stub_Return(
124+
$value
125+
);
126+
127+
return $this->will($stub);
128+
}
129+
130+
/**
131+
* @param array $valueMap
132+
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
133+
*/
134+
public function willReturnMap(array $valueMap)
135+
{
136+
$stub = new PHPUnit_Framework_MockObject_Stub_ReturnValueMap(
137+
$valueMap
138+
);
139+
140+
return $this->will($stub);
141+
}
142+
143+
/**
144+
* @param mixed $argumentIndex
145+
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
146+
*/
147+
public function willReturnArgument($argumentIndex)
148+
{
149+
$stub = new PHPUnit_Framework_MockObject_Stub_ReturnArgument(
150+
$argumentIndex
151+
);
152+
153+
return $this->will($stub);
154+
}
155+
156+
/**
157+
* @param callable $callback
158+
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
159+
*/
160+
public function willReturnCallback($callback)
161+
{
162+
$stub = new PHPUnit_Framework_MockObject_Stub_ReturnCallback(
163+
$callback
164+
);
165+
166+
return $this->will($stub);
167+
}
168+
169+
/**
170+
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
171+
*/
172+
public function willReturnSelf()
173+
{
174+
$stub = new PHPUnit_Framework_MockObject_Stub_ReturnSelf();
175+
176+
return $this->will($stub);
177+
}
178+
179+
/**
180+
* @param mixed $value, ...
181+
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
182+
*/
183+
public function willReturnOnConsecutiveCalls()
184+
{
185+
$args = func_get_args();
186+
187+
$stub = new PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls($args);
188+
189+
return $this->will($stub);
190+
}
191+
192+
/**
193+
* @param Exception $exception
194+
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
195+
*/
196+
public function willThrowException(Exception $exception)
197+
{
198+
$stub = new PHPUnit_Framework_MockObject_Stub_Exception($exception);
199+
200+
return $this->will($stub);
201+
}
202+
117203
/**
118204
* @param mixed $id
119205
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker

Tests/MockObjectTest.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,24 @@ public function testStubbedException()
159159
$this->fail();
160160
}
161161

162+
public function testStubbedWillThrowException()
163+
{
164+
$mock = $this->getMock('AnInterface');
165+
$mock->expects($this->any())
166+
->method('doSomething')
167+
->willThrowException(new Exception);
168+
169+
try {
170+
$mock->doSomething();
171+
}
172+
173+
catch (Exception $e) {
174+
return;
175+
}
176+
177+
$this->fail();
178+
}
179+
162180
public function testStubbedReturnValue()
163181
{
164182
$mock = $this->getMock('AnInterface');
@@ -167,6 +185,13 @@ public function testStubbedReturnValue()
167185
->will($this->returnValue('something'));
168186

169187
$this->assertEquals('something', $mock->doSomething());
188+
189+
$mock = $this->getMock('AnInterface');
190+
$mock->expects($this->any())
191+
->method('doSomething')
192+
->willReturn('something');
193+
194+
$this->assertEquals('something', $mock->doSomething());
170195
}
171196

172197
public function testStubbedReturnValueMap()
@@ -184,6 +209,32 @@ public function testStubbedReturnValueMap()
184209
$this->assertEquals('d', $mock->doSomething('a', 'b', 'c'));
185210
$this->assertEquals('h', $mock->doSomething('e', 'f', 'g'));
186211
$this->assertEquals(NULL, $mock->doSomething('foo', 'bar'));
212+
213+
$mock = $this->getMock('AnInterface');
214+
$mock->expects($this->any())
215+
->method('doSomething')
216+
->willReturnMap($map);
217+
218+
$this->assertEquals('d', $mock->doSomething('a', 'b', 'c'));
219+
$this->assertEquals('h', $mock->doSomething('e', 'f', 'g'));
220+
$this->assertEquals(NULL, $mock->doSomething('foo', 'bar'));
221+
}
222+
223+
public function testStubbedReturnArgument()
224+
{
225+
$mock = $this->getMock('AnInterface');
226+
$mock->expects($this->any())
227+
->method('doSomething')
228+
->will($this->returnArgument(1));
229+
230+
$this->assertEquals('b', $mock->doSomething('a', 'b'));
231+
232+
$mock = $this->getMock('AnInterface');
233+
$mock->expects($this->any())
234+
->method('doSomething')
235+
->willReturnArgument(1);
236+
237+
$this->assertEquals('b', $mock->doSomething('a', 'b'));
187238
}
188239

189240
public function testFunctionCallback()
@@ -194,6 +245,51 @@ public function testFunctionCallback()
194245
->will($this->returnCallback('functionCallback'));
195246

196247
$this->assertEquals('pass', $mock->doSomething('foo', 'bar'));
248+
249+
$mock = $this->getMock('SomeClass', array('doSomething'), array(), '', FALSE);
250+
$mock->expects($this->once())
251+
->method('doSomething')
252+
->willReturnCallback('functionCallback');
253+
254+
$this->assertEquals('pass', $mock->doSomething('foo', 'bar'));
255+
}
256+
257+
public function testStubbedReturnSelf()
258+
{
259+
$mock = $this->getMock('AnInterface');
260+
$mock->expects($this->any())
261+
->method('doSomething')
262+
->will($this->returnSelf());
263+
264+
$this->assertEquals($mock, $mock->doSomething());
265+
266+
$mock = $this->getMock('AnInterface');
267+
$mock->expects($this->any())
268+
->method('doSomething')
269+
->willReturnSelf();
270+
271+
$this->assertEquals($mock, $mock->doSomething());
272+
}
273+
274+
public function testStubbedReturnOnConsecutiveCalls()
275+
{
276+
$mock = $this->getMock('AnInterface');
277+
$mock->expects($this->any())
278+
->method('doSomething')
279+
->will($this->onConsecutiveCalls('a', 'b', 'c'));
280+
281+
$this->assertEquals('a', $mock->doSomething());
282+
$this->assertEquals('b', $mock->doSomething());
283+
$this->assertEquals('c', $mock->doSomething());
284+
285+
$mock = $this->getMock('AnInterface');
286+
$mock->expects($this->any())
287+
->method('doSomething')
288+
->willReturnOnConsecutiveCalls('a', 'b', 'c');
289+
290+
$this->assertEquals('a', $mock->doSomething());
291+
$this->assertEquals('b', $mock->doSomething());
292+
$this->assertEquals('c', $mock->doSomething());
197293
}
198294

199295
public function testStaticMethodCallback()
@@ -304,6 +400,21 @@ public function testStubbedReturnValueForStaticMethod()
304400
$this->assertEquals(
305401
'something', StaticMockTestClassMock::doSomething()
306402
);
403+
404+
$this->getMockClass(
405+
'StaticMockTestClass',
406+
array('doSomething'),
407+
array(),
408+
'StaticMockTestClassMock'
409+
);
410+
411+
StaticMockTestClassMock::staticExpects($this->any())
412+
->method('doSomething')
413+
->willReturn('something');
414+
415+
$this->assertEquals(
416+
'something', StaticMockTestClassMock::doSomething()
417+
);
307418
}
308419

309420
public function testStubbedReturnValueForStaticMethod2()
@@ -322,6 +433,21 @@ public function testStubbedReturnValueForStaticMethod2()
322433
$this->assertEquals(
323434
'something', StaticMockTestClassMock2::doSomethingElse()
324435
);
436+
437+
$this->getMockClass(
438+
'StaticMockTestClass',
439+
array('doSomething'),
440+
array(),
441+
'StaticMockTestClassMock2'
442+
);
443+
444+
StaticMockTestClassMock2::staticExpects($this->any())
445+
->method('doSomething')
446+
->willReturn('something');
447+
448+
$this->assertEquals(
449+
'something', StaticMockTestClassMock2::doSomethingElse()
450+
);
325451
}
326452

327453
public function testGetMockForAbstractClass()

0 commit comments

Comments
 (0)