feat: cli command to anonymize local database

This commit is contained in:
Björn Fromme
2026-06-17 10:32:02 +02:00
parent 66d0f511df
commit 80569e0977
7 changed files with 786 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Tests\Command;
use App\Command\DbAnonymizeCommand;
use App\Service\DatabaseAnonymizer;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
class DbAnonymizeCommandTest extends TestCase
{
public function testCommandRefusesToRunInProdEnvironment(): void
{
$anonymizer = $this->createMock(DatabaseAnonymizer::class);
$anonymizer->expects(self::never())->method('anonymizeAll');
$tester = new CommandTester($this->createCommand($anonymizer, 'prod'));
$tester->execute([]);
self::assertSame(Command::FAILURE, $tester->getStatusCode());
self::assertStringContainsString('disabled in the prod environment', $tester->getDisplay());
}
public function testCommandDelegatesOutsideProdEnvironment(): void
{
$anonymizer = $this->createMock(DatabaseAnonymizer::class);
$anonymizer->expects(self::once())
->method('anonymizeAll')
->willReturn([
'users' => 1,
'newsletterConsents' => 2,
'newsletterOptInRequests' => 3,
'bookingEditDrafts' => 4,
]);
$tester = new CommandTester($this->createCommand($anonymizer, 'dev'));
$tester->execute([]);
self::assertSame(Command::SUCCESS, $tester->getStatusCode());
self::assertStringContainsString('Anonymized 1 users, 2 newsletter consents, 3 newsletter opt-in requests, and 4 booking drafts.', $tester->getDisplay());
}
private function createCommand(DatabaseAnonymizer $anonymizer, string $environment): DbAnonymizeCommand
{
return new DbAnonymizeCommand(
$anonymizer,
$this->createMock(LoggerInterface::class),
$environment,
);
}
}