|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
15 | 15 | use Psr\Container\ContainerInterface;
|
| 16 | +use Symfony\Component\HttpFoundation\Request; |
| 17 | +use Symfony\Component\HttpFoundation\RequestStack; |
16 | 18 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
17 | 19 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
18 | 20 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
19 | 21 | use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
20 | 22 | use Symfony\Component\Security\Core\Security;
|
21 | 23 | use Symfony\Component\Security\Core\User\InMemoryUser;
|
| 24 | +use Symfony\Component\Security\Http\Event\LogoutEvent; |
| 25 | +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
22 | 26 |
|
23 | 27 | class SecurityTest extends TestCase
|
24 | 28 | {
|
@@ -85,6 +89,52 @@ public function testIsGranted()
|
85 | 89 | $this->assertTrue($security->isGranted('SOME_ATTRIBUTE', 'SOME_SUBJECT'));
|
86 | 90 | }
|
87 | 91 |
|
| 92 | + public function testAutoLogout(): void |
| 93 | + { |
| 94 | + $request = new Request(); |
| 95 | + $requestStack = $this->createMock(RequestStack::class); |
| 96 | + $requestStack |
| 97 | + ->expects($this->once()) |
| 98 | + ->method('getCurrentRequest') |
| 99 | + ->willReturn($request) |
| 100 | + ; |
| 101 | + |
| 102 | + $token = $this->createMock(TokenInterface::class); |
| 103 | + $tokenStorage = $this->createMock(TokenStorageInterface::class); |
| 104 | + $tokenStorage |
| 105 | + ->expects($this->once()) |
| 106 | + ->method('getToken') |
| 107 | + ->willReturn($token) |
| 108 | + ; |
| 109 | + $tokenStorage |
| 110 | + ->expects($this->once()) |
| 111 | + ->method('setToken') |
| 112 | + ; |
| 113 | + |
| 114 | + $eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
| 115 | + $eventDispatcher |
| 116 | + ->expects($this->once()) |
| 117 | + ->method('dispatch') |
| 118 | + ->with(new LogoutEvent($request, $token)) |
| 119 | + ; |
| 120 | + |
| 121 | + $container = $this->createMock(ContainerInterface::class); |
| 122 | + $container |
| 123 | + ->expects($this->atLeastOnce()) |
| 124 | + ->method('get') |
| 125 | + ->willReturnMap([ |
| 126 | + ['request_stack', $requestStack], |
| 127 | + ['security.token_storage', $tokenStorage], |
| 128 | + ['event_dispatcher', $eventDispatcher], |
| 129 | + ]) |
| 130 | + ; |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + $security = new Security($container); |
| 135 | + $security->autoLogout(); |
| 136 | + } |
| 137 | + |
88 | 138 | private function createContainer($serviceId, $serviceObject)
|
89 | 139 | {
|
90 | 140 | $container = $this->createMock(ContainerInterface::class);
|
|
0 commit comments