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?
The issue I see is that you try to access the value of the MockedTrait::$model using the reflection of the A trait. But probably the "signature" of MockedTrait::$model is different from the one of A::$model.
Since Mock objects class are declared using eval in phpunit, MockedTrait class is reachable and you can try something like below:
$reflection = new \ReflectionClass(MockedTrait::class);
and this should work (probably).