2009年7月9日星期四

[fw-mvc] Using Zend_Test with Controller Plugins

Brenton,

Thanks for the feedback....I kept at it and I came up with a solution that sounds similar to what you have suggested.

I don't think its a bug, but more like a missing functionality. 

There should be a Zend_Application::run()  equivalent implemented in Zend_Test_PHPUnit_ControllerTestCase that accounts for this behaviour.

So when writing the test ....

$this->dispatch('/');

becomes

$this->run('/');


 .....Until then,  for others that may come across this issue this is what I implemented...

abstract class My_ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    protected $application;

    public function setUp()
    {

       
        $this->bootstrap = array($this, 'appBootstrap');
            
       
        return parent::setUp();
    }
   
    public function appBootstrap()
       {
      
        $this->application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/config/app.ini');
       
        $this->application->bootstrap();
       
        $bootstrap = $this->application->getBootstrap();
       
        $front = $bootstrap->getResource('FrontController');
       
        $front->setParam('bootstrap', $bootstrap);
                  
       }
}


class MyControllerTest extends My_ControllerTestCase
{

}

Regards,

Nomolos


On Thu, Jul 9, 2009 at 6:34 PM, Brenton Alker <brenton@tekerson.com> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nomolos Sharpe wrote:
> Greetings,
>
> I am having trouble using Zend_Test. I am using ZF trunk as at July 8
> (Yesterday) 09:31 (GMT-5)
>
> My Application works as intended when ran from the browser, I am now
> trying to implement Zend_Test but getting the following error.
>
> Fatal error: Call to a member function getPluginResource() on a
> non-object in ......./Controller/Plugin/Log.php on line 10.
>
> Up to Line 10 reads as follows:
>
>     .
>     .
>     .
>     public function preDispatch($request)
>     {
>         $bootstrap =
> Zend_Controller_Front::getInstance()->getParam('bootstrap');
>
>         $resource = $bootstrap->getPluginResource('session');
>
>
> I have tried adding the in the setUp() following but without success:
>
> $this->getFrontController()->setParam('bootstrap', $this->bootstrap);
>
> Please let me know what I need to do in order to get the bootstrap to be
> available within the Plugin?

The bootstrap doesn't seem to get injected as a invokeArg to the
controllers when dispatched through the test case. I'm not sure if this
is a bug or not?

Took me a while to get working too, this is what I came up with.

In the Test class itself:

class WhateverTest extends Zend_Test_PHPUnit_ControllerTestCase
{
   public $bootstrap = 'library/bootstrap.php';
   ...
}

and library/bootstrap.php looks like:

$application = new Zend_Application(
   APPLICATION_ENV,
   APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();

$this->getFrontController()->setParam('bootstrap',
$application->getBootstrap());

I actually do this in an abstract class, then extend my real test cases
from there.

- --

Brenton Alker
PHP Developer - Brisbane, Australia

http://blog.tekerson.com/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkpWfngACgkQ7bkAtAithusTUwCeIYxMiTlxnzWJRrdCfCFIyZbY
/NMAnjHeo8NcKhKLeB4Fa9TOYHKvmcLx
=Kib9
-----END PGP SIGNATURE-----


没有评论: