作曲家包的Travis配置

时间:2015-06-17 06:28:42

标签: php unit-testing package laravel-5 travis-ci

在使用laravel 5.1+软件包时,我需要通过travis-ci.org运行自动化测试。与常规自动化测试的区别在于需要将此包包含到框架中并设置特定的配置选项以运行测试。

所以要求是:

  • 安装laravel
  • 将我的包添加为依赖
  • 设置一些特拉维斯特定的配置,如travis数据库访问
  • 运行laravel的迁移
  • 运行特定于程序包的迁移或运行artisan命令
  • 运行包特定单元测试
我到处搜寻;在laravel forums询问,在travis社区聊天中询问并看到this topic因为过于本地化而被关闭(尽管答案现在肯定会有所帮助)。我希望我的问题能够保持开放。

此时我有以下配置:

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

然而,我对travis的了解(到目前为止)是有限的,在我发送不必要的数量的提交来解决我的问题之前,我宁愿你对测试集成到框架中的好方法有什么看法。 / p>

聚苯乙烯。这涉及到包hyn/multi-tenant

建议如何尽可能保持这个问题的通用性会有所帮助。我希望在没有明确提及最佳实践的情况下,请求集成到框架示例中有助于定义答案的范围。

1 个答案:

答案 0 :(得分:2)

因此,经过几周的提交进入travis,我终于完成了这项工作。

.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

脚本/ 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

由于新的Laravel版本或travis中的更改,此代码可能会更改。如果是这种情况,您将找到最新版本here