#development #laravel #php #testing

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:

1public static function getProperty($object, $property)
2{
3    $reflectedClass = new \ReflectionClass($object);
4    $reflection = $reflectedClass->getProperty($property);
5    $reflection->setAccessible(true);
6    return $reflection->getValue($object);
7}

And then in the unit test, use that like this:

1$value = ReflectionHelper::getProperty($class, 'someProperty');
2$this->assertSame($expected, $value);

Originally found the code here.