PHPUnit layout parent method

I have a problem with a mocking parent method, here is an 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 the mock getPath () method and the getParam () call method, which retrieves the mocked value.

$mock = $this->getMock('PathProvider');

$mock->expects($this->any())
->method('getPath')
->will($this->returnValue('/panel2.0/user/index/id/5'));

I wrote this part, but I do not know how to pass this mocked value to the testing method.

+5
source share
2 answers

You just need a mock Uriclass. You can make fun of only one method ( getPath), for example:

$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 check your object as usual:

$this->assertEquals($expectedParam, $sut->getParam('someParam'));
+5
source

My friends and I write mockito as a mock library. ouzo-goddies # mocking

$mock = Mock::create('\Appropriate\Namespace\Uri');
Mock::when($mock)->getPath()->thenReturn(result);
+4
source

All Articles