name: Tests

on:
  pull_request:
  push:
    # trying and staging branches are for BORS config
    branches:
      - trying
      - staging
      - main
      - feature/**
      - v1.x

jobs:
  yaml-lint:
    name: Yaml linting check
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Yaml lint check
        uses: ibiqlik/action-yamllint@v3
        with:
          config_file: .yamllint.yml

  lint:
    runs-on: ubuntu-latest
    name: linter-check
    env:
      PHP_CS_FIXER_IGNORE_ENV: 1
    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Install PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.4
          coverage: none

      - name: Install dependencies
        uses: ramsey/composer-install@v3

      - name: Run linter
        run: composer lint

  phpstan:
    runs-on: ubuntu-latest
    name: phpstan-tests
    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Install PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.4
          coverage: none

      - name: Install dependencies
        uses: ramsey/composer-install@v3

      - name: Run types tests with PHPStan
        run: composer phpstan

  tests:
    # Will not run if the event is a PR to bump-meilisearch-v* (so a pre-release PR)
    # Will still run for each push to bump-meilisearch-v*
    if: github.event_name != 'pull_request' || !startsWith(github.base_ref, 'bump-meilisearch-v')
    runs-on: ubuntu-latest
    name: integration-tests (PHP ${{ matrix.php-version }})
    services:
      meilisearch:
        image: getmeili/meilisearch:latest
        ports:
          - '7700:7700'
        env:
          MEILI_MASTER_KEY: masterKey
          MEILI_NO_ANALYTICS: true
    strategy:
      matrix:
        php-version: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4']

    steps:
      - uses: actions/checkout@v5

      - name: Install PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php-version }}
          coverage: pcov

      - name: Validate composer.json and composer.lock
        run: composer validate

      - name: Install dependencies
        uses: ramsey/composer-install@v3

      - name: Run test suite
        run: sh scripts/tests.sh --coverage-clover coverage-${{ matrix.php-version }}.xml

      - name: Upload coverage file
        uses: actions/upload-artifact@v4
        with:
          name: 'phpunit-${{ matrix.php-version }}-coverage'
          path: 'coverage*.xml'

  upload-coverage:
    name: Upload coverage to Codecov
    runs-on: ubuntu-latest
    needs:
      - tests

    steps:
      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 2

      - name: Download coverage files
        uses: actions/download-artifact@v4
        with:
          path: reports

      - name: Upload to Codecov
        uses: codecov/codecov-action@v5
        with:
          directory: reports
