tags:

views:

190

answers:

3

Hi Guys!

I'm new on testing, I'm using PHPUnit to write test. All the site has been designed using the MVC pattern.

I would like to test each method on my controllers, the problem is that such methods receives the parameters though the $_POST variable. How can I overwrite this variable?

Thanks in advance Alejandra

+4  A: 

The best approach would be to abstract the Request into a separate class and not access the superglobals at all. This way you decouple the actual server and request environment from the application. You can then mock the Request easily.

Gordon
+1  A: 

Even if some will say it's not clean, you could set data in $_POST, before callng your methods : it's not read only ;-)

Pascal MARTIN
+1  A: 

First, if you are using Zend_Test, use

$this->request->setMethod('POST')->setPost(array(insert array info here));

If your not using that, try this:

Warning: Not the best solution, but it will work!

Put this in your setUp fixture

 protected function setUp()
    {
        parent::setUp();
        $_POST = array();
    }

Then before you call your test just do something similar to this:

$_POST = array(insert array data here)
Enrique