2

So I've made an integration test where my setUp() method add a query parameter to the request like this:

Mage::app()->getRequest()->setQuery('entry', 'value');

However, the tests that run after this test will have their request still containing the query parameter.

The only way I managed to avoid that was by adding the following line in my tearDown() method :

unset($_GET['entry']);

I want to know what is the proper Magento way of doing that ?

I tried calling:

Mage::app()->getRequest()->setQuery('entry', null);

But I get the following error:

Invalid value passed to setQuery(); must be either array of values or key/value pair

I also tried:

Mage::app()->getRequest()->setParam('entry', null);

No error but still does not remove the query parameter.

Even reinit the entire request would be fine if that's possible.

2

Magento 1 does not have a "proper Magento way" for integration tests because it does not have them.

The EcomDev_PHPUnit framework replaces Mage_Core_Controller_Request_Http with an extended version which has a reset() method, but without such a workaround it is not possible to reset the properties of the request object.


The good thing about setQuery() is that it internally modifies the superglobal $_GET, which is why unset($_GET['entry']); works.

The same goes for setPost() and $_POST, and to revert a call to setParam() you can use setParam($key, null).


Another idea is to replace the request object with a new instance in your setUp() or tearDown() methods:

Mage::app()->setRequest(new Mage_Core_Controller_Request_Http());

You can also do the same with the response object:

$response = new Mage_Core_Controller_Response_Http();
$response->headersSentThrowsException = Mage::$headersSentThrowsException;
$this->_response->setHeader("Content-Type", "text/html; charset=UTF-8");
Mage::app()->setResponse($response);
| improve this answer | |
  • I tried setQuery($key, null) as well as setParam($key, null) but of them work. I also tried Mage::app()->setRequest(new Mage_Core_Controller_Request_Http()); but it didn't work either :( – Raphael at Digital Pianism Jun 17 '16 at 17:32
  • setParam and setQuery do different things, that's why I said using $_GET is what you have to do in your case – Fabian Schmengler Jun 17 '16 at 17:34
  • yeah that's what I thought too, @vinai also suggested to use Mage::reset() but it seems kind of slow – Raphael at Digital Pianism Jun 17 '16 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.