name: "Continuous Integration"

on:
  push:
    paths-ignore:
      - 'README.md'
  pull_request:
    paths-ignore:
      - 'README.md'

env:
  COMPOSER_FLAGS: "--ansi --no-interaction --no-progress --prefer-dist"
  COVERAGE_FLAGS: "--coverage-text --coverage-clover build/logs/clover.xml"

jobs:
  tests:
    name: PHP ${{ matrix.php-version }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        php-version:
          - "7.2"
          - "7.3"
          - "7.4"
          - "8.0"
          - "8.1"
          - "8.2"
          - "8.3"
        include:
          - php-version: 8.2
            coverage: coverage
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Setup PHP, with composer and extensions
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php-version }}
          extensions: dom
          coverage: xdebug
        env:
          update: true
          COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Setup Problem Matchers for PHPUnit
        run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
      - name: Get composer cache directory
        id: composer-cache
        run: echo "::set-output name=dir::$(composer config cache-files-dir)"
      - name: Cache composer dependencies
        uses: actions/cache@v1
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.json') }}
          restore-keys: ${{ matrix.php-version }}-composer-
      - name: Install Composer
        uses: nick-invision/retry@v1
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: composer update ${{ env.COMPOSER_FLAGS }}
      - name: composer validate
        run: composer validate --strict
      - name: Test with phpunit (with coverage)
        id: phpunit-coverage
        if: "matrix.coverage == 'coverage'"
        run: composer run test -- --verbose ${{ env.COVERAGE_FLAGS }}
      - name: Test with phpunit (without coverage)
        if: "matrix.coverage != 'coverage'"
        run: composer run test -- --verbose
      - name: Submit Coveralls
        if: steps.phpunit-coverage.outcome == 'success'
        uses: nick-invision/retry@v1
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: |
            [[ -f vendor/bin/php-coveralls ]] && COVERALLS_PATH=vendor/bin/php-coveralls || COVERALLS_PATH=vendor/bin/coveralls
            [[ -n $COVERALLS_REPO_TOKEN ]] && php $COVERALLS_PATH -v --exclude-no-stmt || true
        env:
          COVERALLS_RUN_LOCALLY: 1
          COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
