116 lines
4.1 KiB
PHP
116 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Utilities;
|
|
|
|
use App\Utilities\SystemDatabaseConfiguration;
|
|
use RuntimeException;
|
|
use Tests\TestCase;
|
|
|
|
class SystemDatabasePersistenceTest extends TestCase
|
|
{
|
|
public function test_failed_fresh_process_verification_restores_original_environment(): void
|
|
{
|
|
$environmentPath = storage_path('framework/testing-system-database.env');
|
|
$original = "DB_HOST=mysql\nDB_DATABASE=akaunting\nSYSTEM_DATABASE_MODE=bootstrap\n";
|
|
file_put_contents($environmentPath, $original);
|
|
|
|
$configuration = new class($environmentPath) extends SystemDatabaseConfiguration {
|
|
public array $commands = [];
|
|
|
|
protected function runFreshVerification(string $command): void
|
|
{
|
|
$this->commands[] = $command;
|
|
|
|
if ($command === 'system-database:verify') {
|
|
throw new RuntimeException('candidate failed');
|
|
}
|
|
}
|
|
};
|
|
|
|
try {
|
|
$configuration->persist([
|
|
'host' => 'shared-db.test',
|
|
'port' => 3306,
|
|
'database' => 'shared_system',
|
|
'username' => 'shared_writer',
|
|
'password' => 'shared-secret',
|
|
'prefix' => 'ak_',
|
|
'tls_required' => true,
|
|
'tls_verify_server_cert' => false,
|
|
'tls_ca' => '',
|
|
]);
|
|
|
|
$this->fail('验证失败时应抛出异常。');
|
|
} catch (RuntimeException $exception) {
|
|
$this->assertSame('新配置未通过应用级验证,已自动恢复原系统数据库。', $exception->getMessage());
|
|
$this->assertSame($original, file_get_contents($environmentPath));
|
|
$this->assertSame(['system-database:verify', 'app:ready'], $configuration->commands);
|
|
} finally {
|
|
@unlink($environmentPath);
|
|
}
|
|
}
|
|
|
|
public function test_root_owned_or_invalid_backup_directory_uses_writable_storage_fallback(): void
|
|
{
|
|
$environmentPath = storage_path('framework/testing-system-database-fallback.env');
|
|
$blockedBackupPath = storage_path('framework/testing-blocked-backup-path');
|
|
file_put_contents($environmentPath, "DB_HOST=mysql\nDB_DATABASE=akaunting\n");
|
|
file_put_contents($blockedBackupPath, 'not-a-directory');
|
|
|
|
$configuration = new class($environmentPath, $blockedBackupPath) extends SystemDatabaseConfiguration {
|
|
protected function runFreshVerification(string $command): void
|
|
{
|
|
}
|
|
};
|
|
|
|
try {
|
|
$backupPath = $configuration->persist([
|
|
'host' => 'shared-db.test',
|
|
'port' => 3306,
|
|
'database' => 'shared_system',
|
|
'username' => 'shared_writer',
|
|
'password' => 'shared-secret',
|
|
'prefix' => 'ak_',
|
|
'tls_required' => true,
|
|
'tls_verify_server_cert' => false,
|
|
'tls_ca' => '',
|
|
]);
|
|
|
|
$this->assertSame(storage_path('app'), dirname($backupPath));
|
|
$this->assertFileExists($backupPath);
|
|
} finally {
|
|
@unlink($environmentPath);
|
|
@unlink($blockedBackupPath);
|
|
|
|
if (isset($backupPath)) {
|
|
@unlink($backupPath);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function test_fresh_verification_does_not_inherit_stale_database_environment(): void
|
|
{
|
|
$configuration = new class extends SystemDatabaseConfiguration {
|
|
public function verificationEnvironment(): array
|
|
{
|
|
return $this->freshVerificationEnvironment();
|
|
}
|
|
};
|
|
|
|
$environment = $configuration->verificationEnvironment();
|
|
|
|
foreach ([
|
|
'DB_HOST',
|
|
'DB_DATABASE',
|
|
'DB_PASSWORD',
|
|
'DB_PREFIX',
|
|
'SYSTEM_DATABASE_MODE',
|
|
'UPSTREAM_STORED_CONFIG_ENABLED',
|
|
'UPSTREAM_LOCAL_CONFIG_ENABLED',
|
|
] as $key) {
|
|
$this->assertArrayHasKey($key, $environment);
|
|
$this->assertFalse($environment[$key]);
|
|
}
|
|
}
|
|
}
|