name: Test

on:
  # Run on all pushes and on all pull requests.
  # Prevent the build from running when there are only irrelevant changes.
  push:
    paths-ignore:
      - '**.md'
  pull_request:
    paths-ignore:
      - '**.md'
  # Allow manually triggering the workflow.
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        php: ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2']

    name: "Build Phar on PHP: ${{ matrix.php }}"

    continue-on-error: ${{ matrix.php == '8.2' }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          coverage: none
          ini-values: phar.readonly=Off, error_reporting=-1, display_errors=On

      - name: Build the phar
        run: php scripts/build-phar.php

      - name: Upload the PHPCS phar
        uses: actions/upload-artifact@v3
        if: ${{ success() && matrix.php == '8.0' }}
        with:
          name: phpcs-phar
          path: ./phpcs.phar
          if-no-files-found: error
          retention-days: 28

      - name: Upload the PHPCBF phar
        uses: actions/upload-artifact@v3
        if: ${{ success() && matrix.php == '8.0' }}
        with:
          name: phpcbf-phar
          path: ./phpcbf.phar
          if-no-files-found: error
          retention-days: 28

      # Both the below only check a few files which are rarely changed and therefore unlikely to have issues.
      # This test is about testing that the phars are functional, *not* about whether the code style complies.
      - name: 'PHPCS: check code style using the Phar file to test the Phar is functional'
        run: php phpcs.phar ./scripts

      - name: 'PHPCBF: fix code style using the Phar file to test the Phar is functional'
        run: php phpcbf.phar ./scripts

  test:
    runs-on: ubuntu-latest
    needs: build

    strategy:
      # Keys:
      # - custom_ini: Whether to run with specific custom ini settings to hit very specific
      #   code conditions.
      matrix:
        php: ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
        custom_ini: [false]

        include:
          # Builds running the basic tests with different PHP ini settings.
          - php: '5.5'
            custom_ini: true
          - php: '7.0'
            custom_ini: true

    name: "PHP: ${{ matrix.php }} ${{ matrix.custom_ini && ' with custom ini settings' || '' }}"

    continue-on-error: ${{ matrix.php == '8.2' }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Setup ini config
        id: set_ini
        run: |
          # Set the "short_open_tag" ini to make sure specific conditions are tested.
          # Also turn on error_reporting to ensure all notices are shown.
          if [[ ${{ matrix.custom_ini }} == true && "${{ matrix.php }}" == '5.5' ]]; then
            echo '::set-output name=PHP_INI::error_reporting=-1, display_errors=On, date.timezone=Australia/Sydney, short_open_tag=On, asp_tags=On'
          elif [[ ${{ matrix.custom_ini }} == true && "${{ matrix.php }}" == '7.0' ]]; then
            echo '::set-output name=PHP_INI::error_reporting=-1, display_errors=On, date.timezone=Australia/Sydney, short_open_tag=On'
          else
            echo '::set-output name=PHP_INI::error_reporting=-1, display_errors=On'
          fi

      - name: Install PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          ini-values: ${{ steps.set_ini.outputs.PHP_INI }}
          coverage: none
          tools: cs2pr

      # Install dependencies and handle caching in one go.
      # @link https://github.com/marketplace/actions/install-composer-dependencies
      - name: Install Composer dependencies - normal
        if: ${{ matrix.php < '8.0' }}
        uses: "ramsey/composer-install@v2"

      # For PHP 8.0+, we need to install with ignore platform reqs as PHPUnit 7 is still used.
      - name: Install Composer dependencies - with ignore platform
        if: ${{ matrix.php >= '8.0' }}
        uses: "ramsey/composer-install@v2"
        with:
          composer-options: --ignore-platform-reqs

      # Note: The code style check is run multiple times against every PHP version
      # as it also acts as an integration test.
      - name: 'PHPCS: set the path to PHP'
        run: php bin/phpcs --config-set php_path php

      - name: 'PHPUnit: run the tests'
        if: ${{ matrix.php != '8.1' && matrix.php != '8.2' }}
        run: vendor/bin/phpunit tests/AllTests.php

      # We need to ignore the config file so that PHPUnit doesn't try to read it.
      # The config file causes an error on PHP 8.1+ with PHPunit 7, but it's not needed here anyway
      # as we can pass all required settings in the phpunit command.
      - name: 'PHPUnit: run the tests on PHP > 8.0'
        if: ${{ matrix.php == '8.1' || matrix.php == '8.2' }}
        run: vendor/bin/phpunit tests/AllTests.php --no-configuration --bootstrap=tests/bootstrap.php --dont-report-useless-tests

      - name: 'PHPCS: check code style without cache, no parallel'
        if: ${{ matrix.custom_ini == false && matrix.php != '7.4' }}
        run: php bin/phpcs --no-cache --parallel=1

      - name: 'PHPCS: check code style to show results in PR'
        if: ${{ matrix.custom_ini == false && matrix.php == '7.4' }}
        continue-on-error: true
        run: php bin/phpcs --no-cache --parallel=1 --report-full --report-checkstyle=./phpcs-report.xml

      - name: Show PHPCS results in PR
        if: ${{ matrix.custom_ini == false && matrix.php == '7.4' }}
        run: cs2pr ./phpcs-report.xml

      - name: 'Composer: validate config'
        if: ${{ matrix.custom_ini == false }}
        run: composer validate --no-check-all --strict

      - name: Download the PHPCS phar
        uses: actions/download-artifact@v3
        with:
          name: phpcs-phar

      # This test specifically tests that the Phar which will be released works correctly on all PHP versions.
      - name: 'PHPCS: check code style using the Phar file'
        if: ${{ matrix.custom_ini == false }}
        run: php phpcs.phar
