Tôi đang gặp sự cố khi thử nghiệm đơn vị một hành động sử dụng ZfcUser để xác thực. Tôi cần một số cách để giả lập các plugin điều khiển ZfcUser nhưng tôi không chắc chắn như thế nào để làm điều này. Tôi đã quản lý thành công để tạo ra một số xét nghiệm đơn vị cho các bảng và mô hình nhưng bộ điều khiển đòi hỏi rất nhiều đối tượng được tiêm và đang gây ra vấn đề. Có ai biết làm thế nào để thiết lập các ZfcUser mocks để thành công đơn vị kiểm tra một bộ điều khiển?Kiểm tra đơn vị ZF2 đơn giản cho bộ điều khiển sử dụng ZfcUser
Đây là thử nghiệm của tôi (sao chép từ các hướng dẫn 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());
}
}
Tôi đã thử các sau đây trong các phương pháp thiết lập:
$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)));
Nhưng tôi không chắc chắn làm thế nào để tiêm này trong cho cá thể bộ điều khiển.
Hãy giả vờ mã hành động chỉ số của tôi chỉ là như sau:
public function indexAction() {
//Check if logged in
if (!$this->zfcUserAuthentication()->hasIdentity()) {
return $this->redirect()->toRoute('zfcuser/login');
}
return new ViewModel(array(
'success' => true,
));
}
Kết quả thử nghiệm:
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
Dòng gây ngoại lệ này là bộ điều khiển là: nếu (!$this->zfcUserAuthentication()->hasIdentity()) {
dòng đó liên quan đến dòng 974 trong SmsController.
Rõ ràng là tôi không có quyền truy cập vào dịch vụ ZfcUserAuthentication, do đó, câu hỏi là, Làm cách nào để thử dịch vụ ZfcUserAuthentication và đưa nó vào Bộ điều khiển của tôi?
Để tiếp tục chủ đề, tôi sẽ làm cách nào để mocking một người dùng đã đăng nhập để thử nghiệm thành công hành động của tôi đang hoạt động để chỉ định?