Skip to content

Commit 62228ec

Browse files
committed
Updated testing/* articles to Symfony 4
1 parent 4d5ebc4 commit 62228ec

File tree

3 files changed

+76
-79
lines changed

3 files changed

+76
-79
lines changed

testing.rst

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,22 +158,26 @@ As an example, a test could look like this::
158158

159159
.. tip::
160160

161-
To run your functional tests, the ``WebTestCase`` class bootstraps the
162-
kernel of your application. In most cases, this happens automatically.
163-
However, if your kernel is in a non-standard directory, you'll need
164-
to modify your ``phpunit.xml.dist`` file to set the ``KERNEL_DIR``
165-
environment variable to the directory of your kernel:
161+
To run your functional tests, the ``WebTestCase`` class needs to know which
162+
is the application kernel to bootstrap it. The kernel class is usually
163+
defined in the ``KERNEL_CLASS`` environment variable (included in the
164+
default ``phpunit .xml-dist`` file provided by Symfony):
166165

167166
.. code-block:: xml
168167
169168
<?xml version="1.0" charset="utf-8" ?>
170169
<phpunit>
171170
<php>
172-
<server name="KERNEL_DIR" value="/path/to/your/app/" />
171+
<!-- the value is the FQCN of the application kernel -->
172+
<server name="KERNEL_CLASS" value="App\Kernel" />
173173
</php>
174174
<!-- ... -->
175175
</phpunit>
176176
177+
If your use case is more complex, you can also override the
178+
``createKernel()`` or ``getKernelClass()`` methods of your functional test,
179+
which take precedence over the ``KERNEL_CLASS`` env var.
180+
177181
The ``createClient()`` method returns a client, which is like a browser that
178182
you'll use to crawl your site::
179183

testing/database.rst

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -113,44 +113,32 @@ not to overwrite data you entered when developing the application and also
113113
to be able to clear the database before every test.
114114

115115
To do this, you can specify a database configuration which overwrites the default
116-
configuration:
116+
configuration just in the ``test`` environment:
117117

118118
.. configuration-block::
119119

120120
.. code-block:: yaml
121121
122-
# app/config/config_test.yml
122+
# config/packages/test/doctrine.yaml
123123
doctrine:
124124
# ...
125125
dbal:
126-
host: localhost
127-
dbname: testdb
128-
user: testdb
129-
password: testdb
126+
url: 'mysql://USERNAME:[email protected]/DB_NAME?charset=utf8mb4&serverVersion=5.7'
130127
131128
.. code-block:: xml
132129
133-
<!-- app/config/config_test.xml -->
130+
<!-- config/packages/test/doctrine.xml -->
134131
<doctrine:config>
135132
<doctrine:dbal
136-
host="localhost"
137-
dbname="testdb"
138-
user="testdb"
139-
password="testdb"
133+
url="mysql://USERNAME:[email protected]/DB_NAME?charset=utf8mb4&serverVersion=5.7"
140134
/>
141135
</doctrine:config>
142136
143137
.. code-block:: php
144138
145-
// app/config/config_test.php
139+
// config/packages/test/doctrine.php
146140
$container->loadFromExtension('doctrine', array(
147141
'dbal' => array(
148-
'host' => 'localhost',
149-
'dbname' => 'testdb',
150-
'user' => 'testdb',
151-
'password' => 'testdb',
142+
'url' => 'mysql://USERNAME:[email protected]/DB_NAME?charset=utf8mb4&serverVersion=5.7',
152143
),
153144
));
154-
155-
Make sure that your database runs on localhost and has the defined database and
156-
user credentials set up.

testing/profiling.rst

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,65 @@ you write functional tests that monitor your production servers, you might
99
want to write tests on the profiling data as it gives you a great way to check
1010
various things and enforce some metrics.
1111

12-
:doc:`The Symfony Profiler </profiler>` gathers a lot of data for
13-
each request. Use this data to check the number of database calls, the time
14-
spent in the framework, etc. But before writing assertions, enable the profiler
15-
and check that the profiler is indeed available (it is enabled by default in
16-
the ``test`` environment)::
12+
.. _speeding-up-tests-by-not-collecting-profiler-data:
13+
14+
Enabling the Profiler in Tests
15+
------------------------------
16+
17+
Collecting data with :doc:`the Symfony Profiler </profiler>` can slow down your
18+
tests significantly. That's why Symfony disables it by default:
19+
20+
.. configuration-block::
21+
22+
.. code-block:: yaml
23+
24+
# config/packages/test/web_profiler.yaml
25+
26+
# ...
27+
framework:
28+
profiler: { collect: false }
29+
30+
.. code-block:: xml
31+
32+
<!-- config/packages/test/web_profiler.xml -->
33+
<?xml version="1.0" encoding="UTF-8" ?>
34+
<container xmlns="http://symfony.com/schema/dic/services"
35+
xmlns:framework="http://symfony.com/schema/dic/symfony"
36+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
37+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
38+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
39+
40+
<!-- ... -->
41+
42+
<framework:config>
43+
<framework:profiler enabled="true" collect="false" />
44+
</framework:config>
45+
</container>
46+
47+
.. code-block:: php
48+
49+
// config/packages/test/web_profiler.php
50+
51+
// ...
52+
$container->loadFromExtension('framework', array(
53+
// ...
54+
'profiler' => array(
55+
'enabled' => true,
56+
'collect' => false,
57+
),
58+
));
59+
60+
Setting ``collect`` to ``true`` enables the profiler for all tests. However, if
61+
you need the profiler just in a few tests, you can keep it disabled globally and
62+
enable the profiler individually on each test by calling
63+
``$client->enableProfiler()``.
64+
65+
Testing the Profiler Information
66+
--------------------------------
67+
68+
The data collected by the Symfony Profiler can be used to check the number of
69+
database calls, the time spent in the framework, etc. All this information is
70+
provided by the collectors obtained through the ``$client->getProfile()`` call::
1771

1872
class LuckyControllerTest extends WebTestCase
1973
{
@@ -74,52 +128,3 @@ finish. It's easy to achieve if you embed the token in the error message::
74128

75129
Read the API for built-in :doc:`data collectors </profiler/data_collector>`
76130
to learn more about their interfaces.
77-
78-
Speeding up Tests by not Collecting Profiler Data
79-
-------------------------------------------------
80-
81-
To avoid collecting data in each test you can set the ``collect`` parameter
82-
to false:
83-
84-
.. configuration-block::
85-
86-
.. code-block:: yaml
87-
88-
# app/config/config_test.yml
89-
90-
# ...
91-
framework:
92-
profiler:
93-
enabled: true
94-
collect: false
95-
96-
.. code-block:: xml
97-
98-
<!-- app/config/config.xml -->
99-
<?xml version="1.0" encoding="UTF-8" ?>
100-
<container xmlns="http://symfony.com/schema/dic/services"
101-
xmlns:framework="http://symfony.com/schema/dic/symfony"
102-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
103-
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
104-
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
105-
106-
<!-- ... -->
107-
108-
<framework:config>
109-
<framework:profiler enabled="true" collect="false" />
110-
</framework:config>
111-
</container>
112-
113-
.. code-block:: php
114-
115-
// app/config/config.php
116-
117-
// ...
118-
$container->loadFromExtension('framework', array(
119-
'profiler' => array(
120-
'enabled' => true,
121-
'collect' => false,
122-
),
123-
));
124-
125-
In this way only tests that call ``$client->enableProfiler()`` will collect data.

0 commit comments

Comments
 (0)