diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-05-26 08:39:07 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-05-26 08:39:07 +0300 |
| commit | 1cea8a710a8dffd9f8286c3b7e46a9379841e2b4 (patch) | |
| tree | 72d4be3525a2390eeec3eb0188d750b85b3c2766 /tests_integration | |
| parent | dc60880d9cc4074a231fd21c9508d0f78beaf1f1 (diff) | |
| download | the-epube-1cea8a710a8dffd9f8286c3b7e46a9379841e2b4.tar.gz the-epube-1cea8a710a8dffd9f8286c3b7e46a9379841e2b4.tar.xz | |
import integration test scaffold from tt-rss
Diffstat (limited to 'tests_integration')
| -rw-r--r-- | tests_integration/DbTestCase.php | 13 | ||||
| -rw-r--r-- | tests_integration/bootstrap.php | 10 | ||||
| -rw-r--r-- | tests_integration/pg_helper.sh | 182 | ||||
| -rw-r--r-- | tests_integration/run.sh | 218 |
4 files changed, 423 insertions, 0 deletions
diff --git a/tests_integration/DbTestCase.php b/tests_integration/DbTestCase.php new file mode 100644 index 0000000..b4c7db5 --- /dev/null +++ b/tests_integration/DbTestCase.php @@ -0,0 +1,13 @@ +<?php +use PHPUnit\Framework\TestCase; + +abstract class DbTestCase extends TestCase { + + protected function setUp(): void { + $_SESSION['uid'] = 1; + } + + protected function tearDown(): void { + unset($_SESSION['uid']); + } +} diff --git a/tests_integration/bootstrap.php b/tests_integration/bootstrap.php new file mode 100644 index 0000000..d5fd639 --- /dev/null +++ b/tests_integration/bootstrap.php @@ -0,0 +1,10 @@ +<?php + set_include_path(dirname(__DIR__) ."/include" . PATH_SEPARATOR . + get_include_path()); + + require_once "common.php"; + + require_once __DIR__ . "/DbTestCase.php"; + + // Force Config singleton to re-read the environment variables + Config::get_instance(); diff --git a/tests_integration/pg_helper.sh b/tests_integration/pg_helper.sh new file mode 100644 index 0000000..73c5ac1 --- /dev/null +++ b/tests_integration/pg_helper.sh @@ -0,0 +1,182 @@ +#!/usr/bin/env bash +# +# PostgreSQL lifecycle helper for integration tests. +# Manages a temporary PG instance: init, start, stop, db creation. +# +# Usage: +# pg_helper.sh init — initialize data directory +# pg_helper.sh start — start the server +# pg_helper.sh stop — stop the server +# pg_helper.sh create-db — create the test database +# pg_helper.sh drop-db — drop the test database +# pg_helper.sh status — print status +# +# Environment variables (with defaults): +# __EPUBE_PG_PORT — port for the test PG instance (default: 55432) +# __EPUBE_PG_DATA_DIR — temp data directory (default: auto-created in /tmp) +# __EPUBE_PG_USER — database user (default: epube_test) +# __EPUBE_PG_PASS — database password (default: epube_test) +# __EPUBE_PG_DB — database name (default: epube_test) +# __EPUBE_PG_BIN_DIR — PostgreSQL bin directory (default: detected) +# + +set -euo pipefail + +# ── Helpers ────────────────────────────────────────────────────────────────── + +log() { + echo "[$(date '+%H:%M:%S')] [pg_helper] $*" +} + +die() { + echo "ERROR: $*" >&2 + exit 1 +} + +wait_for_pg() { + local max_wait="${1:-30}" + local waited=0 + + while ! "$PG_BIN_DIR"/pg_isready -h localhost -p "$PG_PORT" -q 2>/dev/null; do + if (( waited >= max_wait )); then + die "PostgreSQL did not start within ${max_wait}s" + fi + sleep 0.5 + (( waited++ )) + done +} + +# ── Configuration ──────────────────────────────────────────────────────────── + +PG_PORT="${__EPUBE_PG_PORT:-55432}" +PG_USER="${__EPUBE_PG_USER:-epube_test}" +PG_PASS="${__EPUBE_PG_PASS:-epube_test}" +PG_DB="${__EPUBE_PG_DB:-epube_test}" + +# Superuser for admin operations (created by initdb, matches OS user) +PG_SUPERUSER="${__EPUBE_PG_SUPERUSER:-$(whoami)}" + +# Auto-detect PG bin directory +if [[ -n "${__EPUBE_PG_BIN_DIR:-}" ]]; then + PG_BIN_DIR="$__EPUBE_PG_BIN_DIR" +else + # Try common locations for PostgreSQL + for candidate in \ + "/usr/bin" \ + "/usr/lib/postgresql/16/bin" \ + "/usr/local/pgsql/bin" \ + ; do + if [[ -x "$candidate/pg_ctl" ]]; then + PG_BIN_DIR="$candidate" + break + fi + done + [[ -z "${PG_BIN_DIR:-}" ]] && die "Cannot find pg_ctl. Set __EPUBE_PG_BIN_DIR." +fi + +# Create a unique temp data directory (use PID to avoid collisions) +if [[ -n "${__EPUBE_PG_DATA_DIR:-}" ]]; then + PG_DATA_DIR="$__EPUBE_PG_DATA_DIR" +else + # Use a deterministic name based on port so init/start/use all share the same dir + PG_DATA_DIR="/tmp/ttrss-pg-${PG_PORT}" +fi + +PG_LOG_FILE="$PG_DATA_DIR/pg.log" +PG_PID_FILE="$PG_DATA_DIR/postmaster.pid" + +# ── Commands ───────────────────────────────────────────────────────────────── + +cmd_init() { + if [[ -f "$PG_DATA_DIR/PG_VERSION" ]]; then + log "Data directory already initialized: $PG_DATA_DIR" + return 0 + fi + log "Initializing PostgreSQL data directory: $PG_DATA_DIR" + "$PG_BIN_DIR"/initdb --auth=trust --auth-host=md5 --encoding=UTF8 -D "$PG_DATA_DIR" 2>&1 | tail -3 + log "Data directory initialized." +} + +cmd_start() { + if [[ -f "$PG_PID_FILE" ]] && "$PG_BIN_DIR"/pg_isready -h localhost -p "$PG_PORT" -q 2>/dev/null; then + log "PostgreSQL already running on port $PG_PORT." + return 0 + fi + + log "Starting PostgreSQL on port $PG_PORT ..." + "$PG_BIN_DIR"/pg_ctl start \ + -D "$PG_DATA_DIR" \ + -l "$PG_LOG_FILE" \ + -o "-c port=$PG_PORT -c unix_socket_directories=." \ + -w + + # Trust all local connections (TCP and socket) for testing + cat > "$PG_DATA_DIR/pg_hba.conf" <<'HBA' +# TYPE DATABASE USER ADDRESS METHOD +local all all trust +host all all 127.0.0.1/32 trust +host all all ::1/128 trust +HBA + + # Reload to pick up new pg_hba.conf + "$PG_BIN_DIR"/pg_ctl reload -D "$PG_DATA_DIR" -w + + wait_for_pg 30 + log "PostgreSQL started on port $PG_PORT." +} + +cmd_stop() { + if [[ ! -f "$PG_PID_FILE" ]]; then + log "PostgreSQL not running." + return 0 + fi + + log "Stopping PostgreSQL ..." + "$PG_BIN_DIR"/pg_ctl stop -D "$PG_DATA_DIR" -w 2>/dev/null || true + rm -f "$PG_PID_FILE" + log "PostgreSQL stopped." +} + +cmd_create_user() { + log "Creating user '$PG_USER' ..." + "$PG_BIN_DIR"/createuser -h localhost -p "$PG_PORT" -U "$PG_SUPERUSER" --superuser "$PG_USER" 2>/dev/null || true + log "User ready." +} + +cmd_create_db() { + log "Creating database '$PG_DB' ..." + # Create user first if it doesn't exist + "$PG_BIN_DIR"/createuser -h localhost -p "$PG_PORT" -U "$PG_SUPERUSER" --superuser "$PG_USER" 2>/dev/null || true + "$PG_BIN_DIR"/createdb -h localhost -p "$PG_PORT" -U "$PG_SUPERUSER" "$PG_DB" 2>/dev/null || true + log "Database ready." +} + +cmd_drop_db() { + log "Dropping database '$PG_DB' ..." + "$PG_BIN_DIR"/dropdb -h localhost -p "$PG_PORT" -U "$PG_USER" "$PG_DB" --if-exists 2>/dev/null || true +} + +cmd_status() { + if [[ -f "$PG_PID_FILE" ]]; then + echo "running (pid: $(cat "$PG_PID_FILE" | head -1))" + else + echo "stopped" + fi +} + +# ── Main ───────────────────────────────────────────────────────────────────── + +case "${1:-help}" in + init) cmd_init ;; + start) cmd_start ;; + stop) cmd_stop ;; + create-user) cmd_create_user ;; + create-db) cmd_create_db ;; + drop-db) cmd_drop_db ;; + status) cmd_status ;; + cleanup) cmd_stop; rm -rf "$PG_DATA_DIR"; log "Cleaned up $PG_DATA_DIR" ;; + *) + echo "Usage: $0 {init|start|stop|create-user|create-db|drop-db|status|cleanup}" + exit 1 + ;; +esac diff --git a/tests_integration/run.sh b/tests_integration/run.sh new file mode 100644 index 0000000..328c090 --- /dev/null +++ b/tests_integration/run.sh @@ -0,0 +1,218 @@ +#!/usr/bin/env bash +# +# Integration test runner for The Epube. +# +# Orchestrates the full lifecycle: +# 1. Start temporary PostgreSQL instance +# 2. Create database and run schema migrations +# 3. Start PHP development server +# 4. Run PHPUnit integration tests +# 5. Clean up everything +# +# Usage: +# ./tests_integration/run.sh [--cleanup-only] [--keep-db] [phpunit args...] +# + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" + +# ── Helpers ────────────────────────────────────────────────────────────────── + +log() { + echo "[$(date '+%H:%M:%S')] [run] $*" +} + +# ── Configuration ──────────────────────────────────────────────────────────── + +export PG_PORT=55432 +export PHP_PORT1=9080 +export PHP_PORT2=9081 + +export PG_USER="epube_test" +export PG_PASS="epube_test" +export PG_DB="epube_test" + +if [[ -n "${__EPUBE_PG_DATA_DIR:-}" ]]; then + export PG_DATA_DIR="$__EPUBE_PG_DATA_DIR" +fi + +PHP_BIN="" + +if [[ -n "${__EPUBE_PHP_BIN:-}" ]]; then + PHP_BIN="${__EPUBE_PHP_BIN}" +else + # Try common locations for php + for candidate in \ + "php84" \ + "php8.4" \ + "php" \ + ; do + if command -v $candidate >/dev/null; then + PHP_BIN=$(command -v $candidate) + break + fi + done +fi + +CLEANUP_ONLY=false +KEEP_DB=false +PHPUNIT_ARGS=() + +# Parse arguments +while [[ $# -gt 0 ]]; do + case "$1" in + --cleanup-only) CLEANUP_ONLY=true; shift ;; + --keep-db) KEEP_DB=true; shift ;; + -*) PHPUNIT_ARGS+=("$1"); shift ;; + *) PHPUNIT_ARGS+=("$1"); shift ;; + esac +done + +# ── Functions ──────────────────────────────────────────────────────────────── + +cleanup() { + local RC=$? + + log "Cleaning up ..." + + # Stop PHP server + if [[ -n "${PHP_SERVER1_PID:-}" ]] && kill -0 "$PHP_SERVER1_PID" 2>/dev/null; then + log "Stopping PHP server 1 (pid $PHP_SERVER1_PID) ..." + kill "$PHP_SERVER1_PID" 2>/dev/null || true + wait "$PHP_SERVER1_PID" 2>/dev/null || true + fi + + if [[ -n "${PHP_SERVER2_PID:-}" ]] && kill -0 "$PHP_SERVER2_PID" 2>/dev/null; then + log "Stopping PHP server 2 (pid $PHP_SERVER2_PID) ..." + kill "$PHP_SERVER2_PID" 2>/dev/null || true + wait "$PHP_SERVER2_PID" 2>/dev/null || true + fi + + # Stop PostgreSQL + if [[ -n "${PG_HELPER:-}" ]]; then + "$PG_HELPER" stop 2>/dev/null || true + if [[ "$KEEP_DB" != "true" ]]; then + "$PG_HELPER" cleanup 2>/dev/null || true + fi + fi + + # if [ $RC != 0 ]; then + # cat "$PROJECT_ROOT/tests_integration/php_server1.log" + # cat "$PROJECT_ROOT/tests_integration/php_server2.log" + # fi + + rm -f -- "$PROJECT_ROOT/tests_integration/php_server1.log" + rm -f -- "$PROJECT_ROOT/tests_integration/php_server2.log" +} + +# ── Main ───────────────────────────────────────────────────────────────────── + +# Register cleanup trap +trap cleanup EXIT INT TERM + +# Cleanup-only mode +if [[ "$CLEANUP_ONLY" == "true" ]]; then + log "Cleanup-only mode." + exit 0 +fi + +# Ensure we're in the project root +cd "$PROJECT_ROOT" + +# Make sure pg_helper is executable +PG_HELPER="$SCRIPT_DIR/pg_helper.sh" + +log "═══════════════════════════════════════════════════════" +log " The Epube Integration Test Runner" +log "═══════════════════════════════════════════════════════" +log "PHP: $PHP_BIN" +log "PG Port: $PG_PORT" +log "PHP Server Port: $PHP_PORT1 / $PHP_PORT2" +log "PG Data Dir: ${PG_DATA_DIR:-<auto>}" +log "───────────────────────────────────────────────────────" + +# Step 1: Initialize PostgreSQL +log "Step 1/5: Initializing PostgreSQL ..." +"$PG_HELPER" init +"$PG_HELPER" start + +# Step 2: Create database user and database +log "Step 2/5: Creating database user and database ..." +"$PG_HELPER" create-user +"$PG_HELPER" create-db + +# Step 3: Run schema migrations +log "Step 3/5: Running schema migrations ..." + +export EPUBE_DB_TYPE=pgsql +export EPUBE_DB_HOST=localhost +export EPUBE_DB_PORT=${PG_PORT} +export EPUBE_DB_NAME="${PG_DB}" +export EPUBE_DB_USER="${PG_USER}" +export EPUBE_DB_PASS="${PG_PASS}" +export EPUBE_DB_SSLMODE=disable +export EPUBE_SINGLE_USER_MODE=1 +export OTEL_PHP_AUTOLOAD_ENABLED=false +export OTEL_TRACES_EXPORTER=none +export OTEL_METRICS_EXPORTER=none +export OTEL_LOGS_EXPORTER=none +export OTEL_PHP_DISABLED_INSTRUMENTATIONS=pdo,guzzle,psr18,slim,io +export IS_INTEGRATION_TESTING=true + +$PHP_BIN ./update.php --update-schema=force-yes + +# Step 4: Start PHP development server +log "Step 4/5: Starting PHP development servers, ports: $PHP_PORT1 / $PHP_PORT2..." + +# Start PHP server in background, redirect its output +nohup "$PHP_BIN" -S "0.0.0.0:$PHP_PORT1" -t "$PROJECT_ROOT" \ + > "$PROJECT_ROOT/tests_integration/php_server1.log" 2>&1 & +PHP_SERVER1_PID=$! + +# Since it is single-threaded we need to run another one for mock outgoing requests +nohup "$PHP_BIN" -S "0.0.0.0:$PHP_PORT2" -t "$PROJECT_ROOT" \ + > "$PROJECT_ROOT/tests_integration/php_server2.log" 2>&1 & +PHP_SERVER2_PID=$! + +# Wait for PHP server to be ready +log "Waiting for PHP server 1 to start ..." +for i in $(seq 1 30); do + if curl -s "http://localhost:$PHP_PORT1/api/" >/dev/null 2>&1; then + log "PHP server is ready." + break + fi + if (( i == 30 )); then + log "PHP server did not start in time. Logs:" + cat "$PROJECT_ROOT/tests_integration/php_server1.log" >&2 + exit 1 + fi + sleep 0.5 +done + +log "Waiting for PHP server 2 to start ..." +for i in $(seq 1 30); do + if curl -s "http://localhost:$PHP_PORT2/api/" >/dev/null 2>&1; then + log "PHP server is ready." + break + fi + if (( i == 30 )); then + log "PHP server did not start in time. Logs:" + cat "$PROJECT_ROOT/tests_integration/php_server2.log" >&2 + exit 1 + fi + sleep 0.5 +done + +# Step 5: Run PHPUnit +log "Step 5/5: Running PHPUnit integration tests ..." + +# Export env vars for tests +export API_URL="http://localhost:$PHP_PORT1/api/index.php" +export APP_URL="http://localhost:$PHP_PORT2" + +# Run PHPUnit with the integration testsuite +"$PHP_BIN" vendor/bin/phpunit \ + --configuration "$PROJECT_ROOT/phpunit.integration.xml" \ + "${PHPUNIT_ARGS[@]+"${PHPUNIT_ARGS[@]}"}" |
