#!/usr/bin/env bash

set -e

if [[ `basename "$PWD"` == *"-bundle" ]]; then

    echo "Running functional tests ..."

    BUNDLE_PATH=`pwd`
    BUNDLE_NAME=endroid/"$(basename "$BUNDLE_PATH")"
    APPLICATION_PATH=`pwd`/vendor/endroid/quality/application
    SYMFONY_VERSION=^5.2

    if [ ! -z $1 ]; then
        SYMFONY_VERSION=$1
    fi

    rm -rf $APPLICATION_PATH

    composer create-project --no-progress symfony/website-skeleton:$SYMFONY_VERSION $APPLICATION_PATH

    cp composer $APPLICATION_PATH/composer
    cp -r tests/application "$(dirname "$APPLICATION_PATH")"
    mkdir -p $APPLICATION_PATH/vendor/endroid
    rsync -a --exclude tests --exclude vendor $BUNDLE_PATH $APPLICATION_PATH/vendor/endroid

    (
        cd $APPLICATION_PATH
        composer config extra.symfony.allow-contrib true
        composer config prefer-stable true
        composer config repositories.$BUNDLE_NAME path $APPLICATION_PATH/vendor/$BUNDLE_NAME
        composer require $BUNDLE_NAME @dev

        if [ "$1" = "^3.4" ]; then
            sed -i 's/excluded_http_codes/#excluded_http_codes/g' $APPLICATION_PATH/config/packages/test/monolog.yaml
        fi

        bin/phpunit
    )

fi
