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

Re: How to use PHPUnit in a MVC project

by Matthew Weier O'Phinney-3 :: Rate this Message:

Reply to Author | View in Thread

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