3

使用 Composer 的 autoload.php 作为引导文件时,我遇到了 PHPUnit Code_Coverage 错误。我已经创建了自己的 boostrap.php 文件,并且简单地单独包含每个文件,一切正常。我是否偶然发现了 Composer 错误,或者我在某处犯了错误?

// 编辑:我正在使用所有东西的最新(最新)版本:PHPUnit 3.7.12、Code_Coverage 1.2.7、Composer b51a4a7

Fatal error: Cannot redeclare class C3\CDN\EdgeCast in /Users/kperrine/Projects/edgecast-custom-reports/src/C3/CDN/EdgeCast.php on line 11

Call Stack:
    0.0017     641760   1. {main}() /Applications/MAMP/bin/php/php5.3.14/bin/phpunit:0
    0.2232    1111312   2. PHPUnit_TextUI_Command::main() /Applications/MAMP/bin/php/php5.3.14/bin/phpunit:46
    0.2232    1112040   3. PHPUnit_TextUI_Command->run() /Applications/MAMP/bin/php/php5.3.14/lib/php/PHPUnit/TextUI/Command.php:129
    0.3027    4636952   4. PHPUnit_TextUI_TestRunner->doRun() /Applications/MAMP/bin/php/php5.3.14/lib/php/PHPUnit/TextUI/Command.php:176
   21.5764    6604032   5. PHP_CodeCoverage_Report_HTML->process() /Users/kperrine/Projects/edgecast-custom-reports/vendor/phpunit/phpunit/PHPUnit/TextUI/TestRunner.php:384
   21.5764    6604240   6. PHP_CodeCoverage->getReport() /Users/kperrine/Projects/edgecast-custom-reports/vendor/phpunit/php-code-coverage/PHP/CodeCoverage/Report/HTML.php:120
   21.5768    6663512   7. PHP_CodeCoverage_Report_Factory->create() /Users/kperrine/Projects/edgecast-custom-reports/vendor/phpunit/php-code-coverage/PHP/CodeCoverage.php:158
   21.5769    6663512   8. PHP_CodeCoverage->getData() /Users/kperrine/Projects/edgecast-custom-reports/vendor/phpunit/php-code-coverage/PHP/CodeCoverage/Report/Factory.php:64
   21.5769    6663512   9. PHP_CodeCoverage->addUncoveredFilesFromWhitelist() /Users/kperrine/Projects/edgecast-custom-reports/vendor/phpunit/php-code-coverage/PHP/CodeCoverage.php:190
   21.5769    6664408  10. PHP_CodeCoverage->processUncoveredFileFromWhitelist() /Users/kperrine/Projects/edgecast-custom-reports/vendor/phpunit/php-code-coverage/PHP/CodeCoverage.php:535
   21.5778    6757272  11. include_once('/Users/kperrine/Projects/edgecast-custom-reports/src/C3/CDN/EdgeCast.php') /Users/kperrine/Projects/edgecast-custom-reports/vendor/phpunit/php-code-coverage/PHP/CodeCoverage.php:558

我的 phpunit.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false"
>
    <testsuites>
        <testsuite name="C3 EdgeCast Tests">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">src</directory>
        </whitelist>
    </filter>

    <logging>
        <log type="coverage-html" target="log/report" charset="UTF-8"
            highlight="false" lowUpperBound="35" highLowerBound="70"/>
    </logging>
</phpunit>
4

3 回答 3

3

问题在于我在项目中使用命名空间。我的项目命名空间是C3\CDN\EdgeCast(注意 EdgeCast 中的大写“C”),但是当我在测试文件中使用命名空间时,我不小心输入use C3\CDN\Edgecast;了(注意 Edgecast 中的小写“c”)。名称的不同导致 CodeCoverage 或 Composer(我不知道究竟是哪个)认为该类没有被包含,因此试图再次包含它。

故事的寓意:始终仔细检查您的类和命名空间名称。

于 2013-01-11T00:38:03.773 回答
0

phpunit 是否也与项目中的 composer 一起安装?如果是这样,您可能希望删除引导程序,因为它已经通过调用 phpunit 包含在内。

我不确定“仅单独包含每个文件”是什么意思。你的意思是如果你不使用自动加载器但手动包含东西它可以工作吗?那会很奇怪。使用自动加载器通常可以正常工作。

如果所有其他方法都失败了,您是否可以尝试使用 composer.json 和几个使其在运行 phpunit 时失败的类创建一个简单的重现案例?

于 2013-01-10T08:27:20.140 回答
0

包含是在processUncoveredFileFromWhitelist="true"PHPUnit 包含所有src没有覆盖信息的文件的处理步骤期间进行的。

我的猜测是里面的东西包括一些东西。已经自动加载的类也可能是一个问题(iirc 作曲家只使用require而不是require_once(尽管正确!)被再次包含。

到目前为止,在所有出现此错误的情况下,都是因为代码中的 if include/require 语句。如果不是这种情况,而且它是可能的,那么这是 phpunit 中某个地方的一个问题,但是一个非常奇怪的问题。

始终有效的是设置processUncoveredFileFromWhitelist="false"仍会列出所有文件但不包括它们。未发现文件的覆盖信息将略有不同。(然后可以巧妙地猜测可执行的行数,而不是从 xdebug 中获取)

于 2013-01-10T15:10:25.203 回答