Skip to content

Instantly share code, notes, and snippets.

@danielcosta
Created September 25, 2015 16:22
Show Gist options
  • Save danielcosta/524b2e664233450c0d0e to your computer and use it in GitHub Desktop.
Save danielcosta/524b2e664233450c0d0e to your computer and use it in GitHub Desktop.
If you really need to test a protected class...
<?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