name: CI

on:
  - pull_request
  - push

permissions:
  contents: read # to fetch code (actions/checkout)

concurrency:
  group: ci-${{ github.head_ref || github.run_id }} # will be canceled on subsequent pushes in pull requests but not branches
  cancel-in-progress: true

env:
  PHP_MAX: '8.2'
  PHP_MIN: '7.4'

jobs:
  tests:
    strategy:
      fail-fast: false
      matrix:
        include:
          - operating-system: 'ubuntu-20.04'
            php-version: '7.4'
            job-description: 'Fixer with lowest deps'
            run-fixer: 'yes'
            composer-flags: '--prefer-lowest' # should be checked on the lowest supported PHP version
            execute-flex-with-symfony-version: '^5' # explicit check for Symfony 5.x compatibility

          - operating-system: 'ubuntu-20.04'
            php-version: '7.4'
            job-description: 'tests with lowest deps'
            run-tests: 'yes'
            composer-flags: '--prefer-lowest' # should be checked on the lowest supported PHP version
            execute-flex-with-symfony-version: '^5' # explicit check for Symfony 5.x compatibility

          - operating-system: 'ubuntu-20.04'
            php-version: '8.0'
            job-description: 'Fixer'
            run-fixer: 'yes'

          - operating-system: 'ubuntu-20.04'
            php-version: '8.0'
            job-description: 'tests'
            run-tests: 'yes'

          - operating-system: 'ubuntu-20.04'
            php-version: '8.1'
            job-description: 'Fixer with Symfony ^6'
            run-fixer: 'yes'
            execute-flex-with-symfony-version: '^6' # explicit check for Symfony 6.x compatibility

          - operating-system: 'ubuntu-20.04'
            php-version: '8.1'
            job-description: 'tests with Symfony ^6'
            run-tests: 'yes'
            execute-flex-with-symfony-version: '^6' # explicit check for Symfony 6.x compatibility

          - operating-system: 'ubuntu-20.04'
            php-version: '8.2'
            job-description: 'Fixer'
            run-fixer: 'yes'

          - operating-system: 'ubuntu-20.04'
            php-version: '8.2'
            job-description: 'Fixer with migration rules'
            run-fixer: 'yes'
            run-migration-rules: 'yes' # should be checked on the highest supported PHP version

          - operating-system: 'ubuntu-20.04'
            php-version: '8.2'
            job-description: 'tests'
            run-tests: 'yes'

          - operating-system: 'ubuntu-20.04'
            php-version: '8.2'
            job-description: 'tests with migration rules'
            run-tests: 'yes'
            run-migration-rules: 'yes' # should be checked on the highest supported PHP version

          - operating-system: 'ubuntu-20.04'
            php-version: '8.2'
            job-description: 'code coverage'
            run-tests: 'yes'
            collect-code-coverage: 'yes'

          - operating-system: 'windows-latest'
            php-version: '8.2'
            job-description: 'Fixer on Windows'
            run-fixer: 'yes'
            FAST_LINT_TEST_CASES: 1 # we need full syntax check on one job at least, no need to do it on additional

          - operating-system: 'windows-latest'
            php-version: '8.2'
            job-description: 'tests on Windows'
            run-tests: 'yes'
            FAST_LINT_TEST_CASES: 1 # we need full syntax check on one job at least, no need to do it on additional systems

          - operating-system: 'macos-latest'
            php-version: '8.2'
            job-description: 'Fixer on macOS'
            run-fixer: 'yes'
            FAST_LINT_TEST_CASES: 1 # we need full syntax check on one job at least, no need to do it on additional systems

          - operating-system: 'macos-latest'
            php-version: '8.2'
            job-description: 'tests on macOS'
            run-tests: 'yes'
            FAST_LINT_TEST_CASES: 1 # we need full syntax check on one job at least, no need to do it on additional

          - operating-system: 'ubuntu-22.04'
            php-version: '8.3'
            job-description: 'Fixer'
            run-fixer: 'yes'
            composer-flags: '--ignore-platform-req=PHP'
            PHP_CS_FIXER_IGNORE_ENV: 1

          - operating-system: 'ubuntu-22.04'
            php-version: '8.3'
            job-description: 'tests'
            run-tests: 'yes'
            composer-flags: '--ignore-platform-req=PHP'
            PHP_CS_FIXER_IGNORE_ENV: 1

    name: PHP ${{ matrix.php-version }} ${{ matrix.job-description }}

    runs-on: ${{ matrix.operating-system }}

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

      - name: Setup PHP with Composer deps
        uses: ./.github/composite-actions/setup-php-with-composer-deps
        with:
          os: ${{ runner.os }}
          php: ${{ matrix.php-version }}
          php-coverage: ${{ matrix.collect-code-coverage }}
          composer-flags: ${{ matrix.composer-flags }}
          composer-flex-with-symfony-version: ${{ matrix.execute-flex-with-symfony-version }}

      # Execute migration rules before running tests and self-fixing,
      # so we know that our codebase is future-ready.
      # Should be checked on the highest supported PHP version.
      - name: Run PHP CS Fixer with migration rules
        if: env.PHP_MAX == matrix.php-version && matrix.run-migration-rules == 'yes'
        env:
          PHP_CS_FIXER_FUTURE_MODE: 1
        run: php php-cs-fixer fix --config .php-cs-fixer.php-highest.php -q

      - name: Disable time limit for tests when collecting coverage
        if: matrix.collect-code-coverage == 'yes'
        run: sed 's/enforceTimeLimit="true"/enforceTimeLimit="false"/g' phpunit.xml.dist > phpunit.xml

      - name: Run tests
        if: matrix.run-tests == 'yes' && matrix.collect-code-coverage != 'yes'
        env:
          PHP_CS_FIXER_IGNORE_ENV: ${{ matrix.PHP_CS_FIXER_IGNORE_ENV }}
          FAST_LINT_TEST_CASES: ${{ matrix.FAST_LINT_TEST_CASES }}
        run: vendor/bin/paraunit run --testsuite unit,integration

      - name: Run tests and collect code coverage
        if: matrix.run-tests == 'yes' && matrix.collect-code-coverage == 'yes'
        env:
          FAST_LINT_TEST_CASES: 1
        run: vendor/bin/paraunit coverage --testsuite unit --exclude-group covers-nothing --clover build/logs/clover.xml

      - name: Upload coverage results to Coveralls
        if: matrix.run-tests == 'yes' && matrix.collect-code-coverage == 'yes'
        env:
          COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: php vendor/bin/php-coveralls --verbose

      - name: Run PHP CS Fixer
        if: matrix.run-fixer == 'yes'
        env:
          PHP_CS_FIXER_IGNORE_ENV: ${{ matrix.PHP_CS_FIXER_IGNORE_ENV }}
          PHP_CS_FIXER_FUTURE_MODE: 1
        run: php php-cs-fixer check --diff -v

      # Should be checked on the lowest supported PHP version.
      # If any type can be converted from PHPDoc to native type on lowest supported PHP, we should commit such change.
      - name: Run PHP CS Fixer with PHPDoc to type rules
        if: env.PHP_MIN == matrix.php-version && matrix.run-fixer == 'yes'
        env:
          PHP_CS_FIXER_FUTURE_MODE: 1
        run: php php-cs-fixer check --diff -v --config .php-cs-fixer.php-lowest.php

  deployment:
    needs: tests
    strategy:
      fail-fast: false
      matrix:
        include:
          - operating-system: 'ubuntu-20.04'
            php-version: '8.2'

    name: Deployment checks

    runs-on: ${{ matrix.operating-system }}

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

      - name: Setup PHP with Composer deps
        uses: ./.github/composite-actions/setup-php-with-composer-deps
        with:
          os: ${{ runner.os }}
          php: ${{ matrix.php-version }}

      - name: Build phar
        run: ./dev-tools/build.sh

      - name: Run smoke tests
        env:
          FAST_LINT_TEST_CASES: 1
        run: vendor/bin/phpunit --testsuite smoke
