How to use PHPUnit in a MVC project

View: New views
20 Messages — Rating Filter:   Alert me  
< Prev | 1 - 2 | Next >

How to use PHPUnit in a MVC project

by chelala :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

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.

Thanks

Harold J. A. Chelala

Re: How to use PHPUnit in a MVC project

by weierophinney :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

RE: How to use PHPUnit in a MVC project

by Bill Karwin from Zend :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm experimenting with using DOM and Xquery to find if a given element
exists in the page body.
I implemented two new "assert" methods, to convert the HTML to DOM and
then evaluate an Xquery expression against it.

Function assertXquery($query, $htmlString) just checks if the Xquery
returned non-empty results.
Function assertXqueryContains($query, $needle, $htmlString) checks if
the string value of the element returned by the Xquery contains the
string in $needle.

I'm hoping to improve this code and post it soon, to accompany my
example application I used for the webinar.

Regards,
Bill Karwin

class GridTest extends PHPUnit_Framework_TestCase
{

    ...setUp() function omitted...

    protected function _xquery($query, $htmlString)
    {
        $doc = new DOMDocument();
        $doc->preserveWhiteSpace = false;
        $doc->loadHTML($htmlString);
        $xpath = new DOMXPath($doc);
        $results = $xpath->query($query);
        $this->assertFalse($results === false, "Xquery '$query'
failed");
        return $results;
    }

    public function assertXqueryContains($query, $needle, $htmlString)
    {
        $results = $this->_xquery($query, $htmlString);
        $this->assertNotEquals(0, $results->length,
            "Found no match for xquery '$query'");
        $haystack = $results->item(0)->nodeValue;
        $this->assertType('string', $haystack);
        $this->assertContains($needle, $haystack,
            "Found no match for '$needle' contained in '$haystack'");
    }

    public function assertXquery($query, $htmlString)
    {
        $results = $this->_xquery($query, $htmlString);
        $this->assertNotEquals(0, $results->length,
            "Found no match for xquery '$query'");
    }

    public function testIndexControllerIndexAction()
    {
        $this->_request->setControllerName('grid');
        $this->_request->setActionName('show');
        $response = $this->_front->dispatch();
        $this->assertFalse($response->isException());
        $body = $response->getBody();

        // probably should test one thing at a time, but this is just
for proof of concept
        $this->assertXqueryContains('//head/title', 'Zend Framework',
$body);
        $this->assertXqueryContains('//body/h1', 'Zend Framework',
$body);
        $this->assertXqueryContains('//body/h2', 'Tables', $body);
        $this->assertXquery('//body/ul', $body);
    }

}

> -----Original Message-----
> From: Matthew Weier O'Phinney [mailto:matthew@...]
> Sent: Friday, June 22, 2007 9:50 AM
> To: fw-mvc@...
> Subject: Re: [fw-mvc] How to use PHPUnit in a MVC project
>
> -- 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/
>

Re: How to use PHPUnit in a MVC project

by chelala :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks very much for the fast answer I will start using it. And will feed back any experience!

Any other place to read about this subject ? wiki ? tutorial ? forum thread ?

thanks again

Harold Chelala

Re: How to use PHPUnit in a MVC project

by Bryce Lohr :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You might want to also check out this great article by Luke Crouch:

http://tulsaphp.net/?q=node/40

Hope this helps,
Bryce Lohr


chelala wrote:
> Thanks very much for the fast answer I will start using it. And will feed
> back any experience!
>
> Any other place to read about this subject ? wiki ? tutorial ? forum thread
> ?
>
> thanks again
>
> Harold Chelala

Re: How to use PHPUnit in a MVC project

by 13sio :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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());
    $this->assertContains('index page', $response->getBody());
}
//...

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

Thanks!

Matthew Weier O'Phinney-3 wrote:
-- chelala <chelala@vcl.desoft.cu> 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.com
Zend - The PHP Company   | http://www.zend.com/

Re: How to use PHPUnit in a MVC project

by weierophinney :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: How to use PHPUnit in a MVC project

by 13sio :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Matthew,

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? any benefit?

Thanks.
lee

Matthew Weier O'Phinney-3 wrote:
-- 13sio <ng_mo@yahoo.com.hk> 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@vcl.desoft.cu> 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.com
Zend - The PHP Company   | http://www.zend.com/

Re: How to use PHPUnit in a MVC project

by weierophinney :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: How to use PHPUnit in a MVC project

by Nick Lo-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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

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

As a response the the "is it okay" question:  More and more of us  
(myself included) have some kind of "must do more testing" resolution  
but there is the perception, particularly for small teams and sole  
developer situations, that unit testing adds an overhead, albeit in  
the short term, to the work. If you do have the webserver setup then  
using Zend_Http_Client is a very quick and simple way to run these  
kind of tests and in that respect a fast forward to developing the  
"test first" habit. Once that habit is established then you can look  
at the different ways of testing.

In short I think if you're just getting into setting up your tests  
then go with the simplest route that works. You can always refine  
them later but initially it's more important to actually have the  
tests in the first place.

Nick

 

Re: How to use PHPUnit in a MVC project

by Ralf Eggert :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

here are my two cents to this issue. For my tests I am using both
PHPUnit and the Simpletest webtester. When I started I devided my tests
into three groups:

a) Unit Tests (to test ACL, Models, Plugins, Setup, Routing, Framework
   components (my extensions), etc.)

b) Navi Tests (to test the navigation of the website, i.e. main
   menu, sub menu, page menu, context menu, etc.)

c) Web Tests (to test the content, forms and processing of my website,
   i.e. the complete registration process from calling the
   registration page and filling in the registration form until
   checking errors, sending mails and calling the activation link)

For a) I am using PHPUnit and for b) and c) the Simpletest webtester.
Both work like a charm for me. I was playing around with PHPUnits
Selenium extension but did not really like it.

Although I turned into a "test-first" maniac since I am developing with
the Zend Framework I am still highly productive. Since I reached nearly
100% test-covered code I have a high degree of trust into my code.
Whenever there is a new ZF release I only have to run my tests until all
of them work correctly again.

Ok, I am conscious that my code is not 100% bug free, because no code
will ever be 100% bug free. But since I started using the ZF and the
test-driven development approach I really have a better feeling.
Although it was quite hard in the beginning to stop hacking in some code
here and there without any written test for it.

Best Regards,

Ralf


Re: How to use PHPUnit in a MVC project

by Kamil N :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a question how to test models .i  mean functions where i use
database insert rows, update and another select rows

Thanks



Re: How to use PHPUnit in a MVC project

by yara :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Matthew!

I try to follow your recommendations to implement tests via PHPUnit and get following error:

yara@cc-yara application $ php AllTests.php
PHPUnit 3.1.7 by Sebastian Bergmann.

.E.

Time: 0 seconds

There was 1 error:

1) testEditAction(BuildingsControllerTests)
Undefined index:  default
/usr/local/php5/lib/php/Zend/Controller/Dispatcher/Standard.php:318
/usr/local/php5/lib/php/Zend/Controller/Dispatcher/Standard.php:160
/usr/local/php5/lib/php/Zend/Controller/Dispatcher/Standard.php:190
/usr/local/php5/lib/php/Zend/Controller/Front.php:911
/home/yara/work/Zend/address/test/application/controllers/BuildingsControllerTests.php:53
/home/yara/work/Zend/address/test/application/AllTests.php:46
/home/yara/work/Zend/address/test/application/AllTests.php:58

FAILURES!
Tests: 3, Errors: 1.

My directory structure:
application/
    controllers/
        BuildingsController.php
        ...
    models/
        ...
    views/
        ...
public/
    ...
test/
    ...

What's wrong?
Matthew Weier O'Phinney-3 wrote:
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
    }

    ...
}

Hope that will help you get started.

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

Re: How to use PHPUnit in a MVC project

by Leo Büttiker :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you Matthew! Your example just work great for use. At the moment we use it for testing non-zend-view-helpers and not-yet-zend-view-helper.

