How to Customize the Bootstrap Process before Running Tests

How to Customize the Bootstrap Process before Running Tests

Sometimes when running tests, you need to do additional bootstrap work before 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.

To do this, first add a file that executes your bootstrap work:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// tests/bootstrap.php
if (isset($_ENV['BOOTSTRAP_CLEAR_CACHE_ENV'])) {
    // executes the "php bin/console cache:clear" command
    passthru(sprintf(
        'php "%s/../bin/console" cache:clear --env=%s --no-warmup',
        __DIR__,
        $_ENV['BOOTSTRAP_CLEAR_CACHE_ENV']
    ));
}

require __DIR__.'/../vendor/autoload.php';

Then, configure phpunit.xml.dist to execute this bootstrap.php file before running the tests:

1
2
3
4
5
6
7
<!-- phpunit.xml.dist -->
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    bootstrap="tests/bootstrap.php"
>
    <!-- ... -->
</phpunit>

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

1
2
3
4
5
6
7
8
9
<!-- 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).

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.