name: Tests

on: [ pull_request, push ]

jobs:
  ubuntu:
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        php: [ '8.1', '8.2', '8.3' ]
        carbon: [ '^2.71.0', '^3.0.0' ]
        setup: [ 'lowest', 'stable' ]

    name: PHP ${{ matrix.php }} - Carbon ${{ matrix.carbon }} - ${{ matrix.setup }}

    steps:
      - name: Checkout the code
        uses: actions/checkout@v3

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          tools: composer:v2
          coverage: none

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

      - name: Install dependencies
        if: steps.composer-cache.outputs.cache-hit != 'true'
        uses: nick-fields/retry@v2
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: |
            composer require --no-update --dev nesbot/carbon:${{ matrix.carbon }} doctrine/dbal:${{ matrix.dbal }}
            composer update --prefer-dist --no-progress --prefer-${{ matrix.setup }} ${{ matrix.dbal == '2.10.2' && '--ignore-platform-req=php' || '' }}

      - name: Run test suite
        run: vendor/bin/phpunit --no-coverage --colors=always
