IndexController
クラスでユニットテストを実行する際に問題が発生しました。ZF3ユニットテスト認証onBootstrap
ユニットテストがちょうど(unit-test tutorial of zf3からインスピレーションを得た)以下のん:
IndexControllerTest.php
:
Module.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');
}
彼は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
問題のステータスコードをリダイレクトする:ユニットテストが失敗:200「レスポンスコードアサート失敗 『302『 したがってユニットテストは、にジャンプ』、実際のステータスコードが』リダイレクトが起こるModule.php
の私onBootstrap
方法から他のケース
私はのTestCaseで次setUp
をしましたが、それは動作しません:。
ヒントは非常にコードがZend Frameworkの2から少し異なる場合がありますが、あなたはZF2で簡単な作業例を持っている場合は、多分私はZF3スタイルにそれを変換することができ
を高く評価しています。
私はZfcUserを使用していない - 私は実用的なソリューションを持って頭痛の数日後にただのZend-ACL/Zendの認証のもの