#!/bin/bash

source "${BASH_SOURCE[0]%/*}/.common.sh"


run_test() (
	local vbin="$VTE_ROOT/vendor/bin"

	if ! [[ -f "$vbin/phpunit" ]]; then
		composer_dev  # crmv@303843
	fi
	
	cd "$VTE_ROOT"
	
	local a=(
		--color=always
		--configuration "tests/phpunit.xml"
		--bootstrap     "tests/bootstrap.php"
		--no-coverage
		--testdox
		"${ARGS[@]:-tests}"
	)
	php "$vbin/phpunit" "${a[@]}"
)


# if sourced from another script, run directly
if (return 0 2>/dev/null); then
	[[ -v ARGS ]] || ARGS=()
	run_test
	return
fi


help() {
	cat <<-EOF
		Usage: ${SELF##*/}  [options..]  [phpunit options..]
		
		Run unit and functional tests.
		
		Options:
		    --help        Show this help
		    --helpt       Show tool help
		    --debug       Enable xdebug
		    --            Stop options processing
		EOF
}


parse_args() {
	local i=0
	while ((i < ${#ARGS[@]})); do
		local -n arg=ARGS[$i]
		case "$arg" in
			--debug)
				XDEBUG=1
				;;
			-h|--help)
				help
				exit 1
				;;
			--helpt)
				arg='--help'
				((i++)) || :
				continue
				;;
			--)
				array_shift ARGS
				break
				;;
			-*)
				((i++)) || :
				continue
				;;
			*)
				break
				;;
		esac
		array_shift ARGS
	done
}


main() {
	parse_args
	run_test
}

ARGS=("$@")
main
