Skip to content

Fix entity mapping not working correctly #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Exception/SearchHitsNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace MeiliSearch\Bundle\Exception;

use LogicException;

/**
* Class SearchHitsNotFoundException.
*
* @package MeiliSearch\Bundle\Exception
*/
final class SearchHitsNotFoundException extends LogicException
{
}
8 changes: 7 additions & 1 deletion src/Services/MeiliSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\Persistence\ObjectManager;
use MeiliSearch\Bundle\Engine;
use MeiliSearch\Bundle\Entity\Aggregator;
use MeiliSearch\Bundle\Exception\SearchHitsNotFoundException;
use MeiliSearch\Bundle\SearchableEntity;
use MeiliSearch\Bundle\SearchService;
use Symfony\Component\Config\Definition\Exception\Exception;
Expand Down Expand Up @@ -215,7 +216,12 @@ public function search(
$ids = $this->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);
Expand Down
2 changes: 2 additions & 0 deletions tests/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Post
/**
* @var string
* @ORM\Column(type="string")
* @Groups({"searchable"})
* ^ Note that Groups work on private properties
*/
private $title;

Expand Down
144 changes: 144 additions & 0 deletions tests/TestCase/SearchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace MeiliSearch\Bundle\Test\TestCase;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\Persistence\ObjectManager;
use Exception;
use MeiliSearch\Bundle\Services\MeiliSearchService;
use MeiliSearch\Bundle\Test\BaseTest;
use MeiliSearch\Bundle\Test\Entity\Comment;
use MeiliSearch\Bundle\Test\Entity\ContentAggregator;
use MeiliSearch\Bundle\Test\Entity\Post;
use MeiliSearch\Client;
use MeiliSearch\Endpoints\Indexes;
use MeiliSearch\Exceptions\HTTPRequestException;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

/**
* Class SearchTest
*
* @package MeiliSearch\Bundle\Test\TestCase
*/
class SearchTest extends BaseTest
{

/** @var MeiliSearchService $searchService */
protected $searchService;

/** @var Client $client */
protected $client;

/** @var ObjectManager $objectManager */
protected $objectManager;

/** @var Connection $connection */
protected $connection;

/** @var Application $application */
protected $application;

/** @var string $indexName */
protected $indexName;

/** @var AbstractPlatform|null $platform */
protected $platform;

/** @var Indexes $index */
protected $index;

/**
* @inheritDoc
* @throws DBALException
* @throws HTTPRequestException
* @throws Exception
*/
public function setUp(): void
{
parent::setUp();
$this->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();
}
}