Skip to content

Commit

Permalink
fix phpunit.xml.Bootstrap has to run database and clear cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Apr 14, 2026
1 parent 2df3943 commit aa5d790
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
2 changes: 0 additions & 2 deletions app/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.3/phpunit.xsd">
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>


<!-- Registry setup inputs -->
<env name="COMANAGE_REGISTRY_ADMIN_GIVEN_NAME" value="Admin"/>
Expand Down
64 changes: 35 additions & 29 deletions app/tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,29 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

use App\Application;
use Cake\Chronos\Chronos;
use Cake\Console\CommandRunner;
use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\Driver\Mysql;
use Cake\Database\Driver\Postgres;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\ConnectionHelper;
use Cake\Utility\Security;
use Migrations\TestSuite\Migrator;

/**
* Test runner bootstrap.
*
* Add additional configuration/setup your application needs when running
* unit tests in this file.
* This file must ensure the application is runnable in the same way as CI/ops:
* - configure datasources (default + test) against the test DB
* - clear caches
* - apply Registry schema via `bin/cake database`
*/
require dirname(__DIR__) . '/vendor/autoload.php';

require dirname(__DIR__) . '/config/bootstrap.php';

// Ensure Security salt is set for tests (needed by CSRF middleware, hashes, etc).
// Security::getSalt() throws when not set, so don't call it to check.
$salt = getenv('COMANAGE_REGISTRY_SECURITY_SALT') ?: 'phpunit-security-salt';
Security::setSalt($salt);

Expand All @@ -47,9 +34,7 @@
// Fixate now to avoid one-second-leap-issues
Chronos::setTestNow(Chronos::now());

// Fixate sessionid early on, as php7.2+
// does not allow the sessionid to be set after stdout
// has been written to.
// Fixate sessionid early on (CLI)
session_id('cli');

/**
Expand All @@ -63,7 +48,6 @@

// -----------------------------------------------------
// Database configuration via environment variables
// (GitHub Actions services or any externally-provided DB; no Testcontainers)
// -----------------------------------------------------
$dbEngine = getenv('DB_ENGINE') ?: 'mysql';

Expand Down Expand Up @@ -120,14 +104,36 @@
];
}

// Make both datasources point at the same test database.
// Most application code uses the `default` datasource.
ConnectionManager::setConfig('test', $config);
ConnectionManager::setConfig('default', $config);

// -----------------------------------------------------

// Connection aliasing needs to happen before migrations are run.
// Otherwise, table objects inside migrations would use the default datasource
// Connection aliasing needs to happen before schema is built.
// Otherwise, objects created during CLI commands might use the wrong datasource.
ConnectionHelper::addTestAliases();

// Use migrations to build test database schema.
(new Migrator())->run();
/**
* Run a Cake CLI command in-process (no shelling out).
*
* @param list<string> $argv
* @return int exit code
*/
$runCake = static function (array $argv): int {
$app = new Application(dirname(__DIR__) . '/config');
$runner = new CommandRunner($app);

return $runner->run($argv);
};

// 1) Clear caches (matches: ./bin/cake cache clear_all)
$exit = $runCake(['bin/cake', 'cache', 'clear_all']);
if ($exit !== 0) {
throw new \RuntimeException('Failed running: bin/cake cache clear_all (exit=' . $exit . ')');
}

// 2) Apply database schema (matches: ./bin/cake database)
$exit = $runCake(['bin/cake', 'database']);
if ($exit !== 0) {
throw new \RuntimeException('Failed running: bin/cake database (exit=' . $exit . ')');
}

0 comments on commit aa5d790

Please sign in to comment.