Is there way to test Magento POST controllers actions? E.g. customer login:
Mage_Customer_AccountController::loginPostAction()
I’m using EcomDev_PHPUnit module for test. It works great with basic actions, but I can’t invoke POST actions.
$this->getRequest()->setMethod('POST');
// ivokes loginAction instead loginPostAction
$this->dispatch('customer/account/login');
// fails also
$this->dispatch('customer/account/loginPost');
// but this assert pass
$this->assertEquals('POST', $_SERVER['REQUEST_METHOD']);
I would like to make tests more or less like
// ... some setup
$this->getRequest()->setMethod('POST');
$this->dispatch('customer/account/login');
// since login uses singleton, then...
$session = Mage::getSingleton('customer/session');
$this->assertTrue($session->isLoggedIn());
In customer account controller login and loginPost is two different actions. As for logging in, you need to do something like this:
But this kind of test body is only required if you need to test login process, but if just want to simulate that customer is logged in, you can create a stub that particular customer is logged in. I used it in few projects. Just add this method to your test case and then use when you need it. It will login customer within single test run and will tear down all the session changes afterwards.
Also renewSession method is mocked in above code, because it is using direct setcookie function, instead of core/cookie model, so in command line it produces an error.
Sincerely,
Ivan