15
15
use Symfony \Bundle \WebProfilerBundle \Controller \ProfilerController ;
16
16
use Symfony \Bundle \WebProfilerBundle \Csp \ContentSecurityPolicyHandler ;
17
17
use Symfony \Component \HttpFoundation \Request ;
18
+ use Symfony \Component \HttpFoundation \Response ;
19
+ use Symfony \Component \HttpKernel \DataCollector \DumpDataCollector ;
20
+ use Symfony \Component \HttpKernel \DataCollector \ExceptionDataCollector ;
21
+ use Symfony \Component \HttpKernel \DataCollector \RequestDataCollector ;
18
22
use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
19
23
use Symfony \Component \HttpKernel \Profiler \Profile ;
24
+ use Symfony \Component \HttpKernel \Profiler \Profiler ;
25
+ use Twig \Environment ;
26
+ use Twig \Loader \SourceContextLoaderInterface ;
27
+ use Twig \Source ;
20
28
21
29
class ProfilerControllerTest extends TestCase
22
30
{
@@ -185,17 +193,101 @@ public function provideCspVariants()
185
193
];
186
194
}
187
195
188
- private function createController ($ profiler , $ twig , $ withCSP ): ProfilerController
196
+ /**
197
+ * @dataProvider defaultPanelProvider
198
+ */
199
+ public function testDefaultPanel (string $ expectedPanel , Profile $ profile )
200
+ {
201
+ $ profiler = $ this ->createMock (Profiler::class);
202
+ $ profiler
203
+ ->expects ($ this ->atLeastOnce ())
204
+ ->method ('loadProfile ' )
205
+ ->with ($ profile ->getToken ())
206
+ ->willReturn ($ profile );
207
+
208
+ $ profiler
209
+ ->expects ($ this ->atLeastOnce ())
210
+ ->method ('has ' )
211
+ ->with ($ this ->logicalXor ($ collectorsNames = array_keys ($ profile ->getCollectors ())))
212
+ ->willReturn (true );
213
+
214
+ $ loader = $ this ->createMock (SourceContextLoaderInterface::class);
215
+ $ loader
216
+ ->expects ($ this ->atLeastOnce ())
217
+ ->method ('getSourceContext ' )
218
+ ->with ($ this ->logicalXor ($ expectedTemplate = 'expected_template.html.twig ' , 'other_template.html.twig ' ))
219
+ ->willReturn (new Source ('foo ' , 'bar ' ));
220
+
221
+ $ twig = $ this ->createMock (Environment::class);
222
+ $ twig
223
+ ->expects ($ this ->atLeastOnce ())
224
+ ->method ('getLoader ' )
225
+ ->willReturn ($ loader );
226
+ $ twig
227
+ ->expects ($ this ->once ())
228
+ ->method ('render ' )
229
+ ->with ($ expectedTemplate );
230
+
231
+ $ this
232
+ ->createController ($ profiler , $ twig , false , array_map (function (string $ collectorName ) use ($ expectedPanel , $ expectedTemplate ): array {
233
+ if ($ collectorName === $ expectedPanel ) {
234
+ return [$ expectedPanel , $ expectedTemplate ];
235
+ }
236
+
237
+ return [$ collectorName , 'other_template.html.twig ' ];
238
+ }, $ collectorsNames ))
239
+ ->panelAction (new Request (), $ profile ->getToken ());
240
+ }
241
+
242
+ public function defaultPanelProvider (): \Generator
243
+ {
244
+ // Test default behavior
245
+ $ profile = new Profile ('xxxxxx ' );
246
+ $ profile ->addCollector ($ requestDataCollector = new RequestDataCollector ());
247
+ yield [$ requestDataCollector ->getName (), $ profile ];
248
+
249
+ // Test exception
250
+ $ profile = new Profile ('xxxxxx ' );
251
+ $ profile ->addCollector ($ exceptionDataCollector = new ExceptionDataCollector ());
252
+ $ exceptionDataCollector ->collect (new Request (), new Response (), new \DomainException ());
253
+ yield [$ exceptionDataCollector ->getName (), $ profile ];
254
+
255
+ // Test exception priority
256
+ $ dumpDataCollector = $ this ->createMock (DumpDataCollector::class);
257
+ $ dumpDataCollector
258
+ ->expects ($ this ->atLeastOnce ())
259
+ ->method ('getName ' )
260
+ ->willReturn ('dump ' );
261
+ $ dumpDataCollector
262
+ ->expects ($ this ->atLeastOnce ())
263
+ ->method ('getDumpsCount ' )
264
+ ->willReturn (1 );
265
+ $ profile = new Profile ('xxxxxx ' );
266
+ $ profile ->setCollectors ([$ exceptionDataCollector , $ dumpDataCollector ]);
267
+ yield [$ exceptionDataCollector ->getName (), $ profile ];
268
+
269
+ // Test exception priority when defined afterwards
270
+ $ profile = new Profile ('xxxxxx ' );
271
+ $ profile ->setCollectors ([$ dumpDataCollector , $ exceptionDataCollector ]);
272
+ yield [$ exceptionDataCollector ->getName (), $ profile ];
273
+
274
+ // Test dump
275
+ $ profile = new Profile ('xxxxxx ' );
276
+ $ profile ->addCollector ($ dumpDataCollector );
277
+ yield [$ dumpDataCollector ->getName (), $ profile ];
278
+ }
279
+
280
+ private function createController ($ profiler , $ twig , $ withCSP , array $ templates = []): ProfilerController
189
281
{
190
282
$ urlGenerator = $ this ->getMockBuilder ('Symfony\Component\Routing\Generator\UrlGeneratorInterface ' )->getMock ();
191
283
192
284
if ($ withCSP ) {
193
285
$ nonceGenerator = $ this ->getMockBuilder ('Symfony\Bundle\WebProfilerBundle\Csp\NonceGenerator ' )->getMock ();
194
286
$ nonceGenerator ->method ('generate ' )->willReturn ('dummy_nonce ' );
195
287
196
- return new ProfilerController ($ urlGenerator , $ profiler , $ twig , [] , new ContentSecurityPolicyHandler ($ nonceGenerator ));
288
+ return new ProfilerController ($ urlGenerator , $ profiler , $ twig , $ templates , new ContentSecurityPolicyHandler ($ nonceGenerator ));
197
289
}
198
290
199
- return new ProfilerController ($ urlGenerator , $ profiler , $ twig , [] );
291
+ return new ProfilerController ($ urlGenerator , $ profiler , $ twig , $ templates );
200
292
}
201
293
}
0 commit comments