Today I ran in a small problem, I just found out that $response->getBody() collects the html from every test I run. Is there a easy way to reset all all vars. Looks like there are some lazy static stuff is still hanging around...
Cheers,
leo

Matthew Weier O'Phinney-3 wrote:
It's not too difficult, fortunately. Here's an example skeleton:

class FooControllerTest extends PHPUnit_Framework_TestCase
{
    public function setUp()
    {

RE: How to use PHPUnit in a MVC project

by Bill Karwin from Zend :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Look in the unit test script for the Front Controller in Zend Framework:
<zf-home>/tests/Zend/Controller/FrontTest.php.

In the setUp() method, it resets the front controller from one test to the next.  

For example:

        $this->_controller = Zend_Controller_Front::getInstance();
        $this->_controller->resetInstance();
        Zend_Controller_Action_HelperBroker::resetHelpers();

Regards,
Bill Karwin

> -----Original Message-----
> From: Leo Büttiker [mailto:leo.buettiker@...]
> Sent: Wednesday, August 29, 2007 9:25 AM
> To: fw-mvc@...
> Subject: Re: [fw-mvc] How to use PHPUnit in a MVC project
>
>
> Thank you Matthew! Your example just work great for use. At
> the moment we use it for testing non-zend-view-helpers and
> not-yet-zend-view-helper.
>
> Today I ran in a small problem, I just found out that
> $response->getBody() collects the html from every test I run.
> Is there a easy way to reset all all vars. Looks like there
> are some lazy static stuff is still hanging around...
> Cheers,
> leo
>
>
> Matthew Weier O'Phinney-3 wrote:
> >
> > It's not too difficult, fortunately. Here's an example skeleton:
> >
> > class FooControllerTest extends PHPUnit_Framework_TestCase {
> >     public function setUp()
> >     {
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/How-to-use-PHPUnit-in-a-MVC-project-tf39
65725s16154.html#a12390650
> Sent from the Zend MVC mailing list archive at Nabble.com.
>
>

Re: How to use PHPUnit in a MVC project

by weierophinney :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

-- Bill Karwin <bill.k@...> wrote
(on Wednesday, 29 August 2007, 10:56 AM -0700):

> Look in the unit test script for the Front Controller in Zend Framework:
> <zf-home>/tests/Zend/Controller/FrontTest.php.
>
> In the setUp() method, it resets the front controller from one test to the next.  
>
> For example:
>
>         $this->_controller = Zend_Controller_Front::getInstance();
>         $this->_controller->resetInstance();
>         Zend_Controller_Action_HelperBroker::resetHelpers();

It's also a good idea to initialize a new request and response object
for each test case. I usually do this in the test cases themselves:

    public function testSomething()
    {
        $request  = new Zend_Controller_Request_Http($someUrl);
        $response = new Zend_Controller_Response_Http();

        // assumes you set 'returnResponse' in the setUp() method:
        $response = $this->_controller->dispatch($request, $response);

        // test response...
    }

That part can be done in the setUp() method as well (though the request
will likely need to be done in each test method, as it contains the
information specific to what you're testing).

> > -----Original Message-----
> > From: Leo Büttiker [mailto:leo.buettiker@...]
> > Sent: Wednesday, August 29, 2007 9:25 AM
> > To: fw-mvc@...
> > Subject: Re: [fw-mvc] How to use PHPUnit in a MVC project
> >
> >
> > Thank you Matthew! Your example just work great for use. At
> > the moment we use it for testing non-zend-view-helpers and
> > not-yet-zend-view-helper.
> >
> > Today I ran in a small problem, I just found out that
> > $response->getBody() collects the html from every test I run.
> > Is there a easy way to reset all all vars. Looks like there
> > are some lazy static stuff is still hanging around...
> > Cheers,
> > leo
> >
> >
> > Matthew Weier O'Phinney-3 wrote:
> > >
> > > It's not too difficult, fortunately. Here's an example skeleton:
> > >
> > > class FooControllerTest extends PHPUnit_Framework_TestCase {
> > >     public function setUp()
> > >     {
> > >
> > >
> >
> > --
> > View this message in context:
> > http://www.nabble.com/How-to-use-PHPUnit-in-a-MVC-project-tf39
> 65725s16154.html#a12390650
> > 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/

Re: How to use PHPUnit in a MVC project

by Kevin McArthur-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

A manual section on this would probably be a good idea. Anyone in the docs
area able to do this?

K
----- Original Message -----
From: "Matthew Weier O'Phinney" <matthew@...>
To: <fw-mvc@...>
Sent: Wednesday, August 29, 2007 11:32 AM
Subject: Re: [fw-mvc] How to use PHPUnit in a MVC project


> -- Bill Karwin <bill.k@...> wrote
> (on Wednesday, 29 August 2007, 10:56 AM -0700):
>> Look in the unit test script for the Front Controller in Zend Framework:
>> <zf-home>/tests/Zend/Controller/FrontTest.php.
>>
>> In the setUp() method, it resets the front controller from one test to
>> the next.
>>
>> For example:
>>
>>         $this->_controller = Zend_Controller_Front::getInstance();
>>         $this->_controller->resetInstance();
>>         Zend_Controller_Action_HelperBroker::resetHelpers();
>
> It's also a good idea to initialize a new request and response object
> for each test case. I usually do this in the test cases themselves:
>
>    public function testSomething()
>    {
>        $request  = new Zend_Controller_Request_Http($someUrl);
>        $response = new Zend_Controller_Response_Http();
>
>        // assumes you set 'returnResponse' in the setUp() method:
>        $response = $this->_controller->dispatch($request, $response);
>
>        // test response...
>    }
>
> That part can be done in the setUp() method as well (though the request
> will likely need to be done in each test method, as it contains the
> information specific to what you're testing).
>
>> > -----Original Message-----
>> > From: Leo Büttiker [mailto:leo.buettiker@...]
>> > Sent: Wednesday, August 29, 2007 9:25 AM
>> > To: fw-mvc@...
>> > Subject: Re: [fw-mvc] How to use PHPUnit in a MVC project
>> >
>> >
>> > Thank you Matthew! Your example just work great for use. At
>> > the moment we use it for testing non-zend-view-helpers and
>> > not-yet-zend-view-helper.
>> >
>> > Today I ran in a small problem, I just found out that
>> > $response->getBody() collects the html from every test I run.
>> > Is there a easy way to reset all all vars. Looks like there
>> > are some lazy static stuff is still hanging around...
>> > Cheers,
>> > leo
>> >
>> >
>> > Matthew Weier O'Phinney-3 wrote:
>> > >
>> > > It's not too difficult, fortunately. Here's an example skeleton:
>> > >
>> > > class FooControllerTest extends PHPUnit_Framework_TestCase {
>> > >     public function setUp()
>> > >     {
>> > >
>> > >
>> >
>> > --
>> > View this message in context:
>> > http://www.nabble.com/How-to-use-PHPUnit-in-a-MVC-project-tf39
>> 65725s16154.html#a12390650
>> > 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/ 


Re: How to use PHPUnit in a MVC project

by weierophinney :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

-- Kevin McArthur <kevin@...> wrote
(on Wednesday, 29 August 2007, 12:04 PM -0700):
> A manual section on this would probably be a good idea. Anyone in the docs
> area able to do this?

I'll add it to my TODO list; it's something I've been wanting to address
in the docs anyways. :-)


> ----- Original Message -----
> From: "Matthew Weier O'Phinney" <matthew@...>
> To: <fw-mvc@...>
> Sent: Wednesday, August 29, 2007 11:32 AM
> Subject: Re: [fw-mvc] How to use PHPUnit in a MVC project
>
>
> >-- Bill Karwin <bill.k@...> wrote
> >(on Wednesday, 29 August 2007, 10:56 AM -0700):
> >>Look in the unit test script for the Front Controller in Zend Framework:
> >><zf-home>/tests/Zend/Controller/FrontTest.php.
> >>
> >>In the setUp() method, it resets the front controller from one test to
> >>the next.
> >>
> >>For example:
> >>
> >>        $this->_controller = Zend_Controller_Front::getInstance();
> >>        $this->_controller->resetInstance();
> >>        Zend_Controller_Action_HelperBroker::resetHelpers();
> >
> >It's also a good idea to initialize a new request and response object
> >for each test case. I usually do this in the test cases themselves:
> >
> >   public function testSomething()
> >   {
> >       $request  = new Zend_Controller_Request_Http($someUrl);
> >       $response = new Zend_Controller_Response_Http();
> >
> >       // assumes you set 'returnResponse' in the setUp() method:
> >       $response = $this->_controller->dispatch($request, $response);
> >
> >       // test response...
> >   }
> >
> >That part can be done in the setUp() method as well (though the request
> >will likely need to be done in each test method, as it contains the
> >information specific to what you're testing).
> >
> >>> -----Original Message-----
> >>> From: Leo Büttiker [mailto:leo.buettiker@...]
> >>> Sent: Wednesday, August 29, 2007 9:25 AM
> >>> To: fw-mvc@...
> >>> Subject: Re: [fw-mvc] How to use PHPUnit in a MVC project
> >>>
> >>>
> >>> Thank you Matthew! Your example just work great for use. At
> >>> the moment we use it for testing non-zend-view-helpers and
> >>> not-yet-zend-view-helper.
> >>>
> >>> Today I ran in a small problem, I just found out that
> >>> $response->getBody() collects the html from every test I run.
> >>> Is there a easy way to reset all all vars. Looks like there
> >>> are some lazy static stuff is still hanging around...
> >>> Cheers,
> >>> leo
> >>>
> >>>
> >>> Matthew Weier O'Phinney-3 wrote:
> >>> >
> >>> > It's not too difficult, fortunately. Here's an example skeleton:
> >>> >
> >>> > class FooControllerTest extends PHPUnit_Framework_TestCase {
> >>> >     public function setUp()
> >>> >     {
> >>> >
> >>> >
> >>>
> >>> --
> >>> View this message in context:
> >>> http://www.nabble.com/How-to-use-PHPUnit-in-a-MVC-project-tf39
> >>65725s16154.html#a12390650
> >>> 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/ 
>

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

Re: How to use PHPUnit in a MVC project

by lcrouch :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

a while back I wrote some tutorials on unit-testing zend controllers:

http://tulsaphp.net/node/40

it's sorta old and I think the methods mentioned here are preferable to those that I used in my BaseControllerTestCase.php, but I'd love to help ZF in any way, so I'll volunteer to write up some manual section on unit-testing controllers as discussed, if that's acceptable.

-L

On 8/29/07, Matthew Weier O'Phinney <matthew@...> wrote:
-- Kevin McArthur <kevin@...> wrote
(on Wednesday, 29 August 2007, 12:04 PM -0700):
> A manual section on this would probably be a good idea. Anyone in the docs
> area able to do this?

I'll add it to my TODO list; it's something I've been wanting to address
in the docs anyways. :-)


> ----- Original Message -----
> From: "Matthew Weier O'Phinney" < matthew@...>
> To: <fw-mvc@...>
> Sent: Wednesday, August 29, 2007 11:32 AM
> Subject: Re: [fw-mvc] How to use PHPUnit in a MVC project
>
>
> >-- Bill Karwin <bill.k@...> wrote
> >(on Wednesday, 29 August 2007, 10:56 AM -0700):
> >>Look in the unit test script for the Front Controller in Zend Framework:
> >><zf-home>/tests/Zend/Controller/FrontTest.php.
> >>
> >>In the setUp() method, it resets the front controller from one test to
> >>the next.
> >>
> >>For example:
> >>
> >>        $this->_controller = Zend_Controller_Front::getInstance();
> >>        $this->_controller->resetInstance();
> >>        Zend_Controller_Action_HelperBroker::resetHelpers();
> >
> >It's also a good idea to initialize a new request and response object
> >for each test case. I usually do this in the test cases themselves:
> >
> >   public function testSomething()
> >   {
> >       $request  = new Zend_Controller_Request_Http($someUrl);
> >       $response = new Zend_Controller_Response_Http();
> >
> >       // assumes you set 'returnResponse' in the setUp() method:
> >       $response = $this->_controller->dispatch($request, $response);
> >
> >       // test response...
> >   }
> >
> >That part can be done in the setUp() method as well (though the request
> >will likely need to be done in each test method, as it contains the
> >information specific to what you're testing).
> >
> >>> -----Original Message-----
> >>> From: Leo Büttiker [mailto: leo.buettiker@...]
> >>> Sent: Wednesday, August 29, 2007 9:25 AM
> >>> To: fw-mvc@...
> >>> Subject: Re: [fw-mvc] How to use PHPUnit in a MVC project
> >>>
> >>>
> >>> Thank you Matthew! Your example just work great for use. At
> >>> the moment we use it for testing non-zend-view-helpers and
> >>> not-yet-zend-view-helper.
> >>>
> >>> Today I ran in a small problem, I just found out that
> >>> $response->getBody() collects the html from every test I run.
> >>> Is there a easy way to reset all all vars. Looks like there
> >>> are some lazy static stuff is still hanging around...
> >>> Cheers,
> >>> leo
> >>>
> >>>
> >>> Matthew Weier O'Phinney-3 wrote:
> >>> >
> >>> > It's not too difficult, fortunately. Here's an example skeleton:
> >>> >
> >>> > class FooControllerTest extends PHPUnit_Framework_TestCase {
> >>> >     public function setUp()
> >>> >     {
> >>> >
> >>> >
> >>>
> >>> --
> >>> View this message in context:
> >>> http://www.nabble.com/How-to-use-PHPUnit-in-a-MVC-project-tf39
> >>65725s16154.html#a12390650
> >>> 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/
>

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


AW: How to use PHPUnit in a MVC project

by Leo Büttiker :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you both! It works now as it should.

In setUp I have a very similar configuration like in the bootstrap. In every
test method i do:
   $request = new
Zend_Controller_Request_Http('http://localhost/Test/Index/view');
   $response = $this->front->dispatch($request);
   $this->assertFalse($response->isException()); // no exceptions!
   $this->assertContains('<html>', $response->getBody(), 'does not load
layout');
   [more lines like the last follow here]

And finally I reset the frontcontroller and the view in the tearDown Method:
protected function tearDown()
{
   $this->front->resetInstance();
   Zend_Controller_Action_HelperBroker::resetHelpers();
   $this->front = null;
   $this->view = null;
}

Hopefully this might help others until this find it's way in the
documentation.
leo


-----Ursprüngliche Nachricht-----
Von: Matthew Weier O'Phinney [mailto:matthew@...]
Gesendet: Mittwoch, 29. August 2007 20:33

-- Bill Karwin <bill.k@...> wrote
(on Wednesday, 29 August 2007, 10:56 AM -0700):
> Look in the unit test script for the Front Controller in Zend Framework:
> <zf-home>/tests/Zend/Controller/FrontTest.php.
>
> In the setUp() method, it resets the front controller from one test to the
next.  
>
> For example:
>
>         $this->_controller = Zend_Controller_Front::getInstance();
>         $this->_controller->resetInstance();
>         Zend_Controller_Action_HelperBroker::resetHelpers();

It's also a good idea to initialize a new request and response object
for each test case. I usually do this in the test cases themselves:

    public function testSomething()
    {
        $request  = new Zend_Controller_Request_Http($someUrl);
        $response = new Zend_Controller_Response_Http();

        // assumes you set 'returnResponse' in the setUp() method:
        $response = $this->_controller->dispatch($request, $response);

        // test response...
    }

That part can be done in the setUp() method as well (though the request
will likely need to be done in each test method, as it contains the
information specific to what you're testing).

< Prev | 1 - 2 | Next >