【问题标题】:Cannot redeclare class error when generating PHPUnit code coverage report生成 PHPUnit 代码覆盖率报告时无法重新声明类错误
【发布时间】:2011-02-18 11:01:30
【问题描述】:

使用 Zend Framework 1.10 和 Doctrine 2 (Beta1) 开始一个项目。我在自己的库代码中使用命名空间。

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

这是我的终端输出:

$ php单元 塞巴斯蒂安伯格曼的 PHPUnit 3.4.12。 ........... 时间:4 秒,内存:16.50Mb OK(8 个测试,14 个断言) 生成代码覆盖率报告,这可能需要一点时间。PHP 致命错误:Cannot redeclare class Cob\Application\Resource\HelperBroker in /Users/Cobby/Sites/project/trunk/code/library/Cob/Application/Resource/HelperBroker.php在第 93 行 PHP堆栈跟踪: 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 致命错误:无法在第 93 行的 /Users/Cobby/Sites/project/trunk/code/library/Cob/Application/Resource/HelperBroker.php 中重新声明类 Cob\Application\Resource\HelperBroker 调用堆栈: 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

(我不知道为什么它会显示两次错误...?)

这是我的 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 资源)。我不确定为什么它特定于这个类,我的整个库都是自动加载的,所以它们在任何地方都没有任何 include/require 调用。我想应该注意的是,HelperBroker 是文件系统中从 library/Cob 产生的第一个文件

我在 Snow Leopard 上使用所有软件(Zend Server、Zend Framework、Doctrine 2 Beta1、Phing、PHPUnit、PEAR)的最新/最新版本。

【问题讨论】:

  • 在编辑 pear/PHPUnit/Framework/TestResult.php 以转储包含的文件时,由于某种原因,列出的有问题的文件有 Helperbroker.php 而不是 HelperBroker.php! (该文件实际上是 HelperBroker.php),这就是为什么 TestResult.php 中的 include_once 语句仍在尝试加载该文件的原因。包含的所有其他文件都是正确的大小写。我现在将调查为什么文件不区分大小写
  • 似乎 Zend 框架插件(包括 Zend_Application 资源)的标准命名约定是 ucfirst() 而不是像常规库文件那样的驼峰命名法。将文件重命名为 Helperbroker.php 可解决此问题。
  • 把你的答案写成答案并接受它,这样问题在这里就有一席之地,也可以帮助别人!
  • @alexander-v-ilyin 您对适用于赏金的答案有何期望? Op 似乎在一定程度上解决了他的问题,但可以写出一个通用的“如何摆脱那个错误”的答案或其他东西。答案如何过时以及当前问题是什么:)

标签: php zend-framework code-coverage phpunit


【解决方案1】:

我遇到了类似的问题,因为我使用了所有文件作为覆盖说明。当我设置具体目录和所需文件时,我的问题就解决了。

我认为你的问题在这里:

<testsuite name="My Promotions">
    <directory>./</directory>
</testsuite>

这里你需要所有的文件和目录。

试试这个案例只需要测试(或设置你的文件夹):

<testsuite name="My Promotions">
    <directory>tests</directory>
</testsuite>

您还可以使用前缀、后缀和文件:

【讨论】:

    【解决方案2】:

    我发现当使用不正确 @coversDefaultClass(同样可能适用于@covers)注释时收到此错误。在我的情况下,它是 \Path\To\\Class(带有双 '\'),它与 require 调用混淆了。

    【讨论】:

      【解决方案3】:

      这发生在我身上,因为我的 @covers 声明中有错字。注意第一行中多余的 \。

      App\Controller\\Account\UsersController::forgotPassword()
      

      VS

      App\Controller\Account\UsersController::forgotPassword()
      

      【讨论】:

        【解决方案4】:

        要检查的另一件事(特别是如果您正在使用 VM)是文件是否已重命名但仍以原始形式存在。例如。 fooMapper.php 重命名为 FooMapper.php(例如,通过 SVN 更新),您的 IDE 会检测到新文件并将其发送到 VM。即使您的 IDE 在本地仅显示一个,您现在在您的 VM 上也拥有这两个版本。小心。

        【讨论】:

          【解决方案5】:

          一种快速的解决方法是在类声明的开头添加一个 if 语句,以避免在运行 phpunit 时重新声明(当且仅当这是您遇到问题的唯一类)

          if (!class_exists("class_name")) {
              // declare the class
              class class_name
              {
                  //...
              }
          }
          

          【讨论】:

          • 这增加了太多的复杂性,“消除”了错误的后果,而不是理解和修复真正的错误。
          【解决方案6】:

          解决这个问题的一般答案:

          此错误消息通常表明自动加载和/或要求语句存在问题。

          在代码覆盖率报告生成期间,phpunit 是requireing 每个*.php,即在&lt;whitelist&gt; 中。这可以使用addUncoveredFilesFromWhitelist=false 作为关闭 参数,但建议保持打开状态。

          可能的原因

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

          其他原因

          • 类的重复定义(一个用于调试,一个用于生产,应该通过继承解决,而不是通过加载正确的php文件)

          • 大小写不一致导致 php 代码出现错误,例如:

            if( !$classesLoaded['ThIsClass']) require_once(...); 位于多个不同大小写的地方。

          • 该类在测试中根本没有被加载,但使用随后加载的类的名称创建了一个模拟对象。这些也可能发生冲突并导致该错误

          【讨论】:

          • 在我的情况下,致命的发生在“require_once”地方,所以我不太明白为什么会这样。
          • @AlexanderV.Ilyin 在声明类的文件中放置一个 debug_backtrace 并在需要它的文件中放置一个(“再次”),看看是否有帮助。只是为了“快速”摆脱它,您可以将文件从白名单中排除,看看是否有帮助并让您走上正确的轨道。
          • @AlexanderV.Ilyin 也看看@XDebug - 它提供了关于任何 PHP 错误/异常的很好的堆栈跟踪。 (我确实希望 XDebug 能够识别 CLI - 在控制台中扫描 HTML 表以获取堆栈详细信息会很累,而且凌晨 4 点) - 看起来 XDebug 确实比我想象的更好地支持 CLI:derickrethans.nl/cli-color.html
          • 当使用自动加载时——即使用作曲家——调用不同大小写的类来触发这个错误就足够了!我偶然发现了它,因为在测试中通过new WhiteSpaceTokenizer 调用了一个名为WhitespaceTokenizer 的类。结果是该类完全缺少代码覆盖。
          • 通过composer update 更新 PHPUnit 为我解决了一些问题,所以 PHPUnit 中可能有一些东西处理不同版本之间的文件自动加载。这可能是显而易见的尝试,但以防万一其他人遇到类似问题。
          猜你喜欢
          • 2021-12-31
          • 1970-01-01
          • 2011-03-09
          • 1970-01-01
          • 2014-03-21
          • 1970-01-01
          • 2014-07-01
          • 2019-04-24
          • 2018-05-29
          相关资源
          最近更新 更多