使用PHPUnit测试Magento:Memcache会话的问题
我们正在使用EcomDev软件包测试我们的Magento商店,其中一个默认测试引起了一场骚动: 运行phpunit返回:使用PHPUnit测试Magento:Memcache会话的问题,php,unit-testing,magento,session,phpunit,Php,Unit Testing,Magento,Session,Phpunit,我们正在使用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 m
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)
到目前为止,我确定:
- Memcache似乎已打开,并且在正确的端口上
- 当我将其设置为使用外部(AWS)Memcache服务时,也会出现同样的问题
- 如果我将会话设置为由文件系统处理,也会发生同样的情况
<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>
我回答了一个错误的问题,这是一种关联,所以我会保留它,这是如何解决的
警告:session_start():无法发送会话cookie-标头已由发送(输出从/var/www/magento/vendor/phpunit开始)
/phpunit/src/Util/Printer.php:172),在第125行的/var/www/magento/app/code/core/Mage/core/Model/Session/Abstract/Varien.php中
当我偶然发现你的问题时,我将讨论如何解决这个问题,当我创建这样一个测试时:
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\u PHPUnit\u Test\u Case\u Controller::setUp()
整理cookies并初始化控制器,因此确保调用parent::setUp()代码>
我知道这是一个愚蠢的错误,但是其他测试用例没有使用modifysetUp
方法,这不是您看到的问题。我确实理解了你看到的问题,我只是嘲笑了会议
如果您仍然看到这个问题,请看@,它基本上是说使用@session\u start
,因此为了尝试回答原始问题,我的代码在尝试使用正常的客户/会话时也遇到了这个问题
我通过模拟会话并替换singleton实例来修复(解决了吗?)它,如下所示:
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()
public function setUp()
{
parent::setUp();
// do some stuff
}
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'));
}