name: "Continuous Integration"

on:
  pull_request:
    branches:
      - "*.x"
    paths:
      - .github/workflows/continuous-integration.yml
      - ci/**
      - composer.*
      - src/**
      - phpunit.xml.dist
      - tests/**
  push:
    branches:
      - "*.x"
    paths:
      - .github/workflows/continuous-integration.yml
      - ci/**
      - composer.*
      - src/**
      - phpunit.xml.dist
      - tests/**

env:
  fail-fast: true

jobs:
  phpunit-smoke-check:
    name: "PHPUnit with SQLite"
    runs-on: "ubuntu-22.04"

    strategy:
      matrix:
        php-version:
          - "8.1"
          - "8.2"
          - "8.3"
        dbal-version:
          - "default"
          - "3.7"
        extension:
          - "sqlite3"
          - "pdo_sqlite"
        deps:
          - "highest"
        include:
          - php-version: "8.2"
            dbal-version: "4@dev"
            extension: "pdo_sqlite"
          - php-version: "8.2"
            dbal-version: "4@dev"
            extension: "sqlite3"
          - php-version: "8.1"
            dbal-version: "default"
            deps: "lowest"
            extension: "pdo_sqlite"

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

      - name: "Install PHP"
        uses: "shivammathur/setup-php@v2"
        with:
          php-version: "${{ matrix.php-version }}"
          extensions: "apcu, pdo, ${{ matrix.extension }}"
          coverage: "pcov"
          ini-values: "zend.assertions=1, apc.enable_cli=1"

      - name: "Require specific DBAL version"
        run: "composer require doctrine/dbal ^${{ matrix.dbal-version }} --no-update"
        if: "${{ matrix.dbal-version != 'default' }}"

      - name: "Install dependencies with Composer"
        uses: "ramsey/composer-install@v3"
        with:
          composer-options: "--ignore-platform-req=php+"
          dependency-versions: "${{ matrix.deps }}"

      - name: "Run PHPUnit"
        run: "vendor/bin/phpunit -c ci/github/phpunit/${{ matrix.extension }}.xml --coverage-clover=coverage-no-cache.xml"
        env:
            ENABLE_SECOND_LEVEL_CACHE: 0

      - name: "Run PHPUnit with Second Level Cache"
        run: "vendor/bin/phpunit -c ci/github/phpunit/${{ matrix.extension }}.xml --exclude-group performance,non-cacheable,locking_functional --coverage-clover=coverage-cache.xml"
        env:
            ENABLE_SECOND_LEVEL_CACHE: 1

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


  phpunit-postgres:
    name: "PHPUnit with PostgreSQL"
    runs-on: "ubuntu-22.04"
    needs: "phpunit-smoke-check"

    strategy:
      matrix:
        php-version:
          - "8.2"
          - "8.3"
        dbal-version:
          - "default"
          - "3.7"
        postgres-version:
          - "15"
        extension:
          - pdo_pgsql
          - pgsql
        include:
          - php-version: "8.2"
            dbal-version: "4@dev"
            postgres-version: "14"
            extension: pdo_pgsql
          - php-version: "8.2"
            dbal-version: "3.7"
            postgres-version: "9.6"
            extension: pdo_pgsql

    services:
      postgres:
        image: "postgres:${{ matrix.postgres-version }}"
        env:
          POSTGRES_PASSWORD: "postgres"

        options: >-
          --health-cmd "pg_isready"

        ports:
          - "5432:5432"

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

      - name: "Install PHP"
        uses: "shivammathur/setup-php@v2"
        with:
          php-version: "${{ matrix.php-version }}"
          extensions: "pgsql pdo_pgsql"
          coverage: "pcov"
          ini-values: "zend.assertions=1, apc.enable_cli=1"

      - name: "Require specific DBAL version"
        run: "composer require doctrine/dbal ^${{ matrix.dbal-version }} --no-update"
        if: "${{ matrix.dbal-version != 'default' }}"

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

      - name: "Run PHPUnit"
        run: "vendor/bin/phpunit -c ci/github/phpunit/pdo_pgsql.xml --coverage-clover=coverage.xml"

      - name: "Upload coverage file"
        uses: "actions/upload-artifact@v4"
        with:
          name: "${{ github.job }}-${{ matrix.postgres-version }}-${{ matrix.php-version }}-${{ matrix.dbal-version }}-${{ matrix.extension }}-coverage"
          path: "coverage.xml"


  phpunit-mariadb:
    name: "PHPUnit with MariaDB"
    runs-on: "ubuntu-22.04"
    needs: "phpunit-smoke-check"

    strategy:
      matrix:
        php-version:
          - "8.2"
          - "8.3"
        dbal-version:
          - "default"
          - "3.7"
          - "4@dev"
        mariadb-version:
          - "10.9"
        extension:
          - "mysqli"
          - "pdo_mysql"

    services:
      mariadb:
        image: "mariadb:${{ matrix.mariadb-version }}"
        env:
          MYSQL_ALLOW_EMPTY_PASSWORD: yes
          MYSQL_DATABASE: "doctrine_tests"

        options: >-
          --health-cmd "mysqladmin ping --silent"

        ports:
          - "3306:3306"

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

      - name: "Require specific DBAL version"
        run: "composer require doctrine/dbal ^${{ matrix.dbal-version }} --no-update"
        if: "${{ matrix.dbal-version != 'default' }}"

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

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

      - name: "Run PHPUnit"
        run: "vendor/bin/phpunit -c ci/github/phpunit/${{ matrix.extension }}.xml --coverage-clover=coverage.xml"

      - name: "Upload coverage file"
        uses: "actions/upload-artifact@v4"
        with:
          name: "${{ github.job }}-${{ matrix.mariadb-version }}-${{ matrix.extension }}-${{ matrix.php-version }}-${{ matrix.dbal-version }}-coverage"
          path: "coverage.xml"


  phpunit-mysql:
    name: "PHPUnit with MySQL"
    runs-on: "ubuntu-22.04"
    needs: "phpunit-smoke-check"

    strategy:
      matrix:
        php-version:
          - "8.2"
          - "8.3"
        dbal-version:
          - "default"
          - "3.7"
        mysql-version:
          - "5.7"
          - "8.0"
        extension:
          - "mysqli"
          - "pdo_mysql"
        include:
          - php-version: "8.2"
            dbal-version: "4@dev"
            mysql-version: "8.0"
            extension: "mysqli"
          - php-version: "8.2"
            dbal-version: "4@dev"
            mysql-version: "8.0"
            extension: "pdo_mysql"

    services:
      mysql:
        image: "mysql:${{ matrix.mysql-version }}"

        options: >-
          --health-cmd "mysqladmin ping --silent"
          -e MYSQL_ALLOW_EMPTY_PASSWORD=yes
          -e MYSQL_DATABASE=doctrine_tests

        ports:
          - "3306:3306"

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

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

      - name: "Require specific DBAL version"
        run: "composer require doctrine/dbal ^${{ matrix.dbal-version }} --no-update"
        if: "${{ matrix.dbal-version != 'default' }}"

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

      - name: "Run PHPUnit"
        run: "vendor/bin/phpunit -c ci/github/phpunit/${{ matrix.extension }}.xml --coverage-clover=coverage-no-cache.xml"
        env:
            ENABLE_SECOND_LEVEL_CACHE: 0

      - name: "Run PHPUnit with Second Level Cache"
        run: "vendor/bin/phpunit -c ci/github/phpunit/${{ matrix.extension }}.xml --exclude-group performance,non-cacheable,locking_functional --coverage-clover=coverage-no-cache.xml"
        env:
            ENABLE_SECOND_LEVEL_CACHE: 1

      - name: "Upload coverage files"
        uses: "actions/upload-artifact@v4"
        with:
          name: "${{ github.job }}-${{ matrix.mysql-version }}-${{ matrix.extension }}-${{ matrix.php-version }}-${{ matrix.dbal-version }}-coverage"
          path: "coverage*.xml"

  upload_coverage:
    name: "Upload coverage to Codecov"
    runs-on: "ubuntu-22.04"
    needs:
      - "phpunit-smoke-check"
      - "phpunit-postgres"
      - "phpunit-mariadb"
      - "phpunit-mysql"

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

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

      - name: "Upload to Codecov"
        uses: "codecov/codecov-action@v4"
        with:
          directory: reports
        env:
          CODECOV_TOKEN: "${{ secrets.CODECOV_TOKEN }}"
