2020use Sylius \ImportExport \Messenger \Command \ImportCommand ;
2121use Sylius \ImportExport \Messenger \Handler \ImportCommandHandler ;
2222use Sylius \Resource \Doctrine \Persistence \RepositoryInterface ;
23+ use Symfony \Component \Messenger \Exception \HandlerFailedException ;
24+ use Symfony \Component \Messenger \MessageBusInterface ;
2325use Symfony \Component \Uid \Uuid ;
2426use Tests \Sylius \ImportExport \Entity \Dummy ;
2527use Tests \Sylius \ImportExport \Functional \FunctionalTestCase ;
2628
2729final class ImportHandlerTest extends FunctionalTestCase
2830{
29- private ImportCommandHandler $ handler ;
31+ private MessageBusInterface $ commandBus ;
3032
3133 private string $ importsDir ;
3234
@@ -36,7 +38,7 @@ protected function setUp(): void
3638 {
3739 parent ::setUp ();
3840
39- $ this ->handler = $ this ->getContainer ()->get ('sylius_import_export.messenger.command_handler. import ' );
41+ $ this ->commandBus = $ this ->getContainer ()->get ('sylius_import_export.import.command_bus ' );
4042 $ this ->importsDir = $ this ->getContainer ()->getParameter ('sylius_import_export.import_files_directory ' );
4143 $ this ->processRepository = $ this ->getContainer ()->get ('sylius_import_export.repository.process_import ' );
4244
@@ -63,7 +65,7 @@ public function it_imports_data_from_array(array $importData, int $expectedCount
6365 [],
6466 );
6567
66- $ this ->handler -> __invoke (new ImportCommand ($ processUuid , $ importData ));
68+ $ this ->commandBus -> dispatch (new ImportCommand ($ processUuid , $ importData ));
6769
6870 $ dummyRepository = $ this ->entityManager ->getRepository (Dummy::class);
6971 $ importedDummies = $ dummyRepository ->findAll ();
@@ -90,7 +92,7 @@ public function it_imports_single_dummy_with_basic_data(): void
9092 $ processUuid = (string ) Uuid::v7 ();
9193 $ process = $ this ->createImportProcess ($ processUuid , 'json ' , '/tmp/test.json ' , []);
9294
93- $ this ->handler -> __invoke (new ImportCommand ($ processUuid , $ importData ));
95+ $ this ->commandBus -> dispatch (new ImportCommand ($ processUuid , $ importData ));
9496
9597 $ dummyRepository = $ this ->entityManager ->getRepository (Dummy::class);
9698 $ dummy = $ dummyRepository ->findOneBy (['uuid ' => 'test-uuid-1 ' ]);
@@ -122,7 +124,7 @@ public function it_imports_dummy_with_nested_config(): void
122124 $ processUuid = (string ) Uuid::v7 ();
123125 $ process = $ this ->createImportProcess ($ processUuid , 'json ' , '/tmp/test.json ' , []);
124126
125- $ this ->handler -> __invoke (new ImportCommand ($ processUuid , $ importData ));
127+ $ this ->commandBus -> dispatch (new ImportCommand ($ processUuid , $ importData ));
126128
127129 $ dummyRepository = $ this ->entityManager ->getRepository (Dummy::class);
128130 $ dummy = $ dummyRepository ->findOneBy (['uuid ' => 'test-uuid-2 ' ]);
@@ -152,7 +154,7 @@ public function it_handles_multiple_batches_correctly(): void
152154 ['uuid ' => 'batch1-1 ' , 'text ' => 'Batch 1 Item 1 ' , 'counter ' => 1 , 'config ' => []],
153155 ['uuid ' => 'batch1-2 ' , 'text ' => 'Batch 1 Item 2 ' , 'counter ' => 2 , 'config ' => []],
154156 ];
155- $ this ->handler -> __invoke (new ImportCommand ($ processUuid , $ batchData1 ));
157+ $ this ->commandBus -> dispatch (new ImportCommand ($ processUuid , $ batchData1 ));
156158
157159 $ this ->entityManager ->refresh ($ process );
158160 $ this ->assertSame (2 , $ process ->getImportedCount ());
@@ -162,7 +164,7 @@ public function it_handles_multiple_batches_correctly(): void
162164 $ batchData2 = [
163165 ['uuid ' => 'batch2-1 ' , 'text ' => 'Batch 2 Item 1 ' , 'counter ' => 3 , 'config ' => []],
164166 ];
165- $ this ->handler -> __invoke (new ImportCommand ($ processUuid , $ batchData2 ));
167+ $ this ->commandBus -> dispatch (new ImportCommand ($ processUuid , $ batchData2 ));
166168
167169 $ this ->entityManager ->refresh ($ process );
168170 $ this ->assertSame (3 , $ process ->getImportedCount ());
@@ -172,7 +174,7 @@ public function it_handles_multiple_batches_correctly(): void
172174 $ batchData3 = [
173175 ['uuid ' => 'batch3-1 ' , 'text ' => 'Batch 3 Item 1 ' , 'counter ' => 4 , 'config ' => []],
174176 ];
175- $ this ->handler -> __invoke (new ImportCommand ($ processUuid , $ batchData3 ));
177+ $ this ->commandBus -> dispatch (new ImportCommand ($ processUuid , $ batchData3 ));
176178
177179 $ this ->entityManager ->refresh ($ process );
178180 $ this ->assertSame (4 , $ process ->getImportedCount ());
@@ -204,9 +206,10 @@ public function it_handles_validation_errors(): void
204206 ],
205207 ];
206208
207- $ this ->handler -> __invoke (new ImportCommand ($ processUuid , $ invalidData ));
209+ $ this ->commandBus -> dispatch (new ImportCommand ($ processUuid , $ invalidData ));
208210
209- $ process = $ this ->processRepository ->find ($ processUuid );
211+ $ processRepository = $ this ->getContainer ()->get ('sylius_import_export.repository.process_import ' );
212+ $ process = $ processRepository ->find ($ processUuid );
210213 $ this ->assertSame ('failed ' , $ process ->getStatus ());
211214 $ this ->assertNotNull ($ process ->getErrorMessage ());
212215 $ this ->assertStringContainsString ('Validation failed ' , $ process ->getErrorMessage ());
@@ -232,7 +235,7 @@ public function it_uses_custom_validation_groups(): void
232235 ],
233236 ];
234237
235- $ this ->handler -> __invoke (new ImportCommand ($ processUuid , $ importData ));
238+ $ this ->commandBus -> dispatch (new ImportCommand ($ processUuid , $ importData ));
236239
237240 $ this ->entityManager ->refresh ($ process );
238241 $ this ->assertSame (1 , $ process ->getImportedCount ());
@@ -242,10 +245,10 @@ public function it_uses_custom_validation_groups(): void
242245 #[Test]
243246 public function it_handles_process_not_found_error (): void
244247 {
245- $ this ->expectException (\ Sylius \ ImportExport \ Exception \ImportFailedException ::class);
248+ $ this ->expectException (HandlerFailedException ::class);
246249 $ this ->expectExceptionMessage ('Process with uuid "non-existent-uuid" not found. ' );
247250
248- $ this ->handler -> __invoke (new ImportCommand ('non-existent-uuid ' , []));
251+ $ this ->commandBus -> dispatch (new ImportCommand ('non-existent-uuid ' , []));
249252 }
250253
251254 public static function getImportData (): array
0 commit comments