简体   繁体   English

PHPUnit:在没有具体方法的情况下测试抽象类

[英]PHPUnit: Test an abstract class without concrete methods

I've following abstract class and have a question about how to write a unit test for this. 我正在关注抽象类,并且对如何为此编写单元测试有疑问。 Is this in fact needed? 实际上这是必需的吗? As this class doesn't have any concrete methods. 由于此类没有任何具体方法。

<?php

abstract class PickupPoint_Abstract {
    public function __construct($client) {}
    public function getPickupPoints($countryCode, $postalCode, $city) {}
    public function getPickupPointDetails($pickupPointId, $countryCode) {} }

Since you can't test for any expected behavior it seems really hard to produce any meaningful test. 由于您无法测试任何预期的行为,因此似乎很难进行任何有意义的测试。


If you have at least one concrete method you can mock the class and test that one method but without any code to be tested i'd say you are good to go. 如果您至少有一个具体方法,则可以模拟该类并测试该方法,但无需测试任何代码,我会说您很好。

( I guess you know that always but for sake of completion: See Example 11.8 for getMockForAbstractClass on the phpunit site ) (我想你总是知道,但是为了完成: 请参见示例11.8,了解phpunit网站上的getMockForAbstractClass


Even so i'm curios to why you didn't define the methods as abstract like this: 即便如此,我还是想知道为什么您没有将方法定义为这样的抽象:

<?php
abstract class PickupPoint_Abstract {
    abstract public function __construct($client);
    abstract public function getPickupPoints($countryCode, $postalCode, $city);
    abstract public function getPickupPointDetails($pickupPointId, $countryCode); }

since only then the interpreter will enforce that all methods are implemented in the child classes. 因为只有这样,解释器才会强制所有方法都在子类中实现。

No, you don't need to test it, because there is nothing to test. 不,您不需要测试,因为没有要测试的东西。

By the way abstract methods should be defined like 顺便说一句,抽象方法应该像

<?php
abstract class PickupPoint_Abstract {
    public function __construct($client) {}
    abstract public function getPickupPoints($countryCode, $postalCode, $city);
    abstract public function getPickupPointDetails($pickupPointId, $countryCode);
}
?>

You made hooks, which may not be overridden. 您制作了挂钩,这些挂钩可能不会被覆盖。

See Class Abstraction . 请参阅类抽象

For this case and for testing an Interface I would write at least 3 tests: 对于这种情况和测试接口,我将至少编写3个测试:

protected setUp() {
    $this->_object = $this->getMockForAbstractClass(
       'PickupPoint_Abstract', array(), '', false
    );
}

public function testInstanceOf() {
    $this->assertInstanceOf('PickupPoint_Abstract', $this->_object);
}

public function testMethodsExistance() {
    $methods = get_class_methods($this->_object);
    $this->assertTrue(in_array('getPickupPoints', $methods));
    $this->assertTrue(in_array('getPickupPointDetails', $methods));
    $this->assertTrue(in_array('__construct', $methods));
}

public function testMethodCount() {
    $methods = get_class_methods($this->_object);
    /**
     * PHPUnit add seven own methods in 3.6.11 + __clone + count of these methods
     */
    $this->assertEquals(11, count($methods));
}

With these tests you will prevent typos, check the existance of the required methods and if any new methods will be added, this test will be broken, because the number of methods has changed, and this is the behavior we want. 使用这些测试,您可以防止输入错误,检查是否存在必需的方法,并且如果要添加任何新方法,则该测试将被破坏,因为方法的数量已更改,这就是我们想要的行为。

Well this works fine for me. 好吧,这对我来说很好。 I always use this tests for interfaces, but i think it can be used for abstract classes to! 我一直将此测试用于接口,但我认为它可以用于抽象类!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM