8000 minor #31522 Added more tests for WebProfilerBundle (javiereguiluz) · symfony/symfony@347f7cf · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 347f7cf

Browse files
minor #31522 Added more tests for WebProfilerBundle (javiereguiluz)
This PR was merged into the 3.4 branch. Discussion ---------- Added more tests for WebProfilerBundle | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | - <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | - Thanks to @jpauli Code Coverage info about Symfony (http://cov.jpauli.tech/) I found that WebProfiler's controllers are pretty badly covered: ![image](https://user-images.githubusercontent.com/73419/57919817-ec390500-7899-11e9-81b7-763a0b35d0ec.png) This PR focuses on testing the main controller class: ![image](https://user-images.githubusercontent.com/73419/57919877-04108900-789a-11e9-8a93-3466b672d873.png) Commits ------- 2f7a820 Added more tests for WebProfilerBundle
2 parents 7e3d899 + 2f7a820 commit 347f7cf

File tree

3 files changed

+270
-11
lines changed

3 files changed

+270
-11
lines changed

src/Symfony/Bundle/WebProfilerBundle/Tests/Controller/ProfilerControllerTest.php

Lines changed: 182 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,110 @@
1111

1212
namespace Symfony\Bundle\WebProfilerBundle\Tests\Controller;
1313

14-
use PHPUnit\Framework\TestCase;
14+
use Symfony\Bundle\FrameworkBundle\Client;
15+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1516
use Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController;
1617
use Symfony\Bundle\WebProfilerBundle\Csp\ContentSecurityPolicyHandler;
18+
use Symfony\Bundle\WebProfilerBundle\Tests\Functional\WebProfilerBundleKernel;
1719
use Symfony\Component\HttpFoundation\Request;
1820
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1921
use Symfony\Component\HttpKernel\Profiler\Profile;
2022

21-
class ProfilerControllerTest extends TestCase
23+
class ProfilerControllerTest extends WebTestCase
2224
{
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 &quot;this-token-does-not-exist&quot; 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+
23114
/**
24115
* @dataProvider getEmptyTokenCases
25116
*/
26-
public function testEmptyToken($token)
117+
public function testToolbarActionWithEmptyToken($token)
27118
{
28119
$urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock();
29120
$twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
@@ -111,10 +202,36 @@ public function testReturns404onTokenNotFound($withCsp)
111202
$this->assertEquals(404, $response->getStatusCode());
112203
}
113204

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+
114231
/**
115232
* @dataProvider provideCspVariants
116233
*/
117-
public function testSearchResult($withCsp)
234+
public function testSearchResultsAction($withCsp)
118235
{
119236
$twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
120237
$profiler = $this
@@ -177,6 +294,67 @@ public function testSearchResult($withCsp)
177294
$this->assertEquals(200, $response->getStatusCode());
178295
}
179296

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+
180358
public function provideCspVariants()
181359
{
182360
return [
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\WebProfilerBundle\Tests\Functional;
4+
5+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
6+
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
7+
use Symfony\Bundle\TwigBundle\TwigBundle;
8+
use Symfony\Bundle\WebProfilerBundle\WebProfilerBundle;
9+
use Symfony\Component\Config\Loader\LoaderInterface;
10+
use Symfony\Component\DependencyInjection\ContainerBuilder;
11+
use Symfony\Component\HttpFoundation\Response;
12+
use Symfony\Component\HttpKernel\Kernel;
13+
use Symfony\Component\Routing\RouteCollectionBuilder;
14+
15+
class WebProfilerBundleKernel extends Kernel
16+
{
17+
use MicroKernelTrait;
18+
19+
public function __construct()
20+
{
21+
parent::__construct('test', false);
22+
}
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function getName()
28+
{
29+
if (null === $this->name) {
30+
$this->name = parent::getName().substr(md5(__CLASS__), -16);
31+
}
32+
33+
return $this->name;
34+
}
35+
36+
public function registerBundles()
37+
{
38+
return [
39+
new FrameworkBundle(),
40+
new TwigBundle(),
41+
new WebProfilerBundle(),
42+
];
43+
}
44+
45+
protected function configureRoutes(RouteCollectionBuilder $routes)
46+
{
47+
$routes->import(__DIR__.'/../../Resources/config/routing/profiler.xml', '/_profiler');
48+
$routes->import(__DIR__.'/../../Resources/config/routing/wdt.xml', '/_wdt');
49+
$routes->add('/', 'kernel:homepageController');
50+
}
51+
52+
protected function configureContainer(ContainerBuilder $containerBuilder, LoaderInterface $loader)
53+
{
54+
$containerBuilder->loadFromExtension('framework', [
55+
'secret' => 'foo-secret',
56+
'profiler' => ['only_exceptions' => false],
57+
'session' => ['storage_id' => 'session.storage.mock_file'],
58+
]);
59+
60+
$containerBuilder->loadFromExtension('web_profiler', [
61+
'toolbar' => true,
62+
'intercept_redirects' => false,
63+
]);
64+
}
65+
66+
public function getCacheDir()
67+
{
68+
return sys_get_temp_dir().'/cache-'.spl_object_hash($this);
69+
}
70+
71+
public function getLogDir()
72+
{
73+
return sys_get_temp_dir().'/log-'.spl_object_hash($this);
74+
}
75+
76+
public function homepageController()
77+
{
78+
return new Response('<html><head></head><body>Homepage Controller.</body></html>');
79+
}
80+
}

src/Symfony/Bundle/WebProfilerBundle/composer.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@
1717
],
1818
"require": {
1919
"php": "^5.5.9|>=7.0.8",
20+
"symfony/config": "~3.4|~4.0",
2021
"symfony/http-kernel": "~3.4.25|^4.2.6",
2122
"symfony/polyfill-php70": "~1.0",
22-
"symfony/routing": "~2.8|~3.0|~4.0",
23-
"symfony/twig-bundle": "~2.8|~3.0|~4.0",
23+
"symfony/routing": "~3.4.7|~4.0",
24+
"symfony/twig-bundle": "~3.4|~4.0",
2425
"symfony/var-dumper": "~3.3|~4.0",
2526
"twig/twig": "~1.34|~2.4"
2627
},
2728
"require-dev": {
28-
"symfony/config": "~3.4|~4.0",
29-
"symfony/console": "~2.8|~3.0|~4.0",
30-
"symfony/dependency-injection": "~3.4|~4.0",
31-
"symfony/stopwatch": "~2.8|~3.0|~4.0"
29+
"symfony/browser-kit": "~3.4|~4.0",
30+
"symfony/console": "~3.4|~4.0",
31+
"symfony/css-selector": "~3.4|~4.0",
32+
"symfony/framework-bundle": "~3.4|~4.0",
33+
"symfony/stopwatch": "~3.4|~4.0"
3234
},
3335
"conflict": {
34-
"symfony/config": "<3.4",
3536
"symfony/dependency-injection": "<3.4",
3637
"symfony/event-dispatcher": "<3.3.1",
3738
"symfony/var-dumper": "<3.3"

0 commit comments

Comments
 (0)
0