name: Tests

on:
  push:
    branches: [2.x, 1.x]
  pull_request:
    branches: [2.x, 1.x]
  schedule:
    - cron: '3 2 1 * *'

env:
  SYMFONY_DEPRECATIONS_HELPER: "max[direct]=0"

jobs:
  build:
    runs-on: ubuntu-latest

    continue-on-error: ${{ matrix.experimental }}
    name: '${{ matrix.deps }} deps: PHP=${{ matrix.php }}, Symfony=${{ matrix.symfony }}, Laravel=${{ matrix.laravel }}'
    strategy:
      fail-fast: false
      matrix:
        include:
          - deps: 'low'
            php: 7.2
            experimental: false
            composer: v2

          - deps: 'stable'
            php: 8.1
            symfony: '5.x'
            laravel: '7.x'
            experimental: false
            composer: v2

          - deps: 'stable'
            php: 8.1
            symfony: '5.x'
            laravel: '8.x'
            experimental: false
            composer: v2

          - deps: 'stable'
            php: 8.1
            symfony: '6.x'
            laravel: '9.x'
            experimental: false
            composer: v2

          - deps: 'dev'
            php: 8.2
            experimental: true
            composer: preview

    steps:
      - uses: actions/checkout@v2

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: xml, pdo_sqlite
          tools: "composer:${{ matrix.composer }}"

      - name: Set-up env variables
        id: ctx
        run: |
          echo "::set-output name=composer_dir::$(composer config cache-files-dir)"

          case "${{ matrix.deps }}" in
            "low")
              echo "COMPOSER_FLAGS=--prefer-lowest" >> $GITHUB_ENV
              echo "SYMFONY_DEPRECATIONS_HELPER=disabled=1" >> $GITHUB_ENV

              ;;
            "dev")
              echo "COMPOSER_FLAGS=--ignore-platform-req=php" >> $GITHUB_ENV

              composer config minimum-stability dev

              ;;
          esac

          if [ -n "${{ matrix.symfony }}" ]; then
              echo "SYMFONY_REQUIRE=$(echo '${{ matrix.symfony }}' | tr x \\*)" >> $GITHUB_ENV
          fi

          if [ -n "${{ matrix.laravel }}" ]; then
              echo "LARAVEL_REQUIRE=$(echo '${{ matrix.laravel }}' | tr x \\*)" >> $GITHUB_ENV
          fi

          if [ "${{ matrix.laravel }}" = "7.x" ]; then
              echo "SYMFONY_DEPRECATIONS_HELPER=max[direct]=5" >> $GITHUB_ENV
          fi

          if [ "${{ matrix.php }}" = "8.1" ]; then
              echo "SYMFONY_PHPUNIT_VERSION=9.5" >> $GITHUB_ENV
          fi

          echo "PHP_VERSION=${{ matrix.php }}" >> $GITHUB_ENV

      - name: Cache dependencies
        uses: actions/cache@v2
        with:
          path: ${{ steps.ctx.outputs.composer_dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('composer.json') }}

      - name: Install dependencies
        run: |
          composer global require --no-progress --no-scripts --no-plugins symfony/flex
          if [ "$LARAVEL_REQUIRE" != "" ]; then composer require --no-update illuminate/database:$LARAVEL_REQUIRE; fi
          composer update --prefer-dist --no-progress $COMPOSER_FLAGS
          ./vendor/bin/simple-phpunit install

      - name: Show info
        run: |
          echo "$(php --version)"
          echo ""
          echo "$(php tests/Functional/app/bin/console --version)"

      - name: Run test suite
        run: ./vendor/bin/simple-phpunit

      - name: Run static analysis
        if: '!matrix.experimental'
        run: ./vendor/bin/psalm --php-version=$PHP_VERSION
