99use MeiliSearch \Bundle \Test \BaseKernelTestCase ;
1010use MeiliSearch \Bundle \Test \Entity \Page ;
1111use MeiliSearch \Bundle \Test \Entity \Post ;
12+ use MeiliSearch \Client ;
1213
1314class DoctrineEventSubscriberTest extends BaseKernelTestCase
1415{
16+ protected Client $ client ;
17+
18+ /**
19+ * @throws \Exception
20+ */
21+ public function setUp (): void
22+ {
23+ parent ::setUp ();
24+
25+ $ this ->client = $ this ->get ('search.client ' );
26+ }
27+
1528 /**
16- * This tests creates two posts in the database, but only one is triggered via an event to MS .
29+ * This tests creates two posts in the database, but only one is triggered via an event to Meilisearch .
1730 */
1831 public function testPostPersist (): void
1932 {
@@ -25,6 +38,8 @@ public function testPostPersist(): void
2538 $ subscriber = new DoctrineEventSubscriber ($ this ->searchService , []);
2639 $ subscriber ->postPersist ($ eventArgs );
2740
41+ $ this ->waitForAllTasks ();
42+
2843 $ result = $ this ->searchService ->search ($ this ->entityManager , Post::class, $ post ->getTitle ());
2944
3045 $ this ->assertCount (1 , $ result );
@@ -40,7 +55,8 @@ public function testPostPersistWithObjectId(): void
4055
4156 $ subscriber = new DoctrineEventSubscriber ($ this ->searchService , []);
4257 $ subscriber ->postPersist ($ eventArgs );
43- sleep (1 );
58+
59+ $ this ->waitForAllTasks ();
4460
4561 $ result = $ this ->searchService ->search ($ this ->entityManager , Page::class, $ page ->getTitle ());
4662
@@ -49,7 +65,7 @@ public function testPostPersistWithObjectId(): void
4965 }
5066
5167 /**
52- * This tests creates two posts in the database, but only one is triggered via an event to MS .
68+ * This tests creates two posts in the database, but only one is triggered via an event to Meilisearch .
5369 */
5470 public function testPostUpdate (): void
5571 {
@@ -61,6 +77,8 @@ public function testPostUpdate(): void
6177 $ subscriber = new DoctrineEventSubscriber ($ this ->searchService , []);
6278 $ subscriber ->postUpdate ($ eventArgs );
6379
80+ $ this ->waitForAllTasks ();
81+
6482 $ result = $ this ->searchService ->search ($ this ->entityManager , Post::class, $ post ->getTitle ());
6583
6684 $ this ->assertCount (1 , $ result );
@@ -76,7 +94,8 @@ public function testPostUpdateWithObjectId(): void
7694
7795 $ subscriber = new DoctrineEventSubscriber ($ this ->searchService , []);
7896 $ subscriber ->postUpdate ($ eventArgs );
79- sleep (1 );
97+
98+ $ this ->waitForAllTasks ();
8099
81100 $ result = $ this ->searchService ->search ($ this ->entityManager , Page::class, $ page ->getTitle ());
82101
@@ -85,7 +104,7 @@ public function testPostUpdateWithObjectId(): void
85104 }
86105
87106 /**
88- * This tests creates posts in the database, send it to MS via a trigger. Afterwards Doctrines 'preRemove' event
107+ * This tests creates posts in the database, send it to Meilisearch via a trigger. Afterwards Doctrines 'preRemove' event
89108 * is going to remove that entity from MS.
90109 */
91110 public function testPreRemove (): void
@@ -97,18 +116,16 @@ public function testPreRemove(): void
97116 $ subscriber = new DoctrineEventSubscriber ($ this ->searchService , []);
98117 $ subscriber ->postPersist ($ eventArgs );
99118
119+ $ this ->waitForAllTasks ();
120+
100121 $ result = $ this ->searchService ->search ($ this ->entityManager , Post::class, $ post ->getTitle ());
101122
102123 $ this ->assertCount (1 , $ result );
103124 $ this ->assertSame (1 , $ result [0 ]->getId ());
104125
105126 $ subscriber ->preRemove ($ eventArgs );
106127
107- /*
108- * As the deletion of a document is an asyncronous transaction, we need to wait some seconds
109- * till this is executed. This was introduced as with Github actions there was no other option.
110- */
111- sleep (2 );
128+ $ this ->waitForAllTasks ();
112129
113130 $ result = $ this ->searchService ->search ($ this ->entityManager , Post::class, $ post ->getTitle ());
114131
@@ -123,18 +140,29 @@ public function testPreRemoveWithObjectId(): void
123140
124141 $ subscriber = new DoctrineEventSubscriber ($ this ->searchService , []);
125142 $ subscriber ->postPersist ($ eventArgs );
126- sleep (1 );
143+
144+ $ this ->waitForAllTasks ();
127145
128146 $ result = $ this ->searchService ->search ($ this ->entityManager , Page::class, $ page ->getTitle ());
129147
130148 $ this ->assertCount (1 , $ result );
131149 $ this ->assertSame ((string ) $ page ->getId (), (string ) $ result [0 ]->getId ());
132150
133151 $ subscriber ->preRemove ($ eventArgs );
134- sleep (1 );
152+
153+ $ this ->waitForAllTasks ();
135154
136155 $ result = $ this ->searchService ->search ($ this ->entityManager , Page::class, $ page ->getTitle ());
137156
138157 $ this ->assertCount (0 , $ result );
139158 }
159+
160+ /**
161+ * Waits for all the tasks to be finished by checking the topest one (so the newest one).
162+ */
163+ private function waitForAllTasks (): void
164+ {
165+ $ firstTask = $ this ->client ->getTasks ()['results ' ][0 ];
166+ $ this ->client ->waitForTask ($ firstTask ['uid ' ]);
167+ }
140168}
0 commit comments