单元测试代码覆盖率显示零百分比?

时间:2018-10-12 07:44:08

标签: php unit-testing zend-framework2 phpunit xdebug

正在运行测试,但是我获得成功的结果,但是打开Resource时,它显示了0%的类和方法代码覆盖率,但列出了白名单文件。这些方法以红色突出显示,表示未执行。

这是phpunit.xml:

dashboard.html

测试控制器

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="Bootstrap.php" colors="true" verbose="true">
    <php>
        <env name="APPLICATION_ENV" value="local"/>
    </php>
    <testsuites>
        <testsuite name="page">
            <directory>./</directory>
        </testsuite>
    </testsuites>
    <!-- This block is important. We define the type of output (HTML) and where the html report files will be generated (./coverage) after each test run, so we can see the statistics. -->
    <logging>
        <log type='coverage-html' target='./coverage' />
    </logging>
    <filter>
        <!-- You can use <directory> to run code coverage / unit tests on a directory, or <file> tags to explicitly include a file from a directory.-->
        <whitelist>
            <directory suffix=".php">../src/Model</directory>
        </whitelist>
    </filter>
</phpunit>

Bootstrap.php

namespace Page\Model;

use Page\Model\Pages;
use PHPUnit\Framework\TestCase;

class PagesTest extends TestCase
{
    protected $stubData = [
        'page_id' => '5555',
        'parent_page_id'=> '0',
    ];

    public function testPage()
    {
        $stub = $this->createMock(Pages::class);
        $stub->method('exchangeArray')->willReturn(true);

        $this->assertSame(true, $stub->exchangeArray($this->stubData));
    }
}

我已安装xdebug:

namespace Page;

use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
use RuntimeException;

error_reporting(E_ALL | E_STRICT);
chdir(__DIR__);

class Bootstrap
{
    protected static $serviceManager;
    protected static $config;
    protected static $bootstrap;

    public static function init()
    {
        // Load the user-defined test configuration file, if it exists; otherwise, load
        if (is_readable(__DIR__ . '/TestConfig.php')) {
            $testConfig = include __DIR__ . '/TestConfig.php';
        } else {
            $testConfig = include __DIR__ . '/TestConfig.php.dist';
        }

        $zf2ModulePaths = array();

        if (isset($testConfig['module_listener_options']['module_paths'])) {
            $modulePaths = $testConfig['module_listener_options']['module_paths'];
            foreach ($modulePaths as $modulePath) {
                if (($path = static::findParentPath($modulePath))) {
                    $zf2ModulePaths[] = $path;
                }
            }
        }

        $zf2ModulePaths = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR;
        $zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ? : (defined('ZF2_MODULES_TEST_PATHS') ? ZF2_MODULES_TEST_PATHS : '');

        static::initAutoloader();

        // use ModuleManager to load this module and it's dependencies
        $baseConfig = array(
            'module_listener_options' => array(
                'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths),
            ),
        );

        $config = ArrayUtils::merge($baseConfig, $testConfig);

        $serviceManager = new ServiceManager(new ServiceManagerConfig());
        $serviceManager->setService('ApplicationConfig', $config);
        $serviceManager->get('ModuleManager')->loadModules();

        static::$serviceManager = $serviceManager;
        static::$config = $config;
    }

    public static function getServiceManager()
    {
        return static::$serviceManager;
    }

    public static function getConfig()
    {
        return static::$config;
    }

    protected static function initAutoloader()
    {
        $vendorPath = static::findParentPath('vendor');

        if (is_readable($vendorPath . '/autoload.php')) {
            $loader = include $vendorPath . '/autoload.php';
        } else {
            $zf2Path = getenv('ZF2_PATH') ? : (defined('ZF2_PATH') ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library') ? $vendorPath . '/ZF2/library' : false));

            if (!$zf2Path) {
                throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
            }

            include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
        }

        ## You can remove this block if you are using psr-4 autoloading.
        AutoloaderFactory::factory(array(
            'Zend\Loader\StandardAutoloader' => array(
                'autoregister_zf' => true,
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
                ),
            ),
        ));
    }

    protected static function findParentPath($path)
    {
        $dir = __DIR__;
        $previousDir = '.';
        while (!is_dir($dir . '/' . $path)) {
            $dir = dirname($dir);
            if ($previousDir === $dir) return false;
            $previousDir = $dir;
        }
        return $dir . '/' . $path;
    }
}
Bootstrap::init();

我正在使用PHP 7.2.5和xdebug v2.6.0在docker容器中运行它。

1 个答案:

答案 0 :(得分:0)

我已经找到了问题的原因。

这是因为我执行测试的路径中的小写字母本应大写,即我在/ p roject / test而不是/ 内执行P 拒绝/测试。