#!/usr/bin/env bash

#ddev-generated
## Command provided by https://github.com/ddev/ddev-drupal-contrib
## Description: Run phpunit inside the web container
## Usage: phpunit [flags] [args]
## Example: "ddev phpunit" or "ddev phpunit --stop-on-failure"
## ExecRaw: true

set -eu -o pipefail

if ! command -v phpunit >/dev/null; then
  echo "phpunit is not available. You may need to 'ddev composer install'"
  exit 1
fi

if [ -f "phpunit.xml" ]; then
    # If there is local config, defer to it.
    phpunit "$@"
elif [ $# -eq 0 ]; then
    # If there are no arguments, run all custom tests.
    phpunit --bootstrap $PWD/$DDEV_DOCROOT/core/tests/bootstrap.php $DDEV_DOCROOT/$DRUPAL_PROJECTS_PATH
else
    # If there are arguments, allow developers the convenience of specifying
    # easy "top-level" paths by prefixing with "web/modules/custom" as needed.
    PROJECT_NAME="${DDEV_SITENAME//-/_}"
    args=()
    for arg in "$@"; do
        if [[ "$arg" != *"${DDEV_DOCROOT:-web}/"* ]] && [ -f "$arg" ] && [ -f "$DDEV_DOCROOT/$DRUPAL_PROJECTS_PATH/$PROJECT_NAME/$arg" ]; then
            args+=("$DDEV_DOCROOT/$DRUPAL_PROJECTS_PATH/$PROJECT_NAME/$arg")
        else
            args+=("$arg")
        fi
    done
    phpunit --bootstrap $PWD/$DDEV_DOCROOT/core/tests/bootstrap.php "${args[@]}"
fi
