14
14
use Symfony \Bundle \FrameworkBundle \CacheWarmer \ConfigBuilderCacheWarmer ;
15
15
use Symfony \Bundle \FrameworkBundle \FrameworkBundle ;
16
16
use Symfony \Bundle \FrameworkBundle \Tests \TestCase ;
17
+ use Symfony \Component \Config \Definition \Builder \NodeDefinition ;
18
+ use Symfony \Component \Config \Definition \Builder \TreeBuilder ;
19
+ use Symfony \Component \Config \Definition \ConfigurationInterface ;
17
20
use Symfony \Component \Config \Loader \LoaderInterface ;
21
+ use Symfony \Component \DependencyInjection \ContainerBuilder ;
22
+ use Symfony \Component \DependencyInjection \Extension \Extension ;
23
+ use Symfony \Component \DependencyInjection \Extension \ExtensionInterface ;
18
24
use Symfony \Component \Filesystem \Filesystem ;
25
+ use Symfony \Component \HttpKernel \Bundle \Bundle ;
19
26
use Symfony \Component \HttpKernel \Kernel ;
20
27
21
28
class ConfigBuilderCacheWarmerTest extends TestCase
@@ -38,41 +45,163 @@ protected function tearDown(): void
38
45
39
46
public function testBuildDirIsUsedAsConfigBuilderOutputDir ()
40
47
{
41
- $ kernel = new class ($ this ->varDir ) extends Kernel {
42
- private $ varDir ;
48
+ $ kernel = new TestKernel ($ this ->varDir );
49
+ $ kernel ->boot ();
50
+
51
+ $ warmer = new ConfigBuilderCacheWarmer ($ kernel );
52
+ $ warmer ->warmUp ($ kernel ->getCacheDir ());
53
+
54
+ self ::assertDirectoryExists ($ kernel ->getBuildDir ().'/Symfony ' );
55
+ self ::assertDirectoryDoesNotExist ($ kernel ->getCacheDir ().'/Symfony ' );
56
+ }
43
57
44
- public function __construct (string $ varDir )
58
+ public function testExtensionsExtendedInBuildMethods ()
59
+ {
60
+ $ kernel = new class ($ this ->varDir ) extends TestKernel {
61
+ protected function build (ContainerBuilder $ container ): void
45
62
{
46
- parent ::__construct ('test ' , false );
63
+ /** @var TestSecurityExtension $extension */
64
+ $ extension = $ container ->getExtension ('test_security ' );
65
+ $ extension ->addAuthenticatorFactory (new class () implements TestAuthenticatorFactoryInterface {
66
+ public function getKey (): string
67
+ {
68
+ return 'token ' ;
69
+ }
47
70
48
- $ this ->varDir = $ varDir ;
71
+ public function addConfiguration (NodeDefinition $ node ): void
72
+ {
73
+ }
74
+ });
49
75
}
50
76
51
77
public function registerBundles (): iterable
52
78
{
53
- yield new FrameworkBundle ();
54
- }
79
+ yield from parent ::registerBundles ();
55
80
56
- public function getBuildDir (): string
57
- {
58
- return $ this ->varDir .'/build ' ;
59
- }
81
+ yield new class () extends Bundle {
82
+ public function getContainerExtension (): ExtensionInterface
83
+ {
84
+ return new TestSecurityExtension ();
85
+ }
86
+ };
60
87
61
- public function getCacheDir (): string
62
- {
63
- return $ this ->varDir .'/cache ' ;
64
- }
88
+ yield new class () extends Bundle {
89
+ public function build (ContainerBuilder $ container ): void
90
+ {
91
+ /** @var TestSecurityExtension $extension */
92
+ $ extension = $ container ->getExtension ('test_security ' );
93
+ $ extension ->addAuthenticatorFactory (new class () implements TestAuthenticatorFactoryInterface {
94
+ public function getKey (): string
95
+ {
96
+ return 'form-login ' ;
97
+ }
65
98
66
- public function registerContainerConfiguration (LoaderInterface $ loader )
67
- {
99
+ public function addConfiguration (NodeDefinition $ node ): void
100
+ {
101
+ $ node
102
+ ->children ()
103
+ ->scalarNode ('provider ' )->end ()
104
+ ->end ()
105
+ ;
106
+ }
107
+ });
108
+ }
109
+ };
68
110
}
69
111
};
70
112
$ kernel ->boot ();
71
113
72
114
$ warmer = new ConfigBuilderCacheWarmer ($ kernel );
73
115
$ warmer ->warmUp ($ kernel ->getCacheDir ());
74
116
75
- self ::assertDirectoryExists ($ kernel ->getBuildDir ().'/Symfony ' );
76
- self ::assertDirectoryDoesNotExist ($ kernel ->getCacheDir ().'/Symfony ' );
117
+ self ::assertFileExists ($ kernel ->getBuildDir ().'/Symfony/Config/FrameworkConfig.php ' );
118
+ self ::assertFileExists ($ kernel ->getBuildDir ().'/Symfony/Config/SecurityConfig.php ' );
119
+ self ::assertFileExists ($ kernel ->getBuildDir ().'/Symfony/Config/Security/FirewallConfig.php ' );
120
+ self ::assertFileExists ($ kernel ->getBuildDir ().'/Symfony/Config/Security/FirewallConfig/FormLoginConfig.php ' );
121
+ self ::assertFileExists ($ kernel ->getBuildDir ().'/Symfony/Config/Security/FirewallConfig/TokenConfig.php ' );
122
+ }
123
+ }
124
+
125
+ class TestKernel extends Kernel
126
+ {
127
+ private $ varDir ;
128
+
129
+ public function __construct (string $ varDir )
130
+ {
131
+ parent ::__construct ('test ' , false );
132
+
133
+ $ this ->varDir = $ varDir ;
134
+ }
135
+
136
+ public function registerBundles (): iterable
137
+ {
138
+ yield new FrameworkBundle ();
139
+ }
140
+
141
+ public function getBuildDir (): string
142
+ {
143
+ return $ this ->varDir .'/build ' ;
144
+ }
145
+
146
+ public function getCacheDir (): string
147
+ {
148
+ return $ this ->varDir .'/cache ' ;
149
+ }
150
+
151
+ public function registerContainerConfiguration (LoaderInterface $ loader ): void
152
+ {
153
+ }
154
+ }
155
+
156
+ interface TestAuthenticatorFactoryInterface
157
+ {
158
+ public function getKey (): string ;
159
+
160
+ public function addConfiguration (NodeDefinition $ builder ): void ;
161
+ }
162
+
163
+ class TestSecurityExtension extends Extension implements ConfigurationInterface
164
+ {
165
+ /** @var TestAuthenticatorFactoryInterface[] */
166
+ private $ factories ;
167
+
168
+ public function load (array $ configs , ContainerBuilder $ container ): void
169
+ {
170
+ }
171
+
172
+ public function getConfiguration (array $ config , ContainerBuilder $ container ): ConfigurationInterface
173
+ {
174
+ return $ this ;
175
+ }
176
+
177
+ public function addAuthenticatorFactory (TestAuthenticatorFactoryInterface $ factory ): void
178
+ {
179
+ $ this ->factories [] = $ factory ;
180
+ }
181
+
182
+ public function getConfigTreeBuilder (): TreeBuilder
183
+ {
184
+ $ treeBuilder = new TreeBuilder ('security ' );
185
+ $ rootNode = $ treeBuilder ->getRootNode ();
186
+
187
+ $ firewallNodeBuilder = $ rootNode
188
+ ->fixXmlConfig ('firewall ' )
189
+ ->children ()
190
+ ->arrayNode ('firewalls ' )
191
+ ->isRequired ()
192
+ ->requiresAtLeastOneElement ()
193
+ ->useAttributeAsKey ('name ' )
194
+ ->prototype ('array ' )
195
+ ->children ()
196
+ ;
197
+
198
+ foreach ($ this ->factories as $ factory ) {
199
+ $ name = str_replace ('- ' , '_ ' , $ factory ->getKey ());
200
+ $ factoryNode = $ firewallNodeBuilder ->arrayNode ($ name );
201
+
202
+ $ factory ->addConfiguration ($ factoryNode );
203
+ }
204
+
205
+ return $ treeBuilder ;
77
206
}
78
207
}
0 commit comments