language: php

# The Travis CI container mode has random functional test fails, so we must use
# sudo here.
sudo: true

php:
  - 7.1
  - 7.3

services:
  - mysql

env:
  global:
    - MODULE=devel
  matrix:
    - DRUPAL_CORE=8.8.x
    - DRUPAL_CORE=8.9.x
    - DRUPAL_CORE=9.0.x

matrix:
  fast_finish: true
  # PHP7.1 is not supported from Core 9.0 onwards.
  exclude:
    - php: 7.1
      env: DRUPAL_CORE=9.0.x

# Be sure to cache composer downloads.
cache:
  directories:
    - $HOME/.composer

before_script:
  - echo $MODULE

  # Remove Xdebug as we don't need it and it causes
  # PHP Fatal error:  Maximum function nesting level of '256' reached.
  # We also don't care if that file exists or not on PHP 7.
  - phpenv config-rm xdebug.ini || true

  # Navigate out of module directory to prevent blown stack by recursive module
  # lookup.
  - cd ..

  # Create database.
  - mysql -e "create database $MODULE"
  # Export database variable for kernel tests.
  - export SIMPLETEST_DB=mysql://root:@127.0.0.1/$MODULE

  # Download Drupal core from the Github mirror because it is faster.
  - travis_retry git clone --branch $DRUPAL_CORE --depth 1 https://github.com/drupal/drupal.git
  - cd drupal
  # Store the path to Drupal root.
  - DRUPAL_ROOT=$(pwd)
  - echo $DRUPAL_ROOT

  # Make a directory for our module and copy the built source into it.
  - mkdir $DRUPAL_ROOT/modules/$MODULE
  - cp -R $TRAVIS_BUILD_DIR/* $DRUPAL_ROOT/modules/$MODULE/

  # Apply patch to reverse the addition of doctrine/debug in composer.json so that tests can run again.
  - cd $DRUPAL_ROOT/modules/$MODULE;
  - wget -q -O - https://www.drupal.org/files/issues/2020-04-17/3125678-9.remove-doctrine-common.patch | patch -p1 --verbose;
  - cd $DRUPAL_ROOT;

  # Run composer self-update and install.
  - travis_retry composer self-update && travis_retry composer install

  # Run composer update in the module directory in order to fetch dependencies.
  - travis_retry composer update -d $DRUPAL_ROOT/modules/$MODULE

  # Install drush
  - travis_retry composer require drush/drush:"^9.0 || ^10.0"

  # Coder is already installed as part of composer install. We just need to set
  # the installed_paths to pick up the Drupal standards.
  - $DRUPAL_ROOT/vendor/bin/phpcs --config-set installed_paths $DRUPAL_ROOT/vendor/drupal/coder/coder_sniffer

  # Start a web server on port 8888, run in the background.
  - php -S localhost:8888 &

  # Export web server URL for browser tests.
  - export SIMPLETEST_BASE_URL=http://localhost:8888

  # Interim patch for 9.0 only to avoid the Webprofiler toolbar halting the test run with 'class not found'.
  - if [ $DRUPAL_CORE == "9.0.x" ]; then
      cd $DRUPAL_ROOT/modules/$MODULE;
      wget -q -O - https://www.drupal.org/files/issues/2020-04-07/3097125-33.replace-JavascriptTestBase.patch | patch -p1 --verbose;
    fi

script:
  # Run the PHPUnit tests.
  - cd $DRUPAL_ROOT
  - ./vendor/bin/phpunit -c ./core/phpunit.xml.dist --verbose --group=devel,devel_generate,webprofiler ./modules/$MODULE

  # Check for coding standards. First change directory to our module.
  - cd $DRUPAL_ROOT/modules/$MODULE

  # List all the sniffs that were used.
  - $DRUPAL_ROOT/vendor/bin/phpcs --version
  - $DRUPAL_ROOT/vendor/bin/phpcs -i
  - $DRUPAL_ROOT/vendor/bin/phpcs -e

  # Show the violations in detail and do not fail for any errors or warnings.
  - $DRUPAL_ROOT/vendor/bin/phpcs -s --report-width=130 --colors --runtime-set ignore_warnings_on_exit 1 --runtime-set ignore_errors_on_exit 1 .

  # Run again to give a summary and total count.
  - $DRUPAL_ROOT/vendor/bin/phpcs --report-width=130 --colors --runtime-set ignore_warnings_on_exit 1 --runtime-set ignore_errors_on_exit 1 --report=summary .
