Question

I am getting a mock for an abstract class and am using expects to verify a specific method is called twice with some particular values. This assertion is failing, even though the production code is correct. This is when I use the getMockForAbstractClass method to create the mock. Now when I create a concrete derivative of this abstract class, and feed that into getMock, it does work. That approach is however not nice, so I want to know if I can avoid it.

First attempt, using getMockForAbstractClass that does not work: http://pastebin.com/09n92Q6h

Second attempt, using a concrete derivative, that does work: http://pastebin.com/SxxgN5Cw

Was it helpful?

Solution

With help of ocramius I found that the concrete methods of abstract classes do not get mocked out by default. One needs to specify the concrete methods that need to be mocked in getMockForAbstractClass.

private function getMockedAbstractClass($className, array $methods)
{
    return $this->getMockForAbstractClass(
        $className,
        array(),
        '',
        true,
        true,
        true,
        $methods
    );
}

Using the mock builder interface for this is somewhat nicer:

    $platform = $this->getMockBuilder($className)
        ->setMethods($methods)
        ->getMockForAbstractClass();

The full solution can be seen here: https://github.com/doctrine/dbal/pull/586/files

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top