1+ <?php
2+
3+ namespace Symfony \Bundle \MakerBundle \Tests \Security ;
4+
5+ use PHPUnit \Framework \TestCase ;
6+ use Symfony \Bundle \MakerBundle \Generator ;
7+ use Symfony \Bundle \MakerBundle \Security \InteractiveSecurityHelper ;
8+ use Symfony \Bundle \MakerBundle \Util \ClassNameDetails ;
9+ use Symfony \Component \Console \Input \InputInterface ;
10+ use Symfony \Component \Console \Style \SymfonyStyle ;
11+
12+ class InteractiveSecurityHelperTest extends TestCase
13+ {
14+ /**
15+ * @dataProvider getFirewallNameTests
16+ *
17+ * @param array $securityData
18+ * @param string $expectedFirewallName
19+ * @param bool $multipleValues
20+ */
21+ public function testGuessFirewallName (array $ securityData , string $ expectedFirewallName , $ multipleValues = false )
22+ {
23+ /** @var InputInterface|\PHPUnit_Framework_MockObject_MockObject $input */
24+ $ input = $ this ->createMock (InputInterface::class);
25+ $ input ->expects ($ this ->once ())
26+ ->method ('setOption ' )
27+ ->with ('firewall-name ' , $ expectedFirewallName );
28+
29+ /** @var SymfonyStyle|\PHPUnit_Framework_MockObject_MockObject $io */
30+ $ io = $ this ->createMock (SymfonyStyle::class);
31+ $ io ->expects ($ this ->exactly (false === $ multipleValues ? 0 : 1 ))
32+ ->method ('choice ' )
33+ ->willReturn ($ expectedFirewallName );
34+
35+ $ helper = new InteractiveSecurityHelper ();
36+ $ helper ->guessFirewallName ($ input , $ io , $ securityData );
37+ }
38+
39+ public function getFirewallNameTests ()
40+ {
41+ yield 'empty_security ' => [
42+ [],
43+ 'main ' ,
44+ ];
45+
46+ yield 'no_firewall ' => [
47+ ['security ' => ['firewalls ' => []]],
48+ 'main ' ,
49+ ];
50+
51+ yield 'no_secured_firewall ' => [
52+ ['security ' => ['firewalls ' => ['dev ' => ['security ' => false ]]]],
53+ 'main ' ,
54+ ];
55+
56+ yield 'main_firewall ' => [
57+ ['security ' => ['firewalls ' => ['dev ' => ['security ' => false ], 'main ' => null ]]],
58+ 'main ' ,
59+ ];
60+
61+ yield 'foo_firewall ' => [
62+ ['security ' => ['firewalls ' => ['dev ' => ['security ' => false ], 'foo ' => null ]]],
63+ 'foo ' ,
64+ ];
65+
66+ yield 'foo_bar_firewalls_1 ' => [
67+ ['security ' => ['firewalls ' => ['dev ' => ['security ' => false ], 'foo ' => null , 'bar ' => null ]]],
68+ 'foo ' ,
69+ true ,
70+ ];
71+
72+ yield 'foo_bar_firewalls_2 ' => [
73+ ['security ' => ['firewalls ' => ['dev ' => ['security ' => false ], 'foo ' => null , 'bar ' => null ]]],
74+ 'bar ' ,
75+ true ,
76+ ];
77+ }
78+
79+ /**
80+ * @expectedException \Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException
81+ */
82+ public function testGuessEntryPointWithNoFirewallNameThrowsException ()
83+ {
84+ /** @var InputInterface|\PHPUnit_Framework_MockObject_MockObject $input */
85+ $ input = $ this ->createMock (InputInterface::class);
86+
87+ /** @var SymfonyStyle|\PHPUnit_Framework_MockObject_MockObject $io */
88+ $ io = $ this ->createMock (SymfonyStyle::class);
89+
90+ /** @var Generator|\PHPUnit_Framework_MockObject_MockObject $generator */
91+ $ generator = $ this ->createMock (Generator::class);
92+
93+ $ helper = new InteractiveSecurityHelper ();
94+ $ helper ->guessEntryPoint ($ input , $ io , $ generator , []);
95+ }
96+
97+ /**
98+ * @expectedException \Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException
99+ */
100+ public function testGuessEntryPointWithNonExistingFirewallThrowsException ()
101+ {
102+ /** @var InputInterface|\PHPUnit_Framework_MockObject_MockObject $input */
103+ $ input = $ this ->createMock (InputInterface::class);
104+ $ input ->expects ($ this ->once ())
105+ ->method ('getOption ' )
106+ ->with ('firewall-name ' )
107+ ->willReturn ('foo ' );
108+
109+ /** @var SymfonyStyle|\PHPUnit_Framework_MockObject_MockObject $io */
110+ $ io = $ this ->createMock (SymfonyStyle::class);
111+
112+ /** @var Generator|\PHPUnit_Framework_MockObject_MockObject $generator */
113+ $ generator = $ this ->createMock (Generator::class);
114+
115+ $ helper = new InteractiveSecurityHelper ();
116+ $ helper ->guessEntryPoint ($ input , $ io , $ generator , []);
117+ }
118+
119+ /**
120+ * @dataProvider getEntryPointTests
121+ */
122+ public function testGuestEntryPoint (array $ securityData , string $ firewallName , bool $ multipleAuthenticators = false )
123+ {
124+ /** @var InputInterface|\PHPUnit_Framework_MockObject_MockObject $input */
125+ $ input = $ this ->createMock (InputInterface::class);
126+ $ input ->expects ($ this ->once ())
127+ ->method ('getOption ' )
128+ ->with ('firewall-name ' )
129+ ->willReturn ($ firewallName );
130+
131+ $ input ->expects ($ this ->exactly (false === $ multipleAuthenticators ? 0 : 1 ))
132+ ->method ('getArgument ' )
133+ ->with ('authenticator-class ' )
134+ ->willReturn ('NewAuthenticator ' );
135+
136+
137+ /** @var SymfonyStyle|\PHPUnit_Framework_MockObject_MockObject $io */
138+ $ io = $ this ->createMock (SymfonyStyle::class);
139+ $ io ->expects ($ this ->exactly (false === $ multipleAuthenticators ? 0 : 1 ))
140+ ->method ('choice ' );
141+
142+ /** @var Generator|\PHPUnit_Framework_MockObject_MockObject $generator */
143+ $ generator = $ this ->createMock (Generator::class);
144+ $ generator ->expects ($ this ->exactly (false === $ multipleAuthenticators ? 0 : 1 ))
145+ ->method ('createClassNameDetails ' )
146+ ->willReturn (new ClassNameDetails ('App \\Security \\NewAuthenticator ' , 'App \\Security ' ));
147+
148+ $ helper = new InteractiveSecurityHelper ();
149+ $ helper ->guessEntryPoint ($ input , $ io , $ generator , $ securityData );
150+ }
151+
152+ public function getEntryPointTests ()
153+ {
154+ yield 'no_guard ' => [
155+ ['security ' => ['firewalls ' => ['main ' => []]]],
156+ 'main '
157+ ];
158+
159+ yield 'no_authenticators_key ' => [
160+ ['security ' => ['firewalls ' => ['main ' => ['guard ' => []]]]],
161+ 'main '
162+ ];
163+
164+ yield 'no_authenticator ' => [
165+ ['security ' => ['firewalls ' => ['main ' => ['guard ' => ['authenticators ' => []]]]]],
166+ 'main '
167+ ];
168+
169+ yield 'one_authenticator ' => [
170+ ['security ' => ['firewalls ' => ['main ' => ['guard ' => ['authenticators ' => ['App \\Security \\Authenticator ' ]]]]]],
171+ 'main ' ,
172+ true
173+ ];
174+ }
175+ }
0 commit comments