name: "Tests"

on:
    workflow_dispatch:
    pull_request: ~
    push:
        branches:
            - main

env:
    fail-fast: true

jobs:
    test:
        name: "PHP ${{ matrix.php-version }} tests (${{ matrix.description }})"
        runs-on: ubuntu-latest

        strategy:
            matrix:
                php-version: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4']
                symfony_constraint: [false]
                include:
                    - description: 'Symfony 3.4'
                      php-version: '7.1'
                      symfony_constraint: 3.4.*
                    - description: 'Symfony 4.4'
                      php-version: '7.3'
                      symfony_constraint: 4.4.*
                    - description: 'Symfony 5.4'
                      php-version: '8.0'
                      symfony_constraint: 5.4.*
                    - description: 'Symfony 6.4'
                      php-version: '8.2'
                      symfony_constraint: 6.4.*
                    - description: 'prefer lowest'
                      php-version: '7.2'
                      composer_option: '--prefer-lowest'

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

            - name: "Install PHP with extensions"
              uses: shivammathur/setup-php@v2
              with:
                  coverage: "none"
                  php-version: ${{ matrix.php-version }}

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

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

            - name: "Cache node"
              uses: actions/cache@v3
              with:
                  path: ~/.npm
                  key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
                  restore-keys: |
                      ${{ runner.os }}-node-

            - name: "Install symfony packages in specific version"
              run: composer require --no-update symfony/process:${{ matrix.symfony_constraint }} symfony/dependency-injection:${{ matrix.symfony_constraint }} symfony/twig-bundle:${{ matrix.symfony_constraint }}
              if: matrix.symfony_constraint

            - name: "Install PHP dependencies"
              run: composer update --no-progress --ansi ${{ matrix.composer_option }}

            - name: "Install MJML"
              run: npm install mjml

            - name: "Show MJML version"
              run: npx mjml --version

            - name: "Run tests"
              run: ./vendor/bin/phpunit
