set accessible does not make the private propery accessible when used it mockery

577 views Asked by At

I am tying to mock a class to test it. It has a private property detail which is set by a network request. I am trying to set the property before testing other methods by mocking a json file. Everything is working fine except, I cannot seem to set the property when it is a private property but works when it is a protected property.

$mockedClass = \Mockery::mock( Myclass::class )->makePartial();
$reflection = new \ReflectionClass($mockedClass);
$property = $reflection->getProperty( 'detail' );
$property->setAccessible(true);
$property->setValue($mockedClass, $jsonData ); 

so when detail is a private property it Mockery throws a Property detail does not exist but when I make detail protected, it works.

I do not want to make detail a protected property because it does not need to be but I am needing to make it so to test it.

As I read somewhere "Like you mother said, do not expose your privates". I do not want to expose my privates.

1

There are 1 answers

1
John Joseph On BEST ANSWER

Try making this slight change:

$mockedClass = \Mockery::mock( Myclass::class )->makePartial();
$reflection = new \ReflectionClass(Myclass::class); // Pass the class name, not the actual mock object
$property = $reflection->getProperty( 'detail' );
$property->setAccessible(true);
$property->setValue($mockedClass, $jsonData );