5
votes

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

2 Answers

2
votes

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

-3
votes

Properly your PHPUnit test is not correct.