Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active July 11, 2022 06:09
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save JeffreyWay/5287312 to your computer and use it in GitHub Desktop.
Save JeffreyWay/5287312 to your computer and use it in GitHub Desktop.
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;
}
}
@troccoli
Copy link

Great gist. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment