2011-02-23 97 views
5

我一直在谷歌和其他各種網站上搜索這個問題的答案,好幾個小時沒有運氣。我爲使用PHPUnit的Zend Framework項目創建了一些單元測試。直到PHPUnit代碼覆蓋率報告結束時,所有測試都很順利。在這一點上,我得到以下錯誤:PHPUnit - 使用Zend Framework應用程序生成代碼覆蓋率報告的問題

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

此錯誤引用下面的代碼塊:

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並且運行xdebug才能在正常運行條件下跟蹤此函數中的代碼時,纔會出現此問題。我有一種感覺,PHPUnit輸入一個條件,其中變量爲null,但在正常操作$ stack和$ categoryRequest不爲null。

我的目錄結構如下:

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

在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的測試是不正確。

+0

我在插件中發現了這個問題,請在下面閱讀該修補程序。 – rossdh 2011-02-23 18:17:06

相關問題