@@ -6,47 +6,64 @@ running those tests. For example, if you're running a functional test and
6
6
have introduced a new translation resource, then you will need to clear your
7
7
cache before running those tests.
8
8
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.
10
12
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:
20
14
21
- require __DIR__.'/../config/bootstrap.php';
15
+ .. code-block :: diff
22
16
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;
25
19
26
- .. code-block :: xml
20
+ require dirname(__DIR__).'/vendor/autoload.php';
27
21
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 ::
35
38
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 ``:
38
54
39
55
.. code-block :: xml
40
56
41
57
<!-- phpunit.xml.dist -->
42
58
<?xml version =" 1.0" encoding =" UTF-8" ?>
43
59
<phpunit >
44
- <!-- ... -->
45
-
46
60
<php >
47
61
<env name =" BOOTSTRAP_CLEAR_CACHE_ENV" value =" test" />
62
+ <!-- ... -->
48
63
</php >
64
+
65
+ <!-- ... -->
49
66
</phpunit >
50
67
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