我发现了关于你是否测试私有方法的讨论。

我已经决定,在某些类中,我希望有受保护的方法,但要测试它们。 其中一些方法是静态的和简短的。因为大多数公共方法都使用了这些测试,所以我以后可能会安全地删除这些测试。但是为了从TDD方法开始并避免调试,我真的很想测试它们。

我想到了以下几点:

在回答中建议的方法对象似乎是多余的。 从公共方法开始,当代码覆盖由更高级别的测试提供时,将它们变为受保护的,并删除测试。 继承一个具有可测试接口的类,该接口使受保护的方法公开

哪种是最佳实践?还有别的事吗?

看起来,JUnit会自动将受保护的方法更改为公共方法,但我并没有深入了解它。PHP不允许通过反射进行此操作。


当前回答

Teastburn的方法是正确的。更简单的方法是直接调用该方法并返回答案:

class PHPUnitUtil
{
  public static function callMethod($obj, $name, array $args) {
        $class = new \ReflectionClass($obj);
        $method = $class->getMethod($name);
        $method->setAccessible(true);
        return $method->invokeArgs($obj, $args);
    }
}

您可以在测试中通过以下方式简单地调用它:

$returnVal = PHPUnitUtil::callMethod(
                $this->object,
                '_nameOfProtectedMethod', 
                array($arg1, $arg2)
             );

其他回答

你确实可以以通用的方式使用__call()来访问受保护的方法。以便能够测试这个类

class Example {
    protected function getMessage() {
        return 'hello';
    }
}

在ExampleTest.php中创建一个子类:

class ExampleExposed extends Example {
    public function __call($method, array $args = array()) {
        if (!method_exists($this, $method))
            throw new BadMethodCallException("method '$method' does not exist");
        return call_user_func_array(array($this, $method), $args);
    }
}

注意,__call()方法不会以任何方式引用类,所以你可以将上面的方法复制到每个你想测试的受保护方法的类中,只需要更改类声明。您可以将此函数放在公共基类中,但我还没有尝试过。

现在,测试用例本身的不同之处在于您在哪里构造要测试的对象,例如交换ExampleExposed。

class ExampleTest extends PHPUnit_Framework_TestCase {
    function testGetMessage() {
        $fixture = new ExampleExposed();
        self::assertEquals('hello', $fixture->getMessage());
    }
}

我相信PHP 5.3允许您使用反射来直接更改方法的可访问性,但我假设您必须对每个方法单独这样做。

我想troelskn就在附近。我会这样做:

class ClassToTest
{
   protected function testThisMethod()
   {
     // Implement stuff here
   }
}

然后,实现如下内容:

class TestClassToTest extends ClassToTest
{
  public function testThisMethod()
  {
    return parent::testThisMethod();
  }
}

然后根据TestClassToTest运行测试。

应该可以通过解析代码自动生成这样的扩展类。如果PHPUnit已经提供了这样的机制,我不会感到惊讶(尽管我还没有检查)。

我建议以下“亨里克·保罗”的解决方案/想法的解决方案:)

您知道类的私有方法的名称。例如,它们像_add(), _edit(), _delete()等。

因此,当你想从单元测试的角度测试它时,只需通过添加前缀和/或后缀一些常见的单词(例如_addPhpunit)来调用私有方法,这样当所有者类的__call()方法被调用时(因为方法_addPhpunit()不存在),你只需在__call()方法中放入必要的代码来删除带前缀/后缀的单词/s (Phpunit),然后从那里调用推断出的私有方法。这是另一个很好的使用魔术的方法。

试试吧。

如果你在PHPUnit中使用PHP5(>= 5.3.2),你可以在运行测试之前使用反射将私有方法和受保护方法设置为公共:

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(...));
  ...
}

Teastburn的方法是正确的。更简单的方法是直接调用该方法并返回答案:

class PHPUnitUtil
{
  public static function callMethod($obj, $name, array $args) {
        $class = new \ReflectionClass($obj);
        $method = $class->getMethod($name);
        $method->setAccessible(true);
        return $method->invokeArgs($obj, $args);
    }
}

您可以在测试中通过以下方式简单地调用它:

$returnVal = PHPUnitUtil::callMethod(
                $this->object,
                '_nameOfProtectedMethod', 
                array($arg1, $arg2)
             );