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]
        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.2.2
      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: Install Dependencies
      run: composer install ${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.2.2
      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.2.2
      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.2.2
      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
