使用 Zend Framework 1.10 和 Doctrine 2 (Beta1) 启动项目.我在自己的库代码中使用命名空间.
在生成代码覆盖率报告时,我收到关于重新声明类的致命错误.为了提供更多信息,我在我的 phpunit 可执行文件中注释掉了 xdebug_disable() 调用,以便您可以看到函数跟踪(禁用局部变量输出,因为输出太多).
这是我的终端输出:
<前>$phpunitPHPUnit 3.4.12 by Sebastian Bergmann.…………时间:4 秒,内存:16.50MbOK(8 个测试,14 个断言)生成代码覆盖率报告,这可能需要一些时间.PHP 致命错误:无法在/Users/Cobby/Sites/project/trunk/code/library/Cob/Application/Resource/HelperBroker.php 中重新声明类 CobApplicationResourceHelperBroker在第 93 行PHP 堆栈跟踪:PHP 1. {main}()/usr/local/zend/bin/phpunit:0PHP 2. PHPUnit_TextUI_Command::main()/usr/local/zend/bin/phpunit:54PHP 3. PHPUnit_TextUI_Command->run()/usr/local/zend/share/pear/PHPUnit/TextUI/Command.php:146PHP 4. PHPUnit_TextUI_TestRunner->doRun()/usr/local/zend/share/pear/PHPUnit/TextUI/Command.php:213PHP 5. PHPUnit_Util_Report::render()/usr/local/zend/share/pear/PHPUnit/TextUI/TestRunner.php:478PHP 6. PHPUnit_Framework_TestResult->getCodeCoverageInformation()/usr/local/zend/share/pear/PHPUnit/Util/Report.php:97PHP 7. PHPUnit_Util_Filter::getFilteredCodeCoverage()/usr/local/zend/share/pear/PHPUnit/Framework/TestResult.php:623致命错误:无法在第 93 行的/Users/Cobby/Sites/project/trunk/code/library/Cob/Application/Resource/HelperBroker.php 中重新声明类 CobApplicationResourceHelperBroker调用堆栈:0.0004 322888 1. {main}()/usr/local/zend/bin/phpunit:00.0816 4114628 2. PHPUnit_TextUI_Command::main()/usr/local/zend/bin/phpunit:540.0817 4114964 3. PHPUnit_TextUI_Command->run()/usr/local/zend/share/pear/PHPUnit/TextUI/Command.php:1460.1151 5435528 4. PHPUnit_TextUI_TestRunner->doRun()/usr/local/zend/share/pear/PHPUnit/TextUI/Command.php:2134.2931 16690760 5. PHPUnit_Util_Report::render()/usr/local/zend/share/pear/PHPUnit/TextUI/TestRunner.php:4784.2931 16691120 6. PHPUnit_Framework_TestResult->getCodeCoverageInformation()/usr/local/zend/share/pear/PHPUnit/Util/Report.php:974.2931 16691148 7. PHPUnit_Util_Filter::getFilteredCodeCoverage()/usr/local/zend/share/pear/PHPUnit/Framework/TestResult.php:623(我不知道为什么它会显示两次错误......?)
这是我的 phpunit.xml:
<!-- bootstrap.php 将目录更改为trunk/code/tests,下面的所有路径都相对于这个目录.--><testsuite name="My Promotions"><目录>./</目录></testsuite><过滤器><白名单><目录后缀=".php">../application</directory><目录后缀=".php">../library/Cob</directory><排除><!-- 通过添加以下行,我可以消除错误--><file>../library/Cob/Application/Resource/HelperBroker.php</file><directory suffix=".phtml">../application</directory><目录后缀=".php">../application/doctrine</directory><文件>../application/Bootstrap.php</file><目录后缀=".php">../library/Cob/Tools</directory></排除></白名单></过滤器><日志记录><log type="junit" target="../../build/reports/tests/report.xml"/><log type="coverage-html" target="../../build/reports/coverage" charset="UTF-8"yui="true" highlight="true" lowUpperBound="50" highLowerBound="80"/></日志记录></phpunit>
我在哪个接缝中添加了一个标签来隐藏这个问题.我确实有另一个应用程序资源,但没有问题(另一个是 Doctrine 2 资源).我不确定为什么它特定于这个类,我的整个库都是自动加载的,所以它们在任何地方都没有任何包含/要求调用.我想应该注意 HelperBroker 是文件系统中第一个源自 library/Cob 的文件
我使用所有软件(Zend Server、Zend Framework、Doctrine 2 Beta1、Phing、PHPUnit、PEAR)的最新/最近版本在 Snow Leopard 上.
总结这个问题的一般答案:
在代码覆盖率报告生成期间,phpunit 要求
处理
中的每个*.php
.这可以关闭使用 addUncoveredFilesFromWhitelist=false
作为参数,但建议保持此状态.
在这些情况下往往会发生的是,其中一个文件有一个 require
语句,该语句再次需要一个已经加载的类(因为它不是 require_once).
其他原因可能是
类的重复定义(一个用于调试,一个用于生产,应该通过继承而不是加载正确的php文件来解决)
大小写不一致导致 php 代码中的错误,例如:
if( !$classesLoaded['ThIsClass']) require_once(...);
在多个不同大小写的地方.
该类在测试中根本没有被加载,但是使用当时加载的类的名称创建了一个模拟对象.那些也可能发生冲突并导致该错误
Starting a project with Zend Framework 1.10 and Doctrine 2 (Beta1). I am using namespaces in my own library code.
When generating code coverage reports I get a Fatal Error about Redeclaring a class. To provide more info, I've commented out the xdebug_disable() call in my phpunit executable so you can see the function trace (disabled local variables output because there was too much output).
Here's my Terminal output:
$ phpunit PHPUnit 3.4.12 by Sebastian Bergmann. ........ Time: 4 seconds, Memory: 16.50Mb OK (8 tests, 14 assertions) Generating code coverage report, this may take a moment.PHP Fatal error: Cannot redeclare class CobApplicationResourceHelperBroker in /Users/Cobby/Sites/project/trunk/code/library/Cob/Application/Resource/HelperBroker.php on line 93 PHP Stack trace: PHP 1. {main}() /usr/local/zend/bin/phpunit:0 PHP 2. PHPUnit_TextUI_Command::main() /usr/local/zend/bin/phpunit:54 PHP 3. PHPUnit_TextUI_Command->run() /usr/local/zend/share/pear/PHPUnit/TextUI/Command.php:146 PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/local/zend/share/pear/PHPUnit/TextUI/Command.php:213 PHP 5. PHPUnit_Util_Report::render() /usr/local/zend/share/pear/PHPUnit/TextUI/TestRunner.php:478 PHP 6. PHPUnit_Framework_TestResult->getCodeCoverageInformation() /usr/local/zend/share/pear/PHPUnit/Util/Report.php:97 PHP 7. PHPUnit_Util_Filter::getFilteredCodeCoverage() /usr/local/zend/share/pear/PHPUnit/Framework/TestResult.php:623 Fatal error: Cannot redeclare class CobApplicationResourceHelperBroker in /Users/Cobby/Sites/project/trunk/code/library/Cob/Application/Resource/HelperBroker.php on line 93 Call Stack: 0.0004 322888 1. {main}() /usr/local/zend/bin/phpunit:0 0.0816 4114628 2. PHPUnit_TextUI_Command::main() /usr/local/zend/bin/phpunit:54 0.0817 4114964 3. PHPUnit_TextUI_Command->run() /usr/local/zend/share/pear/PHPUnit/TextUI/Command.php:146 0.1151 5435528 4. PHPUnit_TextUI_TestRunner->doRun() /usr/local/zend/share/pear/PHPUnit/TextUI/Command.php:213 4.2931 16690760 5. PHPUnit_Util_Report::render() /usr/local/zend/share/pear/PHPUnit/TextUI/TestRunner.php:478 4.2931 16691120 6. PHPUnit_Framework_TestResult->getCodeCoverageInformation() /usr/local/zend/share/pear/PHPUnit/Util/Report.php:97 4.2931 16691148 7. PHPUnit_Util_Filter::getFilteredCodeCoverage() /usr/local/zend/share/pear/PHPUnit/Framework/TestResult.php:623
(I have no idea why it shows the error twice...?)
And here is my phpunit.xml:
<phpunit bootstrap="./code/tests/application/bootstrap.php" colors="true">
<!-- bootstrap.php changes directory to trunk/code/tests,
all paths below are relative to this directory. -->
<testsuite name="My Promotions">
<directory>./</directory>
</testsuite>
<filter>
<whitelist>
<directory suffix=".php">../application</directory>
<directory suffix=".php">../library/Cob</directory>
<exclude>
<!-- By adding the below line I can remove the error -->
<file>../library/Cob/Application/Resource/HelperBroker.php</file>
<directory suffix=".phtml">../application</directory>
<directory suffix=".php">../application/doctrine</directory>
<file>../application/Bootstrap.php</file>
<directory suffix=".php">../library/Cob/Tools</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="junit" target="../../build/reports/tests/report.xml" />
<log type="coverage-html" target="../../build/reports/coverage" charset="UTF-8"
yui="true" highlight="true" lowUpperBound="50" highLowerBound="80" />
</logging>
</phpunit>
I have added a tag inside the which seams to hide this problem. I do have another application resource but it doesn't seam to have a problem (the other one is a Doctrine 2 resource). I'm not sure why it is specific to this class, my entire library is autoloaded so their isn't any include/require calls anywhere. I guess it should be noted that HelperBroker is the first file in the filesystem stemming out from library/Cob
I am on Snow Leopard with the latest/recent versions of all software (Zend Server, Zend Framework, Doctrine 2 Beta1, Phing, PHPUnit, PEAR).
General answer to wrap this problem up:
During code coverage report generation phpunit is require
ing every *.php
that is in <whitelist>
. This can be turned off using addUncoveredFilesFromWhitelist=false
as a
parameter but it is recommended to keep this on.
What tends to happen in these cases is that one of the files has a require
statement that requires an already loaded class again (as it is not a require_once).
Other reasons can be
the duplicate definition of classes (one for debugging one for production, should be solved by inheritance not by loading the right php file)
Inconsistent capitalization leading to errors in the php code like:
if( !$classesLoaded['ThIsClass']) require_once(...);
in multiple places with different capitalizations.
The class not getting loaded at all in the tests but a mock object was created with the name of the then loaded class. Those can collide and lead to that error too
这篇关于生成 PHPUnit 代码覆盖率报告时无法重新声明类错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!