Luceos Luceos - 1 year ago 203
PHP Question

Travis configuration for composer packages

While working on a laravel 5.1+ package I have this need to run automated tests through The difference with regular automated tests is the requirement to include this package into a framework and set specific configuration options to run the tests.

So the requirement would be:

  • install laravel

  • add my package as dependency

  • set some travis specific configurations like the travis database access

  • run migrations of laravel

  • run migrations specific for package or run an artisan command

  • run package specific unit tests

I searched everywhere; asked on laravel forums, asked in a travis community chat and saw this topic being closed as too localized (although an answer would have certainly been helpful now). I'm hoping my question is fit to remain open.

At this time I have the following configuration:

language: php

- 5.5
- 5.6
- hhvm


- sudo composer self-update

- composer create-project laravel/laravel
- cd ./laravel
- composer require hyn-me/multi-tenant ~0.1.0
- composer update

- cp .env.travis .env
- export APP_ENV="testing"
- php artisan migrate -q -n --path ./vendor/hyn-me/multi-tenant/src/migrations
- cd ./vendor/hyn-me/multi-tenant

script: phpunit

Yet my knowledge of travis (thus far) is limited and before I send in an unneeded number of commits to fix my problems I'd rather have your opinion on what would be a good method to test integration into a framework.

Ps. this concerns the package hyn/multi-tenant.

Advise on how to keep this question as generic as possible would be helpful. I hope without explicitly mentioning best practice and requesting integration into framework examples helps in defining the scope of the answers.

Answer Source

So after weeks of pushing commits into travis, I finally made this work.

The .travis.yml:

language: php

sudo: true

  - 5.5
  - 5.6
  - 7.0
  - hhvm


  # fix ipv6 issue that prevented composer requests and resulted in failing builds
  - sudo sh -c "echo 'precedence ::ffff:0:0/96  100' >> /etc/gai.conf"
  # updates composer on travis
  - travis_retry composer self-update
  # clear composer cache, might speed up finding new tags
  - travis_retry composer clear-cache
  # set the global github token, so connections won't be cancelled
  - composer config -g $GITHUB_TOKEN
  # create a new database for the hyn connection
  - mysql -e 'create database hyn;' -uroot
  - mysql -e "grant all privileges on *.* to 'travis'@'localhost' with grant option;" -uroot
  # create a new laravel project in the subfolder laravel (default composer behaviour)
  - composer create-project laravel/laravel
  # set global variables

  # run the script calling unit tests and so on
  - ./scripts/

  - if [[ $TRAVIS_PHP_VERSION != '7.0' ]]; then php vendor/bin/ocular code-coverage:upload --format=php-clover ${TRAVIS_BUILD_DIR}/coverage.clover; fi

And the scripts/


# e causes to exit when one commands returns non-zero
# v prints every line before executing
set -ev

cd ${TRAVIS_BUILD_DIR}/laravel


if [[ ${TRAVIS_BRANCH} =~ $BRANCH_REGEX ]]; then
    echo "composer require ${TRAVIS_REPO_SLUG}:${TRAVIS_BRANCH}"
    composer require ${TRAVIS_REPO_SLUG}:${TRAVIS_BRANCH}
    echo "composer require ${TRAVIS_REPO_SLUG}:dev-${TRAVIS_BRANCH}"
    # development package of framework could be required for the package
    composer require hyn-me/framework "dev-master as 0.1.99"
    composer require "${TRAVIS_REPO_SLUG}:dev-${TRAVIS_BRANCH}#${TRAVIS_COMMIT}"

# moves the unit test to the root laravel directory
cp ./vendor/${TRAVIS_REPO_SLUG}/phpunit.travis.xml ./phpunit.xml

# phpunit --coverage-text --coverage-clover=${TRAVIS_BUILD_DIR}/coverage.clover

This code might change due to new Laravel versions or changes in travis. If this is the case, you will find the latest release here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download