language: php

dist: trusty

sudo: required

before_install:
 - sudo apt-get update
 - sudo apt-get install chromium-chromedriver

cache:
  directories:
    - $HOME/.composer/cache/files

addons:
  apt:
    packages:
      - tidy

env:
  global:
    - TRAVIS_NODE_VERSION="6"
    - COMPOSER_ROOT_VERSION="4.1.x-dev"
    - DISPLAY=":99"
    - XVFBARGS=":99 -ac -screen 0 1024x768x16"
    - SS_BASE_URL="http://localhost:8080/"
    - SS_ENVIRONMENT_TYPE="dev"

matrix:
  fast_finish: true
  include:
    - php: 5.6
      env: DB=MYSQL PHPUNIT_TEST=1 PHPCS_TEST=1
    - php: 7.0
      env: DB=PGSQL PHPUNIT_TEST=1
    - php: 7.1.2
      env: DB=MYSQL PDO=1 PHPUNIT_TEST=1
    - php: 7.0
      env: DB=MYSQL BEHAT_TEST=1
    - php: 7.0
      env: NPM_TEST=1

before_script:
# Extra $PATH
  - export PATH=/usr/lib/chromium-browser/:$PATH

# Init PHP
  - phpenv rehash
  - phpenv config-rm xdebug.ini
  - export PATH=~/.composer/vendor/bin:$PATH
  - echo 'memory_limit = 2G' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini

# Install composer dependencies
  - composer validate
  - composer require --no-update silverstripe/recipe-testing:^1 silverstripe/recipe-cms:1.1.x-dev
  - if [[ $DB == PGSQL ]]; then composer require --no-update silverstripe/postgresql:2.0.x-dev; fi
  - composer install --prefer-source --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile

# Install NPM dependencies
  - if [[ $NPM_TEST ]]; then nvm install $TRAVIS_NODE_VERSION && nvm use $TRAVIS_NODE_VERSION && npm install -g yarn && yarn install --network-concurrency 1 && yarn run build; fi

# Start behat services
  - if [[ $BEHAT_TEST ]]; then mkdir artifacts; fi
  - if [[ $BEHAT_TEST ]]; then cp composer.lock artifacts/; fi
  - if [[ $BEHAT_TEST ]]; then sh -e /etc/init.d/xvfb start; sleep 3; fi
  - if [[ $BEHAT_TEST ]]; then (chromedriver > artifacts/chromedriver.log 2>&1 &); fi
  - if [[ $BEHAT_TEST ]]; then (vendor/bin/serve --bootstrap-file tests/behat/serve-bootstrap.php &> artifacts/serve.log &); fi

script:
 - if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit tests/php/ flush=1; fi
 - if [[ $BEHAT_TEST ]]; then vendor/bin/behat @cms; fi
 - if [[ $NPM_TEST ]]; then git diff-files --quiet -w --relative=client; fi
 - if [[ $NPM_TEST ]]; then git diff -w --no-color --relative=client; fi
 - if [[ $NPM_TEST ]]; then yarn run lint; fi
 - if [[ $NPM_TEST ]]; then yarn run test; fi
 - if [[ $PHPCS_TEST ]]; then composer run-script lint; fi

after_success:
  - if [[ $PHPUNIT_COVERAGE_TEST ]]; then bash <(curl -s https://codecov.io/bash) -f coverage.xml; fi

after_failure:
  - if [[ $BEHAT_TEST ]]; then php ./vendor/silverstripe/framework/tests/behat/travis-upload-artifacts.php --if-env BEHAT_TEST,ARTIFACTS_BUCKET,ARTIFACTS_KEY,ARTIFACTS_SECRET --target-path $TRAVIS_REPO_SLUG/$TRAVIS_BUILD_ID/$TRAVIS_JOB_ID --artifacts-base-url https://s3.amazonaws.com/$ARTIFACTS_BUCKET/ --artifacts-path ./artifacts/; fi