name: build
on:
  pull_request:
  push:
jobs:
  run:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        php-version: ["8.2", "8.3", "8.4"]
        extension: ["gd, :imagick", "imagick, :gd"] # the ':" prefix is used to unload the other extension
    name: PHP ${{ matrix.php-version }} - ${{ matrix.extension }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Check environment
        run: |
          php --version
          composer --version

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

      - name: Cache dependencies
        uses: actions/cache@v4
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ matrix.os }}-composer-${{ matrix.php-version }}-${{ hashFiles('**/composer.lock') }}
          restore-keys: ${{ matrix.os }}-composer-${{ matrix.php-version }}-

      - name: Install dependencies
        run: composer install --prefer-dist

      - name: Run tests
        env:
          PHP_CS_FIXER_IGNORE_ENV: 1
        run: |
          vendor/bin/phpunit --coverage-text --coverage-clover build/logs/clover.xml

      - name: Upload coverage to Codecov
        env: 
          OS: ${{ matrix.os }}
          PHP: ${{ matrix.php-version }}
        uses: codecov/codecov-action@v3
        with:
           file: build/logs/clover.xml
           env_vars: OS,PHP
