diff --git a/PHPUnit/Framework/MockObject/Builder/InvocationMocker.php b/PHPUnit/Framework/MockObject/Builder/InvocationMocker.php index e25edb3e..df72f1fe 100644 --- a/PHPUnit/Framework/MockObject/Builder/InvocationMocker.php +++ b/PHPUnit/Framework/MockObject/Builder/InvocationMocker.php @@ -114,6 +114,92 @@ public function will(PHPUnit_Framework_MockObject_Stub $stub) return $this; } + /** + * @param mixed $value + * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker + */ + public function willReturn($value) + { + $stub = new PHPUnit_Framework_MockObject_Stub_Return( + $value + ); + + return $this->will($stub); + } + + /** + * @param array $valueMap + * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker + */ + public function willReturnMap(array $valueMap) + { + $stub = new PHPUnit_Framework_MockObject_Stub_ReturnValueMap( + $valueMap + ); + + return $this->will($stub); + } + + /** + * @param mixed $argumentIndex + * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker + */ + public function willReturnArgument($argumentIndex) + { + $stub = new PHPUnit_Framework_MockObject_Stub_ReturnArgument( + $argumentIndex + ); + + return $this->will($stub); + } + + /** + * @param callable $callback + * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker + */ + public function willReturnCallback($callback) + { + $stub = new PHPUnit_Framework_MockObject_Stub_ReturnCallback( + $callback + ); + + return $this->will($stub); + } + + /** + * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker + */ + public function willReturnSelf() + { + $stub = new PHPUnit_Framework_MockObject_Stub_ReturnSelf(); + + return $this->will($stub); + } + + /** + * @param mixed $value, ... + * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker + */ + public function willReturnOnConsecutiveCalls() + { + $args = func_get_args(); + + $stub = new PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls($args); + + return $this->will($stub); + } + + /** + * @param Exception $exception + * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker + */ + public function willThrowException(Exception $exception) + { + $stub = new PHPUnit_Framework_MockObject_Stub_Exception($exception); + + return $this->will($stub); + } + /** * @param mixed $id * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker diff --git a/Tests/MockObjectTest.php b/Tests/MockObjectTest.php index a49cf71b..4501025f 100644 --- a/Tests/MockObjectTest.php +++ b/Tests/MockObjectTest.php @@ -159,6 +159,24 @@ public function testStubbedException() $this->fail(); } + public function testStubbedWillThrowException() + { + $mock = $this->getMock('AnInterface'); + $mock->expects($this->any()) + ->method('doSomething') + ->willThrowException(new Exception); + + try { + $mock->doSomething(); + } + + catch (Exception $e) { + return; + } + + $this->fail(); + } + public function testStubbedReturnValue() { $mock = $this->getMock('AnInterface'); @@ -167,6 +185,13 @@ public function testStubbedReturnValue() ->will($this->returnValue('something')); $this->assertEquals('something', $mock->doSomething()); + + $mock = $this->getMock('AnInterface'); + $mock->expects($this->any()) + ->method('doSomething') + ->willReturn('something'); + + $this->assertEquals('something', $mock->doSomething()); } public function testStubbedReturnValueMap() @@ -184,6 +209,32 @@ public function testStubbedReturnValueMap() $this->assertEquals('d', $mock->doSomething('a', 'b', 'c')); $this->assertEquals('h', $mock->doSomething('e', 'f', 'g')); $this->assertEquals(NULL, $mock->doSomething('foo', 'bar')); + + $mock = $this->getMock('AnInterface'); + $mock->expects($this->any()) + ->method('doSomething') + ->willReturnMap($map); + + $this->assertEquals('d', $mock->doSomething('a', 'b', 'c')); + $this->assertEquals('h', $mock->doSomething('e', 'f', 'g')); + $this->assertEquals(NULL, $mock->doSomething('foo', 'bar')); + } + + public function testStubbedReturnArgument() + { + $mock = $this->getMock('AnInterface'); + $mock->expects($this->any()) + ->method('doSomething') + ->will($this->returnArgument(1)); + + $this->assertEquals('b', $mock->doSomething('a', 'b')); + + $mock = $this->getMock('AnInterface'); + $mock->expects($this->any()) + ->method('doSomething') + ->willReturnArgument(1); + + $this->assertEquals('b', $mock->doSomething('a', 'b')); } public function testFunctionCallback() @@ -194,6 +245,51 @@ public function testFunctionCallback() ->will($this->returnCallback('functionCallback')); $this->assertEquals('pass', $mock->doSomething('foo', 'bar')); + + $mock = $this->getMock('SomeClass', array('doSomething'), array(), '', FALSE); + $mock->expects($this->once()) + ->method('doSomething') + ->willReturnCallback('functionCallback'); + + $this->assertEquals('pass', $mock->doSomething('foo', 'bar')); + } + + public function testStubbedReturnSelf() + { + $mock = $this->getMock('AnInterface'); + $mock->expects($this->any()) + ->method('doSomething') + ->will($this->returnSelf()); + + $this->assertEquals($mock, $mock->doSomething()); + + $mock = $this->getMock('AnInterface'); + $mock->expects($this->any()) + ->method('doSomething') + ->willReturnSelf(); + + $this->assertEquals($mock, $mock->doSomething()); + } + + public function testStubbedReturnOnConsecutiveCalls() + { + $mock = $this->getMock('AnInterface'); + $mock->expects($this->any()) + ->method('doSomething') + ->will($this->onConsecutiveCalls('a', 'b', 'c')); + + $this->assertEquals('a', $mock->doSomething()); + $this->assertEquals('b', $mock->doSomething()); + $this->assertEquals('c', $mock->doSomething()); + + $mock = $this->getMock('AnInterface'); + $mock->expects($this->any()) + ->method('doSomething') + ->willReturnOnConsecutiveCalls('a', 'b', 'c'); + + $this->assertEquals('a', $mock->doSomething()); + $this->assertEquals('b', $mock->doSomething()); + $this->assertEquals('c', $mock->doSomething()); } public function testStaticMethodCallback() @@ -304,6 +400,21 @@ public function testStubbedReturnValueForStaticMethod() $this->assertEquals( 'something', StaticMockTestClassMock::doSomething() ); + + $this->getMockClass( + 'StaticMockTestClass', + array('doSomething'), + array(), + 'StaticMockTestClassMock' + ); + + StaticMockTestClassMock::staticExpects($this->any()) + ->method('doSomething') + ->willReturn('something'); + + $this->assertEquals( + 'something', StaticMockTestClassMock::doSomething() + ); } public function testStubbedReturnValueForStaticMethod2() @@ -322,6 +433,21 @@ public function testStubbedReturnValueForStaticMethod2() $this->assertEquals( 'something', StaticMockTestClassMock2::doSomethingElse() ); + + $this->getMockClass( + 'StaticMockTestClass', + array('doSomething'), + array(), + 'StaticMockTestClassMock2' + ); + + StaticMockTestClassMock2::staticExpects($this->any()) + ->method('doSomething') + ->willReturn('something'); + + $this->assertEquals( + 'something', StaticMockTestClassMock2::doSomethingElse() + ); } public function testGetMockForAbstractClass()