name: Infection

on:
  pull_request:
    branches:
      - develop
    paths:
      - '**.php'
      - 'composer.*'
      - 'phpunit*'
      - '.github/workflows/infection.yml'
  push:
    branches:
      - develop
    paths:
      - '**.php'
      - 'composer.*'
      - 'phpunit*'
      - '.github/workflows/infection.yml'

jobs:
  main:
    name: Mutation Testing
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, '[ci skip]')"

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

      - name: Set up PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.1'
          tools: infection, phpunit
          extensions: intl, json, mbstring, gd, xml, sqlite3
          coverage: xdebug
        env:
          COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Set up problem matchers for PHPUnit
        run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

      - name: Configure matchers
        uses: mheap/phpunit-matcher-action@v1

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

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

      - name: Install dependencies
        run: |
          if [ -f composer.lock ]; then
            composer install --no-progress --no-interaction --prefer-dist --optimize-autoloader
          else
            composer update --no-progress --no-interaction --prefer-dist --optimize-autoloader
          fi

      - name: Test with PHPUnit
        run: vendor/bin/phpunit --teamcity

      - name: Mutate with Infection
        run: |
          git fetch --depth=1 origin $GITHUB_BASE_REF
          infection --threads=2 --skip-initial-tests --coverage=build/phpunit --git-diff-base=origin/$GITHUB_BASE_REF --git-diff-filter=AM --logger-github --ignore-msi-with-no-mutations
