name: Test application

on:
    pull_request:
    push:
        branches:
            - '[0-9]+.[0-9]+'
            - '[0-9]+.x'

jobs:
    php:
        name: 'Run tests with php ${{ matrix.php-version }}, symfony ${{ matrix.symfony-version }}'
        runs-on: ubuntu-latest

        strategy:
            fail-fast: false
            matrix:
                include:
                    - php-version: '8.0'
                      lint: false
                      symfony-version: '^2.8'

                    - php-version: '8.0'
                      lint: false
                      symfony-version: '^3.4'

                    - php-version: '8.0'
                      lint: false
                      symfony-version: '^4.4'

                    - php-version: '8.0'
                      lint: true
                      symfony-version: '^4.4'

                    - php-version: '8.1'
                      lint: true
                      symfony-version: '^5.4'

                    - php-version: '8.1'
                      lint: true
                      symfony-version: '^5.4'

                    - php-version: '8.0'
                      lint: true
                      symfony-version: '^6.0'

                    - php-version: '8.1'
                      lint: true
                      symfony-version: '^6.0'

        services:
            elasticsearch:
                image: elasticsearch:8.0.0
                ports:
                    - 9200:9200
                env:
                    discovery.type: 'single-node'
                    xpack.security.enabled: 'false'
                options: --health-cmd="curl http://localhost:9200/_cluster/health" --health-interval=10s --health-timeout=5s --health-retries=5

        steps:
            - name: Checkout project
              uses: actions/checkout@v2

            - name: Install and configure PHP
              uses: shivammathur/setup-php@v2
              with:
                  php-version: ${{ matrix.php-version }}
                  tools: 'composer'

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

            - name: Cache dependencies
              uses: actions/cache@v2
              id: composer-cache
              with:
                  path: ${{ steps.composer-cache-dir.outputs.dir }}
                  key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }}
                  restore-keys: |
                      ${{ runner.os }}-composer-

            - name: Install dependencies
              run: |
                  composer validate --strict
                  composer require --no-update symfony/serializer:${{ matrix.symfony-version }}
                  composer install --no-interaction --prefer-dist

            - name: Fix code style
              if: ${{ matrix.lint }}
              run: vendor/bin/phpcs -p --standard=PSR2 --ignore=vendor/ ./

            - name: Run tests
              run: vendor/bin/phpunit --coverage-clover=coverage.xml

            - name: Code Coverage Upload
              run: curl -s https://codecov.io/bash
