3
votes

I'm trying to do unit testing in symfony 1.4 (actually it's 1.5.3 (https://github.com/LExpress/symfony1)) with phpunit and mockery. Is there a way to load all files of symfony and then, if needed, create mock object from a loaded class? The error message is: "Could not load mock {ClassName}, class already exists", which is pretty self explanatory, but I would like to use some of the original methods, not just the ones I've mocked. Is there a way to do that?

For example:

public funtion testTest() {
    $mock = Mockery::mock("alias:Site")->shouldReceive('getCurrent')->shouldReturn(3);
    $this->assertEquals(3, Project::test());
}

public static function test() {
    return Site::getCurrent();
}

If I include only the Project class, it works, but if all project files are included I get the error message. But what if the test() function uses other methods of the Site object, which I don't want to mock?

1

1 Answers

1
votes

If you would like to use some of the original methods of the class used to produce the mock object via Mockery, there is the makePartial function that can help us. This is called Partial Test Doubles.

class Site {
    function getUrl() { return 'https://stackoverflow.com'; }
    function getCurrent() { return $this->getUrl(); }
}

$foo = mock(Testable::class)->makePartial();
$foo->getUrl(); // 'https://stackoverflow.com';
$foo->getCurrent(); // 'https://stackoverflow.com'

$foo->shouldReceive('getUrl')->andReturn('http://test.com');
$foo->getCurrent(); // 'http://test.com'

To know more about the Partial Test Doubles here is the official documentation link http://docs.mockery.io/en/latest/reference/creating_test_doubles.html#partial-test-doubles