on:
  - pull_request
  - push

name: build

jobs:
  tests:
    name: PHP ${{ matrix.php-version }}-${{ matrix.os }}

    env:
      extensions: curl, mbstring, dom, intl, json, libxml, xml, xmlwriter
      key: cache-v1
      update: true

    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os:
          - ubuntu-latest
          - windows-latest

        php-version:
          - 5.5
          - 5.6
          - 7.0
          - 7.1
          - 7.2
          - 7.3
          - 7.4
          - 8.1

    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Cache extensions
        uses: actions/cache@v3
        with:
          path: ${{ steps.cache-env.outputs.dir }}
          key: ${{ steps.cache-env.outputs.key }}
          restore-keys: ${{ steps.cache-env.outputs.key }}

      - name: Install PHP with extensions
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php-version }}
          extensions: ${{ env.extensions }}
          ini-values: date.timezone='UTC'
          coverage: pcov
          tools: composer:v2

      - name: Determine composer cache directory on Linux.
        if: matrix.os == 'ubuntu-latest'
        run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV

      - name: Determine composer cache directory on Windows.
        if: matrix.os == 'windows-latest'
        run: echo "COMPOSER_CACHE_DIR=~\AppData\Local\Composer" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append

      - name: Cache dependencies installed with composer
        uses: actions/cache@v3
        with:
          path: ${{ env.COMPOSER_CACHE_DIR }}
          key: php${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.json') }}
          restore-keys: |
            php${{ matrix.php-version }}-composer-

      - name: Install dependencies
        run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader

      - name: Run tests with phpunit and coverage on Linux
        if: matrix.os == 'ubuntu-latest' && matrix.php-version == '7.1'
        run: vendor/bin/phpunit --coverage-clover=coverage.clover

      - name: Run tests with phpunit and without coverage on Linux
        if: matrix.os == 'ubuntu-latest' && matrix.php-version != '7.1'
        run: vendor/bin/phpunit

      - name: Run tests with phpunit without coverage on Windows
        if: matrix.os == 'windows-latest'
        run: vendor/bin/phpunit

      - name: Upload code coverage scrutinizer
        if: matrix.os == 'ubuntu-latest' && matrix.php-version == '7.1'
        run: |
          wget https://scrutinizer-ci.com/ocular.phar
          php ocular.phar code-coverage:upload --format=php-clover coverage.clover
