- Я использую Symfony 4.4 и api-platform / api-pack v1.2.1
- Я создал собственного избирателя, который поддерживает строку с префиксом
PERM_
- Я устанавливаю конечную точку (
/api/roles
), чтобы иметь security: 'is_granted("PERM_READ_USER")'
- Я создаю тест, как показано ниже:
/**
* @depends testLoginRegularUser
* @param $token
* @return string JWT Token
*/
public function testRegularUserReadRole($token)
{
$this->expectException(AccessDeniedHttpException::class);
$this->expectExceptionCode(403);
$response = static ::createClient()->request('GET', '/api/roles',[
'json' => [
'page' => 1
],
'headers' => [
'Authorization' => 'Bearer ' . $token,
]
]);
return $token;
}
И я всегда получить сообщение об ошибке:
Testing App\Tests\Functional\GroupRoleTest
.2020-01-06T00:49:48+00:00 [error] Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException: "Access Denied." at /home/vagrant/Code/wikaunting/vendor/symfony/security-http/Firewall/ExceptionListener.php line 137
FS 3 / 3 (100%)
Time: 14.27 seconds, Memory: 44.50 MB
There was 1 failure:
1) App\Tests\Functional\GroupRoleTest::testRegularUserReadRole
Failed asserting that exception of type "Symfony\Component\HttpClient\Exception\AccessDeniedHttpException" is thrown.
Когда я тестирую с помощью rest client (Insomenia), он возвращает
{
"@context": "\/api\/contexts\/Error",
"@type": "hydra:Error",
"hydra:title": "An error occurred",
"hydra:description": "Access Denied.",
"trace": [
{
"namespace": "",
"short_class": "",
"class": "",
"type": "",
"function": "",
"file": "\/home\/vagrant\/Code\/wikaunting\/vendor\/symfony\/security-http\/Firewall\/ExceptionListener.php",
"line": 137,
"args": []
},
{
"namespace": "Symfony\\Component\\Security\\Http\\Firewall",
"short_class": "ExceptionListener",
"class": "Symfony\\Component\\Security\\Http\\Firewall\\ExceptionListener",
"type": "->",
"function": "handleAccessDeniedException",
"file": "\/home\/vagrant\/Code\/wikaunting\/vendor\/symfony\/security-http\/Firewall\/ExceptionListener.php",
"line": 102,
"args": [
[
"object",
"Symfony\\Component\\HttpKernel\\Event\\ExceptionEvent"
],
[
"object",
"Symfony\\Component\\Security\\Core\\Exception\\AccessDeniedException"
]
]
},
{
"namespace": "Symfony\\Component\\Security\\Http\\Firewall",
"short_class": "ExceptionListener",
"class": "Symfony\\Component\\Security\\Http\\Firewall\\ExceptionListener",
"type": "->",
"function": "onKernelException",
"file": "\/home\/vagrant\/Code\/wikaunting\/vendor\/symfony\/event-dispatcher\/Debug\/WrappedListener.php",
"line": 126,
"args": [
[
"object",
"Symfony\\Component\\HttpKernel\\Event\\ExceptionEvent"
],
[
"string",
"kernel.exception"
],
[
"object",
"Symfony\\Component\\HttpKernel\\Debug\\TraceableEventDispatcher"
]
]
},
{
"namespace": "Symfony\\Component\\EventDispatcher\\Debug",
"short_class": "WrappedListener",
"class": "Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener",
"type": "->",
"function": "__invoke",
"file": "\/home\/vagrant\/Code\/wikaunting\/vendor\/symfony\/event-dispatcher\/EventDispatcher.php",
"line": 264,
"args": [
[
"object",
"Symfony\\Component\\HttpKernel\\Event\\ExceptionEvent"
],
[
"string",
"kernel.exception"
],
[
"object",
"Symfony\\Component\\HttpKernel\\Debug\\TraceableEventDispatcher"
]
]
},
{
"namespace": "Symfony\\Component\\EventDispatcher",
"short_class": "EventDispatcher",
"class": "Symfony\\Component\\EventDispatcher\\EventDispatcher",
"type": "->",
"function": "doDispatch",
"file": "\/home\/vagrant\/Code\/wikaunting\/vendor\/symfony\/event-dispatcher\/EventDispatcher.php",
"line": 239,
"args": [
[
"array",
[
[
"object",
"Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
],
[
"object",
"Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
],
[
"object",
"Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
],
[
"object",
"Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
],
[
"object",
"Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
],
[
"object",
"Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
]
]
],
[
"string",
"kernel.exception"
],
[
"object",
"Symfony\\Component\\HttpKernel\\Event\\ExceptionEvent"
]
]
},
{
"namespace": "Symfony\\Component\\EventDispatcher",
"short_class": "EventDispatcher",
"class": "Symfony\\Component\\EventDispatcher\\EventDispatcher",
"type": "->",
"function": "callListeners",
"file": "\/home\/vagrant\/Code\/wikaunting\/vendor\/symfony\/event-dispatcher\/EventDispatcher.php",
"line": 73,
"args": [
[
"array",
[
[
"object",
"Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
],
[
"object",
"Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
],
[
"object",
"Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
],
[
"object",
"Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
],
[
"object",
"Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
],
[
"object",
"Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener"
]
]
],
[
"string",
"kernel.exception"
],
[
"object",
"Symfony\\Component\\HttpKernel\\Event\\ExceptionEvent"
]
]
},
{
"namespace": "Symfony\\Component\\EventDispatcher",
"short_class": "EventDispatcher",
"class": "Symfony\\Component\\EventDispatcher\\EventDispatcher",
"type": "->",
"function": "dispatch",
"file": "\/home\/vagrant\/Code\/wikaunting\/vendor\/symfony\/event-dispatcher\/Debug\/TraceableEventDispatcher.php",
"line": 168,
"args": [
[
"object",
"Symfony\\Component\\HttpKernel\\Event\\ExceptionEvent"
],
[
"string",
"kernel.exception"
]
]
},
{
"namespace": "Symfony\\Component\\EventDispatcher\\Debug",
"short_class": "TraceableEventDispatcher",
"class": "Symfony\\Component\\EventDispatcher\\Debug\\TraceableEventDispatcher",
"type": "->",
"function": "dispatch",
"file": "\/home\/vagrant\/Code\/wikaunting\/vendor\/symfony\/http-kernel\/HttpKernel.php",
"line": 207,
"args": [
[
"object",
"Symfony\\Component\\HttpKernel\\Event\\ExceptionEvent"
],
[
"string",
"kernel.exception"
]
]
},
{
"namespace": "Symfony\\Component\\HttpKernel",
"short_class": "HttpKernel",
"class": "Symfony\\Component\\HttpKernel\\HttpKernel",
"type": "->",
"function": "handleThrowable",
"file": "\/home\/vagrant\/Code\/wikaunting\/vendor\/symfony\/http-kernel\/HttpKernel.php",
"line": 79,
"args": [
[
"object",
"Symfony\\Component\\Security\\Core\\Exception\\AccessDeniedException"
],
[
"object",
"Symfony\\Component\\HttpFoundation\\Request"
],
[
"integer",
1
]
]
},
{
"namespace": "Symfony\\Component\\HttpKernel",
"short_class": "HttpKernel",
"class": "Symfony\\Component\\HttpKernel\\HttpKernel",
"type": "->",
"function": "handle",
"file": "\/home\/vagrant\/Code\/wikaunting\/vendor\/symfony\/http-kernel\/Kernel.php",
"line": 201,
"args": [
[
"object",
"Symfony\\Component\\HttpFoundation\\Request"
],
[
"integer",
1
],
[
"boolean",
true
]
]
},
{
"namespace": "Symfony\\Component\\HttpKernel",
"short_class": "Kernel",
"class": "Symfony\\Component\\HttpKernel\\Kernel",
"type": "->",
"function": "handle",
"file": "\/home\/vagrant\/Code\/wikaunting\/public\/index.php",
"line": 25,
"args": [
[
"object",
"Symfony\\Component\\HttpFoundation\\Request"
]
]
}
]
}
Я думал, что он мог бы вернуть AccessDeniedException , а не AccessDenied Http Исключение , поэтому я изменяю тест на
$this->expectException(\Symfony\Component\Security\Core\Exception\AccessDeniedException::class);
Но все равно возвращается аналогичная ошибка
1) App\Tests\Functional\GroupRoleTest::testRegularUserReadRole
Failed asserting that exception of type "Symfony\Component\Security\Core\Exception\AccessDeniedException" is thrown.
Вопрос
- Это должно вернуть исключение?
- Всегда появляется сообщение
[error] Uncaught PHP Exception ...
. Как заставить его исчезнуть?