name: PHPUnit tests

on:
  push:
  release:
    types:
      - published

jobs:
  php-tests:
    runs-on: ${{ matrix.os }}

    strategy:
      fail-fast: false
      matrix:
        php: [8.0, 8.1, 8.2, 8.3, 8.4]
        laravel: [9.*, 10.*, 11.*, 12.*]
        os: [ubuntu-latest]
        coverage: [none]
        stability: [prefer-lowest, prefer-stable]
        include:
          - php: 8.4
            laravel: 12.*
            os: ubuntu-latest
            coverage: xdebug
            stability: prefer-stable
        exclude:
          - php: 8.0
            laravel: 10.*
          - php: 8.0
            laravel: 11.*
          - php: 8.1
            laravel: 11.*
          - php: 8.0
            laravel: 12.*
          - php: 8.1
            laravel: 12.*

    name: PHP${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}

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

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: mbstring json openssl
          coverage: ${{ matrix.coverage }}
          ini-values: memory_limit=-1

      - name: Get composer cache directory
        id: composer-cache
        run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

      - name: Cache dependencies
        uses: actions/cache@v3
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: "php-${{ matrix.php }}-laravel-${{ matrix.laravel }}-${{ matrix.stability }}-${{ hashFiles('**/composer.json') }}"
          restore-keys: "php-${{ matrix.php }}-laravel-${{ matrix.laravel }}-${{ matrix.stability }}-${{ hashFiles('**/composer.json') }}"

      - name: Install dependencies
        run: |
          composer require "illuminate/contracts:${{ matrix.laravel }}" --no-interaction --no-progress --no-update
          composer update --${{ matrix.stability }} --no-progress --prefer-dist --no-interaction

      - name: Execute tests
        run: composer test:ci

      - name: Upload coverage
        uses: codecov/codecov-action@v1
        if: matrix.coverage == 'xdebug'
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          file: ./coverage.xml
          yml: ./codecov.yml
