Zend Framework

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

Details

  • Type: Unit Tests: Problem Unit Tests: Problem
  • Status: Closed Closed
  • Priority: Trivial Trivial
  • Resolution: Not an Issue
  • Affects Version/s: 1.9.5
  • Fix Version/s: 1.10.0
  • Component/s: Zend_Test_PHPUnit
  • Labels:
    None

Description

IndexController.php
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
    }
}
IndexControllerTest.php
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
    }
}

Issue Links

Activity

Hide
Brian Reich added a comment -

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

<code>
<?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;
}
}
</code>

Show
Brian Reich added a comment - I've worked around this issue by applying the following patch to my Bootstrap: <code> <?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; } } </code>
Hide
Matthew Turland added a comment - - edited

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.

Show
Matthew Turland added a comment - - edited 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.
Hide
Matthew Weier O'Phinney added a comment -

Resolved in trunk with r20256.

Show
Matthew Weier O'Phinney added a comment - Resolved in trunk with r20256.
Hide
miholeus added a comment -

Not resolved in last release versions

Show
miholeus added a comment - Not resolved in last release versions
Hide
Matthew Weier O'Phinney added a comment -

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

Show
Matthew Weier O'Phinney added a comment - Actually, it is. Based on what you reported in ZF-10607, you were setting up your testing bootstrap incorrectly.
Hide
Enrico Zimuel added a comment - - edited

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.

Show
Enrico Zimuel added a comment - - edited 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.
Hide
Bart McLeod added a comment -

This issue seems to be triggered by this blog post (possibly among others): http://ailoo.net/2009/04/set-up-a-zend-framework-application-using-zend_application-including-phpunit-setup/

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.

Show
Bart McLeod added a comment - This issue seems to be triggered by this blog post (possibly among others): http://ailoo.net/2009/04/set-up-a-zend-framework-application-using-zend_application-including-phpunit-setup/ 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.
Hide
Bart McLeod added a comment -

This is no issue, so I close it.

Show
Bart McLeod added a comment - This is no issue, so I close it.

People

Vote (3)
Watch (5)

Dates

  • Created:
    Updated:
    Resolved: