From 0eedd62e77cca322a250cb2a50968d1ea7860a12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9mentine=20Urquizar?= Date: Wed, 12 Jan 2022 17:40:23 +0100 Subject: [PATCH 01/12] Changes regarding task API changes --- composer.json | 2 +- src/Command/MeiliSearchCreateCommand.php | 12 +++++------ src/Command/MeiliSearchImportCommand.php | 26 ++++++++++++------------ src/Engine.php | 2 +- src/Exception/UpdateException.php | 4 ++-- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/composer.json b/composer.json index dd4da730..3d5091b7 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "ext-json": "*", "doctrine/doctrine-bundle": "^2.4", "illuminate/collections": "^8.47", - "meilisearch/meilisearch-php": "^0.20", + "meilisearch/meilisearch-php": "^0.21.0", "symfony/filesystem": "^4.4 || ^5.0 || ^6.0", "symfony/property-access": "^4.4 || ^5.0 || ^6.0", "symfony/serializer": "^4.4 || ^5.0 || ^6.0" diff --git a/src/Command/MeiliSearchCreateCommand.php b/src/Command/MeiliSearchCreateCommand.php index 46c44564..5adbb523 100644 --- a/src/Command/MeiliSearchCreateCommand.php +++ b/src/Command/MeiliSearchCreateCommand.php @@ -5,7 +5,7 @@ namespace MeiliSearch\Bundle\Command; use MeiliSearch\Bundle\Exception\InvalidSettingName; -use MeiliSearch\Bundle\Exception\UpdateException; +use MeiliSearch\Bundle\Exception\TaskException; use MeiliSearch\Bundle\Model\Aggregator; use MeiliSearch\Bundle\SearchService; use MeiliSearch\Client; @@ -74,13 +74,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int throw new InvalidSettingName(sprintf('Invalid setting name: "%s"', $variable)); } - $update = $indexInstance->{$method}($value); + $task = $indexInstance->{$method}($value); - $indexInstance->waitForPendingUpdate($update['updateId']); - $updateStatus = $indexInstance->getUpdateStatus($update['updateId']); + $indexInstance->waitForTask($task['uid']); + $task = $indexInstance->getTask($task['uid']); - if ('failed' === $updateStatus['status']) { - throw new UpdateException($updateStatus['error']); + if ('failed' === $task['status']) { + throw new TaskException($task['error']); } } } diff --git a/src/Command/MeiliSearchImportCommand.php b/src/Command/MeiliSearchImportCommand.php index f9de1e6f..8df07fb1 100644 --- a/src/Command/MeiliSearchImportCommand.php +++ b/src/Command/MeiliSearchImportCommand.php @@ -6,7 +6,7 @@ use Doctrine\Persistence\ManagerRegistry; use MeiliSearch\Bundle\Exception\InvalidSettingName; -use MeiliSearch\Bundle\Exception\UpdateException; +use MeiliSearch\Bundle\Exception\TaskException; use MeiliSearch\Bundle\Model\Aggregator; use MeiliSearch\Bundle\SearchService; use MeiliSearch\Client; @@ -124,14 +124,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int } // Update - $update = $indexInstance->{$method}($value); + $task = $indexInstance->{$method}($value); - // Get Update status from updateID - $indexInstance->waitForPendingUpdate($update['updateId'], $responseTimeout); - $updateStatus = $indexInstance->getUpdateStatus($update['updateId']); + // Get task information using uid + $indexInstance->waitForTask($task['uid'], $responseTimeout); + $task = $indexInstance->getTask($task['uid']); - if ('failed' === $updateStatus['status']) { - throw new UpdateException($updateStatus['error']); + if ('failed' === $task['status']) { + throw new TaskException($task['error']); } else { $output->writeln('Settings updated.'); } @@ -164,15 +164,15 @@ private function formatIndexingResponse(array $batch, int $responseTimeout): arr $indexInstance = $this->searchClient->index($indexName); - // Get Update status from updateID - $indexInstance->waitForPendingUpdate($apiResponse['updateId'], $responseTimeout); - $updateStatus = $indexInstance->getUpdateStatus($apiResponse['updateId']); + // Get task information using uid + $indexInstance->waitForTask($apiResponse['uid'], $responseTimeout); + $task = $indexInstance->getTask($apiResponse['uid']); - if ('failed' === $updateStatus['status']) { - throw new UpdateException($updateStatus['error']); + if ('failed' === $task['status']) { + throw new TaskException($task['error']); } - $formattedResponse[$indexName] += $updateStatus['type']['number']; + $formattedResponse[$indexName] += $task['type']['number']; } } diff --git a/src/Engine.php b/src/Engine.php index bd933b34..051dcf6c 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -112,7 +112,7 @@ public function clear(string $indexUid): array $index = $this->client->getOrCreateIndex($indexUid); $return = $index->deleteAllDocuments(); - return $index->getUpdateStatus($return['updateId']); + return $index->getTask($return['uid']); } /** diff --git a/src/Exception/UpdateException.php b/src/Exception/UpdateException.php index ef887f69..7ceaaa6f 100644 --- a/src/Exception/UpdateException.php +++ b/src/Exception/UpdateException.php @@ -5,8 +5,8 @@ namespace MeiliSearch\Bundle\Exception; /** - * Class UpdateException. + * Class TaskException. */ -final class UpdateException extends \LogicException +final class TaskException extends \LogicException { } From 2707074303ebc003c2463683d78f17aeba162182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9mentine=20Urquizar?= Date: Wed, 12 Jan 2022 17:52:45 +0100 Subject: [PATCH 02/12] Remove getOrCreateIndex usage --- src/Command/MeiliSearchCreateCommand.php | 2 +- src/Command/MeiliSearchImportCommand.php | 2 +- src/Engine.php | 6 +++--- tests/Integration/CommandsTest.php | 4 ++-- tests/Integration/SearchTest.php | 2 +- tests/Integration/SettingsTest.php | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Command/MeiliSearchCreateCommand.php b/src/Command/MeiliSearchCreateCommand.php index 5adbb523..81a86edd 100644 --- a/src/Command/MeiliSearchCreateCommand.php +++ b/src/Command/MeiliSearchCreateCommand.php @@ -64,7 +64,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->writeln('Creating index '.$index['name'].' for '.$entityClassName.''); - $indexInstance = $this->searchClient->getOrCreateIndex($index['name']); + $indexInstance = $this->searchClient->index($index['name']); if (isset($index['settings']) && is_array($index['settings'])) { foreach ($index['settings'] as $variable => $value) { diff --git a/src/Command/MeiliSearchImportCommand.php b/src/Command/MeiliSearchImportCommand.php index 8df07fb1..d35ee7e1 100644 --- a/src/Command/MeiliSearchImportCommand.php +++ b/src/Command/MeiliSearchImportCommand.php @@ -116,7 +116,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int if (isset($index['settings']) && is_array($index['settings']) && count($index['settings']) > 0) { - $indexInstance = $this->searchClient->getOrCreateIndex($index['name']); + $indexInstance = $this->searchClient->index($index['name']); foreach ($index['settings'] as $variable => $value) { $method = sprintf('update%s', ucfirst($variable)); if (false === method_exists($indexInstance, $method)) { diff --git a/src/Engine.php b/src/Engine.php index 051dcf6c..cf0c5c43 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -54,8 +54,8 @@ public function index($searchableEntities): array $result = []; foreach ($data as $indexUid => $objects) { $result[$indexUid] = $this->client - ->getOrCreateIndex($indexUid, ['primaryKey' => 'objectID']) - ->addDocuments($objects); + ->index($indexUid) + ->addDocuments($objects, 'objectID'); } return $result; @@ -109,7 +109,7 @@ public function remove($searchableEntities): array */ public function clear(string $indexUid): array { - $index = $this->client->getOrCreateIndex($indexUid); + $index = $this->client->index($indexUid); $return = $index->deleteAllDocuments(); return $index->getTask($return['uid']); diff --git a/tests/Integration/CommandsTest.php b/tests/Integration/CommandsTest.php index 04130cf9..6e7b2423 100644 --- a/tests/Integration/CommandsTest.php +++ b/tests/Integration/CommandsTest.php @@ -32,7 +32,7 @@ protected function setUp(): void parent::setUp(); $this->client = $this->get('search.client'); - $this->index = $this->client->getOrCreateIndex($this->getPrefix().self::$indexName); + $this->index = $this->client->index($this->getPrefix().self::$indexName); $this->application = new Application(self::createKernel()); } @@ -221,7 +221,7 @@ public function testImportDifferentEntitiesIntoSameIndex(): void $this->assertStringContainsString('Done!', $output); /** @var SearchResult $searchResult */ - $searchResult = $this->client->getOrCreateIndex($this->getPrefix().'tags')->search('Test'); + $searchResult = $this->client->index($this->getPrefix().'tags')->search('Test'); $this->assertCount(8, $searchResult->getHits()); $this->assertSame(8, $searchResult->getHitsCount()); } diff --git a/tests/Integration/SearchTest.php b/tests/Integration/SearchTest.php index 2b443092..abff7ea6 100644 --- a/tests/Integration/SearchTest.php +++ b/tests/Integration/SearchTest.php @@ -40,7 +40,7 @@ protected function setUp(): void $this->client = $this->get('search.client'); $this->objectManager = $this->get('doctrine')->getManager(); - $this->index = $this->client->getOrCreateIndex($this->getPrefix().self::$indexName); + $this->index = $this->client->index($this->getPrefix().self::$indexName); $this->application = new Application(self::createKernel()); } diff --git a/tests/Integration/SettingsTest.php b/tests/Integration/SettingsTest.php index 308d9be5..5d0efa97 100644 --- a/tests/Integration/SettingsTest.php +++ b/tests/Integration/SettingsTest.php @@ -43,8 +43,8 @@ public function setUp(): void public function testGetDefaultSettings(): void { $primaryKey = 'ObjectID'; - $settingA = $this->client->getOrCreateIndex($this->getPrefix().'indexA')->getSettings(); - $settingB = $this->client->getOrCreateIndex($this->getPrefix().'indexB', ['primaryKey' => $primaryKey])->getSettings(); + $settingA = $this->client->index($this->getPrefix().'indexA')->getSettings(); + $settingB = $this->client->index($this->getPrefix().'indexB', ['primaryKey' => $primaryKey])->getSettings(); $this->assertEquals(self::DEFAULT_RANKING_RULES, $settingA['rankingRules']); $this->assertNull($settingA['distinctAttribute']); From 2f4db2f10ec74e3b9179f518d83d3b65f53f87bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9mentine=20Urquizar?= Date: Thu, 27 Jan 2022 13:53:56 +0100 Subject: [PATCH 03/12] Fix typo --- src/Engine.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Engine.php b/src/Engine.php index cf0c5c43..1d237aea 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -104,6 +104,7 @@ public function remove($searchableEntities): array /** * Clear the records of an index. * This method enables you to delete an index’s contents (records). + * Will fail if the index does not exists. * * @throws ApiException */ @@ -116,7 +117,7 @@ public function clear(string $indexUid): array } /** - * Delete an index and it's content. + * Delete an index and its content. */ public function delete(string $indexUid): ?array { From a52b79c2cb95dd97c31de82a72b765c5713c6b92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9mentine=20Urquizar?= Date: Mon, 31 Jan 2022 14:11:40 +0100 Subject: [PATCH 04/12] Fix import command --- src/Command/MeiliSearchImportCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Command/MeiliSearchImportCommand.php b/src/Command/MeiliSearchImportCommand.php index d35ee7e1..f5676a55 100644 --- a/src/Command/MeiliSearchImportCommand.php +++ b/src/Command/MeiliSearchImportCommand.php @@ -172,7 +172,7 @@ private function formatIndexingResponse(array $batch, int $responseTimeout): arr throw new TaskException($task['error']); } - $formattedResponse[$indexName] += $task['type']['number']; + $formattedResponse[$indexName] += $task['details']['indexedDocuments']; } } From f3f5a66aae8f3e357b7fa19f6d59cd8a0b6c1f4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9mentine=20Urquizar?= Date: Mon, 31 Jan 2022 16:13:11 +0100 Subject: [PATCH 05/12] Fix clear command --- src/Engine.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Engine.php b/src/Engine.php index 1d237aea..ade9e94c 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -111,9 +111,9 @@ public function remove($searchableEntities): array public function clear(string $indexUid): array { $index = $this->client->index($indexUid); - $return = $index->deleteAllDocuments(); + $task = $index->deleteAllDocuments(); - return $index->getTask($return['uid']); + return $task; } /** From ad38235ada97b030fe3f4f150dc785582a695973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9mentine=20Urquizar?= Date: Mon, 31 Jan 2022 16:38:26 +0100 Subject: [PATCH 06/12] Fix doctrine test --- .../DoctrineEventSubscriberTest.php | 33 ++++++++++++++++--- tests/Integration/SettingsTest.php | 12 +++++-- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/tests/Integration/EventListener/DoctrineEventSubscriberTest.php b/tests/Integration/EventListener/DoctrineEventSubscriberTest.php index 49abeb19..e18fd2c2 100644 --- a/tests/Integration/EventListener/DoctrineEventSubscriberTest.php +++ b/tests/Integration/EventListener/DoctrineEventSubscriberTest.php @@ -9,11 +9,23 @@ use MeiliSearch\Bundle\Test\BaseKernelTestCase; use MeiliSearch\Bundle\Test\Entity\Page; use MeiliSearch\Bundle\Test\Entity\Post; +use MeiliSearch\Client; class DoctrineEventSubscriberTest extends BaseKernelTestCase { + protected Client $client; + + /** + * @throws \Exception + */ + public function setUp(): void + { + parent::setUp(); + + $this->client = $this->get('search.client'); + } /** - * This tests creates two posts in the database, but only one is triggered via an event to MS. + * This tests creates two posts in the database, but only one is triggered via an event to Meilisearch. */ public function testPostPersist(): void { @@ -25,6 +37,9 @@ public function testPostPersist(): void $subscriber = new DoctrineEventSubscriber($this->searchService, []); $subscriber->postPersist($eventArgs); + $firstTask = $this->client->getTasks()['results'][0]; + $this->client->waitForTask($firstTask['uid']); + $result = $this->searchService->search($this->entityManager, Post::class, $post->getTitle()); $this->assertCount(1, $result); @@ -40,7 +55,9 @@ public function testPostPersistWithObjectId(): void $subscriber = new DoctrineEventSubscriber($this->searchService, []); $subscriber->postPersist($eventArgs); - sleep(1); + + $firstTask = $this->client->getTasks()['results'][0]; + $this->client->waitForTask($firstTask['uid']); $result = $this->searchService->search($this->entityManager, Page::class, $page->getTitle()); @@ -49,7 +66,7 @@ public function testPostPersistWithObjectId(): void } /** - * This tests creates two posts in the database, but only one is triggered via an event to MS. + * This tests creates two posts in the database, but only one is triggered via an event to Meilisearch. */ public function testPostUpdate(): void { @@ -61,6 +78,9 @@ public function testPostUpdate(): void $subscriber = new DoctrineEventSubscriber($this->searchService, []); $subscriber->postUpdate($eventArgs); + $firstTask = $this->client->getTasks()['results'][0]; + $this->client->waitForTask($firstTask['uid']); + $result = $this->searchService->search($this->entityManager, Post::class, $post->getTitle()); $this->assertCount(1, $result); @@ -85,7 +105,7 @@ public function testPostUpdateWithObjectId(): void } /** - * This tests creates posts in the database, send it to MS via a trigger. Afterwards Doctrines 'preRemove' event + * This tests creates posts in the database, send it to Meilisearch via a trigger. Afterwards Doctrines 'preRemove' event * is going to remove that entity from MS. */ public function testPreRemove(): void @@ -97,6 +117,9 @@ public function testPreRemove(): void $subscriber = new DoctrineEventSubscriber($this->searchService, []); $subscriber->postPersist($eventArgs); + $firstTask = $this->client->getTasks()['results'][0]; + $this->client->waitForTask($firstTask['uid']); + $result = $this->searchService->search($this->entityManager, Post::class, $post->getTitle()); $this->assertCount(1, $result); @@ -106,7 +129,7 @@ public function testPreRemove(): void /* * As the deletion of a document is an asyncronous transaction, we need to wait some seconds - * till this is executed. This was introduced as with Github actions there was no other option. + * till this is executed. This was introduced as with GitHub actions there was no other option. */ sleep(2); diff --git a/tests/Integration/SettingsTest.php b/tests/Integration/SettingsTest.php index 5d0efa97..f9ec6e40 100644 --- a/tests/Integration/SettingsTest.php +++ b/tests/Integration/SettingsTest.php @@ -43,8 +43,16 @@ public function setUp(): void public function testGetDefaultSettings(): void { $primaryKey = 'ObjectID'; - $settingA = $this->client->index($this->getPrefix().'indexA')->getSettings(); - $settingB = $this->client->index($this->getPrefix().'indexB', ['primaryKey' => $primaryKey])->getSettings(); + $indexAName = $this->getPrefix().'indexA'; + $indexBName = $this->getPrefix().'indexB'; + $this->client->createIndex($indexAName); + $this->client->createIndex($indexBName, ['primaryKey' => $primaryKey]); + + $firstTask = $this->client->getTasks()['results'][0]; + $this->client->waitForTask($firstTask['uid']); + + $settingA = $this->client->index($indexAName)->getSettings(); + $settingB = $this->client->index($indexBName)->getSettings(); $this->assertEquals(self::DEFAULT_RANKING_RULES, $settingA['rankingRules']); $this->assertNull($settingA['distinctAttribute']); From dc0b84c2e1aea51bcc6a6db565ce07d64f9d58ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9mentine=20Urquizar?= Date: Mon, 31 Jan 2022 17:03:43 +0100 Subject: [PATCH 07/12] Fix linter errors --- src/SearchableEntity.php | 2 +- tests/Integration/EventListener/DoctrineEventSubscriberTest.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/SearchableEntity.php b/src/SearchableEntity.php index d34ae26d..5e6dc211 100644 --- a/src/SearchableEntity.php +++ b/src/SearchableEntity.php @@ -61,7 +61,7 @@ public function getIndexUid(): string */ public function getSearchableArray(): array { - /** @var \Doctrine\ORM\Mapping\ClassMetadataInfo&\Doctrine\Persistence\Mapping\ClassMetadata $metadata */ + /** @var \Doctrine\ORM\Mapping\ClassMetadataInfo|\Doctrine\Persistence\Mapping\ClassMetadata $metadata */ $metadata = $this->entityMetadata; $context = [ diff --git a/tests/Integration/EventListener/DoctrineEventSubscriberTest.php b/tests/Integration/EventListener/DoctrineEventSubscriberTest.php index e18fd2c2..8e0f1e98 100644 --- a/tests/Integration/EventListener/DoctrineEventSubscriberTest.php +++ b/tests/Integration/EventListener/DoctrineEventSubscriberTest.php @@ -24,6 +24,7 @@ public function setUp(): void $this->client = $this->get('search.client'); } + /** * This tests creates two posts in the database, but only one is triggered via an event to Meilisearch. */ From d97beed6ec478e6aba097493126409ed6e16e974 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9mentine=20Urquizar?= Date: Mon, 31 Jan 2022 17:21:22 +0100 Subject: [PATCH 08/12] Fix PHP stan error --- src/SearchableEntity.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/SearchableEntity.php b/src/SearchableEntity.php index 5e6dc211..61e29572 100644 --- a/src/SearchableEntity.php +++ b/src/SearchableEntity.php @@ -61,7 +61,6 @@ public function getIndexUid(): string */ public function getSearchableArray(): array { - /** @var \Doctrine\ORM\Mapping\ClassMetadataInfo|\Doctrine\Persistence\Mapping\ClassMetadata $metadata */ $metadata = $this->entityMetadata; $context = [ From 4d7af59d91dec8d554221f7dc35c32641fdcdcea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9mentine=20Urquizar?= Date: Mon, 31 Jan 2022 18:14:55 +0100 Subject: [PATCH 09/12] Refector --- .../DoctrineEventSubscriberTest.php | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/tests/Integration/EventListener/DoctrineEventSubscriberTest.php b/tests/Integration/EventListener/DoctrineEventSubscriberTest.php index 8e0f1e98..8db8f799 100644 --- a/tests/Integration/EventListener/DoctrineEventSubscriberTest.php +++ b/tests/Integration/EventListener/DoctrineEventSubscriberTest.php @@ -38,8 +38,7 @@ public function testPostPersist(): void $subscriber = new DoctrineEventSubscriber($this->searchService, []); $subscriber->postPersist($eventArgs); - $firstTask = $this->client->getTasks()['results'][0]; - $this->client->waitForTask($firstTask['uid']); + $this->waitForAllTasks(); $result = $this->searchService->search($this->entityManager, Post::class, $post->getTitle()); @@ -57,8 +56,7 @@ public function testPostPersistWithObjectId(): void $subscriber = new DoctrineEventSubscriber($this->searchService, []); $subscriber->postPersist($eventArgs); - $firstTask = $this->client->getTasks()['results'][0]; - $this->client->waitForTask($firstTask['uid']); + $this->waitForAllTasks(); $result = $this->searchService->search($this->entityManager, Page::class, $page->getTitle()); @@ -79,8 +77,7 @@ public function testPostUpdate(): void $subscriber = new DoctrineEventSubscriber($this->searchService, []); $subscriber->postUpdate($eventArgs); - $firstTask = $this->client->getTasks()['results'][0]; - $this->client->waitForTask($firstTask['uid']); + $this->waitForAllTasks(); $result = $this->searchService->search($this->entityManager, Post::class, $post->getTitle()); @@ -97,7 +94,8 @@ public function testPostUpdateWithObjectId(): void $subscriber = new DoctrineEventSubscriber($this->searchService, []); $subscriber->postUpdate($eventArgs); - sleep(1); + + $this->waitForAllTasks(); $result = $this->searchService->search($this->entityManager, Page::class, $page->getTitle()); @@ -118,8 +116,7 @@ public function testPreRemove(): void $subscriber = new DoctrineEventSubscriber($this->searchService, []); $subscriber->postPersist($eventArgs); - $firstTask = $this->client->getTasks()['results'][0]; - $this->client->waitForTask($firstTask['uid']); + $this->waitForAllTasks(); $result = $this->searchService->search($this->entityManager, Post::class, $post->getTitle()); @@ -128,11 +125,7 @@ public function testPreRemove(): void $subscriber->preRemove($eventArgs); - /* - * As the deletion of a document is an asyncronous transaction, we need to wait some seconds - * till this is executed. This was introduced as with GitHub actions there was no other option. - */ - sleep(2); + $this->waitForAllTasks(); $result = $this->searchService->search($this->entityManager, Post::class, $post->getTitle()); @@ -147,7 +140,8 @@ public function testPreRemoveWithObjectId(): void $subscriber = new DoctrineEventSubscriber($this->searchService, []); $subscriber->postPersist($eventArgs); - sleep(1); + + $this->waitForAllTasks(); $result = $this->searchService->search($this->entityManager, Page::class, $page->getTitle()); @@ -155,10 +149,20 @@ public function testPreRemoveWithObjectId(): void $this->assertSame((string) $page->getId(), (string) $result[0]->getId()); $subscriber->preRemove($eventArgs); - sleep(1); + + $this->waitForAllTasks(); $result = $this->searchService->search($this->entityManager, Page::class, $page->getTitle()); $this->assertCount(0, $result); } + + /** + * Waits for all the tasks to be finished by checking the topest one (so the newest one). + */ + private function waitForAllTasks(): void + { + $firstTask = $this->client->getTasks()['results'][0]; + $this->client->waitForTask($firstTask['uid']); + } } From 251bc3b63e51855f4a4e085b044e400daddcbc71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9mentine=20Urquizar?= Date: Mon, 31 Jan 2022 18:39:46 +0100 Subject: [PATCH 10/12] Ignore PHPstan error --- src/SearchableEntity.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/SearchableEntity.php b/src/SearchableEntity.php index 61e29572..0b541bfe 100644 --- a/src/SearchableEntity.php +++ b/src/SearchableEntity.php @@ -61,10 +61,11 @@ public function getIndexUid(): string */ public function getSearchableArray(): array { - $metadata = $this->entityMetadata; + // $metadata = $this->entityMetadata; $context = [ - 'fieldsMapping' => $metadata->fieldMappings, + /** @phpstan-ignore-next-line */ + 'fieldsMapping' => $this->entityMetadata->fieldMappings, ]; if ($this->useSerializerGroups) { From 23a3fad5dd10f4266caecc7a6f04053219f71b9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9mentine=20Urquizar?= Date: Mon, 31 Jan 2022 18:41:09 +0100 Subject: [PATCH 11/12] Remove useless comment --- src/SearchableEntity.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/SearchableEntity.php b/src/SearchableEntity.php index 0b541bfe..8148108d 100644 --- a/src/SearchableEntity.php +++ b/src/SearchableEntity.php @@ -61,8 +61,6 @@ public function getIndexUid(): string */ public function getSearchableArray(): array { - // $metadata = $this->entityMetadata; - $context = [ /** @phpstan-ignore-next-line */ 'fieldsMapping' => $this->entityMetadata->fieldMappings, From cda20060c836ed9a7dab7c6bb466a926c246f9c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9mentine=20Urquizar?= Date: Mon, 31 Jan 2022 18:42:42 +0100 Subject: [PATCH 12/12] Fix linter errors --- src/SearchableEntity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SearchableEntity.php b/src/SearchableEntity.php index 8148108d..65114f82 100644 --- a/src/SearchableEntity.php +++ b/src/SearchableEntity.php @@ -62,7 +62,7 @@ public function getIndexUid(): string public function getSearchableArray(): array { $context = [ - /** @phpstan-ignore-next-line */ + /* @phpstan-ignore-next-line */ 'fieldsMapping' => $this->entityMetadata->fieldMappings, ];