Skip to content

Fix example code of customization of bootstrapping in test #15428

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
Oct 9, 2022
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
2 changes: 2 additions & 0 deletions testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------------------------

Expand Down
69 changes: 43 additions & 26 deletions testing/bootstrap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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::
When :ref:`installing testing <testing-installation>` using Symfony Flex,
it already created a ``tests/bootstrap.php`` file that is run by PHPUnit
before your tests.

// tests/bootstrap.php
use Symfony\Component\Dotenv\Dotenv;
You can modify this file to add custom logic:

require dirname(__DIR__).'/vendor/autoload.php';
.. code-block:: diff

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');
}
// tests/bootstrap.php
use Symfony\Component\Dotenv\Dotenv;

Then, check that your ``phpunit.xml.dist`` file runs this ``bootstrap.php`` file
before running the tests:
require dirname(__DIR__).'/vendor/autoload.php';

.. code-block:: xml
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');
}

<!-- phpunit.xml.dist -->
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit
bootstrap="tests/bootstrap.php"
>
<!-- ... -->
</phpunit>
+ 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::

If you don't use Symfony Flex, make sure this file is configured as
bootstrap file in your ``phpunit.xml.dist`` file:

Now, you can define in your ``phpunit.xml.dist`` file which environment you want the
cache to be cleared:
.. code-block:: xml

<!-- phpunit.xml.dist -->
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit
bootstrap="tests/bootstrap.php"
>
<!-- ... -->
</phpunit>

Now, you can update the ``phpunit.xml.dist`` file to declare the custom
environment variable introduced to ``tests/bootstrap.php``:

.. code-block:: xml

<!-- phpunit.xml.dist -->
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit>
<!-- ... -->

<php>
<env name="BOOTSTRAP_CLEAR_CACHE_ENV" value="test"/>
<!-- ... -->
</php>

<!-- ... -->
</phpunit>

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.