Skip to content

Updated testing/* articles to Symfony 4 #8705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,26 @@ As an example, a test could look like this::

.. tip::

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

.. code-block:: xml

<?xml version="1.0" charset="utf-8" ?>
<phpunit>
<php>
<server name="KERNEL_DIR" value="/path/to/your/app/" />
<!-- the value is the FQCN of the application kernel -->
<env name="KERNEL_CLASS" value="App\Kernel" />
</php>
<!-- ... -->
</phpunit>

If your use case is more complex, you can also override the
``createKernel()`` or ``getKernelClass()`` methods of your functional test,
which take precedence over the ``KERNEL_CLASS`` env var.

The ``createClient()`` method returns a client, which is like a browser that
you'll use to crawl your site::

Expand Down
2 changes: 1 addition & 1 deletion testing/bootstrap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ To do this, first add a file that executes your bootstrap work::

require __DIR__.'/../vendor/autoload.php';

Then, configure ``phpunit.xml.dist`` to execute this ``bootstra.php`` file
Then, configure ``phpunit.xml.dist`` to execute this ``bootstrap.php`` file
before running the tests:

.. code-block:: xml
Expand Down
55 changes: 13 additions & 42 deletions testing/database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,45 +112,16 @@ Most of the time you want to use a dedicated database connection to make sure
not to overwrite data you entered when developing the application and also
to be able to clear the database before every test.

To do this, you can specify a database configuration which overwrites the default
configuration:

.. configuration-block::

.. code-block:: yaml

# app/config/config_test.yml
doctrine:
# ...
dbal:
host: localhost
dbname: testdb
user: testdb
password: testdb

.. code-block:: xml

<!-- app/config/config_test.xml -->
<doctrine:config>
<doctrine:dbal
host="localhost"
dbname="testdb"
user="testdb"
password="testdb"
/>
</doctrine:config>

.. code-block:: php

// app/config/config_test.php
$container->loadFromExtension('doctrine', array(
'dbal' => array(
'host' => 'localhost',
'dbname' => 'testdb',
'user' => 'testdb',
'password' => 'testdb',
),
));

Make sure that your database runs on localhost and has the defined database and
user credentials set up.
To do this, you can override the value of the ``DATABASE_URL`` env var in the
``phpunit.xml.dist`` to use a diferent database for your tests:

.. code-block:: xml

<?xml version="1.0" charset="utf-8" ?>
<phpunit>
<php>
<!-- the value is the Doctrine connection string in DSN format -->
<env name="DATABASE_URL" value="mysql://USERNAME:[email protected]/DB_NAME?charset=utf8mb4&serverVersion=5.7" />
</php>
<!-- ... -->
</phpunit>
113 changes: 59 additions & 54 deletions testing/profiling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,65 @@ you write functional tests that monitor your production servers, you might
want to write tests on the profiling data as it gives you a great way to check
various things and enforce some metrics.

:doc:`The Symfony Profiler </profiler>` gathers a lot of data for
each request. Use this data to check the number of database calls, the time
spent in the framework, etc. But before writing assertions, enable the profiler
and check that the profiler is indeed available (it is enabled by default in
the ``test`` environment)::
.. _speeding-up-tests-by-not-collecting-profiler-data:

Enabling the Profiler in Tests
------------------------------

Collecting data with :doc:`the Symfony Profiler </profiler>` can slow down your
tests significantly. That's why Symfony disables it by default:

.. configuration-block::

.. code-block:: yaml

# config/packages/test/web_profiler.yaml

# ...
framework:
profiler: { collect: false }

.. code-block:: xml

<!-- config/packages/test/web_profiler.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<!-- ... -->

<framework:config>
<framework:profiler enabled="true" collect="false" />
</framework:config>
</container>

.. code-block:: php

// config/packages/test/web_profiler.php

// ...
$container->loadFromExtension('framework', array(
// ...
'profiler' => array(
'enabled' => true,
'collect' => false,
),
));

Setting ``collect`` to ``true`` enables the profiler for all tests. However, if
you need the profiler just in a few tests, you can keep it disabled globally and
enable the profiler individually on each test by calling
``$client->enableProfiler()``.

Testing the Profiler Information
--------------------------------

The data collected by the Symfony Profiler can be used to check the number of
database calls, the time spent in the framework, etc. All this information is
provided by the collectors obtained through the ``$client->getProfile()`` call::

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

Read the API for built-in :doc:`data collectors </profiler/data_collector>`
to learn more about their interfaces.

Speeding up Tests by not Collecting Profiler Data
-------------------------------------------------

To avoid collecting data in each test you can set the ``collect`` parameter
to false:

.. configuration-block::

.. code-block:: yaml

# app/config/config_test.yml

# ...
framework:
profiler:
enabled: true
collect: false

.. code-block:: xml

<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<!-- ... -->

<framework:config>
<framework:profiler enabled="true" collect="false" />
</framework:config>
</container>

.. code-block:: php

// app/config/config.php

// ...
$container->loadFromExtension('framework', array(
'profiler' => array(
'enabled' => true,
'collect' => false,
),
));

In this way only tests that call ``$client->enableProfiler()`` will collect data.