php - 如何将未覆盖文件添加到 Yii 应用程序的 PHPUnit 代码覆盖率报告中

标签 php yii phpunit code-coverage

我目前正在尝试获取基于 Yii framework 的 PHP 应用程序的代码覆盖率报告.

代码覆盖由 PHPUnit 3.6 生成,我正在使用 whitelist approach源文件过滤。

问题是,当我设置选项 addUncoveredFilesFromWhitelist="true" 时,代码覆盖中断并出现以下错误:

Generating code coverage report, this may take a moment.PHP Warning:  include(CButtonColumn.php): failed to open stream: No such file or directory in /home/hijarian/systems/yii/framework/YiiBase.php on line 418
PHP Stack trace:
PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:125
PHP   4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:187
PHP   5. PHP_CodeCoverage_Report_HTML->process() /usr/share/php/PHPUnit/TextUI/TestRunner.php:373
PHP   6. PHP_CodeCoverage->getReport() /usr/share/php/PHP/CodeCoverage/Report/HTML.php:133
PHP   7. PHP_CodeCoverage_Report_Factory->create() /usr/share/php/PHP/CodeCoverage.php:141
PHP   8. PHP_CodeCoverage->getData() /usr/share/php/PHP/CodeCoverage/Report/Factory.php:65
PHP   9. PHP_CodeCoverage->processUncoveredFilesFromWhitelist() /usr/share/php/PHP/CodeCoverage.php:173
PHP  10. include_once() /usr/share/php/PHP/CodeCoverage.php:516
PHP  11. YiiBase::autoload() /home/hijarian/systems/yii/framework/YiiBase.php:0

PHP Warning:  include(): Failed opening 'CButtonColumn.php' for inclusion (include_path='.:/home/hijarian/projects/lexgarant/webapp/protected/components:/home/hijarian/projects/lexgarant/webapp/protected/models:/usr/share/php:/usr/share/pear') in /home/hijarian/systems/yii/framework/YiiBase.php on line 418
PHP Stack trace:
PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:125
PHP   4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:187
PHP   5. PHP_CodeCoverage_Report_HTML->process() /usr/share/php/PHPUnit/TextUI/TestRunner.php:373
PHP   6. PHP_CodeCoverage->getReport() /usr/share/php/PHP/CodeCoverage/Report/HTML.php:133
PHP   7. PHP_CodeCoverage_Report_Factory->create() /usr/share/php/PHP/CodeCoverage.php:141
PHP   8. PHP_CodeCoverage->getData() /usr/share/php/PHP/CodeCoverage/Report/Factory.php:65
PHP   9. PHP_CodeCoverage->processUncoveredFilesFromWhitelist() /usr/share/php/PHP/CodeCoverage.php:173
PHP  10. include_once() /usr/share/php/PHP/CodeCoverage.php:516
PHP  11. YiiBase::autoload() /home/hijarian/systems/yii/framework/YiiBase.php:0

PHP Fatal error:  Class 'CButtonColumn' not found in /home/hijarian/projects/lexgarant/webapp/protected/components/CLexgarantButtonColumn.php on line 4
PHP Stack trace:
PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:125
PHP   4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:187
PHP   5. PHP_CodeCoverage_Report_HTML->process() /usr/share/php/PHPUnit/TextUI/TestRunner.php:373
PHP   6. PHP_CodeCoverage->getReport() /usr/share/php/PHP/CodeCoverage/Report/HTML.php:133
PHP   7. PHP_CodeCoverage_Report_Factory->create() /usr/share/php/PHP/CodeCoverage.php:141
PHP   8. PHP_CodeCoverage->getData() /usr/share/php/PHP/CodeCoverage/Report/Factory.php:65
PHP   9. PHP_CodeCoverage->processUncoveredFilesFromWhitelist()     /usr/share/php/PHP/CodeCoverage.php:173
PHP  10. include_once() /usr/share/php/PHP/CodeCoverage.php:516

(这本身就很有趣,为什么有三个几乎相同的错误,而不是一个。)当 addUncoveredFilesFromWhitelist="false" 代码覆盖成功生成时,当然,没有未覆盖的文件通过测试。

