95 lines
2.7 KiB
PHP
95 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Utilities;
|
|
|
|
use App\Utilities\SystemDatabaseConfiguration;
|
|
use PHPUnit\Framework\TestCase;
|
|
use RuntimeException;
|
|
|
|
class SystemDatabaseConfigurationTest extends TestCase
|
|
{
|
|
public function test_it_detects_a_complete_system_table_prefix_different_from_current_prefix(): void
|
|
{
|
|
$configuration = new SystemDatabaseConfiguration('unused');
|
|
|
|
$prefix = $configuration->detectTablePrefix([
|
|
'ak_migrations',
|
|
'ak_users',
|
|
'ak_companies',
|
|
'ak_roles',
|
|
'ak_permissions',
|
|
'ak_role_permissions',
|
|
'ak_user_roles',
|
|
'ak_user_companies',
|
|
'ak_settings',
|
|
'ak_system_database_connections',
|
|
], 'a_');
|
|
|
|
$this->assertSame('ak_', $prefix);
|
|
}
|
|
|
|
public function test_it_prefers_the_current_prefix_when_target_contains_multiple_complete_system_sets(): void
|
|
{
|
|
$configuration = new SystemDatabaseConfiguration('unused');
|
|
$bases = $this->requiredTables();
|
|
$tables = [];
|
|
|
|
foreach (['a_', 'ak_'] as $prefix) {
|
|
foreach ($bases as $base) {
|
|
$tables[] = $prefix . $base;
|
|
}
|
|
}
|
|
|
|
$this->assertSame('a_', $configuration->detectTablePrefix($tables, 'a_'));
|
|
}
|
|
|
|
public function test_it_rejects_a_target_without_one_complete_system_table_set(): void
|
|
{
|
|
$configuration = new SystemDatabaseConfiguration('unused');
|
|
|
|
$this->expectException(RuntimeException::class);
|
|
$this->expectExceptionMessage('目标库缺少系统必需表');
|
|
|
|
$configuration->detectTablePrefix([
|
|
'ak_migrations',
|
|
'ak_users',
|
|
'ak_roles',
|
|
'ak_settings',
|
|
], 'a_');
|
|
}
|
|
|
|
public function test_it_rejects_an_ambiguous_target_when_current_prefix_is_not_present(): void
|
|
{
|
|
$configuration = new SystemDatabaseConfiguration('unused');
|
|
$bases = $this->requiredTables();
|
|
$tables = [];
|
|
|
|
foreach (['first_', 'second_'] as $prefix) {
|
|
foreach ($bases as $base) {
|
|
$tables[] = $prefix . $base;
|
|
}
|
|
}
|
|
|
|
$this->expectException(RuntimeException::class);
|
|
$this->expectExceptionMessage('无法自动确定表前缀');
|
|
|
|
$configuration->detectTablePrefix($tables, 'a_');
|
|
}
|
|
|
|
private function requiredTables(): array
|
|
{
|
|
return [
|
|
'migrations',
|
|
'users',
|
|
'companies',
|
|
'roles',
|
|
'permissions',
|
|
'role_permissions',
|
|
'user_roles',
|
|
'user_companies',
|
|
'settings',
|
|
'system_database_connections',
|
|
];
|
|
}
|
|
}
|