【问题标题】:Testing Magento with PHPUnit: Problems with Memcache sessions使用 PHPUnit 测试 Magento:Memcache 会话问题
【发布时间】:2014-02-24 07:00:45
【问题描述】:

我们正在使用 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)

到目前为止,我已经确定:

  • Memcache 似乎已打开,并且在正确的端口上
  • 当我将它设置为使用外部 (AWS) Memcache 服务时,也会出现同样的问题
  • 如果我将会话设置为由文件系统处理,也会发生同样的情况

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"

【问题讨论】:

    标签: php unit-testing magento session phpunit


    【解决方案1】:

    所以为了尝试回答最初的问题,当我的代码尝试使用普通的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')-&gt;getCustomer(),它会返回我的客户模型,然后我可以调用getEmail()

    【讨论】:

      【解决方案2】:

      我回答的问题有点相关,所以我会保留它,这是解决方法

      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

      【讨论】:

        猜你喜欢
        • 2019-04-21
        • 2011-03-04
        • 2017-09-25
        • 2019-11-22
        • 2014-11-03
        • 2018-06-28
        • 2015-08-24
        • 2015-11-24
        • 1970-01-01
        相关资源
        最近更新 更多