« Return to Thread: How to use PHPUnit in a MVC project

Re: How to use PHPUnit in a MVC project

by weierophinney :: Rate this Message:

Reply to Author | View in Thread

-- 13sio <ng_mo@...> wrote
(on Tuesday, 26 June 2007, 07:34 AM -0700):
> Instead of getting a instance of Zend_Controller_Front, is it okay to use
> Zend_Http_Client (more straightforward for me) to perform a HTTP request in
> unit test? For instance
>
> //...
> public function testIndexPageContents() {
>     $client = new Zend_Http_Client('http://localhost/');
>     $response = $client->request();
>     $this->assertFalse($response->isException());

The above assertion likely won't work, as you're using a
Zend_Http_Response, and not the controller response object. You'll want
to check your response codes and/or the page content instead.

>     $this->assertContains('index page', $response->getBody());

The above should still work.

> }
> //...
>
> This case works for my environment (Zf 1.0.0 RC3/PHPunit 3.0.6). If i was
> wrong, please correct me.

That works, but it is dependent on having the web server up and running.
I typically like my tests to be able to run with or without using the
web server -- I think of the tests as my sanity check prior to pushing
live.

Sure, there's a little more work to get the test case ready, but most of
that work is in the setUp() method -- which is only done once. The
actual test cases are basically as straightforward as the test you show
above -- create your request, dispatch it, and then run your assertions
against the response.

> Matthew Weier O'Phinney-3 wrote:
> >
> > -- chelala <chelala@...> wrote
> > (on Friday, 22 June 2007, 09:38 AM -0700):
> > > How to use PHPUnit in my Zend Framework MVC project. Say, for testing one
> > > controller's action, some model class function of my own or a view
> > > scripts
> > > ???
> > >
> > > How to prepare those kind of tests ? I do not know too much, but I can
> > > not
> > > imagine how to setup the test, as all the proccess happens, from the
> > > front
> > > controller in the bootstrapper to the end.
> >
> > It's not too difficult, fortunately. Here's an example skeleton:
> >
> > class FooControllerTest extends PHPUnit_Framework_TestCase
> > {
> >     public function setUp()
> >     {
> >         $this->front = Zend_Controller_Front::getInstance();
> >         $this->front->addModuleDirectory('/path/to/modules'); // path to
> > modules and hence controllers
> >         $this->front->resetInstance();      // clear out any settings from
> > prior runs
> >         $this->front->returnResponse(true); // don't auto-emit the
> > response
> >     }
> >
> >     public function testIndexPageContents()
> >     {
> >         // The URL is primarily used so the request URI and related
> >         // information can be set:
> >         $request = new Zend_Controller_Request_Http('http://localhost/');
> >
> >         // Because returnResponse() has been set to true, we can grab
> >         // the response object this way, and not worry about it being
> >         // auto-emitted:
> >         $response = $this->front->dispatch($request);
> >
> >         // Now you can test!
> >         $this->assertFalse($response->isException()); // no exceptions!
> >
> >         // test that content contains certain strings
> >         $this->assertContains('index page', $response->getBody());
> >
> >         // etc...
> >     }
> > }
> >
> > Hope that will help you get started.

--
Matthew Weier O'Phinney
PHP Developer            | matthew@...
Zend - The PHP Company   | http://www.zend.com/

 « Return to Thread: How to use PHPUnit in a MVC project