Skip to content

Commit ab505c7

Browse files
committed
Command to remove old session tokens
1 parent cc5e35a commit ab505c7

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

config/services/commands.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ services:
88
resource: '../../src/Domain/Messaging/Command'
99
tags: ['console.command']
1010

11+
PhpList\Core\Domain\Identity\Command\:
12+
resource: '../../src/Domain/Identity/Command'
13+
tags: ['console.command']
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\Core\Domain\Identity\Command;
6+
7+
use PhpList\Core\Domain\Identity\Repository\AdministratorTokenRepository;
8+
use Symfony\Component\Console\Attribute\AsCommand;
9+
use Symfony\Component\Console\Command\Command;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use Throwable;
13+
14+
#[AsCommand(
15+
name: 'phplist:clean-up-old-session-tokens',
16+
description: 'Removes expired administrator session tokens.'
17+
)]
18+
class CleanUpOldSessionTokens extends Command
19+
{
20+
private AdministratorTokenRepository $tokenRepository;
21+
22+
public function __construct(AdministratorTokenRepository $tokenRepository)
23+
{
24+
parent::__construct();
25+
$this->tokenRepository = $tokenRepository;
26+
}
27+
28+
/**
29+
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
30+
*/
31+
protected function execute(InputInterface $input, OutputInterface $output): int
32+
{
33+
try {
34+
$deletedCount = $this->tokenRepository->removeExpired();
35+
$output->writeln(sprintf('Successfully removed %d expired session token(s).', $deletedCount));
36+
} catch (Throwable $throwable) {
37+
$output->writeln(sprintf('Error removing expired session tokens: %s', $throwable->getMessage()));
38+
return Command::FAILURE;
39+
}
40+
41+
return Command::SUCCESS;
42+
}
43+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\Core\Tests\Unit\Domain\Identity\Command;
6+
7+
use Exception;
8+
use PhpList\Core\Domain\Identity\Command\CleanUpOldSessionTokens;
9+
use PhpList\Core\Domain\Identity\Repository\AdministratorTokenRepository;
10+
use PHPUnit\Framework\MockObject\MockObject;
11+
use PHPUnit\Framework\TestCase;
12+
use Symfony\Component\Console\Application;
13+
use Symfony\Component\Console\Tester\CommandTester;
14+
15+
class CleanUpOldSessionTokensTest extends TestCase
16+
{
17+
private AdministratorTokenRepository&MockObject $tokenRepository;
18+
private CommandTester $commandTester;
19+
20+
protected function setUp(): void
21+
{
22+
$this->tokenRepository = $this->createMock(AdministratorTokenRepository::class);
23+
24+
$command = new CleanUpOldSessionTokens($this->tokenRepository);
25+
26+
$application = new Application();
27+
$application->add($command);
28+
29+
$this->commandTester = new CommandTester($command);
30+
}
31+
32+
public function testExecuteSuccessfully(): void
33+
{
34+
$this->tokenRepository->expects($this->once())
35+
->method('removeExpired')
36+
->willReturn(5);
37+
38+
$this->commandTester->execute([]);
39+
40+
$output = $this->commandTester->getDisplay();
41+
$this->assertStringContainsString('Successfully removed 5 expired session token(s)', $output);
42+
$this->assertEquals(0, $this->commandTester->getStatusCode());
43+
}
44+
45+
public function testExecuteWithNoExpiredTokens(): void
46+
{
47+
$this->tokenRepository->expects($this->once())
48+
->method('removeExpired')
49+
->willReturn(0);
50+
51+
$this->commandTester->execute([]);
52+
53+
$output = $this->commandTester->getDisplay();
54+
$this->assertStringContainsString('Successfully removed 0 expired session token(s)', $output);
55+
$this->assertEquals(0, $this->commandTester->getStatusCode());
56+
}
57+
58+
public function testExecuteWithException(): void
59+
{
60+
$this->tokenRepository->expects($this->once())
61+
->method('removeExpired')
62+
->willThrowException(new Exception('Test exception'));
63+
64+
$this->commandTester->execute([]);
65+
66+
$output = $this->commandTester->getDisplay();
67+
$this->assertStringContainsString('Error removing expired session tokens: Test exception', $output);
68+
$this->assertEquals(1, $this->commandTester->getStatusCode());
69+
}
70+
}

0 commit comments

Comments
 (0)