|
259 | 259 | require_once __DIR__ . '/ProclaimTestCase.php'; |
260 | 260 |
|
261 | 261 | // --------------------------------------------------------------------------- |
262 | | -// Optional: Bootstrap a real database connection from build.properties |
| 262 | +// Optional: Bootstrap a real database connection |
| 263 | +// Sources: 1) JTEST_DB_* env vars (CI), 2) build.properties → Joomla config |
263 | 264 | // --------------------------------------------------------------------------- |
264 | 265 |
|
265 | 266 | (function () use ($componentRoot) { |
266 | | - $propsFile = $componentRoot . '/build.properties'; |
267 | | - |
268 | | - if (!file_exists($propsFile)) { |
269 | | - return; |
| 267 | + $host = ''; |
| 268 | + $dbName = ''; |
| 269 | + $user = ''; |
| 270 | + $pass = ''; |
| 271 | + $prefix = ''; |
| 272 | + |
| 273 | + // Source 1: JTEST_DB_* environment variables (CI) |
| 274 | + if (getenv('JTEST_DB_HOST') && getenv('JTEST_DB_NAME')) { |
| 275 | + $host = getenv('JTEST_DB_HOST'); |
| 276 | + $dbName = getenv('JTEST_DB_NAME'); |
| 277 | + $user = getenv('JTEST_DB_USER') ?: ''; |
| 278 | + $pass = getenv('JTEST_DB_PASSWORD') ?: ''; |
| 279 | + $prefix = getenv('JTEST_DB_PREFIX') ?: ''; |
270 | 280 | } |
271 | 281 |
|
272 | | - // Parse build.properties |
273 | | - $props = []; |
274 | | - $lines = file($propsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
275 | | - |
276 | | - foreach ($lines as $line) { |
277 | | - $trimmed = trim($line); |
278 | | - |
279 | | - if ($trimmed === '' || str_starts_with($trimmed, '#')) { |
280 | | - continue; |
281 | | - } |
| 282 | + // Source 2: build.properties → Joomla configuration.php |
| 283 | + if ($dbName === '') { |
| 284 | + $propsFile = $componentRoot . '/build.properties'; |
282 | 285 |
|
283 | | - $eq = strpos($trimmed, '='); |
| 286 | + if (file_exists($propsFile)) { |
| 287 | + $props = []; |
| 288 | + $lines = file($propsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
284 | 289 |
|
285 | | - if ($eq === false) { |
286 | | - continue; |
287 | | - } |
| 290 | + foreach ($lines as $line) { |
| 291 | + $trimmed = trim($line); |
288 | 292 |
|
289 | | - $props[trim(substr($trimmed, 0, $eq))] = trim(substr($trimmed, $eq + 1)); |
290 | | - } |
| 293 | + if ($trimmed === '' || str_starts_with($trimmed, '#')) { |
| 294 | + continue; |
| 295 | + } |
291 | 296 |
|
292 | | - // Find the first Joomla installation path (for database config) |
293 | | - $joomlaPath = ''; |
| 297 | + $eq = strpos($trimmed, '='); |
294 | 298 |
|
295 | | - if (!empty($props['builder.joomla_paths'])) { |
296 | | - $paths = array_map('trim', explode(',', $props['builder.joomla_paths'])); |
297 | | - $joomlaPath = $paths[0] ?? ''; |
298 | | - } elseif (!empty($props['builder.joomla_path'])) { |
299 | | - $joomlaPath = $props['builder.joomla_path']; |
300 | | - } |
| 299 | + if ($eq !== false) { |
| 300 | + $props[trim(substr($trimmed, 0, $eq))] = trim(substr($trimmed, $eq + 1)); |
| 301 | + } |
| 302 | + } |
301 | 303 |
|
302 | | - if ($joomlaPath === '' || !is_dir($joomlaPath)) { |
303 | | - return; |
304 | | - } |
| 304 | + $joomlaPath = ''; |
305 | 305 |
|
306 | | - $configFile = rtrim($joomlaPath, '/') . '/configuration.php'; |
| 306 | + if (!empty($props['builder.joomla_paths'])) { |
| 307 | + $paths = array_map('trim', explode(',', $props['builder.joomla_paths'])); |
| 308 | + $joomlaPath = $paths[0] ?? ''; |
| 309 | + } elseif (!empty($props['builder.joomla_path'])) { |
| 310 | + $joomlaPath = $props['builder.joomla_path']; |
| 311 | + } |
307 | 312 |
|
308 | | - if (!file_exists($configFile)) { |
309 | | - return; |
310 | | - } |
| 313 | + $configFile = $joomlaPath !== '' && is_dir($joomlaPath) |
| 314 | + ? rtrim($joomlaPath, '/') . '/configuration.php' |
| 315 | + : ''; |
311 | 316 |
|
312 | | - // Load Joomla's configuration |
313 | | - require_once $configFile; |
| 317 | + if ($configFile !== '' && file_exists($configFile)) { |
| 318 | + require_once $configFile; |
314 | 319 |
|
315 | | - if (!class_exists('JConfig', false)) { |
316 | | - return; |
| 320 | + if (class_exists('JConfig', false)) { |
| 321 | + $config = new \JConfig(); |
| 322 | + $host = $config->host ?? 'localhost'; |
| 323 | + $dbName = $config->db ?? ''; |
| 324 | + $user = $config->user ?? ''; |
| 325 | + $pass = $config->password ?? ''; |
| 326 | + $prefix = $config->dbprefix ?? ''; |
| 327 | + } |
| 328 | + } |
| 329 | + } |
317 | 330 | } |
318 | 331 |
|
319 | | - $config = new \JConfig(); |
320 | | - |
321 | | - // Create a real database connection |
322 | | - $host = $config->host ?? 'localhost'; |
323 | | - $dbName = $config->db ?? ''; |
324 | | - $user = $config->user ?? ''; |
325 | | - $pass = $config->password ?? ''; |
326 | | - $prefix = $config->dbprefix ?? ''; |
327 | | - |
328 | 332 | if ($dbName === '' || $user === '') { |
329 | 333 | return; |
330 | 334 | } |
|
338 | 342 | } |
339 | 343 |
|
340 | 344 | try { |
341 | | - $options = [ |
| 345 | + $db = \Joomla\Database\DatabaseDriver::getInstance([ |
342 | 346 | 'driver' => 'mysqli', |
343 | 347 | 'host' => $host, |
344 | 348 | 'port' => $port, |
345 | 349 | 'user' => $user, |
346 | 350 | 'password' => $pass, |
347 | 351 | 'database' => $dbName, |
348 | 352 | 'prefix' => $prefix, |
349 | | - ]; |
350 | | - |
351 | | - $db = \Joomla\Database\DatabaseDriver::getInstance($options); |
| 353 | + ]); |
352 | 354 | $db->connect(); |
353 | 355 |
|
354 | | - // Register in DI container for Factory::getContainer()->get(DatabaseInterface::class) |
355 | | - try { |
356 | | - $container = \Joomla\CMS\Factory::getContainer(); |
357 | | - |
358 | | - if ($container instanceof \Joomla\DI\Container) { |
359 | | - $container->set(\Joomla\Database\DatabaseInterface::class, $db); |
360 | | - } |
361 | | - } catch (\Throwable) { |
362 | | - // Container not available or key protected — tests use direct DB access |
363 | | - } |
364 | | - |
365 | | - // Store reference for tests that need direct access |
366 | 356 | \define('PROCLAIM_TEST_DB_AVAILABLE', true); |
367 | 357 |
|
368 | 358 | fwrite(STDERR, "Database connected: $dbName@$host:$port" . PHP_EOL); |
369 | 359 | } catch (\Throwable $e) { |
370 | | - // Database not available — integration tests will skip gracefully |
371 | 360 | \define('PROCLAIM_TEST_DB_AVAILABLE', false); |
372 | 361 | fwrite(STDERR, "Database not available: " . $e->getMessage() . PHP_EOL); |
373 | 362 | } |
|
0 commit comments