#!/bin/bash

#
# This script runs the Behat tests.
#

# Authenticate with Terminus
terminus -n auth:login --machine-token="$TERMINUS_TOKEN"

# Source $BASH_ENV
source $BASH_ENV

# Verbose mode and exit on errors
set -ex

# Create a drush alias file so that Behat tests can be executed against Pantheon.
terminus alpha:aliases --no-db-url --only=$TERMINUS_SITE
drush sa @pantheon.$TERMINUS_SITE.$TERMINUS_ENV

# Drush Behat driver fails without this option.
echo "\$options['strict'] = 0;" >> ~/.drush/pantheon.aliases.drushrc.php

# Dynamically set Behat configuration parameters
export BEHAT_PARAMS='{"extensions" : {"Behat\\MinkExtension" : {"base_url" : "https://'$TERMINUS_ENV'-'$TERMINUS_SITE'.pantheonsite.io/"}, "Drupal\\DrupalExtension" : {"drush" :   {  "alias":  "@pantheon.'$TERMINUS_SITE'.'$TERMINUS_ENV'" }}}}'

# Start headless Chrome
echo "\n Starting Chrome in headless mode ..."
google-chrome --disable-gpu --headless --remote-debugging-address=0.0.0.0 --remote-debugging-port=9222 --no-sandbox </dev/null &>/dev/null &

echo "::::::::::::::::::::::::::::::::::::::::::::::::"
echo "Behat test site: $TERMINUS_SITE.$TERMINUS_ENV"
echo "::::::::::::::::::::::::::::::::::::::::::::::::"
echo

# Run the Behat tests
./vendor/bin/behat --config=tests/behat/behat-pantheon.yml --strict --colors  "$@"
