name: Tests

on:
  push:
  pull_request:
  workflow_dispatch:

jobs:
  tests:
    # Don't run the tests on internal PRs
    if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository

    name: PHP ${{ matrix.php }}
    runs-on: ubuntu-latest

    strategy:
      matrix:
        php:
          - "8.1"
          - "8.2"

    env:
      extensions: ctype, dom, grpc, json, mbstring, openssl, xml, zip, zlib
      key: cache-unit-7x-${{ matrix.php }}-v1

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

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

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

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

      - uses: "ramsey/composer-install@v2"
        with:
          composer-options: "${{ matrix.composer-options }}"

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

      - name: Run PHPStan
        run: vendor/bin/phpstan analyse --no-progress

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

      - name: Run PHPUnit
        run: vendor/bin/phpunit --testsuite=unit --coverage-clover=coverage.xml --log-junit=test-report.xml --testdox --verbose

      - name: Upload code coverage
        uses: codecov/codecov-action@v3
        with:
          flags: unit
