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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。