|
| 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