Search code examples
phpphpunittravis-ciphar

How to cache phpunit.phar on Travis CI?


At the moment I use Travis CI in order to test a Symfony Bundle. PHPUnit is installed by Composer since PHPUnit is declared as a dev dependency:

{
    …
    "require" : {
        "php": ">=5.5",
        "symfony/symfony": "~2.7 || ~3.0"
    },
    "require-dev": {
        "doctrine/orm": "^2.4.8",
        "symfony/assetic-bundle": "~2.3",
        "symfony/monolog-bundle": "~2.4",
        "doctrine/doctrine-fixtures-bundle": "~2.3",
        "liip/functional-test-bundle": "~1.4",
        "phpunit/phpunit": "4.8.* || ~5.1"
    },
    …
}

Even if I use Travis CI's cache to cache the ~/.composer folder after each build, installing PHPUnit with Composer takes some time because PHPUnit has to check all the dependencies before installing them.


So I was thinking about using the PHAR version of PHPUnit that can be installed in one command:

wget https://phar.phpunit.de/phpunit.phar

But if I use this command on Travis CI, PHPUnit will be downloaded once in each build. Is there a way to cache this phpunit.phar file? An easy way may be to put the file in the cache, check the date of this file and download a new version if the file is older than 1 day, but there may be a simpler solution.

There is --self-update option for PHPUnit but it will be removed in the next major release.


Solution

  • You can configure Travis to keep certain directories between builds (called "caching" in their documentation).

    Simply add the following configuration to your Travis config file and the /vendor directory should persist across builds:

    cache:
      directories:
        - vendor
    

    You should still include a composer install call to your build to update dependencies when the composer.json file changed. To further speed up the composer install process, you can also add you composer.lock file to version control.


    Alternatively, you can of course use the same feature to simple keep a downloaded phpunit.phar file between builds.