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

Simplified will return #147

Merged
merged 3 commits into from
Dec 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions PHPUnit/Framework/MockObject/Builder/InvocationMocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
126 changes: 126 additions & 0 deletions Tests/MockObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down