name: Tests

on:
  workflow_dispatch:
  push:
    branches:
      - master
    tags:
      - 'v*'
  pull_request:
    branches:
      - master

jobs:
  tests:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        php: [ '8.1', '8.2', '8.3' ]
        ext_base: [ 'none, dom, tokenizer, xml, xmlwriter,' ]
        ext_lib: [ 'curl, mbstring, openssl,' ]
        ext_optional: [ '', 'bcmath', 'gmp' ]

    name: PHP ${{ matrix.php }} (${{ matrix.ext_optional }})

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

      - name: Setup PHP ${{ matrix.php }} (${{ matrix.ext_optional }})
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions:  ${{ matrix.ext_base }}${{ matrix.ext_lib }}${{ matrix.ext_optional }}
          coverage: none

      - name: Get composer cache directory
        id: composer-cache
        run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

      - name: Cache Composer dependencies
        uses: actions/cache@v4
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-composer-

      - name: Install dependencies
        run: composer install --prefer-dist

      - name: Setup node
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install web-push-testing server
        run: |
          npm install -g web-push-testing

      - name: Run unit tests
        run: |
          web-push-testing --port 9012 start
          composer test:unit
          web-push-testing --port 9012 stop

      - name: Run PHPStan
        run: composer test:typing

      - name: Run php-cs-fixer
        run: |
          composer test:syntax
          composer test:syntax_tests
