我们正在使用EcomDev软件包来测试我们的Magento商店,其中一个默认测试正在大惊小怪:
运行phpunit返回:
5) EcomDev_PHPUnitTest_Test_Helper_Customer::testCustomerSession with data set "jane_doe" (2)
Exception: Warning: session_module_name(): A session is active. You cannot change the session module's ini settings at this time in /var/www/htdocs/app/code/core/Mage/Core/Model/Session/Abstract/Varien.php on line 73
Fatal error: Uncaught exception 'Exception' with message 'Warning: Unknown: Failed to write session data (memcache). Please verify that the current setting of session.save_path is correct (tcp://tcp://127.0.0.1:11211?persistent=1&weight=2&timeout=10&retry_interval=10)
到目前为止,我确定了:
magento会话的配置是:
<session_save><![CDATA[memcache]]></session_save>
<session_save_path><![CDATA[tcp://127.0.0.1:11211?persistent=1&weight;=2&timeout;=10&retry;_interval=10]]></session_save_path>
<session_cache_limiter><![CDATA[private]]></session_cache_limiter>
<lifetime>31536000</lifetime>
php5-fpm php.ini有
session.save_handler = memcache
session.save_path = "tcp://127.0.0.1:11211?persistent=1&weight;=2&timeout;=10&retry;_interval=10"
答案 0 :(得分:0)
我回答了一个错误的问题,它有点相关所以我会保留它,这是如何解决的
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /var/www/magento/vendor/phpunit
/phpunit/src/Util/Printer.php:172) in /var/www/magento/app/code/core/Mage/Core/Model/Session/Abstract/Varien.php on line 125
当我偶然发现你的问题时,我会更加努力地解决这个问题,就是我创建了一个类似的测试:
class Namespace_OrderMigration_Test_Controller_OrderController extends EcomDev_PHPUnit_Test_Case_Controller
{
public function setUp()
{
// do some stuff
}
public function testListAction()
{
$this->dispatch('migration/order/list');
$this->assertRequestRoute($route);
}
}
问题是EcomDev_PHPUnit_Test_Case_Controller::setUp()
会对Cookie进行排序并初始化控制器,因此请务必致电parent::setUp();
。
public function setUp()
{
parent::setUp();
// do some stuff
}
愚蠢的错误我知道,但其他测试用例不使用修改setUp
方法,这不是你看到的问题。我确实得到了你所看到的问题,我只是嘲笑了会议。
如果您仍然遇到此问题,请查看@ Cannot send session cookie - headers already sent PHPUnit / Laravel,其中基本上表示使用@session_start
答案 1 :(得分:0)
因此,为了尝试回答原始问题,当我的代码尝试使用普通customer/session
时,我也遇到了此问题。
我通过模拟会话并替换单例实例来修复(解决?),如下所示:
public function setUp()
{
parent::setUp();
$mockSession = $this->getModelMockBuilder('customer/session')
->disableOriginalConstructor()
->getMock();
$this->replaceByMock('singleton', 'customer/session', $mockSession);
/* @var $mockSession PHPUnit_Framework_MockObject_MockObject Stub */
$mockSession = Mage::getSingleton('customer/session');
$mockSession->expects($this->atLeastOnce())
->method('authenticate')
->willReturn(true);
$mockSession->expects($this->atLeastOnce())
->method('getCustomer')
->willReturn(Mage::getModel('customer/customer')->setData('email', 'test@test.com'));
}
这样我就可以致电Mage::getSingleton('customer/session')->getCustomer()
并返回我的客户模型,然后我可以打电话给getEmail()
。