66
77use  Doctrine \ORM \EntityRepository ;
88use  Generator ;
9+ use  JG \BatchEntityImportBundle \Event \RecordImportedSuccessfullyEvent ;
910use  JG \BatchEntityImportBundle \Tests \DatabaseLoader ;
1011use  JG \BatchEntityImportBundle \Tests \Fixtures \Entity \TestEntity ;
12+ use  JG \BatchEntityImportBundle \Tests \Fixtures \Event \TestableEventDispatcher ;
1113use  Symfony \Bundle \FrameworkBundle \KernelBrowser ;
1214use  Symfony \Bundle \FrameworkBundle \Test \WebTestCase ;
1315use  Symfony \Component \DependencyInjection \Exception \ServiceNotFoundException ;
@@ -17,7 +19,8 @@ class ImportControllerTraitTest extends WebTestCase
1719    private  const  DEFAULT_RECORDS_NUMBER  = 20 ;
1820    private  const  NEW_RECORDS_NUMBER  = 30 ;
1921    private  const  URL  = '/jg_batch_entity_import_bundle/import ' ;
20-     protected  KernelBrowser   $ client ;
22+     private  KernelBrowser   $ client ;
23+     private  readonly  TestableEventDispatcher   $ eventDispatcher ;
2124
2225    protected  function  setUp (): void 
2326    {
@@ -26,6 +29,10 @@ protected function setUp(): void
2629        $ databaseLoader  = self ::$ kernel ->getContainer ()->get (DatabaseLoader::class);
2730        $ databaseLoader ->reload ();
2831        $ databaseLoader ->loadFixtures ();
32+ 
33+         $ this  ->eventDispatcher  = self ::$ kernel ->getContainer ()->get (TestableEventDispatcher::class);
34+         $ this  ->assertInstanceOf (TestableEventDispatcher::class, $ this  ->eventDispatcher );
35+         $ this  ->eventDispatcher ->resetDispatchedEvents ();
2936    }
3037
3138    public  function  testInsertNewData (): void 
@@ -51,6 +58,8 @@ public function testInsertNewData(): void
5158        $ this  ->assertEntityValues (['test2 ' , 'lorem ipsum 2 ' , 'qwerty2 ' , ['arr_val_1 ' , 'arr_val_2 ' , 'arr_val_3 ' ]], $ newEntityId2 );
5259        $ this  ->assertEntityValues (['test3 ' , 'lorem ipsum 3 ' , 'qwerty3 ' , []], $ newEntityId3 );
5360        $ this  ->assertEntityValues (['test4 ' , 'lorem ipsum 4 ' , 'qwerty4 ' , ['arr_val_1 ' , '' ]], $ newEntityId4 );
61+ 
62+         $ this  ->checkDispatchedEvents (self ::NEW_RECORDS_NUMBER );
5463    }
5564
5665    /** 
@@ -82,6 +91,8 @@ public function testUpdateExistingRecord(
8291
8392        $ this  ->checkData (0 );
8493        $ this  ->assertEntityValues ($ expectedValuesAfterChange , $ updatedEntityId );
94+ 
95+         $ this  ->checkDispatchedEvents (1 );
8596    }
8697
8798    public  static  function  updateRecordDataProvider (): Generator 
@@ -116,8 +127,10 @@ public function testDuplicationFoundInDatabase(): void
116127
117128        $ this  ->checkData ();
118129        $ this  ->assertEntityValues (['test2 ' , 'lorem ipsum 2 ' , 'qwerty2 ' , ['arr_val_1 ' , 'arr_val_2 ' , 'arr_val_3 ' ]], $ updatedEntityId );
130+         $ this  ->checkDispatchedEvents (self ::NEW_RECORDS_NUMBER );
119131
120132        // update existing data 
133+         $ this  ->eventDispatcher ->resetDispatchedEvents ();
121134        $ this  ->submitSelectFileForm (__DIR__  . '/../Fixtures/Resources/test_updated_data.csv ' );
122135
123136        $ this  ->client ->submitForm ('btn-submit ' , [
@@ -142,6 +155,8 @@ public function testDuplicationFoundInDatabase(): void
142155        );
143156        self ::assertStringContainsString ('Such entity already exists for the same values of fields: test-private-property2. ' , $ response ->getContent ());
144157        self ::assertCount (self ::DEFAULT_RECORDS_NUMBER  + self ::NEW_RECORDS_NUMBER , $ this  ->getRepository ()->findAll ());
158+ 
159+         $ this  ->checkDispatchedEvents (0 );
145160    }
146161
147162    public  function  testImportFileWrongExtension (): void 
@@ -151,6 +166,8 @@ public function testImportFileWrongExtension(): void
151166
152167        self ::assertStringContainsString ('Wrong file extension. ' , $ this  ->client ->getResponse ()->getContent ());
153168        self ::assertStringContainsString ('id="file_import_file" ' , $ this  ->client ->getResponse ()->getContent ());
169+ 
170+         $ this  ->checkDispatchedEvents (0 );
154171    }
155172
156173    public  function  testInvalidDataTypeFlashMessage (): void 
@@ -160,6 +177,8 @@ public function testInvalidDataTypeFlashMessage(): void
160177
161178        $ this  ->client ->submitForm ('btn-submit ' );
162179        self ::assertStringContainsString ('Invalid type of data. Probably missing validation. ' , $ this  ->client ->getResponse ()->getContent ());
180+ 
181+         $ this  ->checkDispatchedEvents (0 );
163182    }
164183
165184    public  function  testImportConfigurationServiceNotFound (): void 
@@ -168,6 +187,8 @@ public function testImportConfigurationServiceNotFound(): void
168187        $ this  ->expectException (ServiceNotFoundException::class);
169188        $ this  ->client ->request ('GET ' , '/jg_batch_entity_import_bundle/import_no_service ' );
170189        $ this  ->client ->submitForm ('btn-submit ' , ['file_import[file] '  => __DIR__  . '/../Fixtures/Resources/test.csv ' ]);
190+ 
191+         $ this  ->checkDispatchedEvents (0 );
171192    }
172193
173194    private  function  submitSelectFileForm (string  $ uploadedFile ): void 
@@ -206,4 +227,15 @@ private function getRepository(): EntityRepository
206227    {
207228        return  self ::$ kernel ->getContainer ()->get ('doctrine.orm.entity_manager ' )->getRepository (TestEntity::class);
208229    }
230+ 
231+     private  function  checkDispatchedEvents (int  $ expectedNumber ): void 
232+     {
233+         $ dispatchedEvents  = $ this  ->eventDispatcher ->getEventsFor (RecordImportedSuccessfullyEvent::class);
234+         self ::assertCount ($ expectedNumber , $ dispatchedEvents );
235+         foreach  ($ dispatchedEvents  as  $ event ) {
236+             self ::assertInstanceOf (RecordImportedSuccessfullyEvent::class, $ event );
237+             self ::assertSame (TestEntity::class, $ event ->class );
238+             self ::assertNotEmpty ($ event ->id );
239+         }
240+     }
209241}
0 commit comments