mercredi 5 avril 2017

How to get private property of a mock trait via reflection?

Trait looks something like this:

trait A{

    private $model;

    public function __construct($model){
        $this->model = $model;
        $this->methodToStub();    
    }
...
}

When I try to get $model property of a mocked trait with a reflection in unit test:

$mock = $this->getMockForTrait(A::class,[],'MockedTrait', false, true, true, ['methodToStub']);
$mock->expects($this->exactly(1))->method('methodToStub');
$mock->__construct('model');

$reflection = new \ReflectionClass(A::class);
$property = $reflection->getProperty('model');
$property->setAccessible(true);
$value = $property->getValue($mock);

I receive this error:

Undefined property: MockedTrait::$model

Any ideas, why?





Aucun commentaire:

Enregistrer un commentaire