Skip to content

Commit 4353b2c

Browse files
committed
[SecurityBundle] Add tests for debug:firewall command
1 parent 348aae1 commit 4353b2c

File tree

5 files changed

+367
-0
lines changed

5 files changed

+367
-0
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\SecurityBundle\Tests\Command;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\SecurityBundle\Command\DebugFirewallCommand;
16+
use Symfony\Bundle\SecurityBundle\Security\FirewallConfig;
17+
use Symfony\Bundle\SecurityBundle\Security\FirewallContext;
18+
use Symfony\Bundle\SecurityBundle\Tests\Fixtures\DummyAuthenticator;
19+
use Symfony\Component\Console\Tester\CommandTester;
20+
use Symfony\Component\DependencyInjection\ContainerInterface;
21+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
23+
24+
class DebugFirewallCommandTest extends TestCase
25+
{
26+
public function testFirewallListOutputMatchesFixture()
27+
{
28+
$firewallNames = ['main', 'api'];
29+
$contexts = $this->createMock(ContainerInterface::class);
30+
$eventDispatchers = $this->createMock(ContainerInterface::class);
31+
32+
$command = new DebugFirewallCommand($firewallNames, $contexts, $eventDispatchers, []);
33+
$tester = new CommandTester($command);
34+
35+
$this->assertSame(0, $tester->execute([]));
36+
37+
$output = $tester->getDisplay();
38+
39+
$this->assertStringContainsString('Firewalls', $output);
40+
$this->assertStringContainsString('The following firewalls are defined:', $output);
41+
$this->assertStringContainsString('* main', $output);
42+
$this->assertStringContainsString('* api', $output);
43+
$this->assertStringContainsString('To view details of a specific firewall', $output);
44+
}
45+
46+
public function testFirewallNotFoundDisplaysError()
47+
{
48+
$firewallNames = ['main', 'api'];
49+
50+
$contexts = $this->createMock(ContainerInterface::class);
51+
$contexts->method('has')->willReturn(false);
52+
53+
$eventDispatchers = $this->createMock(ContainerInterface::class);
54+
$authenticators = [];
55+
56+
$command = new DebugFirewallCommand(
57+
$firewallNames,
58+
$contexts,
59+
$eventDispatchers,
60+
$authenticators
61+
);
62+
63+
$tester = new CommandTester($command);
64+
65+
$this->assertSame(1, $tester->execute(['name' => 'admin']));
66+
67+
$output = trim($tester->getDisplay());
68+
69+
$this->assertStringContainsString('Firewall admin was not found.', $output);
70+
$this->assertStringContainsString('Available firewalls are: main, api', $output);
71+
}
72+
73+
public function testFirewallMainOutputMatchesFixture()
74+
{
75+
$firewallNames = ['main'];
76+
77+
$config = new FirewallConfig(
78+
name: 'main',
79+
userChecker: 'user_checker_service',
80+
requestMatcher: null,
81+
securityEnabled: true,
82+
stateless: false,
83+
provider: 'user_provider_service',
84+
context: 'main',
85+
entryPoint: 'entry_point_service',
86+
accessDeniedHandler: 'access_denied_handler_service',
87+
accessDeniedUrl: '/access-denied',
88+
authenticators: [],
89+
switchUser: null
90+
);
91+
92+
$context = new FirewallContext([], config: $config);
93+
94+
$contexts = $this->createMock(ContainerInterface::class);
95+
$contexts->method('has')->willReturn(true);
96+
$contexts->method('get')->willReturn($context);
97+
98+
$eventDispatchers = $this->createMock(ContainerInterface::class);
99+
$authenticator = new DummyAuthenticator();
100+
$authenticators = ['main' => [$authenticator]];
101+
102+
$command = new DebugFirewallCommand($firewallNames, $contexts, $eventDispatchers, $authenticators);
103+
$tester = new CommandTester($command);
104+
105+
$this->assertSame(0, $tester->execute(['name' => 'main', '--events' => true]));
106+
107+
$expected = $this->getFixtureOutput('firewall_main_output.txt');
108+
109+
$this->assertEquals($expected, trim(str_replace(\PHP_EOL, "\n", $tester->getDisplay())));
110+
}
111+
112+
public function testFirewallWithEventsOutputMatchesFixture()
113+
{
114+
$firewallNames = ['main'];
115+
116+
$config = new FirewallConfig(
117+
name: 'main',
118+
userChecker: 'user_checker_service',
119+
context: 'main',
120+
stateless: false,
121+
provider: 'user_provider_service',
122+
entryPoint: 'entry_point_service',
123+
accessDeniedHandler: 'access_denied_handler_service',
124+
accessDeniedUrl: '/access-denied',
125+
);
126+
127+
$context = new FirewallContext([], config: $config);
128+
129+
$contexts = $this->createMock(ContainerInterface::class);
130+
$contexts->method('has')->willReturn(true);
131+
$contexts->method('get')->willReturn($context);
132+
133+
$dispatcher = $this->createMock(EventDispatcherInterface::class);
134+
$listener = fn () => null;
135+
$listenerTwo = fn (int $number) => $number * 2;
136+
$dispatcher->method('getListeners')->willReturn([
137+
'security.event' => [$listener, $listenerTwo],
138+
]);
139+
$dispatcher->method('getListenerPriority')->willReturn(42);
140+
141+
$eventDispatchers = $this->createMock(ContainerInterface::class);
142+
$eventDispatchers->method('has')->willReturn(true);
143+
$eventDispatchers->method('get')->willReturn($dispatcher);
144+
145+
$authenticator = new DummyAuthenticator();
146+
$authenticatorTwo = new DummyAuthenticator();
147+
$authenticatorThree = new DummyAuthenticator();
148+
$authenticators = ['main' => [$authenticator, $authenticatorTwo], 'api' => [$authenticatorThree]];
149+
150+
$command = new DebugFirewallCommand($firewallNames, $contexts, $eventDispatchers, $authenticators);
151+
$tester = new CommandTester($command);
152+
153+
$this->assertSame(0, $tester->execute(['name' => 'main', '--events' => true]));
154+
155+
$expected = $this->getFixtureOutput('firewall_main_with_events_output.txt');
156+
157+
$this->assertEquals($expected, trim(str_replace(\PHP_EOL, "\n", $tester->getDisplay())));
158+
}
159+
160+
public function testFirewallWithSwitchUserDisplaysSection()
161+
{
162+
$firewallNames = ['main'];
163+
164+
$switchUserConfig = [
165+
'parameter' => '_switch_user_test',
166+
'provider' => 'custom_provider_test',
167+
'role' => 'ROLE_ALLOWED_TO_SWITCH',
168+
];
169+
170+
$config = new FirewallConfig(
171+
name: 'main',
172+
userChecker: 'user_checker_service_test',
173+
context: 'main',
174+
stateless: false,
175+
provider: 'user_provider_service_test',
176+
entryPoint: 'entry_point_service_test',
177+
accessDeniedHandler: 'access_denied_handler_service_test',
178+
accessDeniedUrl: '/access-denied-test',
179+
switchUser: $switchUserConfig,
180+
);
181+
182+
$context = new FirewallContext([], config: $config);
183+
184+
$contexts = $this->createMock(ContainerInterface::class);
185+
$contexts->method('has')->willReturn(true);
186+
$contexts->method('get')->willReturn($context);
187+
188+
$eventDispatchers = $this->createMock(ContainerInterface::class);
189+
$authenticator = new DummyAuthenticator();
190+
$authenticatorTwo = $this->createMock(AuthenticatorInterface::class);
191+
$authenticators = ['main' => [$authenticator], 'api' => [$authenticatorTwo]];
192+
193+
$command = new DebugFirewallCommand(
194+
$firewallNames,
195+
$contexts,
196+
$eventDispatchers,
197+
$authenticators
198+
);
199+
$tester = new CommandTester($command);
200+
201+
$this->assertSame(0, $tester->execute(['name' => 'main']));
202+
203+
$expected = $this->getFixtureOutput('firewall_main_with_switch_user.txt');
204+
205+
$this->assertEquals($expected, trim(str_replace(\PHP_EOL, "\n", $tester->getDisplay())));
206+
}
207+
208+
private function getFixtureOutput(string $file): string
209+
{
210+
return trim(file_get_contents(__DIR__.'/../Fixtures/Descriptor/'.$file));
211+
}
212+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Firewall "main"
2+
===============
3+
4+
----------------------- -------------------------------
5+
Option Value
6+
----------------------- -------------------------------
7+
Name main
8+
Context main
9+
Lazy No
10+
Stateless No
11+
User Checker user_checker_service
12+
Provider user_provider_service
13+
Entry Point entry_point_service
14+
Access Denied URL /access-denied
15+
Access Denied Handler access_denied_handler_service
16+
----------------------- -------------------------------
17+
18+
Event listeners for firewall "main"
19+
===================================
20+
21+
No event dispatcher has been registered for this firewall.
22+
23+
Authenticators for firewall "main"
24+
==================================
25+
26+
-----------------------------------------------------------------
27+
Classname
28+
-----------------------------------------------------------------
29+
Symfony\Bundle\SecurityBundle\Tests\Fixtures\DummyAuthenticator
30+
-----------------------------------------------------------------
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Firewall "main"
2+
===============
3+
4+
----------------------- -------------------------------
5+
Option Value
6+
----------------------- -------------------------------
7+
Name main
8+
Context main
9+
Lazy No
10+
Stateless No
11+
User Checker user_checker_service
12+
Provider user_provider_service
13+
Entry Point entry_point_service
14+
Access Denied URL /access-denied
15+
Access Denied Handler access_denied_handler_service
16+
----------------------- -------------------------------
17+
18+
Event listeners for firewall "main"
19+
===================================
20+
21+
"security.event" event
22+
----------------------
23+
24+
------- ----------- ----------
25+
Order Callable Priority
26+
------- ----------- ----------
27+
#1 Closure() 42
28+
#2 Closure() 42
29+
------- ----------- ----------
30+
31+
Authenticators for firewall "main"
32+
==================================
33+
34+
-----------------------------------------------------------------
35+
Classname
36+
-----------------------------------------------------------------
37+
Symfony\Bundle\SecurityBundle\Tests\Fixtures\DummyAuthenticator
38+
Symfony\Bundle\SecurityBundle\Tests\Fixtures\DummyAuthenticator
39+
-----------------------------------------------------------------
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Firewall "main"
2+
===============
3+
4+
----------------------- ------------------------------------
5+
Option Value
6+
----------------------- ------------------------------------
7+
Name main
8+
Context main
9+
Lazy No
10+
Stateless No
11+
User Checker user_checker_service_test
12+
Provider user_provider_service_test
13+
Entry Point entry_point_service_test
14+
Access Denied URL /access-denied-test
15+
Access Denied Handler access_denied_handler_service_test
16+
----------------------- ------------------------------------
17+
18+
User switching
19+
--------------
20+
21+
----------- ------------------------
22+
Option Value
23+
----------- ------------------------
24+
Parameter _switch_user_test
25+
Provider custom_provider_test
26+
User Role ROLE_ALLOWED_TO_SWITCH
27+
----------- ------------------------
28+
29+
Authenticators for firewall "main"
30+
==================================
31+
32+
-----------------------------------------------------------------
33+
Classname
34+
-----------------------------------------------------------------
35+
Symfony\Bundle\SecurityBundle\Tests\Fixtures\DummyAuthenticator
36+
-----------------------------------------------------------------
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\SecurityBundle\Tests\Fixtures;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
17+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
18+
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
19+
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
20+
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
21+
22+
class DummyAuthenticator implements AuthenticatorInterface
23+
{
24+
public function supports(Request $request): ?bool
25+
{
26+
return null;
27+
}
28+
29+
public function authenticate(Request $request): Passport
30+
{
31+
}
32+
33+
public function createToken(Passport $passport, string $firewallName): TokenInterface
34+
{
35+
}
36+
37+
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
38+
{
39+
return null;
40+
}
41+
42+
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
43+
{
44+
return null;
45+
}
46+
47+
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
48+
{
49+
}
50+
}

0 commit comments

Comments
 (0)