Issues

ZF-4511: Zend_Test_PHPUnit_ControllerTestCase::resetResponse() does not allow a new clean dispatch

Description

After calling a dispatch() in a test, the resetResponse() method cleans the response object and the registry instance. However, the request object still contains param from the previous dispatch and so it contaminates the new one. I personally call $this->setUp() after every dispatch/assertions cycle to ensure cleanliness. Not sure if it is the intended behaviour, I think reset the request object or provide a parameter of resetResponse() or a resetRequest() method would be useful.

Comments

I agree that this is an issue and comes up a lot for me in unit testing, especially when param are set during dispatch.

For example: function testCreateAccount() { $this->_login(); //dispatches to /auth/login and logs the user in $this->dispatch('/account/create'); //any param set during the dispatch to /auth/login remain }

This is particularly a problem when an error is thrown and I want to do a second dispatch. The ErrorHandler plugin sets the 'error_handler' param, which in some cases can cause issues.

I therefore think it would make sense to have the following in Zend_Test_PHPUnit_ControllerTestCase to make it clear that this should be called. function resetRequest() { $this->_request = null; return $this; }

It would go along nicely with resetResponse, which according to the PHPdoc is 'Useful for test cases that need to test multiple trips to the server.'

Actually, dispatch() should simply always create a new request and response pair. (I thought I had it doing this already, but it's not.)

Added resetRequest() in r12337 and r12339.

I'm reopening this due to the exact same problem being caused by the super globals being persistent across multiple requests.

e.g. http://pastie.org/private/mpga8blwtx4gcstiplkjw

In this case, I log my user in with login(), and immediately reset the response and request. Yet, since the $_POST data still contains 'username' and 'password' values, it causes undesired behavior in my changePassword action. Obviously this is easy enough to work around now that I know what's going on. However, it's very misleading to have a resetRequest() that doesn't truly reset the request. The manual even states that these are to be used to simulate multiple requests.

If I submit two forms in a row in my browser, the data from the first doesn't linger and affect the behavior of the second :p.

I wanted to add that while there is a resetRequest() function now, the dispatch() function still doesn't seem to actually create the new request and response pair. That is, I still have to call the resetRequest() and resetResponse() before any second dispatch within a single unit test.

And to second what David said, $_POST and $_GET have to be cleared with clearPost() and clearGet(), since the newly created Request object will simply read from these.

I am with David and Travis.

$_POST and $_GET still persists. The reference guide also gives an example of this to clear POST paramaters:


$this->resetRequest()
         ->resetResponse();

$this->request->setPost(array());

The setPost(array()) will not clear anything. resetRequest and resetResponse should handle calling clearPost() and clearGet(). If not at the very least documentation needs to updated also to use $this->request->clearPost(), but it is deceiving that resetRequest actually doesn't fully reset.

patch for unit tests for resetRequest() and a proposed patch to fix resetRequest()

the patch to fix resetRequest calls getRequest()->clearQuery(); but it could have just as easily called $_GET = array(); to fix the problem

And I'm unsure if we also want to clear $_REQUEST. Some parts of Zend use it, but there is no method in the request object to clear the _REQUEST superglobal.

My own patch calls both clearQuery() and clearPost() prior to unsetting the request.

Resetting $_REQUEST is not a good idea, as it would also reset cookies -- which likely *should* persist between requests.

My patch will apply shortly.

Fixed in trunk and 1.9 release branch