|
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\Bundle\SecurityBundle\Security\FirewallConfig; |
| 18 | +use Symfony\Bundle\SecurityBundle\Security\FirewallMap; |
| 19 | +use Symfony\Component\HttpFoundation\Request; |
| 20 | +use Symfony\Component\HttpFoundation\RequestStack; |
16 | 21 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
17 | 22 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
18 | 23 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
19 | 24 | use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
| 25 | +use Symfony\Component\Security\Core\Exception\LogicException; |
20 | 26 | use Symfony\Component\Security\Core\Security;
|
21 | 27 | use Symfony\Component\Security\Core\User\InMemoryUser;
|
| 28 | +use Symfony\Component\Security\Http\Event\LogoutEvent; |
| 29 | +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
22 | 30 |
|
23 | 31 | class SecurityTest extends TestCase
|
24 | 32 | {
|
@@ -81,7 +89,109 @@ public function testIsGranted()
|
81 | 89 | $this->assertTrue($security->isGranted('SOME_ATTRIBUTE', 'SOME_SUBJECT'));
|
82 | 90 | }
|
83 | 91 |
|
84 |
| - private function createContainer($serviceId, $serviceObject) |
| 92 | + public function testLogout() |
| 93 | + { |
| 94 | + $request = new Request(); |
| 95 | + $requestStack = $this->createMock(RequestStack::class); |
| 96 | + $requestStack |
| 97 | + ->expects($this->once()) |
| 98 | + ->method('getMainRequest') |
| 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 | + $firewallMap = $this->createMock(FirewallMap::class); |
| 122 | + $firewallConfig = new FirewallConfig('my_firewall', 'user_checker'); |
| 123 | + $firewallMap |
| 124 | + ->expects($this->once()) |
| 125 | + ->method('getFirewallConfig') |
| 126 | + ->willReturn($firewallConfig) |
| 127 | + ; |
| 128 | + |
| 129 | + $eventDispatcherLocator = $this->createMock(ContainerInterface::class); |
| 130 | + $eventDispatcherLocator |
| 131 | + ->expects($this->atLeastOnce()) |
| 132 | + ->method('get') |
| 133 | + ->willReturnMap([ |
| 134 | + ['my_firewall', $eventDispatcher], |
| 135 | + ]) |
| 136 | + ; |
| 137 | + |
| 138 | + $container = $this->createMock(ContainerInterface::class); |
| 139 | + $container |
| 140 | + ->expects($this->atLeastOnce()) |
| 141 | + ->method('get') |
| 142 | + ->willReturnMap([ |
| 143 | + ['request_stack', $requestStack], |
| 144 | + ['security.token_storage', $tokenStorage], |
| 145 | + ['security.firewall.map', $firewallMap], |
| 146 | + ['security.firewall.event_dispatcher_locator', $eventDispatcherLocator], |
| 147 | + ]) |
| 148 | + ; |
| 149 | + $security = new Security($container); |
| 150 | + $security->logout(); |
| 151 | + } |
| 152 | + |
| 153 | + public function testLogoutWithoutFirewall() |
| 154 | + { |
| 155 | + $request = new Request(); |
| 156 | + $requestStack = $this->createMock(RequestStack::class); |
| 157 | + $requestStack |
| 158 | + ->expects($this->once()) |
| 159 | + ->method('getMainRequest') |
| 160 | + ->willReturn($request) |
| 161 | + ; |
| 162 | + |
| 163 | + $token = $this->createMock(TokenInterface::class); |
| 164 | + $tokenStorage = $this->createMock(TokenStorageInterface::class); |
| 165 | + $tokenStorage |
| 166 | + ->expects($this->once()) |
| 167 | + ->method('getToken') |
| 168 | + ->willReturn($token) |
| 169 | + ; |
| 170 | + |
| 171 | + $firewallMap = $this->createMock(FirewallMap::class); |
| 172 | + $firewallMap |
| 173 | + ->expects($this->once()) |
| 174 | + ->method('getFirewallConfig') |
| 175 | + ->willReturn(null) |
| 176 | + ; |
| 177 | + |
| 178 | + $container = $this->createMock(ContainerInterface::class); |
| 179 | + $container |
| 180 | + ->expects($this->atLeastOnce()) |
| 181 | + ->method('get') |
| 182 | + ->willReturnMap([ |
| 183 | + ['request_stack', $requestStack], |
| 184 | + ['security.token_storage', $tokenStorage], |
| 185 | + ['security.firewall.map', $firewallMap], |
| 186 | + ]) |
| 187 | + ; |
| 188 | + |
| 189 | + $this->expectException(LogicException::class); |
| 190 | + $security = new Security($container); |
| 191 | + $security->logout(); |
| 192 | + } |
| 193 | + |
| 194 | + private function createContainer($serviceId, $serviceObject): MockObject |
85 | 195 | {
|
86 | 196 | $container = $this->createMock(ContainerInterface::class);
|
87 | 197 |
|
|
0 commit comments