diff --git a/testing.rst b/testing.rst index 96bc16cdc1b..707c95eb8d8 100644 --- a/testing.rst +++ b/testing.rst @@ -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 - + + + 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:: diff --git a/testing/bootstrap.rst b/testing/bootstrap.rst index 82f8c3e04c3..9f4b02af19a 100644 --- a/testing/bootstrap.rst +++ b/testing/bootstrap.rst @@ -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 diff --git a/testing/database.rst b/testing/database.rst index 4dd7f511238..ed94218b529 100644 --- a/testing/database.rst +++ b/testing/database.rst @@ -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 - - - - - - - .. 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 + + + + + + + + + diff --git a/testing/profiling.rst b/testing/profiling.rst index 7455938594e..fc7ee75cef1 100644 --- a/testing/profiling.rst +++ b/testing/profiling.rst @@ -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 ` 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 ` 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 + + + + + + + + + + + + + .. 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 { @@ -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 ` 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 - - - - - - - - - - - - - .. 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.