Skip to content

Commit a432a3e

Browse files
committed
add RestoreCommandTest test cases for --list option of RestoreCommand
1 parent a87da8c commit a432a3e

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

tests/Functional/Command/RestoreCommandTest.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
use MakinaCorpus\DbToolsBundle\Test\FunctionalKernelTestCase;
88
use Symfony\Bundle\FrameworkBundle\Console\Application;
99
use Symfony\Component\Console\Tester\CommandTester;
10+
use MakinaCorpus\DbToolsBundle\Command\RestoreCommand;
11+
use MakinaCorpus\DbToolsBundle\Restorer\AbstractRestorer;
12+
use MakinaCorpus\DbToolsBundle\Restorer\RestorerFactory;
13+
use MakinaCorpus\DbToolsBundle\Storage\Storage;
14+
use PHPUnit\Framework\TestCase;
1015

1116
class RestoreCommandTest extends FunctionalKernelTestCase
1217
{
@@ -26,4 +31,103 @@ public function testExecute(): void
2631

2732
self::assertCommandIsSuccessful($commandTester);
2833
}
34+
35+
/**
36+
* Creates a command tester with mocked dependencies for listBackups method
37+
*
38+
* @param array $backupFiles List of backup files to be returned by storage
39+
*/
40+
private function createCommandTester(array $backupFiles = []): CommandTester
41+
{
42+
// Storage mock returns our test backup files and a fake path
43+
$storage = $this->createMock(Storage::class);
44+
$storage
45+
->method('listBackups')
46+
->willReturn($backupFiles);
47+
$storage
48+
->method('getStoragePath')
49+
->willReturn('/fake/path');
50+
51+
// Restorer mock just needs to provide a file extension
52+
$restorer = $this->createMock(AbstractRestorer::class);
53+
$restorer
54+
->method('getExtension')
55+
->willReturn('sql');
56+
57+
// Factory mock returns our mocked restorer
58+
$restorerFactory = $this->createMock(RestorerFactory::class);
59+
$restorerFactory
60+
->method('create')
61+
->willReturn($restorer);
62+
63+
$command = new RestoreCommand(
64+
'default',
65+
$restorerFactory,
66+
$storage,
67+
null // timeout
68+
);
69+
70+
return new CommandTester($command);
71+
}
72+
73+
/**
74+
* Test listing when no backups are available
75+
*/
76+
public function testListBackupsEmpty(): void
77+
{
78+
$commandTester = $this->createCommandTester([]);
79+
80+
$commandTester->execute([
81+
'--list' => true,
82+
]);
83+
84+
$output = $commandTester->getDisplay();
85+
$this->assertStringContainsString(
86+
'Backups list',
87+
$output,
88+
'Output should contain the section title "Backups list"'
89+
);
90+
$this->assertStringContainsString(
91+
'There is no backup files available in /fake/path',
92+
$output,
93+
'Output should display the "no backups" warning message with the correct path'
94+
);
95+
}
96+
97+
/**
98+
* Test listing with some backup files
99+
* Each backup file is represented by [date, filename]
100+
*/
101+
public function testListBackupsWithFiles(): void
102+
{
103+
// Mock some backup files with date and filename
104+
$backupFiles = [
105+
['1 days', 'backup_2024-03-20_10-30-00.sql'],
106+
['2 days', 'backup_2024-03-21_14-45-00.sql'],
107+
];
108+
109+
$commandTester = $this->createCommandTester($backupFiles);
110+
111+
$commandTester->execute([
112+
'--list' => true,
113+
]);
114+
115+
// Check that output contains both the section title and our backup files
116+
$output = $commandTester->getDisplay();
117+
$this->assertStringContainsString(
118+
'Backups list',
119+
$output,
120+
'Output should start with the section title "Backups list"'
121+
);
122+
$this->assertStringContainsString(
123+
$backupFiles[0][1] . ' (' . $backupFiles[0][0] . ')',
124+
$output,
125+
'Output should contain the first backup file with its age'
126+
);
127+
$this->assertStringContainsString(
128+
$backupFiles[1][1] . ' (' . $backupFiles[1][0] . ')',
129+
$output,
130+
'Output should contain the second backup file with its age'
131+
);
132+
}
29133
}

0 commit comments

Comments
 (0)