#!/usr/bin/env bash

#: exec_target = cli

## Run project's Behat tests.
##
## Usage: fin behat [--profile=profile] [--path=path] [arguments]
##
## path     defaults to tests/behat
## profile  defaults to docksal

# Abort if anything fails
set -e

# Environment variables passed from fin:
#
#   $PROJECT_ROOT - (string) absolute path to NEAREST .docksal folder
#   $VIRTUAL_HOST - (string) ex. projectname.docksal
#   $DOCROOT - name of the docroot folder
#   $DOCKER_RUNNING - (string) "true" or "false"

params=''
path='docroot/profiles/contrib/camp'
profile=default
file=behat.yml
for i in "$@"; do
	case "$i" in
	    --profile=*)
            profile="${i#*=}"
            ;;
		--path=*)
			path="${i#*=}"
			;;
		--file=*)
			file="${i#*=}"
			;;
		*)
			params="$params$i "
			;;
	esac;
done

behat_yml_path="${PROJECT_ROOT}/${path}/${file}"

if [[ ! -f "${behat_yml_path}" ]]; then
	echo "Could not find ${behat_yml_path}"
	exit 1
fi

cd ${PROJECT_ROOT}

# Launch Behat tests
cd $path && /var/www/bin/behat --config ${behat_yml_path} -p ${profile} ${params}
