Luceos Luceos - 1 month ago 12
PHP Question

Travis configuration for composer packages

While working on a laravel 5.1+ package I have this need to run automated tests through travis-ci.org. 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

php:
- 5.5
- 5.6
- hhvm

addons:
hosts:
- system.hyn.me
- tenant.hyn.me

before_install:
- sudo composer self-update

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


before_script:
- 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

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

The .travis.yml:

language: php

sudo: true

php:
  - 5.5
  - 5.6
  - 7.0
  - hhvm

addons:
  hosts:
    - system.hyn.me
    - tenant.hyn.me

install:
  # 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-oauth.github.com $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
  - export DB_USERNAME=travis DB_DATABASE=hyn DB_PASSWORD= QUEUE_DRIVER=sync

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

after_script:
  - 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/travis.sh

#!/bin/bash

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

cd ${TRAVIS_BUILD_DIR}/laravel

BRANCH_REGEX="^(([[:digit:]]+\.)+[[:digit:]]+)$"

if [[ ${TRAVIS_BRANCH} =~ $BRANCH_REGEX ]]; then
    echo "composer require ${TRAVIS_REPO_SLUG}:${TRAVIS_BRANCH}"
    composer require ${TRAVIS_REPO_SLUG}:${TRAVIS_BRANCH}
else
    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}"
fi

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

phpunit
# 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.

Comments