Skip to content

Commit a7c7378

Browse files
committed
[symfony#15428] Add a bit more detail to bootstrap article
1 parent c08e8fd commit a7c7378

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
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: 44 additions & 27 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-
To do this, first add a file that executes your bootstrap work::
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.
1012

11-
// tests/bootstrap.php
12-
if (isset($_ENV['BOOTSTRAP_CLEAR_CACHE_ENV'])) {
13-
// executes the "php bin/console cache:clear" command
14-
passthru(sprintf(
15-
'APP_ENV=%s php "%s/../bin/console" cache:clear --no-warmup',
16-
$_ENV['BOOTSTRAP_CLEAR_CACHE_ENV'],
17-
__DIR__
18-
));
19-
}
13+
You can modify this file to add custom logic:
2014

21-
require __DIR__.'/../config/bootstrap.php';
15+
.. code-block:: diff
2216
23-
Then, check that your ``phpunit.xml.dist`` file runs this ``bootstrap.php`` file
24-
before running the tests:
17+
// tests/bootstrap.php
18+
use Symfony\Component\Dotenv\Dotenv;
2519
26-
.. code-block:: xml
20+
require dirname(__DIR__).'/vendor/autoload.php';
2721
28-
<!-- phpunit.xml.dist -->
29-
<?xml version="1.0" encoding="UTF-8" ?>
30-
<phpunit
31-
bootstrap="tests/bootstrap.php"
32-
>
33-
<!-- ... -->
34-
</phpunit>
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+
}
27+
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::
3538

36-
Now, you can define in your ``phpunit.xml.dist`` file which environment you want the
37-
cache to be cleared:
39+
If you don't use Symfony Flex, make sure this file is configured as
40+
bootstrap file in your ``phpunit.xml.dist`` file:
41+
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)