After some serious debugging I have found I can't call to get a session object in Magento when running a test with EcomDev_PHPUnit module
any singleton/model call i.e. Mage::getSingleton('admin/session')
or Mage::getModel('customer/session')
eventually throws an exception from EcomDev_PHPUnit_Controller_Request_Http::getHttpHost()
saying Cannot run controller test, because the host is not set for base url.
which is caused because the $_SERVER['HTTP_HOST']
index is not set
Is there something in the configuration that I could be missing to cause this?
It is a problem related to Magento session initialization that is internal core part of Magento. In order to get rid of this error is to use a mock object, that does not use standard Magento session initialization process, since it uses core php session.
The replacement of session object with mock can be done by using the following code if you've extended your test case from one of the EcomDev_PHPUnit_Test_Case classes.
$sessionMock = $this->getModelMockBuilder('admin/session')
->disableOriginalConstructor() // This one removes session_start and other methods usage
->setMethods(null) // Enables original methods usage, because by default it overrides all methods
->getMock();
$this->replaceByMock('singleton', 'admin/session', $sessionMock);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With