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