name: "Tests"

on:
  pull_request:
    branches:
      - "v*.*"
      - "master"
      - "feature/*"
  push:
    branches:
      - "v*.*"
      - "master"
      - "feature/*"

jobs:
  tests:
    name: "Tests"
    runs-on: "${{ matrix.os }}"

    strategy:
      fail-fast: true
      matrix:
        os:
          - "ubuntu-20.04"
        php-version:
          - "7.4"
          - "8.0"
          - "8.1"
          - "8.2"
        mongodb-version:
          - "4.4"
        topology:
          - "server"
        include:
          - os: "ubuntu-20.04"
            php-version: "8.0"
            mongodb-version: "6.0"
            topology: "replica_set"
          - os: "ubuntu-20.04"
            php-version: "8.0"
            mongodb-version: "6.0"
            topology: "sharded_cluster"
          - os: "ubuntu-20.04"
            php-version: "8.0"
            mongodb-version: "4.4"
            topology: "replica_set"
          - os: "ubuntu-20.04"
            php-version: "8.0"
            mongodb-version: "4.4"
            topology: "sharded_cluster"

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

      - id: setup-mongodb
        uses: mongodb-labs/drivers-evergreen-tools@master
        with:
          version: ${{ matrix.mongodb-version }}
          topology: ${{ matrix.topology }}

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

      - name: "Configure driver"
        run: .github/workflows/configure.sh

      - name: "Build driver"
        run: "make all"

      - name: "Run Tests"
        run: TEST_PHP_ARGS="-q -x --show-diff -g FAIL,XFAIL,BORK,WARN,LEAK,SKIP" make test
        env:
          MONGODB_URI: ${{ steps.setup-mongodb.outputs.cluster-uri }}

  windows-tests:
    name: "Windows Tests"
    runs-on: ${{ matrix.os }}
    defaults:
      run:
        shell: cmd

    strategy:
      fail-fast: true
      matrix:
        os: [ windows-2019, windows-2022 ]
        php: [ "7.2", "7.3", "7.4", "8.0", "8.1", "8.2" ]
        arch: [ x64, x86 ]
        ts: [ ts, nts ]
        exclude:
          - { os: windows-2019, php: "8.2" }
          - { os: windows-2019, php: "8.1" }
          - { os: windows-2019, php: "8.0" }
          - { os: windows-2019, php: "7.4" }
          - { os: windows-2019, php: "7.3" }
          - { os: windows-2022, php: "7.2" }

    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 2
          submodules: true

      - name: Setup PHP SDK
        id: setup-php
        uses: cmb69/setup-php-sdk@v0.7
        with:
          version: ${{ matrix.php }}
          arch: ${{ matrix.arch }}
          ts: ${{ matrix.ts }}
          deps: openssl

      - name: Enable Developer Command Prompt
        uses: ilammy/msvc-dev-cmd@v1
        with:
          arch: ${{ matrix.arch }}
          toolset: ${{ steps.setup-php.outputs.toolset }}

      - name: phpize
        run: phpize

      - name: configure
        run: configure --enable-mongodb --with-mongodb-sasl=yes --with-mongodb-client-side-encryption=yes --enable-debug-pack --with-prefix=${{ steps.setup-php.outputs.prefix }}

      - name: nmake
        run: nmake /nologo

      - name: Start MongoDB
        run: |
          sc config MongoDB start= auto
          sc start MongoDB

      - name: Wait until MongoDB is available
        run: .github/workflows/wait-for-mongodb.bat

      - name: Run Tests
        run: nmake /nologo test
        env:
          NO_INTERACTION: 1
          REPORT_EXIT_STATUS: 1
          TESTS: --show-diff

      - name: Copy DLL and PDB files to CWD
        if: ${{ github.event_name == 'push' }}
        run: |
          cp .github/workflows/get-build-dir.bat .
          for /F "usebackq tokens=*" %%i in (`get-build-dir.bat`) do set BUILD_DIR=%%i
          echo BUILD_DIR=%BUILD_DIR%
          cp %BUILD_DIR%\php_mongodb.dll .
          cp %BUILD_DIR%\php_mongodb.pdb .

      - name: Upload DLL and PDB files as build artifacts
        if: ${{ github.event_name == 'push' }}
        uses: actions/upload-artifact@v3
        with:
          name: php_mongodb-${{ github.sha }}-${{ matrix.php }}-${{ matrix.ts }}-${{ matrix.arch }}
          path: |
            php_mongodb.dll
            php_mongodb.pdb
          retention-days: 3
