PHPUnit - 在Zend框架应用程序中生成代码覆盖率报告时出现问题

5
我在谷歌和其他网站上搜寻了几个小时,但仍未找到解决此问题的答案。我使用PHPUnit为我的Zend Framework项目创建了一些单元测试。测试进行得很顺利,直到PHPUnit代码覆盖率报告的时候。在这个时候,我遇到了以下错误信息:

正在生成代码覆盖率报告,请稍等片刻。致命错误:在C:\htdocs\ZendFWTutorials\ZendStorefront\library\SF\Plugin\Action.php的第32行中,调用非对象成员函数pushStack()。

这个错误是指以下代码块:

public function
dispatchLoopStartup(Zend_Controller_Request_Abstract $request) 
{
     $stack = $this->getStack();

     // category menu
     $categoryRequest = new Zend_Controller_Request_Simple();
     $categoryRequest->setControllerName('category')
                     ->setActionName('index')
                     ->setParam('responseSegment', 'categoryMain');


     // push requests into the stack
     $stack->pushStack($categoryRequest);
 }

 public function getStack()
 {
     if (null === $this->_stack) {
         $front = Zend_Controller_Front::getInstance();
         if (!$front->hasPlugin('Zend_Controller_Plugin_ActionStack'))
         {
             $stack = new Zend_Controller_Plugin_ActionStack();
             $front->registerPlugin($stack);
         } else {
             $stack = $front->getPlugin('ActionStack');
         }
         $this->_stack = $stack;

     }
     return $this->_stack;
 }

这段代码来自于我没有编写的一个库,所以可能会增加一些复杂性,因为我不太可能理解正在发生的事情。我也不知道 PHPUnit 的逻辑在创建代码覆盖报告时做了什么,所以我不知道如何解决这个问题。只有当我运行PHPUnit并且在正常操作条件下跟踪此函数中的代码时,才会出现此问题。我有一种感觉,PHPUnit进入了一个变量为空的条件,但在正常操作中,$stack和$categoryRequest不为空。

我的目录结构如下:

application
----->bootstrap
----->config
----->layouts
----->modules
-------->storefront
-------------->controllers
-------------->forms
-------------->models
-------------->services
-------------->views
build
data
library
----->SF
----->Zend
Public
----->css
----->images
tests
----->application
------------->modules
------------->controllers
------------->models
----TestHelper.php
----phpunit.xml

phpunit.xml 如下:

./application/

<filter>
    <whitelist>
        <directory suffix=".php">../application/</directory>
        <exclude>
            <directory suffix=".phtml">../application/</directory>
            <directory suffix=".php">../library/</directory>
        </exclude>
    </whitelist>
</filter>

<logging>
    <log type="coverage-html" target="./log/report" charset="UTF-8"
    yui="true" highlight="true" lowUpperBound="50"

highLowerBound="80"/>

TestHelper.php:

<?php // set our app paths and
 environments define('BASE_PATH',
 realpath(dirname(__FILE__) . '/../'));
 define('APPLICATION_PATH', BASE_PATH .
 '/application'); define('TEST_PATH',
 BASE_PATH . '/tests');
 define('APPLICATION_ENV', 'testing');

 //include path set_include_path('.' .
 PATH_SEPARATOR . BASE_PATH .
 '/library'  . PATH_SEPARATOR .
 get_include_path());

 // set the default timezone
 date_default_timezone_set('America/Argentina/Buenos_Aires');

 require_once 'Zend/Application.php';
 $application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/config/store.ini');

 $application->bootstrap();
2个回答

2

在与Twitter上的@derickr交谈后,我在我的PHP INI中使用了xdebug.auto_trace=1来跟踪问题。问题出在这行代码中:

$stack = $front->getPlugin('ActionStack');

这个问题出现在名为getStack()的函数中。auto_trace显示,第一次运行getStack()时,它能够正确地运行。PHPUnit引起错误显示的原因是在运行代码覆盖率报告时,调度程序会多次运行,从而重新进入getStack()并触发上述代码行。要解决该错误并正确定位插件在内存中的位置,代码需要进行以下更改:

$stack = $front->getPlugin('Zend_Controller_Plugin_ActionStack');

代码覆盖率报告现在已经可以正确生成。希望这能帮助那些遇到类似错误的人更好地理解问题。
-Ross

-3

很可能你的PHPUnit测试不正确。


我在一个插件中发现了问题,具体修复方法请参见下文。 - rossdh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接