name: Tests
on: [push]
jobs:
  run:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        php: ['7.4', '8.0', '8.1']
        laravel: [7.*, 8.*, 9.*]
        dependency-version: [prefer-stable]
        include:
          - laravel: 9.*
            testbench: 7.*
          - laravel: 8.*
            testbench: 6.*
          - laravel: 7.*
            testbench: 5.*
        exclude:
          - laravel: 9.*
            php: 7.4
          - laravel: 7.*
            php: 8.1

    name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Setup PHP
        uses: shivammathur/setup-php@2.21.0
        with:
          php-version: ${{ matrix.php }}
          coverage: xdebug

      - name: Get Composer Cache Directory
        id: composer-cache
        run: echo "::set-output name=dir::$(composer config cache-files-dir)"

      - name: Cache dependencies
        uses: actions/cache@v3
        with:
          path: ~/.composer/cache/files
          key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}

      - name: Install dependencies
        run: |
          composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
          composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
        env:
          APP_ENV: testing

      - name: Run tests
        run: composer run test
        env:
          APP_ENV: testing

      - uses: codecov/codecov-action@v3
        name: Push coverage to codecov
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          file: ./clover.xml
          flags: unittests
          name: codecov-umbrella
          fail_ci_if_error: true
