Test against the lowest Composer dependencies on Travis

Composer just got a new awesome addition thanks to Nicolas Grekas: prefer the lowest versions of your dependencies.

1
composer update --prefer-lowest

This amazing option will install the lowest versions possible for all your dependencies.

What for? Tests of course!

Update Composer

OK before we get started, don’t forget to update Composer right now if you want to use the new option:

1
sudo composer self-update

Testing against lowest dependencies

It might not make a lot of sense to test an application against its lowest dependencies, because unless something is done wrong it will be installed with controlled versions.

However, for libraries (or components…) it’s a very good thing.

To give you an example, I wanted to give it a try and ran composer update --prefer-lowest in PHP-DI’s directory: all hell broke loose… PHPUnit wouldn’t even start.

The reason for this were multiples, but they all boiled down to:

  • I used a feature that didn’t yet exist in version X of dependency Y
  • or dependency Y had a bug in version X

and I allowed installing that problematic version X in composer.json.

In the end, I was distributing a library that could not work at all (if people had insane version constraints).

Setting up tests on Travis

Let’s be proactive and use continuous integration to prevent that from happening again.

Here is an example of a .travis.yml configuration that would run the tests using the lowest dependencies with PHP 5.3.3:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
language: php

php:
  - 5.3.3
  - 5.3
  - 5.4
  - 5.5
  - 5.6
  - hhvm

matrix:
  include:
    - php: 5.3.3
      env: dependencies=lowest

before_script:
  - composer self-update
  - composer install -n
  - if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest --prefer-stable -n; fi;

script:
  - phpunit

You’ll notice that we have to run composer self-update because Travis hasn’t picked up the latest Composer version yet.

Here was the result on Travis:

A new job was added to the build matrix, and this job ran on PHP 5.3.3 with the lowest dependency versions.

If you are interested, have a look at the pull request.

Comments