name: CI

on: ["push", "pull_request"]

env:
    COMPOSER_ALLOW_SUPERUSER: '1'
    SYMFONY_DEPRECATIONS_HELPER: max[self]=0

jobs:
    analyze:
        name: Analyze
        runs-on: ubuntu-latest
        container:
            image: php:8.3-alpine
            options: >-
              --tmpfs /tmp:exec
              --tmpfs /var/tmp:exec
        steps:
            - name: Checkout
              uses: actions/checkout@v4
            - name: Install Composer
              run: wget -qO - https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --quiet
            - name: Get Composer Cache Directory
              id: composer-cache
              run: |
                echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
            - uses: actions/cache@v3
              with:
                path: ${{ steps.composer-cache.outputs.dir }}
                key: ${{ runner.os }}-composer-8.3-highest-${{ hashFiles('**/composer.json') }}
                restore-keys: |
                  ${{ runner.os }}-composer-8.3-highest
            - name: Validate Composer
              run: composer validate
            - name: Install highest dependencies with Composer
              run: composer update --no-progress --no-suggest --ansi
            - name: Disable PHP memory limit
              run: echo 'memory_limit=-1' >> /usr/local/etc/php/php.ini
            - name: Run CS-Fixer
              run: vendor/bin/php-cs-fixer fix --dry-run --diff --format=checkstyle

    phpunit:
        name: PHPUnit (PHP ${{ matrix.php }} Deps ${{ matrix.dependencies }})
        runs-on: ubuntu-latest
        container:
            image: php:${{ matrix.php }}-alpine
            options: >-
              --tmpfs /tmp:exec
              --tmpfs /var/tmp:exec
        strategy:
            matrix:
                php:
                    - '8.1'
                    - '8.2'
                    - '8.3'
                dependencies:
                    - 'lowest'
                    - 'highest'
                include:
                    - php: '8.1'
                      phpunit-version: 10
                    - php: '8.2'
                      phpunit-version: 10
                    - php: '8.3'
                      phpunit-version: 10
            fail-fast: false
        steps:
            - name: Checkout
              uses: actions/checkout@v4
            - name: Install Composer
              run: wget -qO - https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --quiet
            - name: Get Composer Cache Directory
              id: composer-cache
              run: |
                echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
            - uses: actions/cache@v3
              with:
                path: ${{ steps.composer-cache.outputs.dir }}
                key: ${{ runner.os }}-composer-${{ matrix.php }}-${{ matrix.dependencies }}-${{ hashFiles('**/composer.json') }}
                restore-keys: |
                  ${{ runner.os }}-composer-${{ matrix.php }}-${{ matrix.dependencies }}
            - name: Install lowest dependencies with Composer
              if: matrix.dependencies == 'lowest'
              run: composer update --no-progress --no-suggest --prefer-stable --prefer-lowest --ansi
            - name: Install highest dependencies with Composer
              if: matrix.dependencies == 'highest'
              run: composer update --no-progress --no-suggest --ansi
            - name: Run tests with PHPUnit
              env:
                SYMFONY_MAX_PHPUNIT_VERSION: ${{ matrix.phpunit-version }}
              run: vendor/bin/simple-phpunit --colors=always

    coverage:
        name: Coverage (PHP 8.3)
        runs-on: ubuntu-latest
        container:
            image: php:8.3-alpine
            options: >-
              --tmpfs /tmp:exec
              --tmpfs /var/tmp:exec
        steps:
            - name: Checkout
              uses: actions/checkout@v4
            - name: Install pcov PHP extension
              run: |
                apk add $PHPIZE_DEPS
                pecl install pcov
                docker-php-ext-enable pcov
            - name: Install Composer
              run: wget -qO - https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --quiet
            - name: Get Composer Cache Directory
              id: composer-cache
              run: |
                echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
            - uses: actions/cache@v3
              with:
                path: ${{ steps.composer-cache.outputs.dir }}
                key: ${{ runner.os }}-composer-8.3-highest-${{ hashFiles('**/composer.json') }}
                restore-keys: |
                  ${{ runner.os }}-composer-8.3-highest
            - name: Install highest dependencies with Composer
              run: composer update --no-progress --no-suggest --ansi
            - name: Run coverage with PHPUnit
              run: vendor/bin/simple-phpunit --coverage-clover ./coverage.xml --colors=always
            - name: Send code coverage report to Codecov.io
              uses: codecov/codecov-action@v3
              with:
                token: ${{ secrets.CODECOV_TOKEN }}
