Skip to content

Commit 61cd54d

Browse files
committed
minor #15428 Fix example code of customization of bootstrapping in test (sudo-barun)
This PR was submitted for the 5.3 branch but it was merged into the 5.4 branch instead. Discussion ---------- Fix example code of customization of bootstrapping in test The change is copied from Symfony 4.4. <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/releases for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `5.x` for features of unreleased versions). --> Commits ------- a7c7378 [#15428] Add a bit more detail to bootstrap article c08e8fd Fix example code of customization of bootstrapping in test
2 parents 117ed8f + a7c7378 commit 61cd54d

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

testing.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Whenever you write a new line of code, you also potentially add new bugs.
88
To build better and more reliable applications, you should test your code
99
using both functional and unit tests.
1010

11+
.. _testing-installation:
12+
1113
The PHPUnit Testing Framework
1214
-----------------------------
1315

testing/bootstrap.rst

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,64 @@ running those tests. For example, if you're running a functional test and
66
have introduced a new translation resource, then you will need to clear your
77
cache before running those tests.
88

9-
Symfony already created the following ``tests/bootstrap.php`` file when installing
10-
the package to work with tests. If you don't have this file, create it::
9+
When :ref:`installing testing <testing-installation>` using Symfony Flex,
10+
it already created a ``tests/bootstrap.php`` file that is run by PHPUnit
11+
before your tests.
1112

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

15-
require dirname(__DIR__).'/vendor/autoload.php';
15+
.. code-block:: diff
1616
17-
if (file_exists(dirname(__DIR__).'/config/bootstrap.php')) {
18-
require dirname(__DIR__).'/config/bootstrap.php';
19-
} elseif (method_exists(Dotenv::class, 'bootEnv')) {
20-
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
21-
}
17+
// tests/bootstrap.php
18+
use Symfony\Component\Dotenv\Dotenv;
2219
23-
Then, check that your ``phpunit.xml.dist`` file runs this ``bootstrap.php`` file
24-
before running the tests:
20+
require dirname(__DIR__).'/vendor/autoload.php';
2521
26-
.. code-block:: xml
22+
if (file_exists(dirname(__DIR__).'/config/bootstrap.php')) {
23+
require dirname(__DIR__).'/config/bootstrap.php';
24+
} elseif (method_exists(Dotenv::class, 'bootEnv')) {
25+
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
26+
}
2727
28-
<!-- phpunit.xml.dist -->
29-
<?xml version="1.0" encoding="UTF-8" ?>
30-
<phpunit
31-
bootstrap="tests/bootstrap.php"
32-
>
33-
<!-- ... -->
34-
</phpunit>
28+
+ if (isset($_ENV['BOOTSTRAP_CLEAR_CACHE_ENV'])) {
29+
+ // executes the "php bin/console cache:clear" command
30+
+ passthru(sprintf(
31+
+ 'APP_ENV=%s php "%s/../bin/console" cache:clear --no-warmup',
32+
+ $_ENV['BOOTSTRAP_CLEAR_CACHE_ENV'],
33+
+ __DIR__
34+
+ ));
35+
+ }
36+
37+
.. note::
38+
39+
If you don't use Symfony Flex, make sure this file is configured as
40+
bootstrap file in your ``phpunit.xml.dist`` file:
3541

36-
Now, you can define in your ``phpunit.xml.dist`` file which environment you want the
37-
cache to be cleared:
42+
.. code-block:: xml
43+
44+
<!-- phpunit.xml.dist -->
45+
<?xml version="1.0" encoding="UTF-8" ?>
46+
<phpunit
47+
bootstrap="tests/bootstrap.php"
48+
>
49+
<!-- ... -->
50+
</phpunit>
51+
52+
Now, you can update the ``phpunit.xml.dist`` file to declare the custom
53+
environment variable introduced to ``tests/bootstrap.php``:
3854

3955
.. code-block:: xml
4056
4157
<!-- phpunit.xml.dist -->
4258
<?xml version="1.0" encoding="UTF-8" ?>
4359
<phpunit>
44-
<!-- ... -->
45-
4660
<php>
4761
<env name="BOOTSTRAP_CLEAR_CACHE_ENV" value="test"/>
62+
<!-- ... -->
4863
</php>
64+
65+
<!-- ... -->
4966
</phpunit>
5067
51-
This now becomes an environment variable (i.e. ``$_ENV``) that's available
52-
in the custom bootstrap file (``tests/bootstrap.php``).
68+
Now, when running ``vendor/bin/phpunit``, the cache will be cleared
69+
automatically by the bootstrap file before running all tests.

0 commit comments

Comments
 (0)