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