duan36000 2011-02-23 00:43
浏览 44
已采纳

PHPUnit - 使用Zend Framework应用程序生成代码覆盖率报告时出现问题

I have been searching for an answer to this problem for hours in Google and various other sites with no luck. I have created some unit tests for my Zend Framework project using PHPUnit. All goes well with the tests up until the point of the the PHPUnit Code Coverage report. At this point I get the following error:

Generating code coverage report, this may take a moment. Fatal error: Call to a member function pushStack() on a non-object in C:\htdocs\ZendFWTutorials\ZendStorefront\library\SF\Plugin\Action.php on line 32

This error references the following block of code:

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;
 }

This code is from a library that I did not write so that probably adds complexity to my problem because I'm less likely to understand what is going on. I also don't know what the PHPUnit logic is doing when it creates the code coverage report so I don't know how to remedy the problem. This problem only occurs when I run PHPUnit and I have run xdebug to trace the code in this function under normal operational conditions. I have a feeling that PHPUnit enters a condition where the variable is null but in normal operation $stack and $categoryRequest are not null.

My directory structure is as follows:

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 is as follows:

./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条回答 默认 最新

  • dsfs5420 2011-02-23 18:16
    关注

    After talking to @derickr on twitter, I used xdebug.auto_trace=1 in my PHP INI to trace the problem. The problem was in this line of code:

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

    This is found in the function above named getStack(). The auto_trace showed that the first time getStack() runs, it runs correctly. The reason that PHPUnit was causing the error to show up is that when it runs the Code Coverage report, the dispatcher runs multiple times thus entering getStack() again and triggering the aforementioned line of code. The code needed the following change to remove the error and properly locate the plug-in in memory:

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

    The Code Coverage report now generates properly. Hopefully this will help explain the problem for others who find themselves with similar errors.

    -Ross

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里