name: CI

on:
  push:
  pull_request:

defaults:
  run:
    shell: bash

jobs:

  tests:
    name: Tests on PHP ${{ matrix.php }} with ${{ matrix.implementation }}${{ matrix.name_suffix }}
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        php: [ '7.2', '7.3', '7.4', '8.0', '8.1' ]
        minimum_stability: [ 'stable' ]
        name_suffix: [ '' ]
        implementation: [ 'http_kernel' ]
        include:
          - php: '8.0'
            minimum_stability: dev
            implementation: 'http_kernel'
            name_suffix: ' and dev deps'
          - php: '7.4'
            minimum_stability: dev
            implementation: 'http_kernel'
            name_suffix: ' and dev deps'
          - php: '8.0'
            implementation: http_client
      fail-fast: false

    env:
      MATRIX_PHP: ${{ matrix.php }}

    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 2

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

      - name: Configure for minimum stability
        if: "${{ matrix.minimum_stability == 'dev' }}"
        run: |
          composer config minimum-stability dev

      - name: Install dependencies
        run: |
          composer update --no-interaction --prefer-dist

      - name: Setup Mink test server
        if: "${{ matrix.implementation == 'http_client'}}"
        run: |
          mkdir ./logs
          ./vendor/bin/mink-test-server &> ./logs/mink-test-server.log &

      - name: Wait for browser & PHP to start
        if: "${{ matrix.implementation == 'http_client'}}"
        run: |
          while ! nc -z localhost 8002 </dev/null; do echo Waiting for PHP server to start...; sleep 1; done

      - name: Run tests
        if: "${{ matrix.implementation == 'http_kernel'}}"
        run: |
          vendor/bin/phpunit -v --coverage-clover=coverage.clover

      - name: Run tests
        if: "${{ matrix.implementation == 'http_client'}}"
        run: |
          vendor/bin/phpunit -c phpunit.http_client.xml -v --coverage-clover=coverage.clover

      - name: Upload code coverage
        uses: codecov/codecov-action@v2
        with:
          files: coverage.clover
