
name: "Continuous Integration"

on:
  pull_request:
    branches:
      - "*.x"
  push:
    branches:
      - "*.x"
  schedule:
    - cron: "42 3 * * *"

env:
  fail-fast: true

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

    strategy:
      matrix:
        php-version:
          - "7.3"
          - "7.4"
          - "8.0"
          - "8.1"
        dependencies:
          - "highest"
        include:
          - dependencies: "lowest"
            php-version: "7.3"

    steps:
      - name: "Checkout"
        uses: "actions/checkout@v2"
        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@v1"
        with:
          dependency-versions: "${{ matrix.dependencies }}"

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

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

  phpunit-lower-php-versions:
    name: "PHPUnit with SQLite"
    runs-on: "ubuntu-20.04"

    strategy:
      matrix:
        php-version:
          - "7.1"
          - "7.2"

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

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

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

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

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

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

    services:
      oracle:
        image: "wnameless/oracle-xe-11g-r2"
        ports:
          - "1521:1521"

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

      - 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@v2"
        with:
          name: "${{ github.job }}-${{ matrix.php-version }}.coverage"
          path: "coverage.xml"

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

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

    services:
      oracle:
        image: "wnameless/oracle-xe-11g-r2"
        ports:
          - "1521:1521"

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

      - 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@v2"
        with:
          name: "${{ github.job }}-${{ matrix.php-version }}.coverage"
          path: "coverage.xml"

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

    strategy:
      matrix:
        php-version:
          - "7.4"
        postgres-version:
          - "9.2"
          - "9.4"
          - "13"
          - "14"
        include:
          - php-version: "8.1"
            postgres-version: "14"

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

        options: >-
          --health-cmd "pg_isready || psql -U postgres -c 'SELECT 1'"

        ports:
          - "5432:5432"

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

      - 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@v2"
        with:
          name: "${{ github.job }}-${{ matrix.postgres-version }}-${{ matrix.php-version }}.coverage"
          path: "coverage.xml"

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

    strategy:
      matrix:
        php-version:
          - "7.4"
        mariadb-version:
          - "10.0"
          - "10.2"
          - "10.5"
        extension:
          - "mysqli"
          - "pdo_mysql"
        include:
          - php-version: "8.1"
            mariadb-version: "10.5"
            extension: "mysqli"
          - php-version: "8.1"
            mariadb-version: "10.5"
            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@v2"
        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@v1"

      - 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@v2"
        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-20.04"
    needs: "phpunit-smoke-check"

    strategy:
      matrix:
        php-version:
          - "7.4"
        mysql-version:
          - "5.7"
          - "8.0"
        extension:
          - "mysqli"
          - "pdo_mysql"
        config-file-suffix:
          - ""
        include:
          - php-version: "7.3"
            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: "7.3"
            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"
          - 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"

    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@v2"
        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@v1"

      - 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@v2"
        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.3"
          - "7.4"
          - "8.1"
        extension:
          - "sqlsrv"
          - "pdo_sqlsrv"
        collation:
          - "Latin1_General_100_CI_AS"
        include:
          - collation: "Latin1_General_100_CS_AS"
            php-version: "7.4"
            extension: "sqlsrv"
          - collation: "Latin1_General_100_CS_AS"
            php-version: "7.4"
            extension: "pdo_sqlsrv"

    services:
      mssql:
        image: "mcr.microsoft.com/mssql/server:2017-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@v2"
        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@v1"

      - 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@v2"
        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-18.04"
    needs: "phpunit-smoke-check"

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

    services:
      ibm_db2:
        image: "ibmcom/db2:11.5.0.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 CONNECT TO doctrine && db2 CREATE USER TEMPORARY TABLESPACE doctrine_tbsp PAGESIZE 4 K'"

      - name: "Checkout"
        uses: "actions/checkout@v2"
        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,extension=ibm_db2.so, ibm_db2.instance_name=db2inst1"

      - name: "Install ibm_db2 extension"
        run: "ci/github/ext/install-ibm_db2.sh ${{ matrix.php-version }}"

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

      - 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@v2"
        with:
          name: "${{ github.job }}-${{ matrix.php-version }}.coverage"
          path: "coverage.xml"

  development-deps:
    name: "PHPUnit with SQLite and development dependencies"
    runs-on: "ubuntu-20.04"

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

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

      - 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@v1"
        with:
          composer-options: "--prefer-dist"

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

  upload_coverage:
    name: "Upload coverage to Codecov"
    runs-on: "ubuntu-20.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@v2"
        with:
          fetch-depth: 2

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

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

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