Unit Testing with Zend_Test

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Unit Testing with Zend_Test

Joseph Crawford
Hello Everyone,

I have been working with Unit Testing in a ZF Application over the last few days and I have hit a problem.  What I am wondering is if my application/Bootstrap.php file will be executed when testing.  The reason I am asking is because I will want all of my custom routes to resolve properly.  I also would like to find out if the Zend_Controller_Front is setup.

In one of my test methods I am doing $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap'); and when I var_dump the $bootstrap variable I am getting back NULL.

My directory structure for my application is the same as generated by Zend_Tool and my tests are in the following structure:

/tests
--/application
----/controllers
------/ControllerTest.php
----/models
------/ModelTest.php
--/library
----/MyLib
------/FileTest.php

I have created the bootstrap.php file which the phpunit.xml executes and I am using a class to actually setup the testing environment.  All of my test cases extend from ControllerTestCase.php which looks like the following:

class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
        /**
         * @var Zend_Application
         */
        protected $application;
       
        public function setUp()
        {
                $this->bootstrap = array($this, 'appBootstrap');
                parent::setUp();
        }
       
        public function appBootstrap()
        {
                $this->application = new Zend_Application(
                        OVISTORE_ENVIRONMENT,
                        APPLICATION_PATH . '/configs/app.ini'
                );
        }
}

So I wonder with this setup is there anything I *have* to do in order to make the Zend_Controller_Front::getInstance()->getParam('bootstrap') not return NULL?

Thanks,
Joseph Crawford
Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing with Zend_Test

Jake McGraw
Joseph:

Within an instance of Zend_Test_PHPUnit_ControllerTestCase:

...

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

...

You have to set the bootstrap param manually!

- jake

On Wed, Mar 24, 2010 at 1:02 PM, Joseph Crawford
<[hidden email]> wrote:

>
> Hello Everyone,
>
> I have been working with Unit Testing in a ZF Application over the last few
> days and I have hit a problem.  What I am wondering is if my
> application/Bootstrap.php file will be executed when testing.  The reason I
> am asking is because I will want all of my custom routes to resolve
> properly.  I also would like to find out if the Zend_Controller_Front is
> setup.
>
> In one of my test methods I am doing $bootstrap =
> Zend_Controller_Front::getInstance()->getParam('bootstrap'); and when I
> var_dump the $bootstrap variable I am getting back NULL.
>
> My directory structure for my application is the same as generated by
> Zend_Tool and my tests are in the following structure:
>
> /tests
> --/application
> ----/controllers
> ------/ControllerTest.php
> ----/models
> ------/ModelTest.php
> --/library
> ----/MyLib
> ------/FileTest.php
>
> I have created the bootstrap.php file which the phpunit.xml executes and I
> am using a class to actually setup the testing environment.  All of my test
> cases extend from ControllerTestCase.php which looks like the following:
>
> class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
> {
>        /**
>         * @var Zend_Application
>         */
>        protected $application;
>
>        public function setUp()
>        {
>                $this->bootstrap = array($this, 'appBootstrap');
>                parent::setUp();
>        }
>
>        public function appBootstrap()
>        {
>                $this->application = new Zend_Application(
>                        OVISTORE_ENVIRONMENT,
>                        APPLICATION_PATH . '/configs/app.ini'
>                );
>        }
> }
>
> So I wonder with this setup is there anything I *have* to do in order to
> make the Zend_Controller_Front::getInstance()->getParam('bootstrap') not
> return NULL?
>
> Thanks,
> Joseph Crawford
> --
> View this message in context: http://n4.nabble.com/Unit-Testing-with-Zend-Test-tp1680861p1680861.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing with Zend_Test

Joseph Crawford
Jake,

is there a reason you have to do it manually in the tests but not in the actual application?

Thanks,
Joseph Crawford
Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing with Zend_Test

Joseph Crawford
In reply to this post by Jake McGraw
Jake,

Will I also have to move my Bootstrap.php file into the /tests/application/ directory or will it know where to find it?

Thanks,
Joseph Crawford
Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing with Zend_Test

