name: CI

on: ["push", "pull_request"]

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

jobs:
    analyze:
        name: Analyze
        runs-on: ubuntu-latest
        container:
            image: php:7.4-alpine
            options: >-
              --tmpfs /tmp:exec
              --tmpfs /var/tmp:exec
        steps:
            - name: Checkout
              uses: actions/checkout@v2
            - name: Install Composer
              run: wget -qO - https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --quiet
            - name: Cache Composer dependencies
              uses: actions/cache@v1
              with:
                path: ~/.composer/cache
                key: composer-php${{ matrix.php }}-highest-
                restore-keys: |
                   composer-php${{ matrix.php }}-highest-
                   composer-
            - name: Validate Composer
              run: composer validate
            - name: Install xsl PHP extension
              run: |
                apk add $PHPIZE_DEPS libxslt-dev
                docker-php-ext-install xsl
            - name: Install highest dependencies with Composer
              run: composer update --no-progress --ansi
            - name: Disable PHP memory limit
              run: echo 'memory_limit=-1' >> /usr/local/etc/php/php.ini
            - name: Analyze
              run: vendor/bin/phpqa --execution no-parallel --progress-delay=-1 --ansi

    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:
                    - '7.2'
                    - '7.3'
                    - '7.4'
                    - '8.0'
                    - '8.1'
                dependencies:
                    - lowest
                    - highest
                include:
                    - php: '7.2'
                      phpunit-version: 8.5
                    - php: '7.3'
                      phpunit-version: 9.5
                    - php: '7.4'
                      phpunit-version: 9.5
                    - php: '8.0'
                      phpunit-version: 9.5
                    - php: '8.1'
                      phpunit-version: 9.5
            fail-fast: false
        steps:
            - name: Checkout
              uses: actions/checkout@v2
            - name: Install Composer
              run: wget -qO - https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --quiet
            - name: Cache Composer dependencies
              uses: actions/cache@v1
              with:
                path: ~/.composer/cache
                key: composer-php${{ matrix.php }}-${{ matrix.dependencies }}-
                restore-keys: |
                    composer-php${{ matrix.php }}-${{ matrix.dependencies }}-
                    composer-
            - name: Install xsl PHP extension
              run: |
                apk add $PHPIZE_DEPS libxslt-dev
                docker-php-ext-install xsl
            - name: Install lowest dependencies with Composer
              if: matrix.dependencies == 'lowest'
              run: composer update --no-progress --prefer-stable --prefer-lowest --ansi
            - name: Install highest dependencies with Composer
              if: matrix.dependencies == 'highest'
              run: composer update --no-progress --ansi
            - name: Run tests with PHPUnit
              env:
                SYMFONY_PHPUNIT_VERSION: ${{ matrix.phpunit-version }}
              run: vendor/bin/simple-phpunit --colors=always

    coverage:
        name: Coverage (PHP ${{ matrix.php }})
        runs-on: ubuntu-latest
        container:
            image: php:${{ matrix.php }}-alpine
            options: >-
              --tmpfs /tmp:exec
              --tmpfs /var/tmp:exec
        strategy:
            matrix:
                php:
                    - '7.4'
        steps:
            - name: Checkout
              uses: actions/checkout@v2
            - 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: Cache Composer dependencies
              uses: actions/cache@v1
              with:
                path: ~/.composer/cache
                key: composer-php${{ matrix.php }}-highest-
                restore-keys: |
                    composer-php${{ matrix.php }}-highest-
                    composer-
            - name: Install xsl PHP extension
              run: |
                apk add $PHPIZE_DEPS libxslt-dev
                docker-php-ext-install xsl
            - name: Install highest dependencies with Composer
              run: composer update --no-progress --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@v1.0.3      # 1.0.4+ uncompatible alpine :/
              with:
                token: ${{ secrets.CODECOV_TOKEN }}
