|
4 | 4 |
|
5 | 5 | namespace PhpList\Core\Tests\Unit\Domain\Identity\Command; |
6 | 6 |
|
7 | | -use Exception; |
| 7 | +use Doctrine\ORM\EntityManagerInterface; |
8 | 8 | use PhpList\Core\Domain\Identity\Command\CleanUpOldSessionTokens; |
9 | 9 | use PhpList\Core\Domain\Identity\Repository\AdministratorTokenRepository; |
10 | | -use PHPUnit\Framework\MockObject\MockObject; |
11 | 10 | use PHPUnit\Framework\TestCase; |
12 | | -use Symfony\Component\Console\Application; |
| 11 | +use Symfony\Component\Console\Command\Command; |
13 | 12 | use Symfony\Component\Console\Tester\CommandTester; |
14 | 13 |
|
15 | | -class CleanUpOldSessionTokensTest extends TestCase |
| 14 | +final class CleanUpOldSessionTokensTest extends TestCase |
16 | 15 | { |
17 | | - private AdministratorTokenRepository&MockObject $tokenRepository; |
18 | | - private CommandTester $commandTester; |
19 | | - |
20 | | - protected function setUp(): void |
| 16 | + public function testItRemovesAllExpiredTokensAndOutputsSuccess(): void |
21 | 17 | { |
22 | | - $this->tokenRepository = $this->createMock(AdministratorTokenRepository::class); |
| 18 | + $repo = $this->createMock(AdministratorTokenRepository::class); |
| 19 | + $em = $this->createMock(EntityManagerInterface::class); |
23 | 20 |
|
24 | | - $command = new CleanUpOldSessionTokens($this->tokenRepository); |
| 21 | + $token1 = (object) ['id' => 1]; |
| 22 | + $token2 = (object) ['id' => 2]; |
| 23 | + $expired = [$token1, $token2]; |
25 | 24 |
|
26 | | - $application = new Application(); |
27 | | - $application->add($command); |
| 25 | + $repo->expects($this->once()) |
| 26 | + ->method('getExpired') |
| 27 | + ->willReturn($expired); |
28 | 28 |
|
29 | | - $this->commandTester = new CommandTester($command); |
30 | | - } |
| 29 | + $removed = []; |
| 30 | + $em->expects($this->exactly(\count($expired))) |
| 31 | + ->method('remove') |
| 32 | + ->willReturnCallback(function (object $o) use (&$removed) { |
| 33 | + $removed[] = $o; |
| 34 | + }); |
31 | 35 |
|
32 | | - public function testExecuteSuccessfully(): void |
33 | | - { |
34 | | - $this->tokenRepository->expects($this->once()) |
35 | | - ->method('removeExpired') |
36 | | - ->willReturn(5); |
| 36 | + $em->expects($this->once()) |
| 37 | + ->method('flush'); |
37 | 38 |
|
38 | | - $this->commandTester->execute([]); |
| 39 | + $command = new CleanUpOldSessionTokens($repo, $em); |
| 40 | + $tester = new CommandTester($command); |
39 | 41 |
|
40 | | - $output = $this->commandTester->getDisplay(); |
41 | | - $this->assertStringContainsString('Successfully removed 5 expired session token(s)', $output); |
42 | | - $this->assertEquals(0, $this->commandTester->getStatusCode()); |
43 | | - } |
| 42 | + $exitCode = $tester->execute([]); |
44 | 43 |
|
45 | | - public function testExecuteWithNoExpiredTokens(): void |
46 | | - { |
47 | | - $this->tokenRepository->expects($this->once()) |
48 | | - ->method('removeExpired') |
49 | | - ->willReturn(0); |
| 44 | + self::assertSame(Command::SUCCESS, $exitCode); |
50 | 45 |
|
51 | | - $this->commandTester->execute([]); |
| 46 | + $display = $tester->getDisplay(); |
| 47 | + self::assertStringContainsString('Successfully removed 2 expired session token(s).', $display); |
52 | 48 |
|
53 | | - $output = $this->commandTester->getDisplay(); |
54 | | - $this->assertStringContainsString('Successfully removed 0 expired session token(s)', $output); |
55 | | - $this->assertEquals(0, $this->commandTester->getStatusCode()); |
| 49 | + self::assertEqualsCanonicalizing($expired, $removed); |
56 | 50 | } |
57 | 51 |
|
58 | | - public function testExecuteWithException(): void |
| 52 | + public function testItHandlesExceptionsAndOutputsFailure(): void |
59 | 53 | { |
60 | | - $this->tokenRepository->expects($this->once()) |
61 | | - ->method('removeExpired') |
62 | | - ->willThrowException(new Exception('Test exception')); |
| 54 | + $repo = $this->createMock(AdministratorTokenRepository::class); |
| 55 | + $em = $this->createMock(EntityManagerInterface::class); |
| 56 | + |
| 57 | + $repo->expects($this->once()) |
| 58 | + ->method('getExpired') |
| 59 | + ->willThrowException(new \RuntimeException('boom')); |
| 60 | + |
| 61 | + $em->expects($this->never())->method('remove'); |
| 62 | + $em->expects($this->never())->method('flush'); |
| 63 | + |
| 64 | + $command = new CleanUpOldSessionTokens($repo, $em); |
| 65 | + $tester = new CommandTester($command); |
63 | 66 |
|
64 | | - $this->commandTester->execute([]); |
| 67 | + $exitCode = $tester->execute([]); |
65 | 68 |
|
66 | | - $output = $this->commandTester->getDisplay(); |
67 | | - $this->assertStringContainsString('Error removing expired session tokens: Test exception', $output); |
68 | | - $this->assertEquals(1, $this->commandTester->getStatusCode()); |
| 69 | + self::assertSame(Command::FAILURE, $exitCode); |
| 70 | + self::assertStringContainsString('Error removing expired session tokens: boom', $tester->getDisplay()); |
69 | 71 | } |
70 | 72 | } |
0 commit comments