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

Commit 457b943

Browse files
committed
Added tests for willThrowException, willReturnArgument, willReturnSelf, willReturnOnConsecutiveCalls
1 parent 22cb30b commit 457b943

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

Tests/MockObjectTest.php

Lines changed: 73 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');
@@ -202,6 +220,23 @@ public function testStubbedReturnValueMap()
202220
$this->assertEquals(NULL, $mock->doSomething('foo', 'bar'));
203221
}
204222

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'));
238+
}
239+
205240
public function testFunctionCallback()
206241
{
207242
$mock = $this->getMock('SomeClass', array('doSomething'), array(), '', FALSE);
@@ -219,6 +254,44 @@ public function testFunctionCallback()
219254
$this->assertEquals('pass', $mock->doSomething('foo', 'bar'));
220255
}
221256

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());
293+
}
294+
222295
public function testStaticMethodCallback()
223296
{
224297
$mock = $this->getMock('SomeClass', array('doSomething'), array(), '', FALSE);

0 commit comments

Comments
 (0)