8
abstract class MyClass
{
    private static makeMePublic()
    {
    }
}

I want to make MyClass::makeMePublic method to be callable from the outside. I saw a solution here: Best practices to test protected methods with PHPUnit but that requires the class to be instantized. In this case its not possible. So, how to make "public" this method?

Community
  • 1
  • 1
John Smith
  • 6,129
  • 12
  • 68
  • 123

1 Answers1

15

The docs say you can just pass null as the first param to invokeArgs to execute a static method.

protected static function getMethod($name) {
  $class = new ReflectionClass('MyClass');
  $method = $class->getMethod($name);
  $method->setAccessible(true);
  return $method;
}

public function testMakeMePublic() {
  $foo = self::getMethod('makeMePublic');
  $foo->invokeArgs(null, $args);
  ...
}
mpen
  • 272,448
  • 266
  • 850
  • 1,236