4ee91a7918
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
57 lines
2.2 KiB
PHP
57 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXT\Unit\Console\Firewall;
|
|
|
|
use KTXC\Console\Firewall\FirewallMaintenanceCommand;
|
|
use KTXC\Console\Firewall\FirewallSetupCommand;
|
|
use KTXC\Service\FirewallService;
|
|
use KTXC\Stores\FirewallStore;
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
final class FirewallCommandsTest extends TestCase
|
|
{
|
|
#[TestDox('Setup verifies every firewall database index')]
|
|
public function testSetup(): void
|
|
{
|
|
$store = $this->createMock(FirewallStore::class);
|
|
$store->expects(self::once())->method('ensureIndexes')->willReturn(array_fill(0, 7, 'index'));
|
|
$tester = new CommandTester(new FirewallSetupCommand($store));
|
|
|
|
self::assertSame(Command::SUCCESS, $tester->execute([]));
|
|
self::assertStringContainsString('7 indexes verified', $tester->getDisplay());
|
|
}
|
|
|
|
#[TestDox('Maintenance reports cleanup counts for schedulers')]
|
|
public function testMaintenance(): void
|
|
{
|
|
$firewall = $this->createMock(FirewallService::class);
|
|
$firewall->expects(self::once())->method('cleanup')->willReturn([
|
|
'expiredRules' => 2,
|
|
'oldLogs' => 3,
|
|
'expiredBruteForceClaims' => 4,
|
|
]);
|
|
$tester = new CommandTester(new FirewallMaintenanceCommand($firewall));
|
|
|
|
self::assertSame(Command::SUCCESS, $tester->execute([]));
|
|
self::assertStringContainsString('2 expired rules, 3 old logs', $tester->getDisplay());
|
|
self::assertStringContainsString('4 expired', $tester->getDisplay());
|
|
self::assertStringContainsString('claims removed', $tester->getDisplay());
|
|
}
|
|
|
|
#[TestDox('Maintenance returns failure to its scheduler')]
|
|
public function testMaintenanceFailure(): void
|
|
{
|
|
$firewall = $this->createStub(FirewallService::class);
|
|
$firewall->method('cleanup')->willThrowException(new \RuntimeException('database unavailable'));
|
|
$tester = new CommandTester(new FirewallMaintenanceCommand($firewall));
|
|
|
|
self::assertSame(Command::FAILURE, $tester->execute([]));
|
|
self::assertStringContainsString('database unavailable', $tester->getDisplay());
|
|
}
|
|
}
|