使用PHPUnit测试受保护方法的最佳实践
发布于 2021-02-02 11:51:52
关注者
0
被浏览
101
1 个回答
-
如果您将PHP5(> = 5.3.2)与PHPUnit一起使用,则可以在运行测试之前通过使用反射将其设置为公共来测试私有和受保护的方法:
protected static function getMethod($name) { $class = new ReflectionClass('MyClass'); $method = $class->getMethod($name); $method->setAccessible(true); return $method; } public function testFoo() { $foo = self::getMethod('foo'); $obj = new MyClass(); $foo->invokeArgs($obj, array(...)); ... }