name: UnitTest & Lint

on:
  push:
    branches:
      - master
      - develop
  pull_request:
    branches:
      - '*'
  schedule:
    - cron: "0 7 1 * *"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  testsuite:
    runs-on: ubuntu-24.04
    strategy:
      fail-fast: false
      matrix:
        php-version: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5']
        include:
          - php-version: '8.4'
            coverage: true
          - php-version: '8.4'
            test-tld: true

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

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php-version }}
          extensions: mbstring, intl
          coverage: xdebug

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

      - name: Cache 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 --no-progress

      - name: Run PHPUnit
        run: |
          if [[ "${{ matrix.coverage }}" == "true" ]]; then
            vendor/bin/phpunit --exclude-group deprecated,tld --stderr --verbose --coverage-clover=coverage.xml
          elif [[ "${{ matrix.test-tld }}" == "true" ]]; then
            vendor/bin/phpunit --group tld --stderr
          else
            vendor/bin/phpunit --exclude-group deprecated,tld --stderr
          fi

      - name: Submit code coverage
        if: matrix.coverage
        uses: codecov/codecov-action@v5
        with:
          files: coverage.xml
          fail_ci_if_error: false

  codestyle:
    runs-on: ubuntu-24.04

    steps:
      - uses: actions/checkout@v5

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.4'
          extensions: mbstring, intl
          coverage: none
          tools: cs2pr, phpcs

      - name: Run PHP CodeSniffer
        run: phpcs -qn --report=checkstyle --extensions=php --standard=PSR12 ./lib ./tests | cs2pr
