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