« 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, 08:14 AM -0700):
> You are right. Zend_Http_Response has no isException() method. i got it
> wrong in copy & paste.
>
> And how do you test your application without a running web server?

The original example I showed does not require a web server. You pass a
URL to Zend_Controller_Request_Http and pass that request object to
the front controller to dispatch. Zend_Controller_Request_Http allows
for optionally passing a URL to the constructor (or using
setRequestUri()) in order to set the request URI, and only grabs it from
the web server if none has been set.

> any benefit?

Yes! You can test your applications *before* deployment, or on developer
machines that don't have a web server running. Additionally, using PHP
directly instead of having to call out to a server is going to be
faster, particularly if you have a large test suite.

Testing against the actual web server is good, too, but I find I often
want to test as I'm developing, and not need to worry about setting up a
vhost. Using the controller's request/response pair lets me do that.

> Matthew Weier O'Phinney-3 wrote:
> >
> > -- 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/
> >
> >
>
> --
> View this message in context: http://www.nabble.com/How-to-use-PHPUnit-in-a-MVC-project-tf3965725s16154.html#a11307464
> Sent from the Zend MVC mailing list archive at Nabble.com.
>

--
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