Permalink
Browse files

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.
  • Loading branch information...
2 parents d4a0df1 + da17b08 commit 3debabfb5ea0a315d95b1a7d2d83dbcf72260224 @fabpot fabpot committed Sep 18, 2012
View
@@ -1,6 +1,11 @@
CHANGELOG
=========
+2.2.0
+-----
+
+ * added Client::enableProfiler()
+
2.1.0
-----
@@ -40,4 +45,4 @@ CHANGELOG
in Route patterns, requirements and defaults. Use '%%' as the escaped value for '%'.
* [BC BREAK] Switched behavior of flash messages to expire flash messages on retrieval
using Symfony\Component\HttpFoundation\Session\Flash\FlashBag as opposed to on
- next pageload regardless of whether they are displayed or not.
+ next pageload regardless of whether they are displayed or not.
View
@@ -26,6 +26,7 @@
class Client extends BaseClient
{
private $hasPerformedRequest = false;
+ private $profiler = false;
/**
* Returns the container.
@@ -62,6 +63,18 @@ public function getProfile()
}
/**
+ * Enables the profiler for the very next request.
+ *
+ * If the profiler is not enabled, the call to this method does nothing.
+ */
+ public function enableProfiler()
+ {
+ if ($this->kernel->getContainer()->has('profiler')) {
+ $this->profiler = true;
+ }
+ }
+
+ /**
* Makes a request.
*
* @param Request $request A Request instance
@@ -78,10 +91,29 @@ protected function doRequest($request)
$this->hasPerformedRequest = true;
}
+ if ($this->profiler) {
+ $this->profiler = false;
+
+ $this->kernel->boot();
+ $this->kernel->getContainer()->get('profiler')->enable();
+ }
+
return $this->kernel->handle($request);
}
/**
+ * {@inheritdoc}
+ */
+ protected function doRequestInProcess($request)
+ {
+ $response = parent::doRequestInProcess($request);
+
+ $this->profiler = false;
+
+ return $response;
+ }
+
+ /**
* Returns the script to execute when the request must be insulated.
*
* It assumes that the autoloader is named 'autoload.php' and that it is
@@ -109,6 +141,11 @@ protected function getScript($request)
$path = str_replace("'", "\\'", $r->getFileName());
+ $profilerCode = '';
+ if ($this->profiler) {
+ $profilerCode = '$kernel->getContainer()->get(\'profiler\')->enable();';
+ }
+
return <<<EOF
<?php
@@ -119,6 +156,7 @@ protected function getScript($request)
\$kernel = unserialize('$kernel');
\$kernel->boot();
+$profilerCode
echo serialize(\$kernel->handle(unserialize('$request')));
EOF;
}
@@ -135,6 +135,7 @@ private function addProfilerSection(ArrayNodeDefinition $rootNode)
->children()
->booleanNode('only_exceptions')->defaultFalse()->end()
->booleanNode('only_master_requests')->defaultFalse()->end()
+ ->booleanNode('enabled')->defaultTrue()->end()
->scalarNode('dsn')->defaultValue('file:%kernel.cache_dir%/profiler')->end()
->scalarNode('username')->defaultValue('')->end()
->scalarNode('password')->defaultValue('')->end()
@@ -229,6 +229,10 @@ private function registerProfilerConfiguration(array $config, ContainerBuilder $
}
}
}
+
+ if (!$config['enabled']) {
+ $container->getDefinition('profiler')->addMethodCall('disable', array());
+ }
}
/**
@@ -50,6 +50,7 @@
<xsd:attribute name="only-exceptions" type="xsd:string" />
<xsd:attribute name="only-master-requests" type="xsd:string" />
+ <xsd:attribute name="enabled" type="xsd:string" />
<xsd:attribute name="dsn" type="xsd:string" />
<xsd:attribute name="username" type="xsd:string" />
<xsd:attribute name="password" type="xsd:string" />
@@ -13,6 +13,7 @@
),
'profiler' => array(
'only_exceptions' => true,
+ 'enabled' => false,
),
'router' => array(
'resource' => '%kernel.root_dir%/config/routing.xml',
@@ -10,7 +10,7 @@
<framework:csrf-protection enabled="true" field-name="_csrf" />
<framework:form />
<framework:esi enabled="true" />
- <framework:profiler only-exceptions="true" />
+ <framework:profiler only-exceptions="true" enabled="false" />
<framework:router resource="%kernel.root_dir%/config/routing.xml" type="xml" />
<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" />
<framework:templating assets-version="SomeVersionScheme" cache="/path/to/cache" >
@@ -9,6 +9,7 @@ framework:
enabled: true
profiler:
only_exceptions: true
+ enabled: false
router:
resource: %kernel.root_dir%/config/routing.xml
type: xml
@@ -48,6 +48,9 @@ public function testProfiler()
$this->assertTrue($container->hasDefinition('data_collector.config'), '->registerProfilerConfiguration() loads collectors.xml');
$this->assertTrue($container->getParameter('profiler_listener.only_exceptions'));
$this->assertEquals('%profiler_listener.only_exceptions%', $container->getDefinition('profiler_listener')->getArgument(2));
+
+ $calls = $container->getDefinition('profiler')->getMethodCalls();
+ $this->assertEquals('disable', $calls[0][0]);
}
public function testRouter()
@@ -0,0 +1,23 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
+
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\DependencyInjection\ContainerAware;
+
+class ProfilerController extends ContainerAware
+{
+ public function indexAction()
+ {
+ return new Response('Hello');
+ }
+}
@@ -17,3 +17,7 @@ session_setflash:
session_showflash:
pattern: /session_showflash
defaults: { _controller: TestBundle:Session:showFlash}
+
+profiler:
+ pattern: /profiler
+ defaults: { _controller: TestBundle:Profiler:index }
@@ -0,0 +1,49 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
+
+/**
+ * @group functional
+ */
+class ProfilerTest extends WebTestCase
+{
+ /**
+ * @dataProvider getConfigs
+ */
+ public function testProfilerIsDisabled($insulate)
+ {
+ $client = $this->createClient(array('test_case' => 'Profiler', 'root_config' => 'config.yml'));
+ if ($insulate) {
+ $client->insulate();
+ }
+
+ $client->request('GET', '/profiler');
+ $this->assertFalse($client->getProfile());
+
+ // enable the profiler for the next request
+ $client->enableProfiler();
+ $crawler = $client->request('GET', '/profiler');
+ $profile = $client->getProfile();
+ $this->assertTrue(is_object($profile));
+
+ $client->request('GET', '/profiler');
+ $this->assertFalse($client->getProfile());
+ }
+
+ public function getConfigs()
+ {
+ return array(
+ array(false),
+ array(true),
+ );
+ }
+}
@@ -0,0 +1,9 @@
+<?php
+
+use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
+use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
+
+return array(
+ new FrameworkBundle(),
+ new TestBundle(),
+);
@@ -0,0 +1,6 @@
+imports:
+ - { resource: ../config/default.yml }
+
+framework:
+ profiler:
+ enabled: false
@@ -0,0 +1,2 @@
+_sessiontest_bundle:
+ resource: @TestBundle/Resources/config/routing.yml

0 comments on commit 3debabf

Please sign in to comment.