Last active
July 11, 2022 06:09
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
Great gist. Thanks.