Jake McGraw
On Wed, Mar 24, 2010 at 1:09 PM, Joseph Crawford
<[hidden email]> wrote:
>
> Jake,
>
> Will I also have to move my Bootstrap.php file into the /tests/application/
> directory or will it know where to find it?

No, just establish where your application.ini file is relative to your
tests. I typically have a TestHelper.php file that sets up the
environment, much like the index.php file in a traditional Zend MVC
application.

>
> Thanks,
> Joseph Crawford
> --
> View this message in context: http://n4.nabble.com/Unit-Testing-with-Zend-Test-tp1680861p1680875.html

> Jake,
>
> is there a reason you have to do it manually in the tests but not in the
> actual application?

Not sure what the reason is, I imagine it's because you aren't
allowing Zend_Application to initialize the bootstrap, instead you're
doing it manually.

>
> Thanks,
> Joseph Crawford
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing with Zend_Test

Joseph Crawford
Jake,

I am not intentionally stopping Zend_Application from loading the bootstrap.  

As you mentioned I do have a bootstrap.php file that is the same as your TestHelper.php it's my index.php from the app except it does not call ->bootstrap()->run();

Thanks,
Joseph Crawford
Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing with Zend_Test

Giorgio Sironi
On Wed, Mar 24, 2010 at 6:32 PM, Joseph Crawford
<[hidden email]> wrote:
>
> Jake,
>
> I am not intentionally stopping Zend_Application from loading the bootstrap.
>
> As you mentioned I do have a bootstrap.php file that is the same as your
> TestHelper.php it's my index.php from the app except it does not call
> ->bootstrap()->run();

It is in Bootstrap::run() that the object sets itself with
setParam('bootstrap', $this). Not sure why.

--
Giorgio Sironi
Piccolo Principe & Web Engineer
http://giorgiosironi.blogspot.com
http://twitter.com/giorgiosironi
Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing with Zend_Test

A.J. Brown-3
Routes should be tested independently of everything else.  You should
be testing your controllers and actions.  Routes are just smoke and
mirrors.

I personally run a seperate bootstrap file for my automated tests.
It's essentially the same as index.php, but without the ->run() call
on the bootstrapper, and with the environment forced to 'testing'.

if you're using PHPUnit, you can set a bootstrap file in the configuration:

http://pastebin.com/Cu6Bcn1r

PHPUnit will run it before each test.


On Wed, Mar 24, 2010 at 1:48 PM, Giorgio Sironi
<[hidden email]> wrote:

> On Wed, Mar 24, 2010 at 6:32 PM, Joseph Crawford
> <[hidden email]> wrote:
>>
>> Jake,
>>
>> I am not intentionally stopping Zend_Application from loading the bootstrap.
>>
>> As you mentioned I do have a bootstrap.php file that is the same as your
>> TestHelper.php it's my index.php from the app except it does not call
>> ->bootstrap()->run();
>
> It is in Bootstrap::run() that the object sets itself with
> setParam('bootstrap', $this). Not sure why.
>
> --
> Giorgio Sironi
> Piccolo Principe & Web Engineer
> http://giorgiosironi.blogspot.com
> http://twitter.com/giorgiosironi
>



--
A.J. Brown
Software Engineer, ZCE
blog : http://ajbrown.org
talk  : (937) 540-0099
chat : IntypicaAJ
Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing with Zend_Test

Joseph Crawford
I think there is some confusion with my use of the word Bootstrap.  When I mean bootstrap I am talking about the

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { }

That was not getting run until I manually called $application->bootstrap(); and the bootstrap object was not being injected into the front controller until i forced it in per Jakes suggestion.

I have the "bootstrap" file that you guys are talking about and it is essentially the same as the /public/index.php except for the fact that i set the environment to testing and did not call ->run();
Reply | Threaded
Open this post in threaded view
|

Re: Unit Testing with Zend_Test

Joseph Crawford
Sorry ignore the last part of the last response with the calling of ->run().  The Zend Application is not setup in that file, rather it is setup in the ControllerTestCase class that extends the Zend testing classes.