Skip to content

Commit b7eaf36

Browse files
committed
Add a cookbook entry to explain how to clear the cache before tests
1 parent 9fa129b commit b7eaf36

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

cookbook/testing/auto_cache_clear.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
How to automatically clear the cache before tests are run
2+
=========================================================
3+
4+
Some features need the cache to be cleared in order to work.
5+
You could have tests failing, because some part of the cache would need to
6+
be cleared. (For example, new translation resources)
7+
8+
It's a good idea to clear the cache before running your tests, and automate it
9+
is even better.
10+
11+
Add the following file::
12+
13+
// app/tests.bootstrap.php
14+
if (isset($_ENV['BOOTSTRAP_CLEAR_CACHE_ENV'])) {
15+
passthru(sprintf(
16+
'php "%s/console" cache:clear --env=%s --no-warmup',
17+
__DIR__,
18+
$_ENV['BOOTSTRAP_CLEAR_CACHE_ENV']
19+
));
20+
}
21+
22+
require __DIR__.'/bootstrap.php.cache';
23+
24+
Replace the test bootstrap file ``bootstrap.php.cache`` in `app/phpunit.xml` by
25+
``tests.bootstrap.php``:
26+
27+
.. code-block:: xml
28+
29+
<!-- app/phpunit.xml -->
30+
bootstrap = "tests.bootstrap.php"
31+
32+
And then, you can define in your `phpunit.xml` file which environment you want the
33+
cache to be cleared:
34+
35+
.. code-block:: xml
36+
37+
<!-- app/phpunit.xml -->
38+
<php>
39+
<env name="BOOTSTRAP_CLEAR_CACHE_ENV" value="test"/>
40+
</php>
41+

0 commit comments

Comments
 (0)