如何使PHPUnit Coverage功能适用于Class中的静态方法

时间:2019-04-02 13:10:15

标签: php phpunit

我正在使用PHPUnit 8编写测试用例,但是遇到一个问题,即PHPUnit 8的代码覆盖功能不适用于类的静态方法。

我的环境在Windows 7下,使用以下组件:

  • Composer版本1.8.0 2018-12-03 10:31:16
  • PHPUnit 8
  • 带有Xdebug v2.6.0的PHP CLI 7.2.4

这是我写的一些代码。

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.0/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         executionOrder="depends,defects"
         forceCoversAnnotation="true"
         beStrictAboutCoversAnnotation="true"
         beStrictAboutOutputDuringTests="true"
         beStrictAboutTodoAnnotatedTests="true"
         verbose="true">
    <testsuites>
        <testsuite name="default">
            <directory suffix="Test.php">tests</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">src</directory>
        </whitelist>
    </filter>
</phpunit>
class UtilsTest extends TestCase {
    /**
     * @return DateTime
     */
    public function testAddDays() {
        $dateTime = new DateTime('2019-04-02');
        $actualDateTime = Utils::addDays($dateTime, 8);
        $this->assertSame('2019-04-10', $actualDateTime->format('Y-m-d'));

        return $actualDateTime;
    }

    /**
     * @param DateTime
     * @depends testAddDays
     */
    public function testAddHours(DateTime $dateTime) {
        $actualDateTime = Utils::addHours($dateTime, 8);
        $this->assertSame('2019-04-10 08', $actualDateTime->format('Y-m-d H'));
    }

    public function testIsWhichDayInWeek() {
        $dt0 = new DateTime('2019-04-08');
        $dt1 = new DateTime('2019-04-09');
        $dt2 = new DateTime('2019-04-10');
        $dt3 = new DateTime('2019-04-11');
        $dt4 = new DateTime('2019-04-12');
        $dt5 = new DateTime('2019-04-13');
        $dt6 = new DateTime('2019-04-14');

        $this->assertSame(true, Utils::isMonday($dt0));
        $this->assertSame(true, Utils::isTuesday($dt1));
        $this->assertSame(true, Utils::isWednesday($dt2));
        $this->assertSame(true, Utils::isThursday($dt3));
        $this->assertSame(true, Utils::isFriday($dt4));
        $this->assertSame(true, Utils::isSaturday($dt5));
        $this->assertSame(true, Utils::isSunday($dt6));
    }
}

运行命令.\vendor\bin\phpunit tests\UtilsTest.php --coverage-html ./build时无法计算代码覆盖率

enter image description here

0 个答案:

没有答案