name: "Continuous Integration"

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

env:
  fail-fast: true

jobs:
  phpunit-smoke-check:
    name: "PHPUnit with SQLite"
    runs-on: "${{ matrix.os }}"

    strategy:
      matrix:
        os:
          - "ubuntu-22.04"
        php-version:
          - "7.4"
          - "8.0"
          - "8.1"
          - "8.2"
        dependencies:
          - "highest"
        extension:
          - "pdo_sqlite"
        include:
          - os: "ubuntu-20.04"
            php-version: "7.4"
            dependencies: "lowest"
            extension: "pdo_sqlite"
          - os: "ubuntu-22.04"
            php-version: "7.4"
            dependencies: "highest"
            extension: "sqlite3"
          - os: "ubuntu-22.04"
            php-version: "8.1"
            dependencies: "highest"
            extension: "sqlite3"

    steps:
      - name: "Checkout"
        uses: "actions/checkout@v3"
        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"

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

      - name: "Print SQLite version"
        run: >
          php -r 'printf("Testing with libsqlite version %s\n", (new PDO("sqlite::memory:"))->query("select sqlite_version()")->fetch()[0]);'
        if: "${{ matrix.extension == 'pdo_sqlite' }}"

      - name: "Print SQLite version"
        run: >
          php -r 'printf("Testing with libsqlite version %s\n", SQLite3::version()["versionString"]);'
        if: "${{ matrix.extension == 'sqlite3' }}"

      - 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@v3"
        with:
          name: "phpunit-${{ matrix.extension }}-${{ matrix.deps }}-${{ matrix.php-version }}.coverage"
          path: "coverage.xml"

  phpunit-oci8:
    name: "PHPUnit on OCI8"
    runs-on: "ubuntu-22.04"
    needs: "phpunit-smoke-check"

    strategy:
      matrix:
        php-version:
          - "7.4"
          - "8.1"
          - "8.2"
        oracle-version:
          - "21"
        include:
          - php-version: "7.4"
            oracle-version: "11"

    services:
      oracle:
        image: gvenzl/oracle-xe:${{ matrix.oracle-version }}
        env:
          ORACLE_PASSWORD: oracle
        ports:
          - "1521:1521"
        options: >-
          --health-cmd healthcheck.sh
          --health-interval 20s
          --health-timeout 10s
          --health-retries 10

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

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

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

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

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

  phpunit-pdo-oci:
    name: "PHPUnit on PDO_OCI"
    runs-on: "ubuntu-22.04"
    needs: "phpunit-smoke-check"

    strategy:
      matrix:
        php-version:
          - "7.4"
          - "8.1"
          - "8.2"
        oracle-version:
          - "21"
        include:
          - php-version: "7.4"
            oracle-version: "11"

    services:
      oracle:
        image: gvenzl/oracle-xe:${{ matrix.oracle-version }}
        env:
          ORACLE_PASSWORD: oracle
        ports:
          - "1521:1521"
        options: >-
          --health-cmd healthcheck.sh
          --health-interval 20s
          --health-timeout 10s
          --health-retries 10

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

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

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

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

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

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

    strategy:
      matrix:
        php-version:
          - "7.4"
        postgres-version:
          - "9.4"
          - "15"
        extension:
          - "pgsql"
          - "pdo_pgsql"
        include:
          - php-version: "8.1"
            postgres-version: "15"
            extension: "pgsql"
          - php-version: "8.2"
            postgres-version: "15"
            extension: "pgsql"
          - php-version: "8.1"
            postgres-version: "15"
            extension: "pdo_pgsql"
          - php-version: "8.2"
            postgres-version: "15"
            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@v3"
        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"

      - name: "Install dependencies with Composer"
        uses: "ramsey/composer-install@v2"
        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@v3"
        with:
          name: "${{ github.job }}-${{ matrix.postgres-version }}-${{ matrix.extension }}-${{ matrix.php-version }}.coverage"
          path: "coverage.xml"

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

    strategy:
      matrix:
        php-version:
          - "7.4"
        mariadb-version:
          - "10.0"
          - "10.2"
          - "10.5"
          - "10.7"
          - "10.9"
          - "10.11"
        extension:
          - "mysqli"
          - "pdo_mysql"
        include:
          - php-version: "8.1"
            mariadb-version: "10.7"
            extension: "mysqli"
          - php-version: "8.1"
            mariadb-version: "10.7"
            extension: "pdo_mysql"
          - php-version: "8.1"
            mariadb-version: "10.11"
            extension: "mysqli"
          - php-version: "8.1"
            mariadb-version: "10.11"
            extension: "pdo_mysql"
          - php-version: "8.2"
            mariadb-version: "10.11"
            extension: "mysqli"
          - php-version: "8.2"
            mariadb-version: "10.11"
            extension: "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@v3"
        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"
          extensions: "${{ matrix.extension }}"

      - name: "Install dependencies with Composer"
        uses: "ramsey/composer-install@v2"
        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@v3"
        with:
          name: "${{ github.job }}-${{ matrix.mariadb-version }}-${{ matrix.extension }}-${{ matrix.php-version }}.coverage"
          path: "coverage.xml"

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

    strategy:
      matrix:
        php-version:
          - "7.4"
          - "8.0"
        mysql-version:
          - "5.7"
          - "8.0"
        extension:
          - "mysqli"
          - "pdo_mysql"
        config-file-suffix:
          - ""
        include:
          - mysql-version: "5.7"
          - mysql-version: "8.0"
            # https://stackoverflow.com/questions/60902904/how-to-pass-mysql-native-password-to-mysql-service-in-github-actions
            custom-entrypoint: >-
              --entrypoint sh mysql:8 -c "exec docker-entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password"
          - config-file-suffix: "-tls"
            php-version: "7.4"
            mysql-version: "8.0"
            extension: "mysqli"
          - php-version: "8.1"
            mysql-version: "8.0"
            extension: "mysqli"
            custom-entrypoint: >-
              --entrypoint sh mysql:8 -c "exec docker-entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password"
          - php-version: "8.1"
            mysql-version: "8.0"
            extension: "pdo_mysql"
            custom-entrypoint: >-
              --entrypoint sh mysql:8 -c "exec docker-entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password"
          - php-version: "8.2"
            mysql-version: "8.0"
            extension: "mysqli"
          - php-version: "8.2"
            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
          ${{ matrix.custom-entrypoint }}

        ports:
          - "3306:3306"

    steps:
      - name: "Checkout"
        uses: "actions/checkout@v3"
        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"
          extensions: "${{ matrix.extension }}"

      - name: "Install dependencies with Composer"
        uses: "ramsey/composer-install@v2"
        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(matrix.config-file-suffix, 'tls') }}"

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

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

  phpunit-mssql:
    name: "PHPUnit with SQL Server"
    runs-on: "ubuntu-20.04"
    needs: "phpunit-smoke-check"

    strategy:
      matrix:
        php-version:
          - "7.4"
          - "8.1"
          - "8.2"
        extension:
          - "sqlsrv"
          - "pdo_sqlsrv"
        collation:
          - "Latin1_General_100_CI_AS_SC_UTF8"
        include:
          - collation: "Latin1_General_100_CS_AS_SC_UTF8"
            php-version: "7.4"
            extension: "sqlsrv"
          - collation: "Latin1_General_100_CS_AS_SC_UTF8"
            php-version: "7.4"
            extension: "pdo_sqlsrv"

    services:
      mssql:
        image: "mcr.microsoft.com/mssql/server:2019-latest"
        env:
          ACCEPT_EULA: "Y"
          SA_PASSWORD: "Doctrine2018"
          MSSQL_COLLATION: "${{ matrix.collation }}"

        options: >-
          --health-cmd "echo quit | /opt/mssql-tools/bin/sqlcmd -S 127.0.0.1 -l 1 -U sa -P Doctrine2018"

        ports:
          - "1433:1433"

    steps:
      - name: "Checkout"
        uses: "actions/checkout@v3"
        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"
          tools: "pecl"
          extensions: "${{ matrix.extension }}-5.10.0beta1"

      - name: "Install dependencies with Composer"
        uses: "ramsey/composer-install@v2"
        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@v3"
        with:
          name: "${{ github.job }}-${{ matrix.extension }}-${{ matrix.php-version }}-${{ matrix.collation }}.coverage"
          path: "coverage.xml"

  phpunit-ibm-db2:
    name: "PHPUnit with IBM DB2"
    runs-on: "ubuntu-22.04"
    needs: "phpunit-smoke-check"

    strategy:
      matrix:
        php-version:
          - "7.4"
          - "8.2"

    services:
      ibm_db2:
        image: "icr.io/db2_community/db2:11.5.8.0"
        env:
          DB2INST1_PASSWORD: "Doctrine2018"
          LICENSE: "accept"
          DBNAME: "doctrine"

        options: "--privileged=true"

        ports:
          - "50000:50000"

    steps:
      - name: "Perform healthcheck from the outside"
        run: "docker logs -f ${{ job.services.ibm_db2.id }} | sed '/(*) Setup has completed./ q'"

      - name: "Create temporary tablespace"
        run: "docker exec ${{ job.services.ibm_db2.id }} su - db2inst1 -c 'db2 -t CONNECT TO doctrine; db2 -t CREATE USER TEMPORARY TABLESPACE doctrine_tbsp PAGESIZE 4 K;'"

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

      - name: "Install IBM DB2 CLI driver"
        working-directory: /tmp
        run: |
          wget https://public.dhe.ibm.com/ibmdl/export/pub/software/data/db2/drivers/odbc_cli/linuxx64_odbc_cli.tar.gz
          tar xf linuxx64_odbc_cli.tar.gz
          rm linuxx64_odbc_cli.tar.gz

      - name: "Install PHP"
        uses: "shivammathur/setup-php@v2"
        with:
          php-version: "${{ matrix.php-version }}"
          extensions: "ibm_db2"
          coverage: "pcov"
          ini-values: "zend.assertions=1, ibm_db2.instance_name=db2inst1"
        env:
          IBM_DB2_CONFIGURE_OPTS: "--with-IBM_DB2=/tmp/clidriver"

      - name: "Install dependencies with Composer"
        uses: "ramsey/composer-install@v2"

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

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

  development-deps:
    name: "PHPUnit with PDO_SQLite and development dependencies"
    runs-on: "ubuntu-22.04"

    strategy:
      matrix:
        php-version:
          - "7.4"

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

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

      - name: "Lower minimum stability"
        run: "composer config minimum-stability dev"

      - name: "Install development dependencies with Composer"
        uses: "ramsey/composer-install@v2"
        with:
          composer-options: "--prefer-dist"

      - name: "Run PHPUnit"
        run: "vendor/bin/phpunit -c ci/github/phpunit/pdo_sqlite.xml"

  upload_coverage:
    name: "Upload coverage to Codecov"
    runs-on: "ubuntu-22.04"
    needs:
      - "phpunit-smoke-check"
      - "phpunit-oci8"
      - "phpunit-pdo-oci"
      - "phpunit-postgres"
      - "phpunit-mariadb"
      - "phpunit-mysql"
      - "phpunit-mssql"
      - "phpunit-ibm-db2"

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

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

      - name: "Display structure of downloaded files"
        run: ls -R
        working-directory: reports

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