|
1 | 1 | .. index::
|
2 | 2 | single: Tests; Profiling
|
3 | 3 |
|
4 |
| -How to use the Profiling Data in a Test |
5 |
| -======================================= |
| 4 | +How to use the Profiler in a Functional Test |
| 5 | +============================================ |
6 | 6 |
|
7 | 7 | It's highly recommended that a functional test only tests the Response. But if
|
8 | 8 | 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. |
10 | 11 |
|
11 | 12 | The Symfony2 :doc:`Profiler </guides/internals/profiler>` gathers a lot of
|
12 | 13 | data for each request. Use these data to check the number of database calls,
|
13 | 14 | the time spent in the framework, ... But before writing assertions, always
|
14 | 15 | check that the profiler is indeed available (it is enabled by default in the
|
15 | 16 | ``test`` environment)::
|
16 | 17 |
|
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'); |
20 | 24 |
|
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 | + } |
23 | 37 | }
|
24 | 38 |
|
| 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 | + |
25 | 54 | .. note::
|
26 | 55 |
|
27 | 56 | The profiler information are available even if you insulate the client or
|
28 | 57 | 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. |
0 commit comments