Skip to content

Fix typed property must not be accessed before initialization error when using --list option #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Anonymization/Config/Loader/AttributesLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function load(AnonymizationConfig $config): void
}

$embeddedClassesConfig = [];
foreach($metadata->embeddedClasses as $name => $embeddedClass) {
foreach ($metadata->embeddedClasses as $name => $embeddedClass) {
$className = $embeddedClass['class'];
$embeddedClassesConfig[$className] = [];
$reflexionClass = new \ReflectionClass($className);
Expand Down
2 changes: 1 addition & 1 deletion src/Backupper/SqliteBackupper.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private function getTablesToBackup(): array

$tables = [];
foreach ($query->executeQuery() as $table) {
if(!\in_array($table['name'], $this->excludedTables)) {
if (!\in_array($table['name'], $this->excludedTables)) {
$tables[] = $table['name'];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function process(ContainerBuilder $container): void
}

if (isset($config['anonymization']) && isset($config['anonymization']['yaml'])) {
foreach($config['anonymization']['yaml'] as $connectionName => $file) {
foreach ($config['anonymization']['yaml'] as $connectionName => $file) {
$loaderId = $this->registerYamlLoader($file, $connectionName, $container);
$anonymazorFactoryDef->addMethodCall('addConfigurationLoader', [new Reference($loaderId)]);
}
Expand Down
1 change: 1 addition & 0 deletions src/Command/RestoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ private function chooseBackup(): bool
private function listBackups(): int
{
$this->io->section('Backups list');
$this->restorer = $this->restorerFactory->create($this->connectionName);

$backupLists = $this->storage->listBackups(
$this->connectionName,
Expand Down
4 changes: 2 additions & 2 deletions src/Helper/Iban.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ private static function mod97(string $number): int
$parts = \str_split($number, 7);
$rest = 0;
foreach ($parts as $part) {
$rest = ($rest . $part) % 97;
$rest = ((int)($rest . $part)) % 97;
}
return $rest;
return (int)$rest;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Test/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function initializeDatabase(): void
$privConnection = $this->createPrivConnection();
try {
$privConnection->createSchemaManager()->createDatabase('test_db');
} catch(\Exception $e) {
} catch (\Exception $e) {

} finally {
$privConnection->close();
Expand Down
104 changes: 104 additions & 0 deletions tests/Functional/Command/RestoreCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use MakinaCorpus\DbToolsBundle\Test\FunctionalKernelTestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use MakinaCorpus\DbToolsBundle\Command\RestoreCommand;
use MakinaCorpus\DbToolsBundle\Restorer\AbstractRestorer;
use MakinaCorpus\DbToolsBundle\Restorer\RestorerFactory;
use MakinaCorpus\DbToolsBundle\Storage\Storage;
use PHPUnit\Framework\TestCase;

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

self::assertCommandIsSuccessful($commandTester);
}

/**
* Creates a command tester with mocked dependencies for listBackups method
*
* @param array $backupFiles List of backup files to be returned by storage
*/
private function createCommandTester(array $backupFiles = []): CommandTester
{
// Storage mock returns our test backup files and a fake path
$storage = $this->createMock(Storage::class);
$storage
->method('listBackups')
->willReturn($backupFiles);
$storage
->method('getStoragePath')
->willReturn('/fake/path');

// Restorer mock just needs to provide a file extension
$restorer = $this->createMock(AbstractRestorer::class);
$restorer
->method('getExtension')
->willReturn('sql');

// Factory mock returns our mocked restorer
$restorerFactory = $this->createMock(RestorerFactory::class);
$restorerFactory
->method('create')
->willReturn($restorer);

$command = new RestoreCommand(
'default',
$restorerFactory,
$storage,
null // timeout
);

return new CommandTester($command);
}

/**
* Test listing when no backups are available
*/
public function testListBackupsEmpty(): void
{
$commandTester = $this->createCommandTester([]);

$commandTester->execute([
'--list' => true,
]);

$output = $commandTester->getDisplay();
$this->assertStringContainsString(
'Backups list',
$output,
'Output should contain the section title "Backups list"'
);
$this->assertStringContainsString(
'There is no backup files available in /fake/path',
$output,
'Output should display the "no backups" warning message with the correct path'
);
}

/**
* Test listing with some backup files
* Each backup file is represented by [date, filename]
*/
public function testListBackupsWithFiles(): void
{
// Mock some backup files with date and filename
$backupFiles = [
['1 days', 'backup_2024-03-20_10-30-00.sql'],
['2 days', 'backup_2024-03-21_14-45-00.sql'],
];

$commandTester = $this->createCommandTester($backupFiles);

$commandTester->execute([
'--list' => true,
]);

// Check that output contains both the section title and our backup files
$output = $commandTester->getDisplay();
$this->assertStringContainsString(
'Backups list',
$output,
'Output should start with the section title "Backups list"'
);
$this->assertStringContainsString(
$backupFiles[0][1] . ' (' . $backupFiles[0][0] . ')',
$output,
'Output should contain the first backup file with its age'
);
$this->assertStringContainsString(
$backupFiles[1][1] . ' (' . $backupFiles[1][0] . ')',
$output,
'Output should contain the second backup file with its age'
);
}
}
Loading