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.3'
  PHP_MIN: '7.4'

jobs:
  entrypoint-low-php-compat:
    name: Entrypoint low-PHP compat
    runs-on: 'ubuntu-24.04'
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup PHP
        uses: ./.github/composite-actions/setup-php
        with:
          version: 7.0

      - name: Verify entrypoint file(s) can be run on low-end unsupported runtime
        run: php -l $(cat composer.json | jq -r '.bin[]')

  tests:
    continue-on-error: ${{ matrix.operating-system == 'macos-latest' }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - operating-system: 'ubuntu-24.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-24.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-24.04'
            php-version: '8.0'
            job-description: 'Fixer'
            run-fixer: 'yes'

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

          - operating-system: 'ubuntu-24.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-24.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-24.04'
            php-version: '8.2'
            job-description: 'Fixer'
            run-fixer: 'yes'

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

          - operating-system: 'ubuntu-24.04'
            php-version: '8.3'
            job-description: 'Fixer'
            run-fixer: 'yes'

          - operating-system: 'ubuntu-24.04'
            php-version: '8.3'
            job-description: 'tests'
            run-tests: 'yes'

          - operating-system: 'ubuntu-24.04'
            php-version: '8.3'
            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-24.04'
            php-version: '8.3'
            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-24.04'
            php-version: '8.3'
            job-description: 'Fixer with Symfony ^7'
            run-fixer: 'yes'
            execute-flex-with-symfony-version: '^7' # explicit check for Symfony 7.x compatibility

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

          - operating-system: 'ubuntu-24.04'
            php-version: '8.3'
            job-description: 'mutation tests'
            run-mutation-tests: 'yes'

          - operating-system: 'windows-latest'
            php-version: '8.3'
            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.3'
            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.3'
            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.3'
            job-description: 'tests on macOS'
            run-tests: 'yes'
            AVOID_PARAUNIT: 'yes' # macOS job is having weird issue on CI under ParaUnit
            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-24.04'
            php-version: '8.4'
            job-description: 'Fixer'
            run-fixer: 'yes'
            PHP_CS_FIXER_IGNORE_ENV: 1

          - operating-system: 'ubuntu-24.04'
            php-version: '8.4'
            job-description: 'tests'
            run-tests: 'yes'
            PHP_CS_FIXER_IGNORE_ENV: 1

          - operating-system: 'ubuntu-24.04'
            php-version: '8.4'
            job-description: 'tests using PHPUnit 12'
            install-phpunit-12: 'yes'
            run-tests: 'yes'
            PHP_CS_FIXER_IGNORE_ENV: 1

          - operating-system: 'ubuntu-24.04'
            php-version: '8.4'
            job-description: 'code coverage'
            run-tests: 'yes'
            collect-code-coverage: 'yes'
            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@v4

      - name: Remove Infection requirement if not needed
        # TODO: Fix it properly. It somehow stopped working on MacOS (Composer not available)
        if: matrix.operating-system != 'macos-latest' && matrix.run-mutation-tests != 'yes'
        run: composer remove --dev infection/infection --no-update

      - 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 || matrix.run-mutation-tests }}
          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 --diff --config .php-cs-fixer.php-highest.php

      - name: Custom PHPUnit config for code coverage
        if: matrix.collect-code-coverage == 'yes' || matrix.run-mutation-tests
        run: sed -e 's/enforceTimeLimit="true"/enforceTimeLimit="false"/g' -e 's/coverage/source/g' phpunit.xml.dist > phpunit.xml

      - name: Disable time limit for tests under Windows # due to https://github.com/sebastianbergmann/phpunit/issues/5589
        if: matrix.operating-system == 'windows-latest'
        run: (Get-Content phpunit.xml.dist) -replace 'enforceTimeLimit="true"', 'enforceTimeLimit="false"' | Out-File phpunit.xml

      - name: Fix PHP 8.4 deprecations for tests
        if: matrix.run-tests == 'yes' && matrix.php-version == '8.4'
        env:
          PHP_CS_FIXER_IGNORE_ENV: ${{ matrix.PHP_CS_FIXER_IGNORE_ENV }}
        run: php php-cs-fixer fix --rules=no_unreachable_default_argument_value,nullable_type_declaration_for_default_null_value --diff vendor

      - name: Install PHPUnit 12
        if: matrix.run-tests == 'yes' && matrix.install-phpunit-12 == 'yes'
        env:
          PHP_CS_FIXER_IGNORE_ENV: ${{ matrix.PHP_CS_FIXER_IGNORE_ENV }}
          FAST_LINT_TEST_CASES: ${{ matrix.FAST_LINT_TEST_CASES }}
        run: |
          composer require --dev --with-all-dependencies phpunit/phpunit:^12.0.2
          php php-cs-fixer fix --quiet --rules=php_unit_attributes

      - name: Run tests (via PHPUnit)
        if: matrix.run-tests == 'yes' && matrix.collect-code-coverage != 'yes' && matrix.AVOID_PARAUNIT == '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/phpunit --testsuite unit,integration

      - name: Run tests (via ParaUnit)
        if: matrix.run-tests == 'yes' && matrix.collect-code-coverage != 'yes' && matrix.AVOID_PARAUNIT != '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 with "short_open_tag" enabled
        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: composer test:short-open-tag

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

      - name: Run mutation tests (Infection)
        # Run only on pull requests because we run mutations tests only for diff between branches
        if: github.event_name == 'pull_request' && matrix.run-mutation-tests == 'yes'
        env:
          FAST_LINT_TEST_CASES: 1
        run: |
          git fetch origin $GITHUB_BASE_REF
          vendor/bin/infection --threads=max --map-source-class-to-test --git-diff-lines --git-diff-base=origin/$GITHUB_BASE_REF --ignore-msi-with-no-mutations --only-covered

      - 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 -vvv --config .php-cs-fixer.php-lowest.php

  deployment:
    needs: tests
    name: Deployment checks

    runs-on: 'ubuntu-24.04'
    env:
      php-version: '8.3'

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

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

      - name: Cache dev-tools
        uses: actions/cache@v4
        with:
          path: dev-tools/bin/
          key: Build-${{ hashFiles('dev-tools/build.sh') }}

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

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