mercredi 31 août 2016

How can I set protected properties of mocks for abstract classes?

I've looked around and I found a solution that works for normal objects, but it doesn't seem to work for mocks.

The test below fails with the message: Property someProperty does not exist.

class sampleTestClass extends PHPUnit_Framework_TestCase
{
    function test() {
        $object     = $this->getMockForAbstractClass(ClassToTest::class, [], '', false);
        $reflection = new ReflectionClass(get_class($object));
        $property   = $reflection->getProperty('someProperty');
        $property->setAccessible(true);
        $property->setValue($object, 'value');
    }
}

abstract class ClassToTest
{
    private $someProperty;
    abstract function someFunc();
}

Aucun commentaire:

Enregistrer un commentaire