#!/bin/bash

# Script to run basic functionality tests

# Load environment variables
source "$(dirname "$0")/../deploy-config-staging.conf"

# Set working directory to plugin root
cd "$(dirname "$0")/.." || exit 1

# Ensure test directory exists
if [ ! -d "tests/basic" ]; then
    echo "Error: Basic test directory not found"
    exit 1
fi

# Determine PHPUnit command
if command -v phpunit &> /dev/null; then
    PHPUNIT_CMD="phpunit"
else
    if [ -f "./vendor/bin/phpunit" ]; then
        PHPUNIT_CMD="./vendor/bin/phpunit"
    else
        echo "PHPUnit not found. Installing PHPUnit..."
        composer require --dev phpunit/phpunit
        PHPUNIT_CMD="./vendor/bin/phpunit"
    fi
fi

echo "Using PHPUnit command: $PHPUNIT_CMD"

# Run tests
echo "Running basic functionality tests..."
$PHPUNIT_CMD --testdox \
    --configuration phpunit-basic.xml \
    --log-junit tests/test-results/basic/results.xml \
    tests/basic/

# Create results directory if it doesn't exist
mkdir -p tests/test-results/basic

# Check exit code and display results
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
    echo "Basic tests completed successfully"
    if [ -f "tests/test-results/basic/results.xml" ]; then
        echo "Test results saved to tests/test-results/basic/results.xml"
    fi
    exit 0
else
    echo "Basic tests failed with exit code $EXIT_CODE"
    if [ -f "tests/test-results/basic/results.xml" ]; then
        echo "See tests/test-results/basic/results.xml for details"
    fi
    exit $EXIT_CODE
fi