. */ declare(strict_types=1); namespace App\Tests\Services\System; use App\Services\System\BackupManager; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; class BackupManagerTest extends KernelTestCase { private ?BackupManager $backupManager = null; protected function setUp(): void { self::bootKernel(); $this->backupManager = self::getContainer()->get(BackupManager::class); } public function testGetBackupDir(): void { $backupDir = $this->backupManager->getBackupDir(); // Should end with var/backups $this->assertStringEndsWith('var/backups', $backupDir); } public function testGetBackupsReturnsEmptyArrayWhenNoBackups(): void { // If there are no backups (or the directory doesn't exist), should return empty array $backups = $this->backupManager->getBackups(); $this->assertIsArray($backups); } public function testGetBackupDetailsReturnsNullForNonExistentFile(): void { $details = $this->backupManager->getBackupDetails('non-existent-backup.zip'); $this->assertNull($details); } public function testGetBackupDetailsReturnsNullForNonZipFile(): void { $details = $this->backupManager->getBackupDetails('not-a-zip.txt'); $this->assertNull($details); } /** * Test that version parsing from filename works correctly. * This tests the regex pattern used in getBackupDetails. */ public function testVersionParsingFromFilename(): void { // Test the regex pattern directly $filename = 'pre-update-v2.5.1-to-v2.6.0-2024-01-30-185400.zip'; $matches = []; $result = preg_match('/pre-update-v([\d.]+)-to-v?([\d.]+)-/', $filename, $matches); $this->assertEquals(1, $result); $this->assertEquals('2.5.1', $matches[1]); $this->assertEquals('2.6.0', $matches[2]); } /** * Test version parsing with different filename formats. */ public function testVersionParsingVariants(): void { // Without 'v' prefix on target version $filename1 = 'pre-update-v1.0.0-to-2.0.0-2024-01-30-185400.zip'; preg_match('/pre-update-v([\d.]+)-to-v?([\d.]+)-/', $filename1, $matches1); $this->assertEquals('1.0.0', $matches1[1]); $this->assertEquals('2.0.0', $matches1[2]); // With 'v' prefix on target version $filename2 = 'pre-update-v1.0.0-to-v2.0.0-2024-01-30-185400.zip'; preg_match('/pre-update-v([\d.]+)-to-v?([\d.]+)-/', $filename2, $matches2); $this->assertEquals('1.0.0', $matches2[1]); $this->assertEquals('2.0.0', $matches2[2]); } }