Skip to content

Commit d154bf1

Browse files
committed
moved profiling info in tests in the recipe
1 parent 1668023 commit d154bf1

File tree

2 files changed

+42
-51
lines changed

2 files changed

+42
-51
lines changed

cookbook/testing/profiling.rst

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,62 @@
11
.. index::
22
single: Tests; Profiling
33

4-
How to use the Profiling Data in a Test
5-
=======================================
4+
How to use the Profiler in a Functional Test
5+
============================================
66

77
It's highly recommended that a functional test only tests the Response. But if
88
you write functional tests that monitor your production servers, you might
9-
want to write tests on the profiling data.
9+
want to write tests on the profiling data as it gives you a great way to check
10+
various things and enforce some metrics.
1011

1112
The Symfony2 :doc:`Profiler </guides/internals/profiler>` gathers a lot of
1213
data for each request. Use these data to check the number of database calls,
1314
the time spent in the framework, ... But before writing assertions, always
1415
check that the profiler is indeed available (it is enabled by default in the
1516
``test`` environment)::
1617

17-
if ($profiler = $client->getProfiler()) {
18-
// check the number of requests
19-
$this->assertTrue($profiler->get('db')->getQueryCount() < 10);
18+
class HelloControllerTest extends WebTestCase
19+
{
20+
public function testIndex()
21+
{
22+
$client = $this->createClient();
23+
$crawler = $client->request('GET', '/hello/Fabien');
2024

21-
// check the time spent in the framework
22-
$this->assertTrue( $profiler->get('timer')->getTime() < 0.5);
25+
// Write some assertions about the Response
26+
// ...
27+
28+
// Check that the profiler is enabled
29+
if ($profiler = $client->getProfiler()) {
30+
// check the number of requests
31+
$this->assertTrue($profiler->get('db')->getQueryCount() < 10);
32+
33+
// check the time spent in the framework
34+
$this->assertTrue( $profiler->get('timer')->getTime() < 0.5);
35+
}
36+
}
2337
}
2438

39+
If a test fails because of profiling data (too many DB queries for instance),
40+
you might want to use the Web Profiler to analyze the request after the tests
41+
finish. It's easy to achieve if you embed the token in the error message::
42+
43+
$this->assertTrue(
44+
$profiler->get('db')->getQueryCount() < 30,
45+
sprintf('Checks that query count is less than 30 (token %s)', $profiler->getToken())
46+
);
47+
48+
.. caution::
49+
50+
The profiler store can be different depending on the environment
51+
(especially if you use the SQLite store, which is the default configured
52+
one).
53+
2554
.. note::
2655

2756
The profiler information are available even if you insulate the client or
2857
if you use an HTTP layer for your tests.
58+
59+
.. tip::
60+
61+
Read the API for built-in `data collectors`_ to learn more about their
62+
interfaces.

guides/internals/profiler.rst

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -48,49 +48,6 @@ in development to debug your code and enhance performance; but it can also be
4848
used to explore problems that occur in production. It exposes all information
4949
collected by the profiler in a web interface.
5050

51-
Using the Profiler in Functional Tests
52-
--------------------------------------
53-
54-
Profiling data are available in functional tests. It gives you a great way to
55-
check various things and enforce some metrics::
56-
57-
class HelloControllerTest extends WebTestCase
58-
{
59-
public function testIndex()
60-
{
61-
$client = $this->createClient();
62-
$crawler = $client->request('GET', '/hello/Fabien');
63-
64-
// Write some assertions about the Response
65-
// ...
66-
67-
// Check that the profiler is enabled
68-
if ($profiler = $client->getProfiler()) {
69-
$this->assertTrue($profiler->get('db')->getQueryCount() < 30);
70-
$this->assertTrue($profiler->get('timer')->getTime() < 50);
71-
}
72-
}
73-
}
74-
75-
.. tip::
76-
77-
Read the API for built-in `data collectors`_ to learn more about their
78-
interfaces.
79-
80-
If a test fails because of profiling data (too many DB queries for instance),
81-
you might want to use the Web Profiler to analyze the request after the tests
82-
finish. It's easy to achieve if you embed the token in the error message::
83-
84-
$this->assertTrue(
85-
$profiler->get('db')->getQueryCount() < 30,
86-
sprintf('checks that query count is less than 30 (token %s)', $profiler->getToken())
87-
);
88-
89-
.. caution::
90-
91-
The profiler store can be different depending on the environment
92-
(especially if you use the SQLite store, which is the default configured
93-
one).
9451

9552
Accessing the Profiling information
9653
-----------------------------------

0 commit comments

Comments
 (0)