|
11 | 11 |
|
12 | 12 | namespace Symfony\Bundle\WebProfilerBundle\Tests\Controller;
|
13 | 13 |
|
14 |
| -use PHPUnit\Framework\TestCase; |
| 14 | +use Symfony\Bundle\FrameworkBundle\Client; |
| 15 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
15 | 16 | use Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController;
|
16 | 17 | use Symfony\Bundle\WebProfilerBundle\Csp\ContentSecurityPolicyHandler;
|
| 18 | +use Symfony\Bundle\WebProfilerBundle\Tests\Functional\WebProfilerBundleKernel; |
17 | 19 | use Symfony\Component\HttpFoundation\Request;
|
18 | 20 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
19 | 21 | use Symfony\Component\HttpKernel\Profiler\Profile;
|
20 | 22 |
|
21 |
| -class ProfilerControllerTest extends TestCase |
| 23 | +class ProfilerControllerTest extends WebTestCase |
22 | 24 | {
|
| 25 | + public function testHomeActionWithProfilerDisabled() |
| 26 | + { |
| 27 | + $this->expectException(NotFoundHttpException::class); |
| 28 | + $this->expectExceptionMessage('The profiler must be enabled.'); |
| 29 | + |
| 30 | + $urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock(); |
| 31 | + $twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock(); |
| 32 | + |
| 33 | + $controller = new ProfilerController($urlGenerator, null, $twig, []); |
| 34 | + $controller->homeAction(); |
| 35 | + } |
| 36 | + |
| 37 | + public function testHomeActionRedirect() |
| 38 | + { |
| 39 | + $kernel = new WebProfilerBundleKernel(); |
| 40 | + $client = new Client($kernel); |
| 41 | + |
| 42 | + $client->request('GET', '/_profiler/'); |
| 43 | + |
| 44 | + $this->assertSame(302, $client->getResponse()->getStatusCode()); |
| 45 | + $this->assertSame('/_profiler/empty/search/results?limit=10', $client->getResponse()->getTargetUrl()); |
| 46 | + } |
| 47 | + |
| 48 | + public function testPanelActionWithLatestTokenWhenNoTokensExist() |
| 49 | + { |
| 50 | + $kernel = new WebProfilerBundleKernel(); |
| 51 | + $client = new Client($kernel); |
| 52 | + |
| 53 | + $client->request('GET', '/_profiler/latest'); |
| 54 | + |
| 55 | + $this->assertStringContainsString('No profiles found.', $client->getResponse()->getContent()); |
| 56 | + } |
| 57 | + |
| 58 | + public function testPanelActionWithLatestToken() |
| 59 | + { |
| 60 | + $kernel = new WebProfilerBundleKernel(); |
| 61 | + $client = new Client($kernel); |
| 62 | + |
| 63 | + $client->request('GET', '/'); |
| 64 | + $client->request('GET', '/_profiler/latest'); |
| 65 | + |
| 66 | + $this->assertStringContainsString('kernel:homepageController', $client->getResponse()->getContent()); |
| 67 | + } |
| 68 | + |
| 69 | + public function testPanelActionWithoutValidToken() |
| 70 | + { |
| 71 | + $kernel = new WebProfilerBundleKernel(); |
| 72 | + $client = new Client($kernel); |
| 73 | + |
| 74 | + $client->request('GET', '/_profiler/this-token-does-not-exist'); |
| 75 | + |
| 76 | + $this->assertStringContainsString('Token "this-token-does-not-exist" not found.', $client->getResponse()->getContent()); |
| 77 | + } |
| 78 | + |
| 79 | + public function testPanelActionWithWrongPanel() |
| 80 | + { |
| 81 | + $kernel = new WebProfilerBundleKernel(); |
| 82 | + $client = new Client($kernel); |
| 83 | + |
| 84 | + $client->request('GET', '/'); |
| 85 | + $client->request('GET', '/_profiler/latest?panel=this-panel-does-not-exist'); |
| 86 | + |
| 87 | + $this->assertSame(404, $client->getResponse()->getStatusCode()); |
| 88 | + } |
| 89 | + |
| 90 | + public function testPanelActionWithValidPanelAndToken() |
| 91 | + { |
| 92 | + $kernel = new WebProfilerBundleKernel(); |
| 93 | + $client = new Client($kernel); |
| 94 | + |
| 95 | + $client->request('GET', '/'); |
| 96 | + $crawler = $client->request('GET', '/_profiler/latest?panel=router'); |
| 97 | + |
| 98 | + $this->assertSame('_', $crawler->filter('.metrics .metric .value')->eq(0)->text()); |
| 99 | + $this->assertSame('12', $crawler->filter('.metrics .metric .value')->eq(1)->text()); |
| 100 | + } |
| 101 | + |
| 102 | + public function testToolbarActionWithProfilerDisabled() |
| 103 | + { |
| 104 | + $this->expectException(NotFoundHttpException::class); |
| 105 | + $this->expectExceptionMessage('The profiler must be enabled.'); |
| 106 | + |
| 107 | + $urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock(); |
| 108 | + $twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock(); |
| 109 | + |
| 110 | + $controller = new ProfilerController($urlGenerator, null, $twig, []); |
| 111 | + $controller->toolbarAction(Request::create('/_wdt/foo-token'), null); |
| 112 | + } |
| 113 | + |
23 | 114 | /**
|
24 | 115 | * @dataProvider getEmptyTokenCases
|
25 | 116 | */
|
26 |
| - public function testEmptyToken($token) |
| 117 | + public function testToolbarActionWithEmptyToken($token) |
27 | 118 | {
|
28 | 119 | $urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock();
|
29 | 120 | $twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
|
@@ -111,10 +202,36 @@ public function testReturns404onTokenNotFound($withCsp)
|
111 | 202 | $this->assertEquals(404, $response->getStatusCode());
|
112 | 203 | }
|
113 | 204 |
|
| 205 | + public function testSearchBarActionWithProfilerDisabled() |
| 206 | + { |
| 207 | + $this->expectException(NotFoundHttpException::class); |
| 208 | + $this->expectExceptionMessage('The profiler must be enabled.'); |
| 209 | + |
| 210 | + $urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock(); |
| 211 | + $twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock(); |
| 212 | + |
| 213 | + $controller = new ProfilerController($urlGenerator, null, $twig, []); |
| 214 | + $controller->searchBarAction(Request::create('/_profiler/search_bar')); |
| 215 | + } |
| 216 | + |
| 217 | + public function testSearchBarActionDefaultPage() |
| 218 | + { |
| 219 | + $kernel = new WebProfilerBundleKernel(); |
| 220 | + $client = new Client($kernel); |
| 221 | + |
| 222 | + $crawler = $client->request('GET', '/_profiler/search_bar'); |
| 223 | + |
| 224 | + $this->assertSame(200, $client->getResponse()->getStatusCode()); |
| 225 | + |
| 226 | + foreach (['ip', 'status_code', 'url', 'token', 'start', 'end'] as $searchCriteria) { |
| 227 | + $this->assertSame('', $crawler->filter(sprintf('form input[name="%s"]', $searchCriteria))->text()); |
| 228 | + } |
| 229 | + } |
| 230 | + |
114 | 231 | /**
|
115 | 232 | * @dataProvider provideCspVariants
|
116 | 233 | */
|
117 |
| - public function testSearchResult($withCsp) |
| 234 | + public function testSearchResultsAction($withCsp) |
118 | 235 | {
|
119 | 236 | $twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
|
120 | 237 | $profiler = $this
|
@@ -177,6 +294,67 @@ public function testSearchResult($withCsp)
|
177 | 294 | $this->assertEquals(200, $response->getStatusCode());
|
178 | 295 | }
|
179 | 296 |
|
| 297 | + public function testSearchActionWithProfilerDisabled() |
| 298 | + { |
| 299 | + $this->expectException(NotFoundHttpException::class); |
| 300 | + $this->expectExceptionMessage('The profiler must be enabled.'); |
| 301 | + |
| 302 | + $urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock(); |
| 303 | + $twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock(); |
| 304 | + |
| 305 | + $controller = new ProfilerController($urlGenerator, null, $twig, []); |
| 306 | + $controller->searchBarAction(Request::create('/_profiler/search')); |
| 307 | + } |
| 308 | + |
| 309 | + public function testSearchActionWithToken() |
| 310 | + { |
| 311 | + $kernel = new WebProfilerBundleKernel(); |
| 312 | + $client = new Client($kernel); |
| 313 | + |
| 314 | + $client->request('GET', '/'); |
| 315 | + $token = $client->getResponse()->headers->get('x-debug-token'); |
| 316 | + $client->request('GET', '/_profiler/search?token='.$token); |
| 317 | + |
| 318 | + $this->assertSame(302, $client->getResponse()->getStatusCode()); |
| 319 | + $this->assertSame('/_profiler/'.$token, $client->getResponse()->getTargetUrl()); |
| 320 | + } |
| 321 | + |
| 322 | + public function testSearchActionWithoutToken() |
| 323 | + { |
| 324 | + $kernel = new WebProfilerBundleKernel(); |
| 325 | + $client = new Client($kernel); |
| 326 | + $client->followRedirects(); |
| 327 | + |
| 328 | + $client->request('GET', '/'); |
| 329 | + $token = $client->getResponse()->headers->get('x-debug-token'); |
| 330 | + $client->request('GET', '/_profiler/search?ip=&method=GET&status_code=&url=&token=&start=&end=&limit=10'); |
| 331 | + |
| 332 | + $this->assertStringContainsString('1 results found', $client->getResponse()->getContent()); |
| 333 | + $this->assertStringContainsString(sprintf('<a href="/_profiler/%s">%s</a>', $token, $token), $client->getResponse()->getContent()); |
| 334 | + } |
| 335 | + |
| 336 | + public function testPhpinfoActionWithProfilerDisabled() |
| 337 | + { |
| 338 | + $this->expectException(NotFoundHttpException::class); |
| 339 | + $this->expectExceptionMessage('The profiler must be enabled.'); |
| 340 | + |
| 341 | + $urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock(); |
| 342 | + $twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock(); |
| 343 | + |
| 344 | + $controller = new ProfilerController($urlGenerator, null, $twig, []); |
| 345 | + $controller->phpinfoAction(Request::create('/_profiler/phpinfo')); |
| 346 | + } |
| 347 | + |
| 348 | + public function testPhpinfoAction() |
| 349 | + { |
| 350 | + $kernel = new WebProfilerBundleKernel(); |
| 351 | + $client = new Client($kernel); |
| 352 | + |
| 353 | + $client->request('GET', '/_profiler/phpinfo'); |
| 354 | + |
| 355 | + $this->assertStringContainsString('PHP License', $client->getResponse()->getContent()); |
| 356 | + } |
| 357 | + |
180 | 358 | public function provideCspVariants()
|
181 | 359 | {
|
182 | 360 | return [
|
|
0 commit comments