使用phpunit模拟抽象类中的具体方法

17
有没有好的方法可以使用PHPUnit模拟抽象类中的具体方法?
目前我找到的解决方案有:
- expects() ->will() 可以很好地用于抽象方法。 - 但是对于具体方法,它不起作用。原始方法仍会运行。 - 使用MockBuilder并将所有抽象方法和具体方法传递给setMethods()可以解决问题,但需要指定所有抽象方法,这样测试就变得脆弱且过度冗长。 - MockBuilder :: getMockForAbstractClass() 忽略setMethod()。
这里是一些单元测试,说明了上述要点:
abstract class AbstractClass {
    public function concreteMethod() {
        return $this->abstractMethod();
    }

    public abstract function abstractMethod();
}


class AbstractClassTest extends PHPUnit_Framework_TestCase {
    /**
     * This works for abstract methods.
     */
    public function testAbstractMethod() {
        $stub = $this->getMockForAbstractClass('AbstractClass');
        $stub->expects($this->any())
                ->method('abstractMethod')
                ->will($this->returnValue(2));

        $this->assertSame(2, $stub->concreteMethod()); // Succeeds
    }

    /**
     * Ideally, I would like this to work for concrete methods too.
     */
    public function testConcreteMethod() {
        $stub = $this->getMockForAbstractClass('AbstractClass');
        $stub->expects($this->any())
                ->method('concreteMethod')
                ->will($this->returnValue(2));

        $this->assertSame(2, $stub->concreteMethod()); // Fails, concreteMethod returns NULL
    }

    /**
     * One way to mock the concrete method, is to use the mock builder,
     * and set the methods to mock.
     *
     * The downside of doing it this way, is that all abstract methods
     * must be specified in the setMethods() call. If you add a new abstract
     * method, all your existing unit tests will fail.
     */
    public function testConcreteMethod__mockBuilder_getMock() {
        $stub = $this->getMockBuilder('AbstractClass')
                ->setMethods(array('concreteMethod', 'abstractMethod'))
                ->getMock();
        $stub->expects($this->any())
                ->method('concreteMethod')
                ->will($this->returnValue(2));

        $this->assertSame(2, $stub->concreteMethod()); // Succeeds
    }

    /**
     * Similar to above, but using getMockForAbstractClass().
     * Apparently, setMethods() is ignored by getMockForAbstractClass()
     */
    public function testConcreteMethod__mockBuilder_getMockForAbstractClass() {
        $stub = $this->getMockBuilder('AbstractClass')
                ->setMethods(array('concreteMethod'))
                ->getMockForAbstractClass();
        $stub->expects($this->any())
                ->method('concreteMethod')
                ->will($this->returnValue(2));

        $this->assertSame(2, $stub->concreteMethod()); // Fails, concreteMethod returns NULL
    }
}

你不需要测试抽象类,因为它们是抽象的。或者你想写一个抽象的测试用例吗? - hakre
抽象类是另一个类的依赖项。因此,我想测试SomeClass::getMyCalculatedValue()方法,该方法使用$object->concreteMethod()。由于concreteMethod()方法可能会更改或设置起来可能很困难,因此我想指定concreteMethod()方法的返回值。 - CheeseSucker
2个回答

19

5

我在基础测试用例中重写了getMock()方法,以添加所有抽象方法,因为你必须无论如何都要模拟它们。你可能可以用类似的方式处理构建器。

重要提示:你不能模拟私有方法。

public function getMock($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE) {
    if ($methods !== null) {
        $methods = array_unique(array_merge($methods, 
                self::getAbstractMethods($originalClassName, $callAutoload)));
    }
    return parent::getMock($originalClassName, $methods, $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload);
}

/**
 * Returns an array containing the names of the abstract methods in <code>$class</code>.
 *
 * @param string $class name of the class
 * @return array zero or more abstract methods names
 */
public static function getAbstractMethods($class, $autoload=true) {
    $methods = array();
    if (class_exists($class, $autoload) || interface_exists($class, $autoload)) {
        $reflector = new ReflectionClass($class);
        foreach ($reflector->getMethods() as $method) {
            if ($method->isAbstract()) {
                $methods[] = $method->getName();
            }
        }
    }
    return $methods;
}

这与 getMockForAbstractClass 有什么不同? - FoolishSeth
1
@FoolishSeth PHPUnit的早期版本不允许您指定要模拟的其他具体方法。由于您无法实例化一个未模拟每个抽象方法的模拟对象,因此在调用getMock()等方法时将它们合并到列表中似乎是合理的。 - David Harkness

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接