blob: 23f5f04564669e2d8b1f528cb3999ffe7d769c68 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
#!/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 dict mock server
# 4. Start PHP development server
# 5. Run PHPUnit integration tests
# 6. 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 dict mock server
if [[ -n "${DICT_MOCK_PID:-}" ]] && kill -0 "$DICT_MOCK_PID" 2>/dev/null; then
log "Stopping dict mock server (pid $DICT_MOCK_PID) ..."
kill "$DICT_MOCK_PID" 2>/dev/null || true
wait "$DICT_MOCK_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"
rm -f -- "$PROJECT_ROOT/tests_integration/dict_mock.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_BOOKS_DIR="$(pwd)/tests_integration/test_data/books"
export EPUBE_CALIBRE_DB="${EPUBE_BOOKS_DIR}/metadata.db"
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
export XDEBUG_MODE=coverage
$PHP_BIN ./update.php --update-schema=force-yes
$PHP_BIN ./update.php --user-add "test:test"
# Step 4: Start dict mock server
log "Step 4/6: Starting dict mock server ..."
DICT_MOCK_PORT=9410
nohup "$PHP_BIN" "$SCRIPT_DIR/dict_mock_server.php" "$DICT_MOCK_PORT" \
> "$PROJECT_ROOT/tests_integration/dict_mock.log" 2>&1 &
DICT_MOCK_PID=$!
export EPUBE_DICT_SERVER="127.0.0.1:$DICT_MOCK_PORT"
# Wait for dict mock server to be ready
for i in $(seq 1 20); do
if nc -z 127.0.0.1 "$DICT_MOCK_PORT" 2>/dev/null; then
# Verify dict protocol banner
BANNER=$(echo "" | nc -w 1 127.0.0.1 "$DICT_MOCK_PORT" -vv | head -1)
if [[ "$BANNER" == "120"* ]]; then
log "Dict mock server is ready on port $DICT_MOCK_PORT."
break
fi
fi
if (( i == 20 )); then
log "Dict mock server did not start in time."
cat "$PROJECT_ROOT/tests_integration/dict_mock.log" >&2
exit 1
fi
sleep 0.2
done
# Step 5: Start PHP development server
log "Step 5/6: 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 -fs "http://localhost:$PHP_PORT1/backend.php?op=healthz" >/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 -fs "http://localhost:$PHP_PORT2/backend.php?op=healthz" >/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
# curl -vsL "http://localhost:$PHP_PORT1/login.php?op=perform-login&user=test&password=test"
# curl -vs "http://localhost:$PHP_PORT1/backend.php?op=healthz"
# Step 6: Run PHPUnit
log "Step 6/6: Running PHPUnit integration tests ..."
# Export env vars for tests
export API_URL="http://localhost:$PHP_PORT1/backend.php"
export APP_URL="http://localhost:$PHP_PORT2"
# Run PHPUnit with the integration testsuite
"$PHP_BIN" -d zend_extension=xdebug vendor/bin/phpunit \
--configuration "$PROJECT_ROOT/phpunit.integration.xml" \
"${PHPUNIT_ARGS[@]+"${PHPUNIT_ARGS[@]}"}"
|