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

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

我想到了以下几点:

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

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

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


当前回答

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

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

然后,实现如下内容:

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

然后根据TestClassToTest运行测试。

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

其他回答

You seem to be aware already, but I'll just restate it anyway; It's a bad sign, if you need to test protected methods. The aim of a unit test, is to test the interface of a class, and protected methods are implementation details. That said, there are cases where it makes sense. If you use inheritance, you can see a superclass as providing an interface for the subclass. So here, you would have to test the protected method (But never a private one). The solution to this, is to create a subclass for testing purpose, and use this to expose the methods. Eg.:

class Foo {
  protected function stuff() {
    // secret stuff, you want to test
  }
}

class SubFoo extends Foo {
  public function exposedStuff() {
    return $this->stuff();
  }
}

注意,您总是可以用组合替换继承。在测试代码时,处理使用这种模式的代码通常要容易得多,因此您可能需要考虑该选项。

的选择。下面的代码是作为示例提供的。 它的实施可以更广泛。 它的实现将帮助您测试私有方法并替换私有属性。

    <?php
    class Helper{
        public static function sandbox(\Closure $call,$target,?string $slaveClass=null,...$args)
        {
            $slaveClass=!empty($slaveClass)?$slaveClass:(is_string($target)?$target:get_class($target));
            $target=!is_string($target)?$target:null;
            $call=$call->bindTo($target,$slaveClass);
            return $call(...$args);
        }
    }
    class A{
        private $prop='bay';
        public function get()
        {
            return $this->prop;    
        }
        
    }
    class B extends A{}
    $b=new B;
    $priv_prop=Helper::sandbox(function(...$args){
        return $this->prop;
    },$b,A::class);
    
    var_dump($priv_prop);// bay
    
    Helper::sandbox(function(...$args){
        $this->prop=$args[0];
    },$b,A::class,'hello');
    var_dump($b->get());// hello

为了单元测试的目的,我做了一个类来调用简单的私有方法(静态和非静态):

class MethodInvoker
{
    public function invoke($object, string $methodName, array $args=[]) {
        $privateMethod = $this->getMethod(get_class($object), $methodName);

        return $privateMethod->invokeArgs($object, $args);
    }

    private function getMethod(string $className, string $methodName) {
        $class = new \ReflectionClass($className);
        
        $method = $class->getMethod($methodName);
        $method->setAccessible(true);
    
        return $method;
    }
}

用法示例:

class TestClass {
    private function privateMethod(string $txt) {
        print_r('invoked privateMethod: ' . $txt);
    }
}

(new MethodInvoker)->invoke(new TestClass, 'privateMethod', ['argument_1']);

我在这里郑重声明:

我使用过__call黑客,成功程度不一。 我想到的另一种选择是使用访问者模式:

1:生成一个stdClass或自定义类(以加强类型)

2:用所需的方法和参数来启动它

3:确保你的SUT有一个acceptVisitor方法,它将使用访问类中指定的参数执行方法

4:将其注入到您希望测试的类中

5: SUT将操作结果注入访问者

6:将测试条件应用到访问者的结果属性

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

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

然后,实现如下内容:

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

然后根据TestClassToTest运行测试。

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