I have problem with mocking parent method, this is example:
class PathProvider
{
public function getPath()
{
return isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
}
}
class Uri extends PathProvider
{
public function getParam($param)
{
$path = $this->getPath();
if ($path == $param)
return 'OK';
else
return 'Bad';
}
}
And now i want mock method getPath(), and call method getParam() which recive mocked value.
$mock = $this->getMock('PathProvider');
$mock->expects($this->any())
->method('getPath')
->will($this->returnValue('/panel2.0/user/index/id/5'));
I was write this part, but I don't know how I must pass this mocked value to testing method.
You just need mock Uri
class. You can mock only one method (getPath
), like that:
$sut = $this->getMock('Appropriate\Namespace\Uri', array('getPath'));
$sut->expects($this->any())
->method('getPath')
->will($this->returnValue('/panel2.0/user/index/id/5'));
And then you can test your object as usual:
$this->assertEquals($expectedParam, $sut->getParam('someParam'));
Me and my friends write mockito like mocking library. ouzo-goddies#mocking
$mock = Mock::create('\Appropriate\Namespace\Uri');
Mock::when($mock)->getPath()->thenReturn(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With