We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
When you create tests in Laravel, you sometimes need to be able to access private properties.
There is a way to get around this by using the following helper method:
public static function getProperty($object, $property)
{
$reflectedClass = new \ReflectionClass($object);
$reflection = $reflectedClass->getProperty($property);
$reflection->setAccessible(true);
return $reflection->getValue($object);
}
And then in the unit test, use that like this:
$value = ReflectionHelper::getProperty($class, 'someProperty');
$this->assertSame($expected, $value);
Originally found the code here.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.