language: php
os: linux
dist: xenial

addons:
  postgresql: "9.6"

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

services:
  - mysql
  - docker

php:
  - 7.2
  - 7.3
  - 7.4

env:
  jobs:
    - DB=pgsql MOODLE_BRANCH=MOODLE_35_STABLE
    - DB=pgsql MOODLE_BRANCH=MOODLE_38_STABLE
    - DB=pgsql MOODLE_BRANCH=MOODLE_39_STABLE
    - DB=pgsql MOODLE_BRANCH=MOODLE_310_STABLE
    - DB=pgsql MOODLE_BRANCH=master
    - DB=mysqli MOODLE_BRANCH=MOODLE_35_STABLE
    - DB=mysqli MOODLE_BRANCH=MOODLE_38_STABLE
    - DB=mysqli MOODLE_BRANCH=MOODLE_39_STABLE
    - DB=mysqli MOODLE_BRANCH=MOODLE_310_STABLE
    - DB=mysqli MOODLE_BRANCH=master

before_install:
  - phpenv config-rm xdebug.ini
  - cd ../..
  - composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^3
  - export PATH="$(cd ci/bin; pwd):$(cd ci/vendor/bin; pwd):$PATH"

jobs:
  fast_finish: true
  include:
    # Prechecks against latest Moodle stable only.
    - stage: static
      php: 7.4
      env: DB=mysqli MOODLE_BRANCH=MOODLE_310_STABLE
      install:
        - moodle-plugin-ci install --no-init
      script:
        - moodle-plugin-ci phpdoc
        - moodle-plugin-ci phplint
        - moodle-plugin-ci phpcpd
        - moodle-plugin-ci phpmd
        - moodle-plugin-ci codechecker
        - moodle-plugin-ci validate
        - moodle-plugin-ci savepoints
        - moodle-plugin-ci mustache
        - moodle-plugin-ci grunt
    # Smaller build matrix for development builds
    - stage: develop
      php: 7.4
      env: DB=mysqli MOODLE_BRANCH=MOODLE_310_STABLE
  exclude:
    - php: 7.3
      env: DB=pgsql MOODLE_BRANCH=MOODLE_35_STABLE
    - php: 7.3
      env: DB=mysqli MOODLE_BRANCH=MOODLE_35_STABLE
    - php: 7.4
      env: DB=pgsql MOODLE_BRANCH=MOODLE_35_STABLE
    - php: 7.4
      env: DB=mysqli MOODLE_BRANCH=MOODLE_35_STABLE

# Unit tests and behat tests against full matrix.
install:
  - moodle-plugin-ci install
script:
  - moodle-plugin-ci phpunit --coverage-clover
  - moodle-plugin-ci behat
after_success:
  - bash <(curl -s https://codecov.io/bash)

stages:
  - static
  - name: develop
    if: branch != master AND (type != pull_request OR head_branch != master) AND (tag IS blank)
  - name: test
    if: branch = master OR (type = pull_request AND head_branch = master) OR (tag IS present)
