8000 merged branch fabpot/profiler-tests (PR #4897) · symfony/symfony@bf41d8b · GitHub
[go: up one dir, main page]

Skip to content

Commit bf41d8b

Browse files
committed
merged branch fabpot/profiler-tests (PR #4897)
Commits ------- 22e9036 updated CHANGELOG bafe890 [FrameworkBundle] changed Client::enableProfiler() behavior to fail silently when the profiler is not available (it makes it easier to write functional tests) f41872b [FrameworkBundle] added a way to enable the profiler for the very next request in functional tests (closes #4307) 67b91e5 [HttpKernel] added a way to enable a disable Profiler Discussion ---------- [2.2] added a way to enable the profiler for the very next request in a functional test Bug fix: yes/no Feature addition: yes Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: #4307 Todo: - License of the code: MIT Documentation PR: should be done before merging After merging this PR, we need to disable the profiler in the test environment in Symfony SE.
2 parents f6857d4 + 22e9036 commit bf41d8b

File tree

16 files changed

+157
-2
lines changed

16 files changed

+157
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
2.2.0
5+
-----
6+
7+
* added Client::enableProfiler()
8+
49
2.1.0
510
-----
611

@@ -40,4 +45,4 @@ CHANGELOG
4045
in Route patterns, requirements and defaults. Use '%%' as the escaped value for '%'.
4146
* [BC BREAK] Switched behavior of flash messages to expire flash messages on retrieval
4247
using Symfony\Component\HttpFoundation\Session\Flash\FlashBag as opposed to on
43-
next pageload regardless of whether they are displayed or not.
48+
next pageload regardless of whether they are displayed or not.

src/Symfony/Bundle/FrameworkBundle/Client.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
class Client extends BaseClient
2727
{
2828
private $hasPerformedRequest = false;
29+
private $profiler = false;
2930

3031
/**
3132
* Returns the container.
@@ -61,6 +62,18 @@ public function getProfile()
6162
return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->response);
6263
}
6364

65+
/**
66+
* Enables the profiler for the very next request.
67+
*
68+
* If the profiler is not enabled, the call to this method does nothing.
69+
*/
70+
public function enableProfiler()
71+
{
72+
if ($this->kernel->getContainer()->has('profiler')) {
73+
$this->profiler = true;
74+
}
75+
}
76+
6477
/**
6578
* Makes a request.
6679
*
@@ -78,9 +91,28 @@ protected function doRequest($request)
7891
$this->hasPerformedRequest = true;
7992
}
8093

94+
if ($this->profiler) {
95+
$this->profiler = false;
96+
97+
$this->kernel->boot();
98+
$this->kernel->getContainer()->get('profiler')->enable();
99+
}
100+
81101
return $this->kernel->handle($request);
82102
}
83103

104+
/**
105+
* {@inheritdoc}
106+
*/
107+
protected function doRequestInProcess($request)
108+
{
109+
$response = parent::doRequestInProcess($request);
110+
111+
$this->profiler = false;
112+
113+
return $response;
114+
}
115+
84116
/**
85117
* Returns the script to execute when the request must be insulated.
86118
*
@@ -109,6 +141,11 @@ protected function getScript($request)
109141

110142
$path = str_replace("'", "\\'", $r->getFileName());
111143

144+
$profilerCode = '';
145+
if ($this->profiler) {
146+
$profilerCode = '$kernel->getContainer()->get(\'profiler\')->enable();';
147+
}
148+
112149
return <<<EOF
113150
<?php
114151
@@ -119,6 +156,7 @@ protected function getScript($request)
119156
120157
\$kernel = unserialize('$kernel');
121158
\$kernel->boot();
159+
$profilerCode
122160
echo serialize(\$kernel->handle(unserialize('$request')));
123161
EOF;
124162
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ private function addProfilerSection(ArrayNodeDefinition $rootNode)
135135
->children()
136136
->booleanNode('only_exceptions')->defaultFalse()->end()
137137
->booleanNode('only_master_requests')->defaultFalse()->end()
138+
->booleanNode('enabled')->defaultTrue()->end()
138139
->scalarNode('dsn')->defaultValue('file:%kernel.cache_dir%/profiler')->end()
139140
->scalarNode('username')->defaultValue('')->end()
140141
->scalarNode('password')->defaultValue('')->end()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ private function registerProfilerConfiguration(array $config, ContainerBuilder $
229229
}
230230
}
231231
}
232+
233+
if (!$config['enabled']) {
234+
$container->getDefinition('profiler')->addMethodCall('disable', array());
235+
}
232236
}
233237

234238
/**

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
<xsd:attribute name="only-exceptions" type="xsd:string" />
5252
<xsd:attribute name="only-master-requests" type="xsd:string" />
53+
<xsd:attribute name="enabled" type="xsd:string" />
5354
<xsd:attribute name="dsn" type="xsd:string" />
5455
<xsd:attribute name="username" type="xsd:string" />
5556
<xsd:attribute name="password" type="xsd:string" />

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
),
1414
'profiler' => array(
1515
'only_exceptions' => true,
16+
'enabled' => false,
1617
),
1718
'router' => array(
1819
'resource' => '%kernel.root_dir%/config/routing.xml',

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<framework:csrf-protection enabled="true" field-name="_csrf" />
1111
<framework:form />
1212
<framework:esi enabled="true" />
13-
<framework:profiler only-exceptions="true" />
13+
<framework:profiler only-exceptions="true" enabled="false" />
1414
<framework:router resource="%kernel.root_dir%/config/routing.xml" type="xml" />
1515
<framework:session gc-maxlifetime="90000" gc-probability="1" gc-divisor="108" storage-id="session.storage.native" handler-id="session.handler.native_file" name="_SYMFONY" cookie-lifetime="86400" cookie-path="/" cookie-domain="example.com" cookie-secure="true" cookie-httponly="true" save-path="/path/to/sessions" />
1616
<framework:templating assets-version="SomeVersionScheme" cache="/path/to/cache" >

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ framework:
99
enabled: true
1010
profiler:
1111
only_exceptions: true
12+
enabled: false
1213
router:
1314
resource: %kernel.root_dir%/config/routing.xml
1415
type: xml

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public function testProfiler()
4848
$this->assertTrue($container->hasDefinition('data_collector.config'), '->registerProfilerConfiguration() loads collectors.xml');
4949
$this->assertTrue($container->getParameter('profiler_listener.only_exceptions'));
5050
$this->assertEquals('%profiler_listener.only_exceptions%', $container->getDefinition('profiler_listener')->getArgument(2));
51+
52+
$calls = $container->getDefinition('profiler')->getMethodCalls();
53+
$this->assertEquals('disable', $calls[0][0]);
5154
}
5255

5356
public function testRouter()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
13+
14+
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\DependencyInjection\ContainerAware;
16+
17+
class ProfilerController extends ContainerAware
18+
{
19+
public function indexAction()
20+
{
21+
return new Response('Hello');
22+
}
23+
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ session_setflash:
1717
session_showflash:
1818
pattern: /session_showflash
1919
defaults: { _controller: TestBundle:Session:showFlash}
20+
21+
profiler:
22+
pattern: /profiler
23+
defaults: { _controller: TestBundle:Profiler:index }
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
/**
15+
* @group functional
16+
*/
17+
class ProfilerTest extends WebTestCase
18+
{
19+
/**
20+
* @dataProvider getConfigs
21+
*/
22+
public function testProfilerIsDisabled($insulate)
23+
{
24+
$client = $this->createClient(array('test_case' => 'Profiler', 'root_config' => 'config.yml'));
25+
if ($insulate) {
26+
$client->insulate();
27+
}
28+
29+
$client->request('GET', '/profiler');
30+
$this->assertFalse($client->getProfile());
31+
32+
// enable the profiler for the next request
33+
$client->enableProfiler();
34+
$crawler = $client->request('GET', '/profiler');
35+
$profile = $client->getProfile();
36+
$this->assertTrue(is_object($profile));
37+
38+
$client->request('GET', '/profiler');
39+
$this->assertFalse($client->getProfile());
40+
}
41+
42+
public function getConfigs()
43+
{
44+
return array(
45+
array(false),
46+
array(true),
47+
);
48+
}
49+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
4+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
5+
6+
return array(
7+
new FrameworkBundle(),
8+
new TestBundle(),
9+
);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
4+
framework:
5+
profiler:
6+
enabled: false
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_sessiontest_bundle:
2+
resource: @TestBundle/Resources/config/routing.yml

src/Symfony/Component/HttpKernel/Profiler/Profiler.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public function disable()
5151
$this->enabled = false;
5252
}
5353

54+
/**
55+
* Enables the profiler.
56+
*/
57+
public function enable()
58+
{
59+
$this->enabled = true;
60+
}
61+
5462
/**
5563
* Loads the Profile for the given Response.
5664
*

0 commit comments

Comments
 (0)
0