Yii框架的文件放在$HOME/systems/yii/目录下。

我试图覆盖的应用程序位于 $HOME/projects/$APPNAME/ 中。

这是我的 phpunit.xml:

<phpunit bootstrap="bootstrap.php"
    colors="true"
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true"
    stopOnFailure="false">

<filter>
<!-- DO NOT SET TO "true" CODE COVERAGE WILL BREAK -->
  <whitelist addUncoveredFilesFromWhitelist="false">
    <directory suffix=".php">..</directory>
    <exclude>
      <directory suffix=".php">../config/</directory>
      <directory suffix=".php">../messages/</directory>
      <directory suffix=".php">../tests/</directory>
      <directory suffix=".php">../runtime/</directory>
      <directory suffix=".php">/home/hijarian/systems/yii/framework/</directory>
    </exclude>
  </whitelist>
</filter>

<logging>
    <log type="coverage-html" 
        target="report/html" 
        title="Lexgarant Online"
        charset="UTF-8" 
        yui="true" 
        highlight="true"
        lowUpperBound="35" highLowerBound="70" />
</logging>

<testsuites>
    <testsuite name="All_Unit_Tests">
        <directory>./unit/models/</directory>
        <directory>./unit/logic/</directory>
        <directory>./unit/components/</directory>
    </testsuite>
</testsuites>

<selenium>
    <browser name="Firefox" browser="*firefox" />
</selenium>
</phpunit>

我将所有测试都放在应用程序目录中的 protected/tests 目录下,如 Test Environment Setup 中所述在 Yii 权威指南中。

PHPUnit 的调用如下:

cd $APPDIR/protected/tests/
phpunit

单元测试本身进行得很顺利,只有代码覆盖中断。

也许我应该设置排除列表来排除带有 Yii 类的目录,但我有点害怕这种体验,因为有几十个文件夹,Yii 是一个高度模块化的系统。当然,如果这是唯一的解决方案......


编辑:所以,出于好奇,完整的解决方案如下所示:

phpunit.xml

<php>
    <!--  HACK -->
    <includePath>YIIPATH/framework/zii/widgets/grid</includePath>
</php>

<filter>
  <whitelist addUncoveredFilesFromWhitelist="true">
    <directory suffix=".php">../models/</directory>
    <directory suffix=".php">../components/</directory>
    <!--  any other directories you want to check -->
  </whitelist>
</filter>

只需将 YIIPATH 占位符替换为 Yii 框架的真实路径即可。

肯定CButtonColumn有关,但现在我只是将这个hack 与includePath 一起使用并且很高兴。删除 hack 会使整个 bug 活跃起来。

最佳答案

为什么会这样:

如果您告诉 PHPUnit addUncoveredFilesFromWhitelist="true"<,PHPUnit 将要求/包括 白名单 中的所有文件.

如果这些文件包含可执行代码,它将被执行并且您将看到出现的问题的结果。


做什么

  • 从白名单中排除所有带有可执行代码的文件

虽然有点麻烦但是很管用

  • 修复您的包含路径,以便 include/require 调用不会破坏任何内容

我不知道为什么会弹出这些错误,但这似乎是一个包含路径问题?该框架应该有一种解决方法,我会说。

  • PHPUnit 3.7 将提供“生成覆盖但不通过包含”模式,该模式会查看文件并为您“猜测”可执行行。

关于php - 如何将未覆盖文件添加到 Yii 应用程序的 PHPUnit 代码覆盖率报告中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9149727/

相关文章:

php - 无法连接MySQL数据库,但可以连接数据库服务器

php - 在 php 编码中将 php 连接到下拉列表

php - Slim - 从浏览器调用端点工作正常,而从 PHPUnit 测试调用端点返回 404

phpunit - 如何在 phpUnit 中的所有测试中传播setUp和tearDown?

php - 如何测试 Laravel 5 工作?

php - php 中剩余的天数、小时数和分钟数

php - 如何更改 Linux 网络服务器上的 URL 超时设置

php - Yii 迁移更新

php - 如何在 Yii 2.0.6 中激活 Gii 代码生成器?

yii - 如何删除 Yii 中的行?