sudo: required

services:
  - docker

notifications:
  email:
    on_success: never
    on_failure: never

branches:
  only:
    - master
    - develop

cache:
  apt: true

# Set the global environment variables
env:
  global:
    - COVERAGE: false
  matrix:
    - WP_MULTISITE=1 PHP_VERSION=5.6 WP_VERSION=4.7.1
    - WP_MULTISITE=1 PHP_VERSION=5.6 WP_VERSION=4.9.4
    - WP_MULTISITE=1 PHP_VERSION=7.0 WP_VERSION=4.7.1
    - WP_MULTISITE=1 PHP_VERSION=7.0 WP_VERSION=4.9.4
    - WP_MULTISITE=1 PHP_VERSION=7.1 WP_VERSION=4.7.1
    - WP_MULTISITE=1 PHP_VERSION=7.1 WP_VERSION=4.9.4
    - PHP_VERSION=5.6 WP_VERSION=4.7.1
    - PHP_VERSION=5.6 WP_VERSION=4.9.4
    - PHP_VERSION=7.0 WP_VERSION=4.7.1
    - PHP_VERSION=7.0 WP_VERSION=4.9.4
    - PHP_VERSION=7.1 WP_VERSION=4.7.1
    - PHP_VERSION=7.1 WP_VERSION=4.9.4 APIGEN_DOCS=true COVERAGE=true LINT_SCHEMA=true

install:
  - docker run --rm -v $(pwd):/app composer install --ignore-platform-reqs
  - docker-compose build

before_script:
  - docker-compose up -d
  - docker-compose run --rm tests ./bin/install-wp-tests.sh ignored root testing mysql_test $WP_VERSION true

script:
  - docker-compose run --rm tests ./vendor/bin/codecept run acceptance --env docker
  - docker-compose run --rm tests ./vendor/bin/codecept run functional --env docker
  - |
    if [ $COVERAGE == true ]; then
      docker-compose run --rm tests phpdbg -qrr ./vendor/bin/codecept run wpunit --env docker --coverage --coverage-xml
    else
      docker-compose run --rm tests ./vendor/bin/codecept run wpunit --env docker
    fi

after_success:
  # Install coveralls.phar
  # Upload coverage to coveralls
  - |
    if [ $COVERAGE == true ]; then
      wget -c -nc --retry-connrefused --tries=0 https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar
      chmod +x coveralls.phar
      php coveralls.phar --version
      travis_retry php coveralls.phar -v
    fi
  # Install GraphQL Schema Linter
  # Move to the WordPress Install
  # Generate the Static Schema
  # Lint the Schema
  - |
    if [ $LINT_SCHEMA == true ]; then
      npm install -g graphql-schema-linter
      cd $WP_CORE_DIR
      wp graphql generate-static-schema
      cd $WP_CORE_DIR/wp-content/plugins/wp-graphql
      graphql-schema-linter ./schema.graphql
    fi
  # Generate the APIGen Docs
  #  - |
  #    if [ $APIGEN_DOCS == true ]; then
  #      cd $TRAVIS_BUILD_DIR
  #      vendor/bin/apigen generate --source="src" --destination $TRAVIS_BUILD_DIR/apigen
  #    fi

deploy:
  # Deploy the Generated API docs to the wp-graphql-api-docs repo
  - provider: pages
    # This is set in the Travis CI dashboard
    github_token: $APIGEN_GITHUB_TOKEN
    local_dir: $TRAVIS_BUILD_DIR/apigen
    repo: wp-graphql/wp-graphql-api-docs
    target_branch: master
    # Make sure this is true to ensure the files that were built in the previous step are not cleaned up before deploy
    skip_cleanup: true
    on:
      # Probably switch this to master?
      all_branches: develop
      # Only deploy when APIGEN_DOCS are generated
      condition: $APIGEN_DOCS == true

after_script:
  - docker-compose down
