From 62228ecbe3bb6716d3ca36dd0d3de826fec86f08 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 20 Nov 2017 16:45:17 +0100 Subject: [PATCH 1/2] Updated testing/* articles to Symfony 4 --- testing.rst | 16 +++--- testing/database.rst | 26 +++------- testing/profiling.rst | 113 ++++++++++++++++++++++-------------------- 3 files changed, 76 insertions(+), 79 deletions(-) diff --git a/testing.rst b/testing.rst index 96bc16cdc1b..e3f0ce225d9 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/database.rst b/testing/database.rst index 4dd7f511238..b144fd5b7da 100644 --- a/testing/database.rst +++ b/testing/database.rst @@ -113,44 +113,32 @@ 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 just in the ``test`` environment: .. configuration-block:: .. code-block:: yaml - # app/config/config_test.yml + # config/packages/test/doctrine.yaml doctrine: # ... dbal: - host: localhost - dbname: testdb - user: testdb - password: testdb + url: 'mysql://USERNAME:PASSWORD@127.0.0.1/DB_NAME?charset=utf8mb4&serverVersion=5.7' .. code-block:: xml - + .. code-block:: php - // app/config/config_test.php + // config/packages/test/doctrine.php $container->loadFromExtension('doctrine', array( 'dbal' => array( - 'host' => 'localhost', - 'dbname' => 'testdb', - 'user' => 'testdb', - 'password' => 'testdb', + 'url' => 'mysql://USERNAME:PASSWORD@127.0.0.1/DB_NAME?charset=utf8mb4&serverVersion=5.7', ), )); - -Make sure that your database runs on localhost and has the defined database and -user credentials set up. 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. From d396d8a599d17437d432ff8bf558b076d77b289f Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 20 Nov 2017 17:27:03 +0100 Subject: [PATCH 2/2] Fixed everything --- testing.rst | 4 ++-- testing/bootstrap.rst | 2 +- testing/database.rst | 43 +++++++++++++------------------------------ 3 files changed, 16 insertions(+), 33 deletions(-) diff --git a/testing.rst b/testing.rst index e3f0ce225d9..707c95eb8d8 100644 --- a/testing.rst +++ b/testing.rst @@ -161,7 +161,7 @@ As an example, a test could look like this:: 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): + default ``phpunit.xml.dist`` file provided by Symfony): .. code-block:: xml @@ -169,7 +169,7 @@ As an example, a test could look like this:: - + 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 b144fd5b7da..ed94218b529 100644 --- a/testing/database.rst +++ b/testing/database.rst @@ -112,33 +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 just in the ``test`` environment: - -.. configuration-block:: - - .. code-block:: yaml - - # config/packages/test/doctrine.yaml - doctrine: - # ... - dbal: - url: 'mysql://USERNAME:PASSWORD@127.0.0.1/DB_NAME?charset=utf8mb4&serverVersion=5.7' - - .. code-block:: xml - - - - - - - .. code-block:: php - - // config/packages/test/doctrine.php - $container->loadFromExtension('doctrine', array( - 'dbal' => array( - 'url' => 'mysql://USERNAME:PASSWORD@127.0.0.1/DB_NAME?charset=utf8mb4&serverVersion=5.7', - ), - )); +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 + + + + + + + + +