|
7 | 7 | use DateTimeImmutable; |
8 | 8 | use Doctrine\ORM\EntityManagerInterface; |
9 | 9 | use PhpList\Core\Domain\Messaging\Model\Bounce; |
| 10 | +use PhpList\Core\Domain\Messaging\Model\UserMessageBounce; |
10 | 11 | use PhpList\Core\Domain\Messaging\Repository\BounceRepository; |
11 | 12 | use PhpList\Core\Domain\Messaging\Repository\UserMessageBounceRepository; |
12 | 13 | use PhpList\Core\Domain\Messaging\Service\Manager\BounceManager; |
| 14 | +use PhpList\Core\Domain\Subscription\Model\Subscriber; |
13 | 15 | use PHPUnit\Framework\MockObject\MockObject; |
14 | 16 | use PHPUnit\Framework\TestCase; |
15 | 17 | use Psr\Log\LoggerInterface; |
16 | 18 |
|
17 | 19 | class BounceManagerTest extends TestCase |
18 | 20 | { |
19 | 21 | private BounceRepository&MockObject $repository; |
| 22 | + private UserMessageBounceRepository&MockObject $userMessageBounceRepository; |
| 23 | + private EntityManagerInterface&MockObject $entityManager; |
| 24 | + private LoggerInterface&MockObject $logger; |
20 | 25 | private BounceManager $manager; |
21 | 26 |
|
22 | 27 | protected function setUp(): void |
23 | 28 | { |
24 | 29 | $this->repository = $this->createMock(BounceRepository::class); |
25 | | - $userMessageBounceRepository = $this->createMock(UserMessageBounceRepository::class); |
| 30 | + $this->userMessageBounceRepository = $this->createMock(UserMessageBounceRepository::class); |
| 31 | + $this->entityManager = $this->createMock(EntityManagerInterface::class); |
| 32 | + $this->logger = $this->createMock(LoggerInterface::class); |
26 | 33 | $this->manager = new BounceManager( |
27 | 34 | bounceRepository: $this->repository, |
28 | | - userMessageBounceRepo: $userMessageBounceRepository, |
29 | | - entityManager: $this->createMock(EntityManagerInterface::class), |
30 | | - logger: $this->createMock(LoggerInterface::class), |
| 35 | + userMessageBounceRepo: $this->userMessageBounceRepository, |
| 36 | + entityManager: $this->entityManager, |
| 37 | + logger: $this->logger, |
31 | 38 | ); |
32 | 39 | } |
33 | 40 |
|
@@ -102,4 +109,103 @@ public function testGetByIdReturnsNullWhenNotFound(): void |
102 | 109 |
|
103 | 110 | $this->assertNull($this->manager->getById(999)); |
104 | 111 | } |
| 112 | + |
| 113 | + public function testUpdateChangesFieldsAndSaves(): void |
| 114 | + { |
| 115 | + $bounce = new Bounce(); |
| 116 | + $this->repository->expects($this->once()) |
| 117 | + ->method('save') |
| 118 | + ->with($bounce); |
| 119 | + |
| 120 | + $updated = $this->manager->update($bounce, 'processed', 'done'); |
| 121 | + $this->assertSame($bounce, $updated); |
| 122 | + $this->assertSame('processed', $bounce->getStatus()); |
| 123 | + $this->assertSame('done', $bounce->getComment()); |
| 124 | + } |
| 125 | + |
| 126 | + public function testLinkUserMessageBounceFlushesAndSetsFields(): void |
| 127 | + { |
| 128 | + $bounce = new Bounce(); |
| 129 | + $this->setId($bounce, 77); |
| 130 | + |
| 131 | + $this->entityManager->expects($this->once())->method('flush'); |
| 132 | + |
| 133 | + $dt = new DateTimeImmutable('2024-05-01 12:34:56'); |
| 134 | + $umb = $this->manager->linkUserMessageBounce($bounce, $dt, 123, 456); |
| 135 | + |
| 136 | + $this->assertSame(77, $umb->getBounceId()); |
| 137 | + $this->assertSame(123, $umb->getUserId()); |
| 138 | + $this->assertSame(456, $umb->getMessageId()); |
| 139 | + } |
| 140 | + |
| 141 | + public function testExistsUserMessageBounceDelegatesToRepo(): void |
| 142 | + { |
| 143 | + $this->userMessageBounceRepository->expects($this->once()) |
| 144 | + ->method('existsByMessageIdAndUserId') |
| 145 | + ->with(456, 123) |
| 146 | + ->willReturn(true); |
| 147 | + |
| 148 | + $this->assertTrue($this->manager->existsUserMessageBounce(123, 456)); |
| 149 | + } |
| 150 | + |
| 151 | + public function testFindByStatusDelegatesToRepository(): void |
| 152 | + { |
| 153 | + $b1 = new Bounce(); |
| 154 | + $b2 = new Bounce(); |
| 155 | + $this->repository->expects($this->once()) |
| 156 | + ->method('findByStatus') |
| 157 | + ->with('new') |
| 158 | + ->willReturn([$b1, $b2]); |
| 159 | + |
| 160 | + $this->assertSame([$b1, $b2], $this->manager->findByStatus('new')); |
| 161 | + } |
| 162 | + |
| 163 | + public function testGetUserMessageBounceCount(): void |
| 164 | + { |
| 165 | + $this->userMessageBounceRepository->expects($this->once()) |
| 166 | + ->method('count') |
| 167 | + ->willReturn(5); |
| 168 | + $this->assertSame(5, $this->manager->getUserMessageBounceCount()); |
| 169 | + } |
| 170 | + |
| 171 | + public function testFetchUserMessageBounceBatchDelegates(): void |
| 172 | + { |
| 173 | + $expected = [['umb' => new UserMessageBounce(1, new \DateTime()), 'bounce' => new Bounce()]]; |
| 174 | + $this->userMessageBounceRepository->expects($this->once()) |
| 175 | + ->method('getPaginatedWithJoinNoRelation') |
| 176 | + ->with(10, 50) |
| 177 | + ->willReturn($expected); |
| 178 | + $this->assertSame($expected, $this->manager->fetchUserMessageBounceBatch(10, 50)); |
| 179 | + } |
| 180 | + |
| 181 | + public function testGetUserMessageHistoryWithBouncesDelegates(): void |
| 182 | + { |
| 183 | + $subscriber = new Subscriber(); |
| 184 | + $expected = []; |
| 185 | + $this->userMessageBounceRepository->expects($this->once()) |
| 186 | + ->method('getUserMessageHistoryWithBounces') |
| 187 | + ->with($subscriber) |
| 188 | + ->willReturn($expected); |
| 189 | + $this->assertSame($expected, $this->manager->getUserMessageHistoryWithBounces($subscriber)); |
| 190 | + } |
| 191 | + |
| 192 | + public function testAnnounceDeletionModeLogsCorrectMessage(): void |
| 193 | + { |
| 194 | + $this->logger->expects($this->exactly(2)) |
| 195 | + ->method('info') |
| 196 | + ->withConsecutive([ |
| 197 | + 'Running in test mode, not deleting messages from mailbox' |
| 198 | + ], [ |
| 199 | + 'Processed messages will be deleted from the mailbox' |
| 200 | + ]); |
| 201 | + |
| 202 | + $this->manager->announceDeletionMode(true); |
| 203 | + $this->manager->announceDeletionMode(false); |
| 204 | + } |
| 205 | + |
| 206 | + private function setId(object $entity, int $id): void |
| 207 | + { |
| 208 | + $ref = new \ReflectionProperty($entity, 'id'); |
| 209 | + $ref->setValue($entity, $id); |
| 210 | + } |
105 | 211 | } |
0 commit comments