#!/usr/bin/env bash

set -e

SYMFONY_VERSION=6.0
SYMFONY_PACKAGE=symfony/skeleton
PHP_VERSION=$(php -r "echo PHP_VERSION_ID;")

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

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

    rm -rf $APPLICATION_PATH

    composer create-project --no-progress $SYMFONY_PACKAGE:^$SYMFONY_VERSION $APPLICATION_PATH --no-interaction

    cp -r vendor/endroid/quality/application-base/* $APPLICATION_PATH
    cp -r tests/application/* $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 --no-plugins allow-plugins.endroid/installer true
        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
        composer require webapp symfony/test-pack --no-interaction

        bin/phpunit
    )

fi
