name: PHPUnit with MySQL

on:
  workflow_call:
    inputs:
      php-version:
        required: true
        type: string
      mysql-version:
        required: true
        type: string
      extension:
        required: true
        type: string
      config-file-suffix:
        required: false
        type: string
        default: ''

jobs:
  phpunit-mysql:
    runs-on: ubuntu-24.04

    services:
      mysql:
        image: mysql:${{ inputs.mysql-version }}
        ports:
          - '3306:3306'
        env:
          MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
          MYSQL_DATABASE: doctrine_tests
        options: >-
          --health-cmd "mysqladmin ping --silent"

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

      - name: Install PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ inputs.php-version }}
          extensions: ${{ inputs.extension }}
          coverage: pcov
          ini-values: zend.assertions=1
        env:
          fail-fast: true

      - name: Install dependencies with Composer
        uses: ramsey/composer-install@v3
        with:
          composer-options: '--ignore-platform-req=php+'

      - name: Copy TLS-related files
        run: docker cp "${{ job.services.mysql.id }}:/var/lib/mysql/ca.pem" . && docker cp "${{ job.services.mysql.id }}:/var/lib/mysql/client-cert.pem" . && docker cp "${{ job.services.mysql.id }}:/var/lib/mysql/client-key.pem" .
        if: ${{ endsWith(inputs.config-file-suffix, 'tls') }}

      - name: Run PHPUnit
        run: vendor/bin/phpunit -c ci/github/phpunit/${{ inputs.extension }}${{ inputs.config-file-suffix }}.xml --coverage-clover=coverage.xml

      - name: Upload coverage file
        uses: actions/upload-artifact@v4
        with:
          name: ${{ github.job }}-${{ inputs.mysql-version }}-php-${{ inputs.php-version }}-${{ inputs.extension }}${{ inputs.config-file-suffix }}.coverage
          path: coverage.xml
