# https://help.github.com/en/categories/automating-your-workflow-with-github-actions

name: "CI Tests"

on:
  pull_request: ~
  push:
    branches:
      - "2.0.x"

jobs:
  phpunit:
    name: "PHPUnit tests"

    runs-on: ${{ matrix.operating-system }}

    strategy:
      matrix:
        dependencies:
          - "lowest"
          - "highest"
          - "locked"
        php-version:
          - "8.1"
          - "8.2"
          - "8.3"
        operating-system:
          - "ubuntu-latest"
          - "windows-latest"

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

      - name: "Install PHP"
        uses: "shivammathur/setup-php@v2"
        with:
          coverage: "pcov"
          php-version: "${{ matrix.php-version }}"
          ini-values: memory_limit=-1

      - name: "Cache dependencies"
        uses: "actions/cache@v4"
        with:
          path: |
            ~/.composer/cache
            vendor
          key: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}"
          restore-keys: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}"

      - name: "Install lowest dependencies"
        if: ${{ matrix.dependencies == 'lowest' }}
        run: "composer update --prefer-lowest --no-interaction --no-progress"

      - name: "Install highest dependencies"
        if: ${{ matrix.dependencies == 'highest' }}
        run: "composer update --no-interaction --no-progress"

      - name: "Install locked dependencies"
        if: ${{ matrix.dependencies == 'locked' }}
        run: "composer install --no-interaction --no-progress"

      - name: "Tests"
        run: "vendor/bin/phpunit"

  coding-standards:
    name: "Check Coding Standards"

    runs-on: ${{ matrix.operating-system }}

    strategy:
      matrix:
        dependencies:
          - "locked"
        php-version:
          - "8.1"
          - "8.2"
          - "8.3"
        operating-system:
          - "ubuntu-latest"

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

      - name: "Install PHP"
        uses: "shivammathur/setup-php@v2"
        with:
          coverage: "none"
          php-version: "${{ matrix.php-version }}"
          ini-values: memory_limit=-1

      - name: "Cache dependencies"
        uses: "actions/cache@v4"
        with:
          path: |
            ~/.composer/cache
            vendor
          key: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}"
          restore-keys: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}"

      - name: "Install lowest dependencies"
        if: ${{ matrix.dependencies == 'lowest' }}
        run: "composer update --prefer-lowest --no-interaction --no-progress"

      - name: "Install highest dependencies"
        if: ${{ matrix.dependencies == 'highest' }}
        run: "composer update --no-interaction --no-progress"

      - name: "Install locked dependencies"
        if: ${{ matrix.dependencies == 'locked' }}
        run: "composer install --no-interaction --no-progress"

      - name: "Install tool dependencies"
        run: "composer bin phpcs install --no-interaction --no-progress"

      - name: "Coding Standard"
        run: ".tools/phpcs/vendor/bin/phpcs"

  static-analysis-psalm:
    name: "Static Analysis by Psalm"

    runs-on: ${{ matrix.operating-system }}

    strategy:
      matrix:
        dependencies:
          - "locked"
        php-version:
          - "8.1"
          - "8.2"
          - "8.3"
        operating-system:
          - "ubuntu-latest"

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

      - name: "Install PHP"
        uses: "shivammathur/setup-php@v2"
        with:
          coverage: "none"
          php-version: "${{ matrix.php-version }}"
          ini-values: memory_limit=-1

      - name: "Cache dependencies"
        uses: "actions/cache@v4"
        with:
          path: |
            ~/.composer/cache
            vendor
          key: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}"
          restore-keys: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}"

      - name: "Install lowest dependencies"
        if: ${{ matrix.dependencies == 'lowest' }}
        run: "composer update --prefer-lowest --no-interaction --no-progress"

      - name: "Install highest dependencies"
        if: ${{ matrix.dependencies == 'highest' }}
        run: "composer update --no-interaction --no-progress"

      - name: "Install locked dependencies"
        if: ${{ matrix.dependencies == 'locked' }}
        run: "composer install --no-interaction --no-progress"

      - name: "Install tool dependencies"
        run: "composer bin infection install --no-interaction --no-progress"

      - name: "psalm"
        run: ".tools/infection/vendor/bin/psalm --shepherd --stats"

  mutation-tests:
    name: "Mutation tests"

    runs-on: ${{ matrix.operating-system }}

    strategy:
      matrix:
        dependencies:
          - "locked"
        php-version:
          - "8.3"
        operating-system:
          - "ubuntu-latest"

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

      - name: "Install PHP"
        uses: "shivammathur/setup-php@v2"
        with:
          coverage: "xdebug"
          php-version: "${{ matrix.php-version }}"
          ini-values: memory_limit=-1, zend.assertions=1

      - name: "Cache dependencies"
        uses: "actions/cache@v4"
        with:
          path: |
            ~/.composer/cache
            vendor
          key: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}"
          restore-keys: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}"

      - name: "Install lowest dependencies"
        if: ${{ matrix.dependencies == 'lowest' }}
        run: "composer update --prefer-lowest --no-interaction --no-progress"

      - name: "Install highest dependencies"
        if: ${{ matrix.dependencies == 'highest' }}
        run: "composer update --no-interaction --no-progress"

      - name: "Install locked dependencies"
        if: ${{ matrix.dependencies == 'locked' }}
        run: "composer install --no-interaction --no-progress"

      - name: "Install tool dependencies"
        run: "composer bin infection install --no-interaction --no-progress"

      - name: "Infection"
        run: ".tools/infection/vendor/bin/roave-infection-static-analysis-plugin --threads=$(nproc)"
        env:
          INFECTION_BADGE_API_KEY: ${{ secrets.INFECTION_BADGE_API_KEY }}
          STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
