本文介绍了如何将未发现的文件添加到Yii应用程序的PHPUnit代码覆盖率报告中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently trying to get the code coverage report for a PHP application based on Yii framework.

代码覆盖率由 PHPUnit 3.6 生成,我正在使用白名单方法来获取源文件过滤.

Code coverage gets generated by PHPUnit 3.6, and I am using whitelist approach to source files filtering.

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

Problem is, when I set the option addUncoveredFilesFromWhitelist="true", code coverage breaks with the following error:

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"代码覆盖率时,但是当然没有测试未覆盖的文件.

(It's interesting by itself, why there is three almost identical errors instead of just one.) When addUncoveredFilesFromWhitelist="false" code coverage generates successfully, but, of course, without the files not covered by tests.

位于$HOME/systems/yii/目录中的Yii框架文件.

Files of the Yii framework placed in the $HOME/systems/yii/ directory.

我要介绍的应用程序放置在$HOME/projects/$APPNAME/中.

Application I'm trying to cover placed in the $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目录下,如.

I have all the tests placed under the protected/tests directory in the application's catalogue, as described in the Test Environment Setup in The Definitive Guide to Yii.

PHPUnit的调用方式如下:

PHPUnit is invoked as follows:

cd $APPDIR/protected/tests/
phpunit

单元测试本身进行得很顺利,只有代码覆盖范围破了.

The unit testing itself goes smoothly, only code coverage breaks.

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

Maybe I should set up the exlude list to exclude directories with Yii's classes, but I am somewhat afraid of this experience because there's dozens of folders, Yii is a highly modular system. Of course, if it's an only solution...

因此,出于好奇,完整的解决方案如下所示:

So, for curious about it, full solution looks like this:

phpunit.xml

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框架的真实路径即可.

Just replace YIIPATH placeholder with real path to Yii framework.

肯定是 CButtonColumn有关,但是现在我只是将与includePath一起使用,对此感到很高兴.删除该hack可以使整个错误复活.

It definitely has something to do with CButtonColumn, but for now I'm just using this hack with includePath and happy about it. Removing the hack brings this whole bug alive.

推荐答案

为什么会这样:

如果您告诉PHPUnit执行addUncoveredFilesFromWhitelist="true",则PHPUnit将require/include 白名单中的所有文件.

PHPUnit will require / include all files in the white-listed if you tell it do addUncoveredFilesFromWhitelist="true".

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

If those files contain executable code it will be executed and you are seeing the results of the arising issues.

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

有点麻烦,但是可以用

  • 修正您的包含路径,以使包含/请求调用不会中断任何操作

我没有真正的线索,为什么会弹出这些错误,但这似乎是一个包含路径问题?该框架应该有一种解决方法,我会说.

I have no real clue as to WHY those errors pop up but it seems to be an include path issue? The framework should have a way of resolving that I'd say.

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

这篇关于如何将未发现的文件添加到Yii应用程序的PHPUnit代码覆盖率报告中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 22:10