#!/usr/bin/env bash
set -o errexit  # Exit the script with error if any of the commands fail

# Supported environment variables
API_VERSION=${API_VERSION:-} # Optional API_VERSION environment variable
CRYPT_SHARED_LIB_PATH="${CRYPT_SHARED_LIB_PATH:-}" # Optional path to crypt_shared library
DRIVER_MONGODB_VERSION=${DRIVER_MONGODB_VERSION:-} # Required if IS_MATRIX_TESTING is "true"
IS_MATRIX_TESTING=${IS_MATRIX_TESTING:-} # Specify "true" to enable matrix testing. Defaults to empty string. If "true", DRIVER_MONGODB_VERSION and MONGODB_VERSION will also be checked.
MONGODB_URI=${MONGODB_URI:-} # Connection string (including credentials and topology info)
MONGODB_SINGLE_MONGOS_LB_URI=${MONGODB_SINGLE_MONGOS_LB_URI:-} # Single-mongos LB connection string
MONGODB_MULTI_MONGOS_LB_URI=${MONGODB_MULTI_MONGOS_LB_URI:-} # Multi-mongos LB connection string
MONGODB_VERSION=${MONGODB_VERSION:-} # Required if IS_MATRIX_TESTING is "true"
SSL=${SSL:-no} # Specify "yes" to enable SSL. Defaults to "no"
TESTS=${TESTS:-} # Optional test group. Defaults to all tests

# For matrix testing, we have to determine the correct driver version
if [ "${IS_MATRIX_TESTING}" = "true" ]; then
   case "${DRIVER_MONGODB_VERSION}" in
      '4.4')
         export EXTENSION_VERSION='1.8.2'
         ;;
      '4.2')
         export EXTENSION_VERSION='1.6.1'
         ;;
      '4.0')
         export EXTENSION_VERSION='1.5.5'
         ;;
   esac

   case "${MONGODB_VERSION}" in
      latest)
         MONGODB_VERSION_NUMBER='5.0'
         ;;
      *)
         MONGODB_VERSION_NUMBER=$MONGODB_VERSION
         ;;
   esac

   PHPUNIT_OPTS="--dont-report-useless-tests --exclude-group matrix-testing-exclude-server-${MONGODB_VERSION_NUMBER}-driver-${DRIVER_MONGODB_VERSION},matrix-testing-exclude-server-${MONGODB_VERSION_NUMBER}-driver-${DRIVER_MONGODB_VERSION}-topology-${TOPOLOGY}"

   DIR=$(dirname $0)
   . $DIR/install-dependencies.sh
fi

# Enable verbose output to see skipped and incomplete tests
PHPUNIT_OPTS="${PHPUNIT_OPTS} --configuration phpunit.evergreen.xml"

if [ "$SSL" = "yes" ]; then
   SSL_OPTS="ssl=true&sslallowinvalidcertificates=true"

   # Determine if MONGODB_URI already has a query string
   SUFFIX=$(echo "$MONGODB_URI" | grep -Eo "\?(.*)" | cat)

   if [ -z "$SUFFIX" ]; then
      MONGODB_URI="${MONGODB_URI}/?${SSL_OPTS}"
   else
      MONGODB_URI="${MONGODB_URI}&${SSL_OPTS}"
   fi

   # Assume LB URIs already have a query string (e.g. "?loadBalanced=true")
   if [ -n "${MONGODB_SINGLE_MONGOS_LB_URI}" ]; then
      MONGODB_SINGLE_MONGOS_LB_URI="${MONGODB_SINGLE_MONGOS_LB_URI}&${SSL_OPTS}"
   fi

   if [ -n "${MONGODB_MULTI_MONGOS_LB_URI}" ]; then
      MONGODB_MULTI_MONGOS_LB_URI="${MONGODB_MULTI_MONGOS_LB_URI}&${SSL_OPTS}"
   fi
fi

echo "Running tests with URI: $MONGODB_URI"

# Disable PHPUnit test failures due to deprecations
# See: https://symfony.com/doc/current/components/phpunit_bridge.html#internal-deprecations
export SYMFONY_DEPRECATIONS_HELPER=999999

# Export environment vars that may be referenced by the test suite
export API_VERSION="${API_VERSION}"
export CRYPT_SHARED_LIB_PATH="${CRYPT_SHARED_LIB_PATH}"
export MONGODB_URI="${MONGODB_URI}"
export MONGODB_SINGLE_MONGOS_LB_URI="${MONGODB_SINGLE_MONGOS_LB_URI}"
export MONGODB_MULTI_MONGOS_LB_URI="${MONGODB_MULTI_MONGOS_LB_URI}"

# Run the tests, and store the results in a junit result file
case "$TESTS" in
   atlas)
      php vendor/bin/phpunit $PHPUNIT_OPTS --group atlas
      ;;

   csfle)
      php vendor/bin/phpunit $PHPUNIT_OPTS --group csfle
      ;;

   csfle-without-aws-creds)
      php vendor/bin/phpunit $PHPUNIT_OPTS --group csfle-without-aws-creds
      ;;

   versioned-api)
      php vendor/bin/phpunit $PHPUNIT_OPTS --group versioned-api
      ;;

   *)
      php vendor/bin/phpunit $PHPUNIT_OPTS
      ;;
esac
