name: Tests

# Run this workflow every time a new commit pushed to your repository
on:
  push:

jobs:
  tests:
    if: "! contains(toJSON(github.event.commits.*.msg), 'skip') && ! contains(toJSON(github.event.commits.*.msg), 'ci')" #skip ci...
    runs-on: ${{ matrix.operating-system }}

    strategy:
      fail-fast: false
      matrix:
        operating-system: [ubuntu-20.04]
        php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1']
        include:
          - operating-system: ubuntu-16.04
            php-versions: '7.1'
            COMPOSER_FLAGS: '--prefer-stable --prefer-lowest'
            PHPUNIT_FLAGS: '--coverage-clover build/coverage.xml'

    name: PHP ${{ matrix.php-versions }} - ${{ matrix.operating-system }}

    env:
      extensions: curl json libxml dom
      key: cache-v1 # can be any string, change to clear the extension cache.

    steps:
      # Checks out a copy of your repository on the ubuntu machine
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Setup cache environment
        id: extcache
        uses: shivammathur/cache-extensions@v1
        with:
          php-version: ${{ matrix.php-versions }}
          extensions: ${{ env.extensions }}
          key: ${{ env.key }}

      - name: Cache PHP Extensions
        uses: actions/cache@v2
        with:
          path: ${{ steps.extcache.outputs.dir }}
          key: ${{ steps.extcache.outputs.key }}
          restore-keys: ${{ steps.extcache.outputs.key }}

      - name: Cache Composer Dependencies
        uses: actions/cache@v1
        with:
          path: ~/.composer/cache/files
          key: dependencies-composer-${{ hashFiles('composer.json') }}

      - name: Setup PHP Action
        uses: shivammathur/setup-php@2.8.0
        with:
          php-version: ${{ matrix.php-versions }}
          extensions: ${{ env.extensions }}
          coverage: xdebug
          tools: pecl, composer

      - 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@v2
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-composer-

      - name: Install Composer dependencies
        run: composer update ${{ matrix.COMPOSER_FLAGS }} --no-interaction

      - name: boot test server
        run: vendor/bin/http_test_server > /dev/null 2>&1 &

      - name: Run tests
        run: composer test
