diff --git a/src/Exception/SearchHitsNotFoundException.php b/src/Exception/SearchHitsNotFoundException.php new file mode 100644 index 00000000..1c734c99 --- /dev/null +++ b/src/Exception/SearchHitsNotFoundException.php @@ -0,0 +1,14 @@ +engine->search($query, $this->searchableAs($className), $requestOptions); $results = []; - foreach ($ids as $objectID) { + // Check if the engine returns results in "hits" key + if (!isset($ids['hits'])) { + throw new SearchHitsNotFoundException('There is no "hits" key in the search results.'); + } + + foreach ($ids['hits'] as $objectID) { if (in_array($className, $this->aggregators, true)) { $entityClass = $className::getEntityClassFromObjectID($objectID); $id = $className::getEntityIdFromObjectID($objectID); diff --git a/tests/Entity/Post.php b/tests/Entity/Post.php index 079c64a7..3a4fdd41 100644 --- a/tests/Entity/Post.php +++ b/tests/Entity/Post.php @@ -27,6 +27,8 @@ class Post /** * @var string * @ORM\Column(type="string") + * @Groups({"searchable"}) + * ^ Note that Groups work on private properties */ private $title; diff --git a/tests/TestCase/SearchTest.php b/tests/TestCase/SearchTest.php new file mode 100644 index 00000000..9be324eb --- /dev/null +++ b/tests/TestCase/SearchTest.php @@ -0,0 +1,144 @@ +searchService = $this->get('search.service'); + $this->client = $this->get('search.client'); + $this->objectManager = $this->get('doctrine')->getManager(); + $this->connection = $this->get('doctrine')->getConnection(); + $this->platform = $this->connection->getDatabasePlatform(); + $this->indexName = 'posts'; + $this->index = $this->client->getOrCreateIndex($this->getPrefix() . $this->indexName); + + $this->application = new Application(self::$kernel); + $this->refreshDb($this->application); + } + + public function cleanUp() + { + try { + $this->searchService->delete(Post::class); + $this->searchService->delete(Comment::class); + $this->searchService->delete(ContentAggregator::class); + } catch (HTTPRequestException $e) { + $this->assertEquals('Index sf_phpunit__comments not found', $e->getMessage()); + } + } + + public function testSearchImportAggregator() + { + $nbEntityIndexed = 0; + // START - Insertion Part took from CommandsTest class + $now = new \DateTime(); + $this->connection->insert( + $this->indexName, + [ + 'title' => 'Test', + 'content' => 'Test content', + 'published_at' => $now->format('Y-m-d H:i:s'), + ] + ); + $nbEntityIndexed++; + + $this->connection->insert( + $this->indexName, + [ + 'title' => 'Test2', + 'content' => 'Test content2', + 'published_at' => $now->format('Y-m-d H:i:s'), + ] + ); + $nbEntityIndexed++; + + $this->connection->insert( + $this->indexName, + [ + 'title' => 'Test3', + 'content' => 'Test content3', + 'published_at' => $now->format('Y-m-d H:i:s'), + ] + ); + $nbEntityIndexed++; + + + $command = $this->application->find('meili:import'); + $commandTester = new CommandTester($command); + $commandTester->execute( + [ + 'command' => $command->getName(), + '--indices' => 'contents', + ] + ); + + // Checks output + $output = $commandTester->getDisplay(); + $this->assertStringContainsString('Done!', $output); + // END - Insertion Part took from CommandsTest class + + // Test searchService + $searchTerm = 'test'; + $results = $this->searchService->search($this->objectManager, Post::class, $searchTerm); + $this->assertCount($nbEntityIndexed , $results); + + // clearup table + $this->connection->executeUpdate($this->platform->getTruncateTableSQL($this->indexName, true)); + $this->cleanUp(); + } +}