gpt4 book ai didi

php - 使用 ZfcUser 对 Controller 进行简单的 ZF2 单元测试

转载 作者:可可西里 更新时间:2023-11-01 12:33:42 25 4
gpt4 key购买 nike

我在尝试对使用 ZfcUser 进行身份验证的操作进行单元测试时遇到问题。我需要一些方法来模拟 ZfcUser Controller 插件,但我不太确定该怎么做。我已经成功地为表和模型生成了一些单元测试,但是 Controller 需要大量注入(inject)的对象并且导致了问题。有谁知道如何设置 ZfcUser 模拟以成功对 Controller 进行单元测试?

这是我的测试(从 ZF2 教程中复制):

<?php
namespace SmsTest\Controller;

use SmsTest\Bootstrap;
use Sms\Controller\SmsController;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\Router\Http\TreeRouteStack as HttpRouter;
use PHPUnit_Framework_TestCase;

class SmsControllerTest extends PHPUnit_Framework_TestCase
{
protected $controller;
protected $request;
protected $response;
protected $routeMatch;
protected $event;

protected function setUp()
{

$serviceManager = Bootstrap::getServiceManager();

$this->controller = new SmsController();
$this->request = new Request();
$this->routeMatch = new RouteMatch(array('controller' => 'index'));
$this->event = new MvcEvent();
$config = $serviceManager->get('Config');
$routerConfig = isset($config['router']) ? $config['router'] : array();
$router = HttpRouter::factory($routerConfig);
$this->event->setRouter($router);
$this->event->setRouteMatch($this->routeMatch);
$this->controller->setEvent($this->event);
$this->controller->setServiceLocator($serviceManager);
}


/* Test all actions can be accessed */

public function testIndexActionCanBeAccessed()
{
$this->routeMatch->setParam('action', 'index');

$result = $this->controller->dispatch($this->request);
$response = $this->controller->getResponse();

$this->assertEquals(200, $response->getStatusCode());
}
}

我在 setUp 方法中尝试了以下内容:

    $mockAuth = $this->getMock('ZfcUser\Entity\UserInterface');


$authMock = $this->getMock('Zend\Authentication\AuthenticationService');
$authMock->expects($this->any())
->method('hasIdentity')
->will($this->returnValue(true));

$authMock->expects($this->any())
->method('getIdentity')
->will($this->returnValue(array('user_id' => 1)));

但我不确定如何将其注入(inject)到 Controller 实例中。

假设我的索引操作代码如下:

public function indexAction() {
//Check if logged in
if (!$this->zfcUserAuthentication()->hasIdentity()) {
return $this->redirect()->toRoute('zfcuser/login');
}

return new ViewModel(array(
'success' => true,
));
}

测试结果:

1) SmsTest\Controller\SmsControllerTest::testIndexActionCanBeAccessed
Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for zfcUserAuthentication

/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:450
/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/ServiceManager/AbstractPluginManager.php:110
/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/PluginManager.php:90
/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php:276
/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php:291
/var/www/soap-app.localhost/Zend/module/Sms/src/Sms/Controller/SmsController.php:974
/var/www/soap-app.localhost/Zend/module/Sms/src/Sms/Controller/SmsController.php:974
/var/www/soap-app.localhost/Zend/module/Sms/src/Sms/Controller/SmsController.php:158
/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractActionController.php:87
/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:468
/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:208
/var/www/soap-app.localhost/Zend/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php:108
/var/www/soap-app.localhost/Zend/module/Sms/test/SmsTest/Controller/SmsControllerTest.php:57

导致此异常的行是 Controller :if (!$this->zfcUserAuthentication()->hasIdentity()) {该行与 SmsController 中的第 974 行相关。

很明显我无权访问 ZfcUserAuthentication 服务,所以问题是,我如何模拟 ZfcUserAuthentication 服务并将其注入(inject)到我的 Controller 中?

继续主题,我将如何模拟登录用户以成功测试我的操作是否符合规范?

最佳答案

ZfcUser 文档表明这是一个插件,因此您需要将其注入(inject)到 Controller 中。

您需要修改您的类名以获取 ZfcUser 类

您的模拟也需要进行调整,因为 getIdenty 返回不同的对象。

以下对我有用 - 插入您的 phpunit setUp() 方法。

$serviceManager = Bootstrap::getServiceManager();
$this->controller = new RegisterController();
$this->request = new Request();
$this->routeMatch = new RouteMatch(array('controller' => 'add'));
$this->event = new MvcEvent();
$config = $serviceManager->get('Config');
$routerConfig = isset($config['router']) ? $config['router'] : array();
$router = HttpRouter::factory($routerConfig);
$this->event->setRouter($router);
$this->event->setRouteMatch($this->routeMatch);
$this->controller->setEvent($this->event);
$this->controller->setServiceLocator($serviceManager);
$mockAuth = $this->getMock('ZfcUser\Entity\UserInterface');

$ZfcUserMock = $this->getMock('ZfcUser\Entity\User');

$ZfcUserMock->expects($this->any())
->method('getId')
->will($this->returnValue('1'));

$authMock = $this->getMock('ZfcUser\Controller\Plugin\ZfcUserAuthentication');

$authMock->expects($this->any())
->method('hasIdentity')
-> will($this->returnValue(true));

$authMock->expects($this->any())
->method('getIdentity')
->will($this->returnValue($ZfcUserMock));

$this->controller->getPluginManager()
->setService('zfcUserAuthentication', $authMock);

可能有更简单的方法欢迎其他想法。

关于php - 使用 ZfcUser 对 Controller 进行简单的 ZF2 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14354456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com