Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用(或不使用)EcomDev PHPUnit模块测试Magento POST controller操作_Magento_Post_Phpunit - Fatal编程技术网

如何使用(或不使用)EcomDev PHPUnit模块测试Magento POST controller操作

如何使用(或不使用)EcomDev PHPUnit模块测试Magento POST controller操作,magento,post,phpunit,Magento,Post,Phpunit,有没有办法测试Magento POST控制器的操作?例如,客户登录: Mage_Customer_AccountController::loginPostAction() 我正在使用EcomDev_PHPUnit模块进行测试。它对基本操作非常有效,但我不能调用POST操作 $this->getRequest()->setMethod('POST'); // ivokes loginAction instead loginPostAction $this->dispatch('

有没有办法测试Magento POST控制器的操作?例如,客户登录:

Mage_Customer_AccountController::loginPostAction()
我正在使用EcomDev_PHPUnit模块进行测试。它对基本操作非常有效,但我不能调用POST操作

$this->getRequest()->setMethod('POST');
// ivokes loginAction instead loginPostAction
$this->dispatch('customer/account/login');
// fails also
$this->dispatch('customer/account/loginPost');
// but this assert pass
$this->assertEquals('POST', $_SERVER['REQUEST_METHOD']);
我想让测试或多或少像

// ... some setup
$this->getRequest()->setMethod('POST');
$this->dispatch('customer/account/login');
// since login uses singleton, then...
$session = Mage::getSingleton('customer/session');
$this->assertTrue($session->isLoggedIn());

在客户帐户控制器中,登录和登录是两种不同的操作。至于登录,您需要执行以下操作:

// Setting data for request
$this->getRequest()->setMethod('POST')
        ->setPost('login', array(
            'username' => 'customer@email.com',
            'password' => 'customer_password'
        ));

$this->dispatch('customer/account/loginpost'); // This will login customer via controller
但只有当您需要测试登录过程时,才需要这种测试主体,但如果只是想模拟客户登录,您可以创建特定客户登录的存根。我在几个项目中使用了它。只需将此方法添加到测试用例中,然后在需要时使用它。它将在一次测试运行中登录客户,并在测试运行后删除所有会话更改

/**
 * Creates information in the session,
 * that customer is logged in
 *
 * @param string $customerEmail
 * @param string $customerPassword
 * @return Mage_Customer_Model_Session|PHPUnit_Framework_MockObject_MockObject
 */
protected function createCustomerSession($customerId, $storeId = null)
{
    // Create customer session mock, for making our session singleton isolated
    $customerSessionMock = $this->getModelMock('customer/session', array('renewSession'));
    $this->replaceByMock('singleton', 'customer/session', $customerSessionMock);

    if ($storeId === null) {
        $storeId = $this->app()->getAnyStoreView()->getCode();
    }

    $this->setCurrentStore($storeId);
    $customerSessionMock->loginById($customerId);

    return $customerSessionMock;
}
此外,在上面的代码中模拟了RenewalSession方法,因为它使用的是direct setcookie函数,而不是core/cookie模型,所以在命令行中它会产生一个错误

真诚地,
伊万

是的!那就是。。。几乎可以工作了。我注意到它必须通过'adminpost'而不是'adminpost'。然后动作被捕获-我在干净的Magento 1.6.2.0上检查了它。是的,我想测试登录过程,而不是模拟客户登录。非常感谢您的快速支持和伟大的模块!这很有趣。我不知道分派方法。这对文件上传也是一个很好的解决方案吗?谢谢Ivan,
loginById()
函数修复了我的问题(客户/会话和客户/客户在将夹具加载到客户/客户并将其设置为会话时是不同的)。无意中修复,但谢谢!(推特上的@robbieaverill)Ivan,请更正您的答案,将
loginPost
更改为
loginPost
,浪费了一些时间在404上