name: Tests

on:
  push:
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        php: ['8.2', '8.3', '8.4', '8.5']

    name: PHP ${{ matrix.php }}

    steps:
      - name: Checkout
        uses: actions/checkout@v5

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: curl
          coverage: ${{ matrix.php == '8.5' && 'pcov' || 'none' }}
          tools: composer:v2

      - name: Cache Composer dependencies
        uses: actions/cache@v5
        with:
          path: ~/.composer/cache
          key: php-${{ matrix.php }}-composer-${{ hashFiles('composer.lock') }}
          restore-keys: php-${{ matrix.php }}-composer-

      - name: Install dependencies
        run: composer install --no-interaction --prefer-dist --no-progress --optimize-autoloader

      - name: Run tests
        run: ./vendor/bin/pest --ci ${{ matrix.php == '8.5' && '--coverage --coverage-clover coverage.xml' || '' }}

      - name: Upload coverage to Codecov
        if: matrix.php == '8.5'
        uses: codecov/codecov-action@v6
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: coverage.xml
          fail_ci_if_error: false
