fix: handle access denied errors properly

This commit is contained in:
Björn Fromme
2026-08-17 16:29:58 +02:00
parent 8271a69f45
commit b5f1163e2d
8 changed files with 260 additions and 10 deletions
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace App\Tests\Security;
use App\Security\DefaultRouteResolver;
use App\Security\Voter\AdministrativeAccessVoter;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class DefaultRouteResolverTest extends TestCase
{
public function testStaffLandsInTheAdminArea(): void
{
self::assertSame('app_admin_dashboard', $this->resolver(true)->resolveRoute());
}
public function testEverybodyElseLandsOnTheirAccount(): void
{
self::assertSame('app_account', $this->resolver(false)->resolveRoute());
}
public function testUrlIsGeneratedForTheResolvedRoute(): void
{
self::assertSame('/admin/dashboard', $this->resolver(true)->resolveUrl());
}
private function resolver(bool $hasAdministrativeAccess): DefaultRouteResolver
{
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
$authorizationChecker
->method('isGranted')
->with(AdministrativeAccessVoter::ADMINISTRATIVE_ACCESS)
->willReturn($hasAdministrativeAccess)
;
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$urlGenerator
->method('generate')
->willReturnCallback(static fn (string $route): string => match ($route) {
'app_admin_dashboard' => '/admin/dashboard',
'app_account' => '/account',
default => self::fail(sprintf('Unexpected route "%s"', $route)),
})
;
return new DefaultRouteResolver($authorizationChecker, $urlGenerator);
}
}