From c08e8fd15af374845239d22959c3ae9d08d009b1 Mon Sep 17 00:00:00 2001 From: Barun Date: Wed, 9 Jun 2021 23:22:27 +0545 Subject: [PATCH 1/2] Fix example code of customization of bootstrapping in test The change is copied from Symfony 4.4. --- testing/bootstrap.rst | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/testing/bootstrap.rst b/testing/bootstrap.rst index 7acdd6e78cc..03210929cf2 100644 --- a/testing/bootstrap.rst +++ b/testing/bootstrap.rst @@ -6,20 +6,20 @@ running those tests. For example, if you're running a functional test and have introduced a new translation resource, then you will need to clear your cache before running those tests. -Symfony already created the following ``tests/bootstrap.php`` file when installing -the package to work with tests. If you don't have this file, create it:: +To do this, first add a file that executes your bootstrap work:: // tests/bootstrap.php - use Symfony\Component\Dotenv\Dotenv; - - require dirname(__DIR__).'/vendor/autoload.php'; - - if (file_exists(dirname(__DIR__).'/config/bootstrap.php')) { - require dirname(__DIR__).'/config/bootstrap.php'; - } elseif (method_exists(Dotenv::class, 'bootEnv')) { - (new Dotenv())->bootEnv(dirname(__DIR__).'/.env'); + if (isset($_ENV['BOOTSTRAP_CLEAR_CACHE_ENV'])) { + // executes the "php bin/console cache:clear" command + passthru(sprintf( + 'APP_ENV=%s php "%s/../bin/console" cache:clear --no-warmup', + $_ENV['BOOTSTRAP_CLEAR_CACHE_ENV'], + __DIR__ + )); } + require __DIR__.'/../config/bootstrap.php'; + Then, check that your ``phpunit.xml.dist`` file runs this ``bootstrap.php`` file before running the tests: From a7c73785bb629b30fc07e3249b1c1a6b9313bbc2 Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Sun, 9 Oct 2022 15:46:54 +0200 Subject: [PATCH 2/2] [#15428] Add a bit more detail to bootstrap article --- testing.rst | 2 ++ testing/bootstrap.rst | 71 +++++++++++++++++++++++++++---------------- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/testing.rst b/testing.rst index 89e670c710e..778c2cb0dd0 100644 --- a/testing.rst +++ b/testing.rst @@ -8,6 +8,8 @@ Whenever you write a new line of code, you also potentially add new bugs. To build better and more reliable applications, you should test your code using both functional and unit tests. +.. _testing-installation: + The PHPUnit Testing Framework ----------------------------- diff --git a/testing/bootstrap.rst b/testing/bootstrap.rst index 03210929cf2..c075552a9e3 100644 --- a/testing/bootstrap.rst +++ b/testing/bootstrap.rst @@ -6,47 +6,64 @@ running those tests. For example, if you're running a functional test and have introduced a new translation resource, then you will need to clear your cache before running those tests. -To do this, first add a file that executes your bootstrap work:: +When :ref:`installing testing ` using Symfony Flex, +it already created a ``tests/bootstrap.php`` file that is run by PHPUnit +before your tests. - // tests/bootstrap.php - if (isset($_ENV['BOOTSTRAP_CLEAR_CACHE_ENV'])) { - // executes the "php bin/console cache:clear" command - passthru(sprintf( - 'APP_ENV=%s php "%s/../bin/console" cache:clear --no-warmup', - $_ENV['BOOTSTRAP_CLEAR_CACHE_ENV'], - __DIR__ - )); - } +You can modify this file to add custom logic: - require __DIR__.'/../config/bootstrap.php'; +.. code-block:: diff -Then, check that your ``phpunit.xml.dist`` file runs this ``bootstrap.php`` file -before running the tests: + // tests/bootstrap.php + use Symfony\Component\Dotenv\Dotenv; -.. code-block:: xml + require dirname(__DIR__).'/vendor/autoload.php'; - - - - - + if (file_exists(dirname(__DIR__).'/config/bootstrap.php')) { + require dirname(__DIR__).'/config/bootstrap.php'; + } elseif (method_exists(Dotenv::class, 'bootEnv')) { + (new Dotenv())->bootEnv(dirname(__DIR__).'/.env'); + } + + + if (isset($_ENV['BOOTSTRAP_CLEAR_CACHE_ENV'])) { + + // executes the "php bin/console cache:clear" command + + passthru(sprintf( + + 'APP_ENV=%s php "%s/../bin/console" cache:clear --no-warmup', + + $_ENV['BOOTSTRAP_CLEAR_CACHE_ENV'], + + __DIR__ + + )); + + } + +.. note:: -Now, you can define in your ``phpunit.xml.dist`` file which environment you want the -cache to be cleared: + If you don't use Symfony Flex, make sure this file is configured as + bootstrap file in your ``phpunit.xml.dist`` file: + + .. code-block:: xml + + + + + + + +Now, you can update the ``phpunit.xml.dist`` file to declare the custom +environment variable introduced to ``tests/bootstrap.php``: .. code-block:: xml - - + + + -This now becomes an environment variable (i.e. ``$_ENV``) that's available -in the custom bootstrap file (``tests/bootstrap.php``). +Now, when running ``vendor/bin/phpunit``, the cache will be cleared +automatically by the bootstrap file before running all tests.