Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't get session singleton in EcomDev PHPUnit test

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?

like image 447
veilig Avatar asked Jan 30 '13 19:01

veilig


1 Answers

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);
like image 159
Ivan Chepurnyi Avatar answered Oct 26 '22 23:10

Ivan Chepurnyi