【发布时间】:2016-12-28 18:03:28
【问题描述】:
我在为我的IndexController 班级运行单元测试时遇到问题。
单元测试只做以下事情(灵感来自unit-test tutorial of zf3):
IndexControllerTest.php:
public function testIndexActionCanBeAccessed()
{
$this->dispatch('/', 'GET');
$this->assertResponseStatusCode(200);
$this->assertModuleName('main');
$this->assertControllerName(IndexController::class); // as specified in router's controller name alias
$this->assertControllerClass('IndexController');
$this->assertMatchedRouteName('main');
}
在Module.php 中,我有一些功能可以检查是否有用户登录,否则他将被重定向到login 路由。
Module.php:
public function onBootstrap(MvcEvent $mvcEvent)
{
/** @var AuthService $authService */
$authService = $mvcEvent->getApplication()->getServiceManager()->get(AuthService::class);
$this->auth = $authService->getAuth(); // returns the Zend AuthenticationService object
// store user and role in global viewmodel
if ($this->auth->hasIdentity()) {
$curUser = $this->auth->getIdentity();
$mvcEvent->getViewModel()->setVariable('curUser', $curUser['system_name']);
$mvcEvent->getViewModel()->setVariable('role', $curUser['role']);
$mvcEvent->getApplication()->getEventManager()->attach(MvcEvent::EVENT_ROUTE, [$this, 'checkPermission']);
} else {
$mvcEvent->getApplication()->getEventManager()->attach(MvcEvent::EVENT_DISPATCH, [$this, 'authRedirect'], 1000);
}
}
checkPermission 方法只是检查用户角色和匹配的路由是否在 acl 存储中。
如果失败,我将重定向状态码 404。
问题:单元测试失败:“Failed asserting response code “200”,实际状态码是“302”
因此,单元测试从我的 onBootstrap 方法跳转到发生重定向的 Module.php 中的 else 情况。
我在 TestCase 中执行了以下setUp,但它不起作用:
public function setUp()
{
// override default configuration values
$configOverrides = [];
$this->setApplicationConfig(ArrayUtils::merge(
include __DIR__ . '/../../../../config/application.config.php',
$configOverrides
));
$user = new Employee();
$user->id = 1;
$user->system_name = 'admin';
$user->role = 'Admin';
$this->authService = $this->prophesize(AuthService::class);
$auth = $this->prophesize(AuthenticationService::class);
$auth->hasIdentity()->willReturn(true);
$auth->getIdentity()->willReturn($user);
$this->authService->getAuth()->willReturn($auth->reveal());
$this->getApplicationServiceLocator()->setAllowOverride(true);
$this->getApplicationServiceLocator()->setService(AuthService::class, $this->authService->reveal());
$this->getApplicationServiceLocator()->setAllowOverride(false);
parent::setUp();
}
非常感谢提示
代码可能与 Zend Framework 2 有点不同,但如果您在 zf2 中有一个简单的工作示例,也许我可以将其转换为 zf3 样式。
我不使用 ZfcUser - 只是 zend-acl / zend-authentication 的东西
【问题讨论】:
标签: php authentication phpunit zend-framework3