PhpUnit – mock – internal set protected / private variable

Publicités

Using mock it is often necessary to give a value to a private or protected variable. To do this, just use the Reflection class.

$basket - $this--getMockBuilder ('Basket')
    OriginalConstructor()
    -getMock(;
$basketReflection - new ReflectionClass ($basket);
$sessionId - $basketReflection--getProperty ('sessionId');
$sessionId-setAccessible (true);
$sessionId-setValue ($basket, 'abc');

so the sessionValueId in our Mock will be 'abc'.

Publicités

Leave a Reply