Skip to content

Commit 504034c

Browse files
committed
Fix tests
1 parent 56edfe6 commit 504034c

File tree

6 files changed

+139
-129
lines changed

6 files changed

+139
-129
lines changed

src/Domain/Messaging/Service/MessageProcessingPreparator.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PhpList\Core\Domain\Messaging\Service;
66

7-
use Doctrine\ORM\EntityManagerInterface;
87
use PhpList\Core\Domain\Analytics\Service\LinkTrackService;
98
use PhpList\Core\Domain\Messaging\Model\Message;
109
use PhpList\Core\Domain\Messaging\Repository\MessageRepository;
@@ -17,20 +16,17 @@ class MessageProcessingPreparator
1716
// phpcs:ignore Generic.Commenting.Todo
1817
// @todo: create functionality to track
1918
public const LINT_TRACK_ENDPOINT = '/api/v2/link-track';
20-
private EntityManagerInterface $entityManager;
2119
private SubscriberRepository $subscriberRepository;
2220
private MessageRepository $messageRepository;
2321
private LinkTrackService $linkTrackService;
2422
private TranslatorInterface $translator;
2523

2624
public function __construct(
27-
EntityManagerInterface $entityManager,
2825
SubscriberRepository $subscriberRepository,
2926
MessageRepository $messageRepository,
3027
LinkTrackService $linkTrackService,
3128
TranslatorInterface $translator,
3229
) {
33-
$this->entityManager = $entityManager;
3430
$this->subscriberRepository = $subscriberRepository;
3531
$this->messageRepository = $messageRepository;
3632
$this->linkTrackService = $linkTrackService;

tests/Unit/Domain/Analytics/Service/LinkTrackServiceTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testExtractAndSaveLinksWithNoLinks(): void
4242
$message->method('getId')->willReturn($messageId);
4343
$message->method('getContent')->willReturn($messageContent);
4444

45-
$this->linkTrackRepository->expects(self::never())->method('save');
45+
$this->linkTrackRepository->expects(self::never())->method('persist');
4646

4747
$result = $this->subject->extractAndSaveLinks($message, $userId);
4848

@@ -63,7 +63,7 @@ public function testExtractAndSaveLinksWithLinks(): void
6363
$message->method('getContent')->willReturn($messageContent);
6464

6565
$this->linkTrackRepository->expects(self::exactly(2))
66-
->method('save')
66+
->method('persist')
6767
->willReturnCallback(function (LinkTrack $linkTrack) use ($messageId, $userId) {
6868
self::assertSame($messageId, $linkTrack->getMessageId());
6969
self::assertSame($userId, $linkTrack->getUserId());
@@ -92,7 +92,7 @@ public function testExtractAndSaveLinksWithFooter(): void
9292
$message->method('getContent')->willReturn($messageContent);
9393

9494
$this->linkTrackRepository->expects(self::exactly(2))
95-
->method('save')
95+
->method('persist')
9696
->willReturnCallback(function (LinkTrack $linkTrack) use ($messageId, $userId) {
9797
self::assertSame($messageId, $linkTrack->getMessageId());
9898
self::assertSame($userId, $linkTrack->getUserId());
@@ -120,7 +120,7 @@ public function testExtractAndSaveLinksWithDuplicateLinks(): void
120120
$message->method('getContent')->willReturn($messageContent);
121121

122122
$this->linkTrackRepository->expects(self::once())
123-
->method('save')
123+
->method('persist')
124124
->willReturnCallback(function (LinkTrack $linkTrack) use ($messageId, $userId) {
125125
self::assertSame($messageId, $linkTrack->getMessageId());
126126
self::assertSame($userId, $linkTrack->getUserId());
@@ -147,7 +147,7 @@ public function testExtractAndSaveLinksWithNullText(): void
147147
$message->method('getContent')->willReturn($messageContent);
148148

149149
$this->linkTrackRepository->expects(self::once())
150-
->method('save')
150+
->method('persist')
151151
->willReturnCallback(function (LinkTrack $linkTrack) use ($messageId, $userId) {
152152
self::assertSame($messageId, $linkTrack->getMessageId());
153153
self::assertSame($userId, $linkTrack->getUserId());
@@ -219,7 +219,7 @@ public function testExtractAndSaveLinksWithExistingLink(): void
219219
->willReturn($existingLinkTrack);
220220

221221
$this->linkTrackRepository->expects(self::never())
222-
->method('save');
222+
->method('persist');
223223

224224
$result = $this->subject->extractAndSaveLinks($message, $userId);
225225

tests/Unit/Domain/Messaging/Command/ProcessQueueCommandTest.php

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,29 @@
44

55
namespace PhpList\Core\Tests\Unit\Domain\Messaging\Command;
66

7+
use Doctrine\ORM\EntityManagerInterface;
78
use Exception;
89
use PhpList\Core\Domain\Configuration\Service\Provider\ConfigProvider;
910
use PhpList\Core\Domain\Messaging\Command\ProcessQueueCommand;
11+
use PhpList\Core\Domain\Messaging\Message\CampaignProcessorMessage;
1012
use PhpList\Core\Domain\Messaging\Model\Message;
1113
use PhpList\Core\Domain\Messaging\Repository\MessageRepository;
1214
use PhpList\Core\Domain\Messaging\Service\MessageProcessingPreparator;
13-
use PhpList\Core\Domain\Messaging\Service\Processor\CampaignProcessor;
1415
use PHPUnit\Framework\MockObject\MockObject;
1516
use PHPUnit\Framework\TestCase;
1617
use Symfony\Component\Console\Application;
1718
use Symfony\Component\Console\Tester\CommandTester;
1819
use Symfony\Component\Lock\LockFactory;
1920
use Symfony\Component\Lock\LockInterface;
21+
use Symfony\Component\Messenger\Envelope;
22+
use Symfony\Component\Messenger\MessageBusInterface;
2023
use Symfony\Component\Translation\Translator;
2124

2225
class ProcessQueueCommandTest extends TestCase
2326
{
2427
private MessageRepository&MockObject $messageRepository;
2528
private MessageProcessingPreparator&MockObject $messageProcessingPreparator;
26-
private CampaignProcessor&MockObject $campaignProcessor;
29+
private MessageBusInterface&MockObject $messageBus;
2730
private LockInterface&MockObject $lock;
2831
private CommandTester $commandTester;
2932
private Translator&MockObject $translator;
@@ -33,7 +36,7 @@ protected function setUp(): void
3336
$this->messageRepository = $this->createMock(MessageRepository::class);
3437
$lockFactory = $this->createMock(LockFactory::class);
3538
$this->messageProcessingPreparator = $this->createMock(MessageProcessingPreparator::class);
36-
$this->campaignProcessor = $this->createMock(CampaignProcessor::class);
39+
$this->messageBus = $this->createMock(MessageBusInterface::class);
3740
$this->lock = $this->createMock(LockInterface::class);
3841
$this->translator = $this->createMock(Translator::class);
3942

@@ -45,9 +48,10 @@ protected function setUp(): void
4548
messageRepository: $this->messageRepository,
4649
lockFactory: $lockFactory,
4750
messagePreparator: $this->messageProcessingPreparator,
48-
campaignProcessor: $this->campaignProcessor,
51+
messageBus: $this->messageBus,
4952
configProvider: $this->createMock(ConfigProvider::class),
5053
translator: $this->translator,
54+
entityManager: $this->createMock(EntityManagerInterface::class),
5155
);
5256

5357
$application = new Application();
@@ -97,8 +101,8 @@ public function testExecuteWithNoCampaigns(): void
97101
->with($this->anything(), $this->anything())
98102
->willReturn([]);
99103

100-
$this->campaignProcessor->expects($this->never())
101-
->method('process');
104+
$this->messageBus->expects($this->never())
105+
->method('dispatch');
102106

103107
$this->commandTester->execute([]);
104108

@@ -121,22 +125,29 @@ public function testExecuteWithCampaigns(): void
121125
->method('ensureCampaignsHaveUuid');
122126

123127
$campaign = $this->createMock(Message::class);
128+
$campaign->method('getId')->willReturn(1);
124129

125130
$this->messageRepository->expects($this->once())
126131
->method('getByStatusAndEmbargo')
127132
->with($this->anything(), $this->anything())
128133
->willReturn([$campaign]);
129134

130-
$this->campaignProcessor->expects($this->once())
131-
->method('process')
132-
->with($campaign, $this->anything());
135+
$this->messageBus->expects($this->once())
136+
->method('dispatch')
137+
->with(
138+
$this->callback(function (CampaignProcessorMessage $message) use ($campaign) {
139+
$this->assertEquals($campaign->getId(), $message->getMessageId());
140+
return true;
141+
}),
142+
$this->equalTo([])
143+
)
144+
->willReturn(new Envelope(new CampaignProcessorMessage($campaign->getId())));
133145

134146
$this->commandTester->execute([]);
135147

136148
$this->assertEquals(0, $this->commandTester->getStatusCode());
137149
}
138150

139-
140151
public function testExecuteWithMultipleCampaigns(): void
141152
{
142153
$this->lock->expects($this->once())
@@ -153,26 +164,36 @@ public function testExecuteWithMultipleCampaigns(): void
153164
->method('ensureCampaignsHaveUuid');
154165

155166
$campaign1 = $this->createMock(Message::class);
167+
$campaign1->method('getId')->willReturn(1);
156168
$campaign2 = $this->createMock(Message::class);
169+
$campaign2->method('getId')->willReturn(2);
157170

158171
$this->messageRepository->expects($this->once())
159172
->method('getByStatusAndEmbargo')
160173
->with($this->anything(), $this->anything())
161174
->willReturn([$campaign1, $campaign2]);
162175

163-
$this->campaignProcessor->expects($this->exactly(2))
164-
->method('process')
165-
->withConsecutive(
166-
[$campaign1, $this->anything()],
167-
[$campaign2, $this->anything()]
168-
);
176+
$this->messageBus->expects($this->exactly(2))
177+
->method('dispatch')
178+
->willReturnCallback(function (CampaignProcessorMessage $message, array $stamps) use ($campaign1, $campaign2) {
179+
static $call = 0;
180+
$call++;
181+
if ($call === 1) {
182+
$this->assertEquals($campaign1->getId(), $message->getMessageId());
183+
} else {
184+
$this->assertEquals($campaign2->getId(), $message->getMessageId());
185+
}
186+
$this->assertSame([], $stamps);
187+
188+
return new Envelope(new CampaignProcessorMessage($message->getMessageId()));
189+
});
169190

170191
$this->commandTester->execute([]);
171192

172193
$this->assertEquals(0, $this->commandTester->getStatusCode());
173194
}
174195

175-
public function testExecuteWithProcessorException(): void
196+
public function testExecuteWithDispatcherException(): void
176197
{
177198
$this->lock->expects($this->once())
178199
->method('acquire')
@@ -188,16 +209,23 @@ public function testExecuteWithProcessorException(): void
188209
->method('ensureCampaignsHaveUuid');
189210

190211
$campaign = $this->createMock(Message::class);
212+
$campaign->method('getId')->willReturn(1);
191213

192214
$this->messageRepository->expects($this->once())
193215
->method('getByStatusAndEmbargo')
194216
->with($this->anything(), $this->anything())
195217
->willReturn([$campaign]);
196218

197-
$this->campaignProcessor->expects($this->once())
198-
->method('process')
199-
->with($campaign, $this->anything())
200-
->willThrowException(new Exception('Test exception'));
219+
$this->messageBus->expects($this->once())
220+
->method('dispatch')
221+
->with(
222+
$this->callback(function (CampaignProcessorMessage $message) use ($campaign) {
223+
$this->assertEquals($campaign->getId(), $message->getMessageId());
224+
return true;
225+
}),
226+
$this->equalTo([])
227+
)
228+
->willThrowException(new Exception());
201229

202230
$this->commandTester->execute([]);
203231

0 commit comments

Comments
 (0)