name: Tests

on:
  pull_request:
  push:
    branches:
      - main

jobs:
  unit:
    name: Unit (required)
    runs-on: ubuntu-latest

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

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.2'
          extensions: mysqli, curl, openssl, json, redis, pcov
          coverage: pcov

      - name: Resolve dependencies
        working-directory: services/nginx/app
        run: composer update --no-interaction --prefer-dist

      - name: Run unit tests
        working-directory: services/nginx/app
        run: composer test:unit

      - name: Generate coverage report
        working-directory: services/nginx/app
        run: composer test:coverage

      - name: Upload coverage artifact
        uses: actions/upload-artifact@v4
        with:
          name: unit-coverage-clover
          path: services/nginx/app/build/logs/clover.xml
          if-no-files-found: warn

  integration:
    name: Integration (advisory)
    runs-on: ubuntu-latest
    continue-on-error: true

    services:
      redis:
        image: redis:7
        ports:
          - 6379:6379
      mysql:
        image: mysql:8
        env:
          MYSQL_DATABASE: app_test
          MYSQL_ROOT_PASSWORD: root
        ports:
          - 3306:3306
        options: >-
          --health-cmd="mysqladmin ping -h 127.0.0.1 -proot"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=10

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

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.2'
          extensions: mysqli, curl, openssl, json, redis

      - name: Resolve dependencies
        working-directory: services/nginx/app
        run: composer update --no-interaction --prefer-dist

      - name: Run integration tests
        working-directory: services/nginx/app
        env:
          RUN_INTEGRATION_TESTS: '1'
          REDIS_CONFIG_HOST: 127.0.0.1
          REDIS_CONFIG_DATABASE: '0'
          REDIS_CONFIG_PASSWORD: ''
          CONFIG_DB_HOST: 127.0.0.1
          CONFIG_DB_USER: root
          CONFIG_DB_PASSWORD: root
          CONFIG_DB_DATABASE: app_test
        run: composer test:integration
