name: Unit Tests

on:
  push:

jobs:
  unit-tests:
    name: PHP ${{matrix.php}}, ${{matrix.stability}} deps
    runs-on: ubuntu-latest

    strategy:
      matrix:
        php: [7.4, 8.0, 8.1]
        stability: [lowest, stable]

    env:
      extensions: dom, mbstring, xml
      key: cache-v1

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Setup cache environment
        id: cache-env
        uses: shivammathur/cache-extensions@v1
        with:
          php-version: ${{ matrix.php }}
          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: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: ${{ env.extensions }}
          tools: composer
          coverage: none

      - name: Setup problem matchers for PHP
        run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"

      - 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@v3
        with:
          path: ${{steps.composer-cache.outputs.dir}}
          key: ${{matrix.php}}-composer-${{matrix.stability}}-${{hashFiles('**/composer.json')}}
          restore-keys: ${{matrix.php}}-${{matrix.stability}}-composer-

      - name: Install dependencies
        run: composer update --prefer-${{matrix.stability}} --prefer-dist --no-interaction --no-suggest --no-progress

      - name: Setup Problem Matchers for PHPUnit
        run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

      - name: Execute tests
        run: vendor/bin/phpunit
