name: PHPUnit

on:
  pull_request:
  push:
    branches:
      - 1.x
      - 2.x

jobs:
  phpunit:
    name: "PHP ${{ matrix.php }} + ${{ matrix.dependencies }} dependencies + Symfony ${{ matrix.symfony }}"
    runs-on: ubuntu-20.04
    strategy:
      fail-fast: false
      matrix:
        php: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
        dependencies: [highest]
        symfony: ['*']
        stability: ['stable']
        include:
          # Minimum supported dependencies with the oldest supported PHP version
          - php: '7.2'
            dependencies: lowest
            symfony: '*'
            stability: 'stable'

          # Minimum supported dependencies with the latest supported PHP version
          - php: '8.2'
            dependencies: lowest
            symfony: '*'
            stability: 'stable'

          # Test each supported Symfony version with the lowest supported PHP version
          - php: '7.2'
            dependencies: highest
            symfony: '3.4.*'
            stability: 'stable'

          - php: '7.2'
            dependencies: highest
            symfony: '4.4.*'
            stability: 'stable'

          - php: '7.2'
            dependencies: highest
            symfony: '5.3.*'
            stability: 'stable'

          # Test Symfony 6.4 dev version
          - php: '8.2'
            dependencies: highest
            symfony: '6.4.*'
            stability: 'dev'

          # Test Symfony 7.0 dev version
          - php: '8.2'
            dependencies: highest
            symfony: '7.0.*'
            stability: 'dev'
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Configure coverage driver
        id: coverage
        run: echo ::set-output name=driver::xdebug

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          coverage: ${{ steps.coverage.outputs.driver }}
          extensions: gd, imagick

      - name: Require Symfony version
        if: matrix.symfony != '*'
        run: |
          composer global config --no-plugins allow-plugins.symfony/flex true
          composer global require --no-interaction --no-progress symfony/flex
          composer config extra.symfony.require ${{ matrix.symfony }}

      - name: Remove non-compatible dependencies with Symfony 7
        if: matrix.symfony == '7.0.*'
        run: |
          composer remove enqueue/enqueue-bundle symfony/templating --dev --no-update

      - name: Set minimum-stability
        run: composer config minimum-stability ${{ matrix.stability }}

      # Incompatible with symfony/framework-bundle v3
      - name: Remove symfony/messenger
        if: ${{ matrix.php == '7.2' && matrix.symfony == '3.4.*' }}
        run: composer remove --dev --no-update symfony/messenger

      - name: Update project dependencies
        uses: ramsey/composer-install@v2
        with:
          dependency-versions: ${{ matrix.dependencies }}

      - name: Cache PHPUnit
        uses: actions/cache@v3
        with:
          path: vendor/bin/.phpunit
          key: ${{ runner.os }}-phpunit-${{ matrix.php }}

      - name: Install PHPUnit
        run: vendor/bin/simple-phpunit install

      - name: Run PHPUnit tests
        env:
          SYMFONY_DEPRECATIONS_HELPER: max[self]=0
        run: vendor/bin/simple-phpunit -v

      - name: Install php-coveralls
        run: composer global require --no-interaction --no-progress php-coveralls/php-coveralls

      - name: Upload coverage results to Coveralls
        uses: nick-invision/retry@v2
        env:
          COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          COVERALLS_PARALLEL: true
          COVERALLS_FLAG_NAME: "PHP ${{ matrix.php }} + ${{ matrix.dependencies }} dependencies + Symfony ${{ matrix.symfony }}"
        with:
          timeout_seconds: 60
          max_attempts: 3
          command: php-coveralls -x var/build/clover.xml -o var/build/upload.json -v

  coveralls-finish:
    needs: [phpunit]
    runs-on: ubuntu-20.04
    steps:
      - name: Notify Coveralls when build is finished
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          parallel-finished: true
