name: Tests

on: [ pull_request, push ]

jobs:
  ubuntu:
    runs-on: ubuntu-latest

    services:
      memcached:
        image: memcached:1.6-alpine
        ports:
          - 11211:11211
      mysql:
        image: mysql:5.7
        env:
          MYSQL_ALLOW_EMPTY_PASSWORD: yes
          MYSQL_DATABASE: forge
        ports:
          - 33306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
      redis:
        image: redis:5.0
        ports:
          - 6379:6379
        options: --entrypoint redis-server
      dynamodb:
        image: amazon/dynamodb-local:latest
        ports:
          - 8888:8000

    strategy:
      fail-fast: false
      matrix:
        include:
          - php: 7.1.8
          - php: 7.1.28
          - php: 7.1
          - php: 7.1
            setup: lowest

          - php: 7.2.0
          - php: 7.2.15
          - php: 7.2
          - php: 7.2
            setup: lowest

          - php: 7.3.0
          - php: 7.3.5
          - php: 7.3
          - php: 7.3
            setup: lowest

          - php: 7.4
          - php: 7.4
            setup: lowest

          - php: 8.0
            classmap-authoritative: true

          - php: 8.0
          - php: 8.0
            setup: lowest

          - php: 8.1
          - php: 8.1
            setup: lowest

          - php: 8.2
          - php: 8.2
            setup: lowest

          - php: 8.3
          - php: 8.3
            setup: lowest

          - php: 8.0
            laravel: true
            reference: 9.x

          - php: 8.0
            laravel: true
            reference: 9.x
            classmap-authoritative: true

          - php: 8.2
            laravel: true
            reference: 9.x

          - php: 8.2
            laravel: true
            reference: 9.x
            classmap-authoritative: true

          - php: 8.2
            coverage: true

    name: ${{ matrix.laravel && 'Laravel compatibility - ' || '' }}${{ matrix.classmap-authoritative && 'classmap-authoritative - ' || '' }}${{ matrix.coverage && 'Coverage - ' || '' }}PHP ${{ matrix.php }} - ${{ matrix.setup || 'stable' }} - ubuntu

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

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: json, msgpack${{ matrix.laravel && ', dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd, redis-phpredis/phpredis@5.3.5, igbinary, msgpack, lzf, zstd, lz4, memcached, gmp' || '' }}
          ini-values: error_reporting=${{ (matrix.setup != 'lowest') && '32767' || '8191' }}
          tools: composer:v2
          coverage: ${{ matrix.coverage && 'xdebug' || 'none' }}
        env:
          REDIS_CONFIGURE_OPTS: --enable-redis --enable-redis-igbinary --enable-redis-msgpack --enable-redis-lzf --with-liblzf --enable-redis-zstd --with-libzstd --enable-redis-lz4 --with-liblz4
          REDIS_LIBS: liblz4-dev, liblzf-dev, libzstd-dev

      - name: Install locales
        run: |
          sudo apt-get update || apt --fix-broken install || echo 'Apt failure ignored'
          sudo apt-get install tzdata locales -y && sudo locale-gen fr_FR.UTF-8 sr_ME.UTF-8 ar_AE.UTF-8 zh_TW.UTF-8 zh_CN.UTF-8 yo_NG.UTF-8 en_US.UTF-8 || echo 'Apt failure ignored'

      - name: Set Minimum PHP 8.1 Versions
        uses: nick-invision/retry@v1
        with:
          timeout_minutes: 5
          max_attempts: 5
          command: composer require ramsey/collection:^1.2 brick/math:^0.9.3 --no-interaction --no-update
        if: matrix.laravel

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

      - name: Cache composer dependencies
        uses: actions/cache@v3
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: "php-${{ matrix.php }}-{{ matrix.setup }}-ubuntu-${{ hashFiles('**/composer.json') }}"
          restore-keys: "php-${{ matrix.php }}-{{ matrix.setup }}-ubuntu-${{ hashFiles('**/composer.json') }}"

      - name: Install dependencies
        uses: nick-fields/retry@v2
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: |
            composer remove --no-update phpmd/phpmd friendsofphp/php-cs-fixer --no-interaction --dev;
            if [[ "${{ matrix.laravel }}" != 'true' ]]; then
              composer remove --no-update kylekatarnls/multi-tester --no-interaction --dev;
            fi;
            ${{ matrix.php >= 8.1 && 'composer require --no-update phpunit/phpunit:^9.5.20 --no-interaction --dev;' || '' }}
            composer update --prefer-dist --no-progress --prefer-${{ matrix.setup || 'stable' }} ${{ matrix.classmap-authoritative && '--classmap-authoritative' || '' }}${{ matrix.php >= 8.2 && ' --ignore-platform-reqs' || '' }};

      - name: Run test suite
        run: |
          if [[ "${{ matrix.laravel }}" != 'true' && "${{ matrix.coverage }}" = 'true' ]]; then
            php -d memory_limit=-1 -d zend.enable_gc=0 -d error_reporting=-1 vendor/phpunit/phpunit/phpunit --coverage-clover=coverage.xml --coverage-text;
          fi;
          if [[ "${{ matrix.laravel }}" != 'true' && "${{ matrix.coverage }}" != 'true' ]]; then
            php -d memory_limit=-1 -d zend.enable_gc=0 -d error_reporting=-1 vendor/phpunit/phpunit/phpunit;
          fi;
          if [[ "${{ matrix.laravel }}" = 'true' ]]; then
            vendor/bin/multi-tester tests/Laravel/laravel-${{ matrix.reference }}.multi-tester.yml;
          fi;
        env:
          PHP_VERSION: ${{ matrix.php }}
          REDIS_HOST: 127.0.0.1
          REDIS_PORT: 6379
          DB_PORT: ${{ job.services.mysql.ports[3306] }}
          DB_USERNAME: root

      - name: Code Climate Test Reporter
        if: matrix.coverage
        uses: aktions/codeclimate-test-reporter@v1
        with:
          codeclimate-test-reporter-id: ${{ secrets.CC_TEST_REPORTER_ID }}
          command: after-build -t clover
        env:
          ACTIONS_ALLOW_UNSECURE_COMMANDS: true
        continue-on-error: true

      - name: Coverage
        uses: codecov/codecov-action@v1
        if: matrix.coverage

  windows:
    runs-on: windows-latest

    strategy:
      fail-fast: false
      matrix:
        include:
          - php: 7.1.8
          - php: 7.1.28
          - php: 7.1
          - php: 7.1
            setup: lowest

          - php: 7.2.0
          - php: 7.2.15
          - php: 7.2
          - php: 7.2
            setup: lowest

          - php: 7.3.0
          - php: 7.3.5
          - php: 7.3
          - php: 7.3
            setup: lowest

          - php: 7.4
            coverage: true
          - php: 7.4
            setup: lowest

          - php: 8.0
          - php: 8.0
            setup: lowest

          - php: 8.1
          - php: 8.1
            setup: lowest

          - php: 8.2
          - php: 8.2
            setup: lowest

          - php: 8.3
          - php: 8.3
            setup: lowest

    name: PHP ${{ matrix.php }} - ${{ matrix.setup || 'stable' }} - windows

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

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: json
          tools: composer:v2
          coverage: none

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

      - name: Cache composer dependencies
        uses: actions/cache@v3
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: "php-${{ matrix.php }}-{{ matrix.setup }}-windows-${{ hashFiles('**/composer.json') }}"
          restore-keys: "php-${{ matrix.php }}-{{ matrix.setup }}-windows-${{ hashFiles('**/composer.json') }}"

      - name: Install dependencies
        uses: nick-fields/retry@v2
        with:
          timeout_minutes: 10
          max_attempts: 3
          command: |
            composer remove --no-update kylekatarnls/multi-tester phpmd/phpmd friendsofphp/php-cs-fixer --no-interaction --dev;
            ${{ matrix.php >= 8.1 && 'composer require --no-update phpunit/phpunit:^9.5.20 --no-interaction --dev;' || '' }}
            composer update --prefer-dist --no-progress --prefer-${{ matrix.setup || 'stable' }}${{ matrix.php >= 8.2 && ' --ignore-platform-reqs' || '' }};

      - name: Run test suite
        run: |
          php -d memory_limit=-1 -d zend.enable_gc=0 -d error_reporting=-1 vendor/phpunit/phpunit/phpunit;
        env:
          PHP_VERSION: ${{ matrix.php }}
          REDIS_HOST: 127.0.0.1
          REDIS_PORT: 6379
