name: Build
on:
    push:
        branches:
            - "4.x"
    pull_request:

jobs:
    phpstan:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4

            - name: Setup PHP, with composer and extensions
              uses: shivammathur/setup-php@v2
              with:
                  php-version: 8.2
                  coverage: none

            - name: Install dependencies
              run: |
                  composer install
                  vendor/bin/simple-phpunit install

            - name: "Run PHPStan analysis"
              run: vendor/bin/phpstan analyse

    phpunit:
        strategy:
            fail-fast: false
            matrix:
                include:
                    # Latest Symfony version support
                    - os: macos-latest
                      php_version: "8.4"
                      symfony_version: "7.3"
                      stability: "stable"
                    - os: windows-latest
                      php_version: "8.4"
                      symfony_version: "7.3"
                      stability: "stable"
                    # LTS Symfony version support
                    - os: macos-latest
                      php_version: "8.4"
                      symfony_version: "6.4"
                      stability: "stable"
                    - os: windows-latest
                      php_version: "8.4"
                      symfony_version: "6.4"
                      stability: "stable"
                    # Lowest deps support
                    - os: ubuntu-latest
                      php_version: "8.1"
                      composer_args: "--prefer-lowest"
                      stability: "stable"
                    # Lowest PHP version support. Remove when it is dropped
                    - os: ubuntu-latest
                      php_version: "8.1"
                      symfony_version: "5.4"
                      stability: "stable"
                    - os: ubuntu-latest
                      php_version: "8.2"
                      symfony_version: "6.4"
                      stability: "stable"
                    - os: ubuntu-latest
                      php_version: "8.3"
                      symfony_version: "7.1"
                      stability: "stable"
                    - os: ubuntu-latest
                      php_version: "8.4"
                      symfony_version: "7.2"
                      stability: "stable"
                    # Upcoming Symfony versions
                    - os: ubuntu-latest
                      php_version: "8.4"
                      symfony_version: "7.4.x"
                      stability: "dev"
                    #- os: ubuntu-latest
                    #  php_version: "8.4"
                    #  symfony_version: "8.0.x"
                    #  stability: "dev"
        runs-on: ${{ matrix.os }}
        continue-on-error: ${{ matrix.stability == 'dev' }}
        steps:
            - uses: actions/checkout@v4

            - name: Setup PHP, with composer and extensions
              uses: shivammathur/setup-php@v2
              with:
                  php-version: ${{ matrix.php_version }}
                  coverage: none
                  extensions: mbstring, intl, pdo, pdo_sqlite, sqlite3
                  ini-values: date.timezone=UTC

            - name: symfony/flex is required to install the correct symfony version
              if: ${{ matrix.symfony_version }}
              run: |
                  composer global config --no-plugins allow-plugins.symfony/flex true
                  composer global require symfony/flex

            - name: Configure Composer stability
              run: |
                  composer config minimum-stability ${{ matrix.stability }}

            - name: Configure Symfony version for symfony/flex
              if: ${{ matrix.symfony_version }}
              run: composer config extra.symfony.require "${{ matrix.symfony_version }}.*"

            - name: Install dependencies
              run: |
                  composer update ${{ matrix.composer_args }};
                  vendor/bin/simple-phpunit install

            - name: Show installed dependencies
              run: |
                  composer show

            - name: Run tests
              env:
                  SYMFONY_DEPRECATIONS_HELPER: "max[total]=999999&ignoreFile=./tests/baseline-ignore.txt"
              run: |
                  ./vendor/bin/simple-phpunit --exclude-group pretty_urls

            - name: Run Pretty URLs tests
                # Symfony 5.4 version doesn't include the #[Route] attribute needed to run these tests (it only includes the @Route annotation)
              if: ${{ matrix.symfony_version != '5.4' && matrix.composer_args != '--prefer-lowest' }}
              env:
                  USE_PRETTY_URLS: "1"
                  SYMFONY_DEPRECATIONS_HELPER: "max[total]=999999&ignoreFile=./tests/baseline-ignore.txt"
              run: |
                  ./vendor/bin/simple-phpunit tests/Controller/PrettyUrls/PrettyUrlsControllerTest.php

    php-linter:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4

            # see https://github.com/OskarStark/php-cs-fixer-ga
            - name: PHP-CS-Fixer
              uses: docker://oskarstark/php-cs-fixer-ga
              with:
                  args: --diff --dry-run

    doc-linter:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4

            - name: DOCtor-RST
              uses: docker://oskarstark/doctor-rst
              with:
                  args: --short
              env:
                  DOCS_DIR: "doc/"

    css-linter:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4

            - name: Setup PHP, with composer and extensions
              uses: shivammathur/setup-php@v2
              with:
                  php-version: 8.2
                  coverage: none

            - name: Install dependencies
              run: |
                  composer install

            -   name: Lint logical CSS properties
                run: |
                    php ./src/Resources/bin/logical-css-properties-linter.php

    js-linter:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4
            - run: npm i -g corepack && corepack enable
            - uses: actions/setup-node@v4
              with:
                  cache: 'yarn'
            - run: yarn --immutable
            - run: yarn ci
