name: CI

on:
  push:
    branches:
    tags:
  pull_request:

jobs:
  build:
    runs-on: ubuntu-18.04
    strategy:
      matrix:
        php: [7.1, 7.2, 7.3, 7.4, 8.0]
        env: [
          'EXECUTOR= DEPENDENCIES=--prefer-lowest',
          'EXECUTOR=coroutine DEPENDENCIES=--prefer-lowest',
          'EXECUTOR=',
          'EXECUTOR=coroutine',
        ]
    name: PHP ${{ matrix.php }} Test ${{ matrix.env }}

    steps:
    - uses: actions/checkout@v2

    - name: Install PHP
      uses: shivammathur/setup-php@2.9.0
      with:
        php-version: ${{ matrix.php  }}
        coverage: none
        extensions: json, mbstring
    - name: Get Composer Cache Directory
      id: composer-cache
      run: echo "::set-output name=dir::$(composer config cache-files-dir)"

    - name: Cache dependencies
      uses: actions/cache@v1
      with:
        path: ${{ steps.composer-cache.outputs.dir }}
        key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
        restore-keys: ${{ runner.os }}-composer-

    - name: Remove dependencies not used in this job for PHP 8 compatibility
      run: |
        composer remove --dev --no-update phpbench/phpbench
        composer remove --dev --no-update phpstan/phpstan
        composer remove --dev --no-update phpstan/phpstan-phpunit
        composer remove --dev --no-update phpstan/phpstan-strict-rules
        composer remove --dev --no-update doctrine/coding-standard

    - name: Install Dependencies
      run: composer update ${DEPENDENCIES}

    - name: Run unit tests
      run: |
        export $ENV
        ./vendor/bin/phpunit --group default,ReactPromise
      env:
        ENV: ${{ matrix.env}}

  coding-standard:
    runs-on: ubuntu-18.04
    name: Coding Standard

    steps:
    - uses: actions/checkout@v2

    - name: Install PHP
      uses: shivammathur/setup-php@2.9.0
      with:
        php-version: 7.1
        coverage: none
        extensions: json, mbstring

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

    - name: Cache dependencies
      uses: actions/cache@v1
      with:
        path: ${{ steps.composer-cache.outputs.dir }}
        key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
        restore-keys: ${{ runner.os }}-composer-

    - name: Install Dependencies
      run: composer install ${DEPENDENCIES}

    - name: Coding Standard
      run: composer lint

  phpstan:
    runs-on: ubuntu-18.04
    name: PHPStan

    steps:
    - uses: actions/checkout@v2

    - name: Install PHP
      uses: shivammathur/setup-php@2.9.0
      with:
        php-version: 7.1
        coverage: none
        extensions: json, mbstring

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

    - name: Cache dependencies
      uses: actions/cache@v1
      with:
        path: ${{ steps.composer-cache.outputs.dir }}
        key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
        restore-keys: ${{ runner.os }}-composer-

    - name: Install Dependencies
      run: composer install ${DEPENDENCIES}

    - name: PHPStan
      run: composer stan

  coverage:
    runs-on: ubuntu-18.04
    name: Code Coverage

    steps:
    - uses: actions/checkout@v2
      with:
        ref: ${{ github.ref }}

    - name: Install PHP
      uses: shivammathur/setup-php@2.9.0
      with:
        php-version: 7.2
        coverage: pcov
        extensions: json, mbstring

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

    - name: Cache dependencies
      uses: actions/cache@v1
      with:
        path: ${{ steps.composer-cache.outputs.dir }}
        key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
        restore-keys: ${{ runner.os }}-composer-

    - name: Install Dependencies
      run: composer install ${DEPENDENCIES}

    - name: Code coverage
      run: |
        ./vendor/bin/phpunit --coverage-clover /tmp/coverage/clover_executor.xml
        EXECUTOR=coroutine ./vendor/bin/phpunit --coverage-clover /tmp/coverage/clover_executor-coroutine.xml

    - name: Report to Coveralls
      env:
        COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        COVERALLS_RUN_LOCALLY: 1
      run: vendor/bin/php-coveralls --verbose
