Files
XC_VM/tests/Unit/ModuleMigratorTest.php
T
Divarion_D 6964e99f35 test(core): cover the DB layer — ModuleMigrator, SettingsRepository, AuthRepository
Fifth coverage batch, all driven against the in-memory SQLite TestDb:

- ModuleMigratorTest: install runs master database.sql (or falls back to
  replaying deltas <= target when there is none); up() applies only the
  (from, to] range ascending; uninstall runs database_drop.sql or no-ops; has()
  and discover() semver rules (non-semver files ignored, comments stripped);
  and failure propagation from a bad statement.
- SettingsRepositoryTest: getAll() JSON/CSV field normalisation (allow_countries,
  allowed_stb_types lowercase/trim/blank-drop, bouquet_name spaces, api_ips,
  shared_mount_prefixes incl. the legacy-CSV self-heal) and empty-list collapse.
  Points CACHE_TMP_PATH at a temp dir for the trailing FileCache write.
- AuthRepositoryTest: access-code and HMAC-key reads (getAllCodes with/without
  type filter, getCodeById, getCurrentCode via XC_CODE, getAllHMAC, getHMACById)
  and deleteHMAC existing/missing. getGroupPermissions (JSON_CONTAINS) and
  deleteCode (nginx config regen) are left for integration.

+20 tests. Suite: 678 tests, 0 errors.
2026-09-13 14:51:45 +03:00

111 lines
4.1 KiB
PHP

<?php
use XcVm\Core\Module\ModuleMigrator;
use PHPUnit\Framework\TestCase;
/**
* ModuleMigrator — file-based module schema (install/update/uninstall).
*
* Runs the real statement splitter and version selection against a throwaway
* module directory and an in-memory SQLite handle: master schema on install,
* the delta-only fallback, forward (from, to] ranges on update, teardown, the
* has()/discover semver rules, comment stripping, and failure propagation.
*/
final class ModuleMigratorTest extends TestCase {
private string $root;
private TestDb $db;
protected function setUp(): void {
$this->root = sys_get_temp_dir() . '/xcvm_mig_' . uniqid('', true);
mkdir($this->root . '/migrations', 0755, true);
$this->db = new TestDb();
}
protected function tearDown(): void {
$this->rrmdir($this->root);
}
private function rrmdir(string $dir): void {
if (!is_dir($dir)) {
return;
}
foreach (scandir($dir) as $entry) {
if ($entry === '.' || $entry === '..') {
continue;
}
$path = $dir . '/' . $entry;
is_dir($path) ? $this->rrmdir($path) : unlink($path);
}
rmdir($dir);
}
private function writeMigration(string $version, string $sql): void {
file_put_contents($this->root . '/migrations/' . $version . '.sql', $sql);
}
public function testInstallRunsMasterSchemaWhenPresent(): void {
file_put_contents(
$this->root . '/database.sql',
"CREATE TABLE mod_items (id INTEGER PRIMARY KEY, name TEXT);\nINSERT INTO mod_items (id, name) VALUES (1, 'seed');"
);
$ran = ModuleMigrator::install($this->root, $this->db, '2.0.0');
$this->assertSame(['database.sql'], $ran);
$this->db->query('SELECT name FROM mod_items WHERE id = 1;');
$this->assertSame('seed', $this->db->get_col(), 'master schema applied');
}
public function testInstallFallsBackToReplayingDeltasWhenNoMaster(): void {
$this->writeMigration('1.0.0', 'CREATE TABLE mod_a (id INTEGER PRIMARY KEY);');
$this->writeMigration('1.5.0', 'CREATE TABLE mod_b (id INTEGER PRIMARY KEY);');
$this->writeMigration('3.0.0', 'CREATE TABLE mod_too_new (id INTEGER PRIMARY KEY);');
$ran = ModuleMigrator::install($this->root, $this->db, '2.0.0');
$this->assertSame(['1.0.0', '1.5.0'], $ran, 'only deltas <= target, ascending');
}
public function testUpAppliesOnlyDeltasInRange(): void {
$this->writeMigration('1.0.0', 'CREATE TABLE mod_a (id INTEGER PRIMARY KEY);');
$this->writeMigration('1.1.0', 'CREATE TABLE mod_b (id INTEGER PRIMARY KEY);');
$this->writeMigration('2.0.0', 'CREATE TABLE mod_c (id INTEGER PRIMARY KEY);');
$ran = ModuleMigrator::up($this->root, $this->db, '1.0.0', '2.0.0');
$this->assertSame(['1.1.0', '2.0.0'], $ran, 'exclusive of $from, inclusive of $to');
}
public function testUninstallRunsDropFileOrNoOps(): void {
$this->assertSame([], ModuleMigrator::uninstall($this->root, $this->db), 'no teardown file');
file_put_contents($this->root . '/database_drop.sql', 'DROP TABLE IF EXISTS whatever;');
$this->assertSame(['database_drop.sql'], ModuleMigrator::uninstall($this->root, $this->db));
}
public function testHasReportsAnySchemaPresence(): void {
$this->assertFalse(ModuleMigrator::has($this->root), 'empty migrations dir, no master/drop');
$this->writeMigration('1.0.0', 'CREATE TABLE x (id INTEGER);');
$this->assertTrue(ModuleMigrator::has($this->root));
}
public function testDiscoverIgnoresNonSemverAndStripsComments(): void {
// Non-semver file must be ignored (else it would run and fail here).
file_put_contents($this->root . '/migrations/notes.sql', 'this is not valid sql at all');
$this->writeMigration('1.0.0', "-- create the table\nCREATE TABLE mod_c (id INTEGER PRIMARY KEY);\n-- trailing comment");
$ran = ModuleMigrator::install($this->root, $this->db, '1.0.0');
$this->assertSame(['1.0.0'], $ran);
$this->db->query('SELECT COUNT(*) FROM mod_c;');
$this->assertSame('0', (string) $this->db->get_col(), 'commented statement ran cleanly');
}
public function testRunFilePropagatesFailure(): void {
$this->writeMigration('1.0.0', 'THIS IS NOT VALID SQL;');
$this->expectException(\Exception::class);
ModuleMigrator::up($this->root, $this->db, null, '1.0.0');
}
}