Issues

ZF-8193: Zend_Controller_Action->getInvokeArg('bootstrap') doesn't work under the unit testing environment

Description


class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        var_dump($this->getInvokeArg('bootstrap')); // It should NOT output NULL, but an instance of class "Zend_Application_Bootstrap_Bootstrap" under the unit testing environment
    }
}

class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{

    public $application;

    public function setUp()
    {
        $this->application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
        );

        $this->bootstrap = array($this, 'appBootstrap');
        parent::setUp();
    }

    public function appBootstrap()
    {
        $this->application->bootstrap();
    }

    public function testIndexAction() {
        $this->dispatch('/');
        $this->assertType('Zend_Application_Bootstrap_Bootstrap', $this->_frontController->getParam('bootstrap')); // FAILURE
    }
}

Comments

I've worked around this issue by applying the following patch to my Bootstrap:

<?php

/** * This file contains the Bootstrap class, which bootstraps resources for the * SUN Tech Web Services application. * * SUN Tech Web Services * PHP 5.2.9, Zend Framework 1.7.5, ExtJS 2.0 * * @author Brian Reich breich@sun-tech.org * @copyright 2008 (C) SUN Area Technical Institute * @category Public * @package Public * @version 2.0 */

/** * Bootstrap extends Zend_Application_Bootstrap_Bootstrap. */ require_once 'Zend/Application/Bootstrap/Bootstrap.php';

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { // ...

/**
 * Initializes the Front Controller
 * 
 * @return Zend_Controller_Front Returns the Front Controller
 */
protected function _initFrontController()
{
    $front = Zend_Controller_Front::getInstance();

    // ...

    /*
    * Fix for ZF-8193
    * Zend_Controller_Action->getInvokeArg('bootstrap') doesn't work
    * under the unit testing environment.
    */
    if($front->getParam('bootstrap') === null)
    {
    $front->setParam('bootstrap', $this);
    }

    return $front;
}

}

The issue is that Zend_Test_PHPUnit_ControllerTestCase doesn't call Zend_Application::run() anywhere. That method is responsible for calling Zend_Application_Bootstrap_Bootstrap::run(), which in turn calls Zend_Controller_Front::setParam() to store a reference to the bootstrap in the front controller as an invoking parameter. I believe Zend_Test_PHPUnit_ControllerTestCase should be modified, likely either in its bootstrap() method or its dispatch() method, to handle this.

Resolved in trunk with r20256.

Not resolved in last release versions

Actually, it is. Based on what you reported in ZF-10607, you were setting up your testing bootstrap incorrectly.

I'm checking with ZF 1.11.3 and seems that the issue is still present. Here my code of testing:


class ControllerTestCase
extends Zend_Test_PHPUnit_ControllerTestCase {
  
  protected $application;
  
  public function setUp() 
  {
    $this->bootstrap= array($this, 'appBootstrap');
    parent::setUp();
  }

  public function appBootstrap()
  {
    $this->application= new Zend_Application(
      APPLICATION_ENV,
      APPLICATION_PATH.'/configs/application.ini'
    );
    $this->application->bootstrap();
  }
}

and the following testCase:


class IndexControllerTest extends ControllerTestCase
{
  public function testBootstrap()
  {
    $this->dispatch('/');
    $this->assertType('Zend_Application_Bootstrap_Bootstrap', $this->_frontController->getParam('bootstrap'));
  }
}

That fails.

This issue seems to be triggered by this blog post (possibly among others): http://ailoo.net/2009/04/…

I commented on that post hoping that Matthias will adjust the example.

A correct setup for your tests looks like this:


    public function setUp()
    {
        $this->bootstrap = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
        );

        parent::setUp();
    }

This is taken from Matthew's example in the related issue.

This is no issue, so I close it.