name: Run tests

permissions:
    contents: read

on:
    pull_request:
    push:

jobs:
    test-node:
        name: Test on node ${{ matrix.node_version }} and ${{ matrix.os }}
        runs-on: ${{ matrix.os }}
        strategy:
            matrix:
                # Source: https://endoflife.date/nodejs
                node_version: ["18", "20", "lts/*", "latest"]
                os: [ubuntu-latest]

        steps:
            - uses: actions/checkout@v4
            - name: Use Node.js ${{ matrix.node_version }}
              uses: actions/setup-node@v4
              with:
                  node-version: ${{ matrix.node_version }}
            - name: Get yarn cache directory path
              id: yarn-cache-dir-path
              run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
            - name: yarn cache
              uses: actions/cache@v4
              with:
                  path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
                  key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
                  restore-keys: |
                      ${{ runner.os }}-yarn-
            - name: Install modules
              run: yarn install
            - name: Run node tests
              run: |
                  yarn run test
                  yarn run report-coverage
            - name: Send coverage
              uses: codecov/codecov-action@v4
              with:
                  flags: node
                  fail_ci_if_error: true # optional (default = false)
                  token: ${{ secrets.CODECOV_TOKEN }}

    test-php:
        name: Test on php ${{ matrix.php-version }} and ${{ matrix.os }}
        runs-on: ${{ matrix.os }}
        continue-on-error: ${{ matrix.experimental }}
        strategy:
            fail-fast: false
            matrix:
                php-version: ["7.3", "7.4", "8.0", "8.1", "8.2", "8.3"]
                os: [ubuntu-latest]
                experimental: [false]
                composer-options: [""]
                phpunit-options: ["--coverage-clover=build/logs/clover.xml --coverage-filter=src"]
                include:
                    - {
                          php-version: "7.2",
                          phpunit-options: "",
                          composer-options: "",
                          experimental: false,
                          os: ubuntu-latest,
                      }
                    - {
                          php-version: "nightly",
                          phpunit-options: "--coverage-clover=build/logs/clover.xml --coverage-filter=src",
                          composer-options: "--ignore-platform-req=php",
                          experimental: true,
                          os: ubuntu-latest,
                      }
        steps:
            - uses: actions/checkout@v4
            - name: Use php ${{ matrix.php-version }}
              uses: shivammathur/setup-php@v2
              with:
                  php-version: ${{ matrix.php-version }}
                  coverage: pcov
            - name: Cache module
              uses: actions/cache@v4
              with:
                  path: ~/.composer/cache/
                  key: composer-cache
            - name: Install dependencies
              run: composer install --no-interaction ${{ matrix.composer-options }}
            - name: Run php tests
              run: composer run test -- ${{ matrix.phpunit-options }}
            - name: Send coverage
              uses: codecov/codecov-action@v4
              with:
                  files: ./build/logs/clover.xml
                  flags: php
                  fail_ci_if_error: true # optional (default = false)
                  name: phpunit-${{ matrix.php-version }}-${{ matrix.os }}
                  token: ${{ secrets.CODECOV_TOKEN }}
