name: CI

on:
    pull_request:
    push:
        branches: [ master, main ]
    schedule:
        - cron: '45 4 1 * *'

jobs:
    tests:
        runs-on: ubuntu-latest
        strategy:
            fail-fast: false
            matrix:
                php:
                    - '8.1'
                    - '8.2'
                    - '8.3'
                    - '8.4'
                symfony-versions:
                    - false
                    - '6.4.*'
                    - '7.*.*'
                exclude:
                    - php: '8.1'
                      symfony-versions: '7.*.*'

        name: PHP ${{ matrix.php }} ${{ matrix.description }} - ${{ matrix.symfony-versions }}
        steps:
            - name: Checkout
              uses: actions/checkout@v4

            - uses: actions/cache@v4
              with:
                  path: ~/.composer/cache/files
                  key: ${{ matrix.php }}-${{ matrix.symfony-versions }}

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

            - name: Install dependencies
              run: composer install

            - name: Setup Symfony version
              run: |
                composer bin symfony require "symfony/framework-bundle:${{ matrix.symfony-versions }}" --no-update
                composer bin symfony require "symfony/serializer:${{ matrix.symfony-versions }}" --no-update
              if: matrix.symfony-versions

            - name: Install bin dependencies
              run: composer bin symfony install
              if: matrix.symfony-versions

            - name: Run PHPUnit tests
              run: bin/phpunit
              if: false == matrix.symfony-versions

            - name: Run Symfony tests
              run: bin/phpunit --configuration phpunit_symfony.xml.dist
              if: matrix.symfony-versions

    # This is a "trick", a meta task which does not change, and we can use in
    # the protected branch rules as opposed to the tests one above which
    # may change regularly.
    validate-tests:
        name: Tests status
        runs-on: ubuntu-latest
        needs:
            - tests
        if: always()
        steps:
            - name: Successful run
              if: ${{ !(contains(needs.*.result, 'failure')) }}
              run: exit 0

            - name: Failing run
              if: ${{ contains(needs.*.result, 'failure') }}
              run: exit 1
