Created
September 25, 2015 16:22
-
-
Save danielcosta/524b2e664233450c0d0e to your computer and use it in GitHub Desktop.
If you really need to test a protected class...
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 | |
class ProtectedMethodTest extends PHPUnit_Framework_TestCase | |
{ | |
public function testProtectedMethod() | |
{ | |
$method = self::getMethod( | |
'MyClass', | |
'getProtectedMethodReturnsTrue' | |
); | |
$realClass = new MyClass(); | |
$this->assertTrue($class->invokeArgs($realClass, array($method))); | |
} | |
protected function getMethod($class, $method) | |
{ | |
$class = new ReflectionClass($class); | |
$method = $class->getMethod($method); | |
$method->setAccessible(true); | |
return $method; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment