生成PHPUnit代码覆盖率报告时无法重新声明类错误 [英] Cannot redeclare class error when generating PHPUnit code coverage report

查看:61
本文介绍了生成PHPUnit代码覆盖率报告时无法重新声明类错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Zend Framework 1.10和Doctrine 2(Beta1)启动一个项目.我在自己的库代码中使用名称空间.

Starting a project with Zend Framework 1.10 and Doctrine 2 (Beta1). I am using namespaces in my own library code.

生成代码覆盖率报告时,出现有关重新声明类的致命错误.为了提供更多信息,我在phpunit可执行文件中注释了xdebug_disable()调用,以便您可以看到函数跟踪(由于输出过多,所以禁用了局部变量输出).

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).

这是我的终端输出:


$ 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 Cob\Application\Resource\HelperBroker 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 Cob\Application\Resource\HelperBroker 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...?)

这是我的phpunit.xml:

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>

我在缝中添加了一个标签来隐藏此问题.我确实有另一个应用程序资源,但没有接缝(另一个是Doctrine 2资源).我不确定为什么它是特定于此类的,我的整个库是自动加载的,因此它们在任何地方都不会包含/调用.我想应该指出,HelperBroker是文件系统中从库/Cob派生的第一个文件

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

我在Snow Leopard上拥有所有软件的最新/最新版本(Zend Server,Zend Framework,Doctrine 2 Beta1,Phing,PHPUnit,PEAR).

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:

在代码覆盖率报告生成期间,phpunit将<whitelist>中的每个*.php进行require.可以使用addUncoveredFilesFromWhitelist=false将其关闭 参数,但建议保持此状态.

During code coverage report generation phpunit is requireing 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.

在这些情况下倾向于发生的是,其中一个文件具有require语句,该语句再次要求已加载的类(因为它不是require_once).

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).

其他原因可能是

  • 类的重复定义(一个用于调试一个用于生产的类,应该通过继承而不是通过加载正确的php文件来解决)
  • 大小写不一致会导致php代码中的错误,例如:

  • 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(...);在大写字母不同的多个地方.

if( !$classesLoaded['ThIsClass']) require_once(...); in multiple places with different capitalizations.

这篇关于生成PHPUnit代码覆盖率报告时无法重新声明类错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