Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
When you need to make a protected/private property public for testing/inspection.
<?php
# ignore
use \Way\Console\Guardfile;
use Mockery as m;
class GuardfileTest extends \PHPUnit_Framework_TestCase {
public function testCanOverrideDefaultPath()
{
$file = m::mock('Illuminate\Filesystem\Filesystem');
$guardFile = new Guardfile($file, 'foo/bar');
# When we need to inspect private properties,
# let's use a helper method.
$pathProperty = $this->makePublic($guardFile, 'path');
# Use getValue($instance) to fetch the value of the prop.
$this->assertEquals('foo/bar', $pathProperty->getValue($guardFile));
}
protected function makePublic($obj, $property)
{
$reflect = new \ReflectionObject($obj);
$property = $reflect->getProperty($property);
$property->setAccessible(true);
return $property;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment