Files
myep/tests/Command/DbAnonymizeCommandTest.php

65 lines
2.1 KiB
PHP

<?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,
'accommodationBookings' => 5,
]);
$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, 4 booking drafts, and 5 accommodation bookings.',
$this->normalizeConsoleOutput($tester->getDisplay()),
);
}
private function createCommand(DatabaseAnonymizer $anonymizer, string $environment): DbAnonymizeCommand
{
return new DbAnonymizeCommand(
$anonymizer,
$this->createStub(LoggerInterface::class),
$environment,
);
}
private function normalizeConsoleOutput(string $output): string
{
return trim((string) preg_replace('/\s+/', ' ', $output));
}
}