Skip to content

Commit

Permalink
tests(core): improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Guikingone committed Nov 1, 2020
1 parent c859005 commit 1bfbd07
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 30 deletions.
2 changes: 0 additions & 2 deletions src/Dump/DumpOrchestrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public function create(): array
} catch (Throwable $throwable) {
$this->logger->critical(sprintf('An error occurred when trying to create a new dump'), [
'error' => $throwable->getMessage(),
'trace' => $throwable->getTraceAsString(),
]);

throw $throwable;
Expand All @@ -74,7 +73,6 @@ public function getStatus(string $uid): array
} catch (Throwable $throwable) {
$this->logger->critical(sprintf('An error occurred when trying to fetch the dump status'), [
'error' => $throwable->getMessage(),
'trace' => $throwable->getTraceAsString(),
]);

throw $throwable;
Expand Down
43 changes: 23 additions & 20 deletions src/Index/IndexOrchestrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,6 @@ public function addIndex(string $uid, ?string $primaryKey = null, array $configu
], $index));
}

public function update(string $uid, array $configuration = []): void
{
try {
$index = $this->getIndex($uid);

if (null === $index->getPrimaryKey()) {
$index->update(['primaryKey' => $configuration['primaryKey']]);
}

$this->handleConfiguration($index, $configuration);
} catch (Throwable $throwable) {
$this->logger->error(sprintf('The index cannot be created, error: "%s"', $throwable->getMessage()), [
'trace' => $throwable->getTrace(),
]);

throw new RuntimeException($throwable->getMessage(), 0, $throwable);
}
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -131,6 +112,28 @@ public function getIndex(string $uid): Indexes
}
}

/**
* {@inheritdoc}
*/
public function update(string $uid, array $configuration = []): void
{
try {
$index = $this->getIndex($uid);

if (null === $index->getPrimaryKey()) {
$index->update(['primaryKey' => $configuration['primaryKey']]);
}

$this->handleConfiguration($index, $configuration);
} catch (Throwable $throwable) {
$this->logger->error(sprintf('The index cannot be created, error: "%s"', $throwable->getMessage()), [
'trace' => $throwable->getTrace(),
]);

throw new RuntimeException($throwable->getMessage(), 0, $throwable);
}
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -183,7 +186,7 @@ private function handleConfiguration(Indexes $index, array $configuration): void
}

if (array_key_exists(self::RANKING_RULES_ATTRIBUTES, $configuration)) {
$index->updateStopWords($configuration[self::RANKING_RULES_ATTRIBUTES]);
$index->updateRankingRules($configuration[self::RANKING_RULES_ATTRIBUTES]);
}

if (array_key_exists(self::SEARCHABLE_ATTRIBUTES, $configuration) && [] !== $configuration[self::SEARCHABLE_ATTRIBUTES]) {
Expand Down
30 changes: 28 additions & 2 deletions tests/Document/DocumentEntryPointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
use MeiliSearchBundle\Bridge\RamseyUuid\Serializer\UuidDenormalizer;
use MeiliSearchBundle\Bridge\RamseyUuid\Serializer\UuidNormalizer;
use MeiliSearchBundle\Document\DocumentEntryPoint;
use MeiliSearchBundle\Event\Document\PostDocumentDeletionEvent;
use MeiliSearchBundle\Event\Document\PostDocumentRetrievedEvent;
use MeiliSearchBundle\Event\Document\PreDocumentDeletionEvent;
use MeiliSearchBundle\Event\Document\PreDocumentRetrievedEvent;
use MeiliSearchBundle\Exception\RuntimeException;
use MeiliSearchBundle\Result\ResultBuilder;
use MeiliSearchBundle\Result\ResultBuilderInterface;
Expand Down Expand Up @@ -248,7 +252,18 @@ public function testDocumentCanBeReturned(): void
$client = $this->createMock(Client::class);
$client->expects(self::once())->method('getIndex')->willReturn($index);

$orchestrator = new DocumentEntryPoint($client, $resultBuilder);
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects(self::exactly(2))->method('dispatch')
->withConsecutive(
[new PreDocumentRetrievedEvent($index, 'test')],
[new PostDocumentRetrievedEvent($index, [
'id' => 'foo',
'value' => 'foo',
])]
)
;

$orchestrator = new DocumentEntryPoint($client, $resultBuilder, $eventDispatcher);
$document = $orchestrator->getDocument('test', 'test');

static::assertArrayHasKey('id', $document);
Expand Down Expand Up @@ -516,7 +531,18 @@ public function testDocumentCanBeRemoved(): void
$client = $this->createMock(Client::class);
$client->expects(self::once())->method('getIndex')->willReturn($index);

$orchestrator = new DocumentEntryPoint($client, $resultBuilder);
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects(self::exactly(2))->method('dispatch')
->withConsecutive(
[new PreDocumentDeletionEvent($index, [
'id' => 1,
'value' => 'foo',
])],
[new PostDocumentDeletionEvent(1)]
)
;

$orchestrator = new DocumentEntryPoint($client, $resultBuilder, $eventDispatcher);
$orchestrator->removeDocument('foo', 1);
}

Expand Down
14 changes: 10 additions & 4 deletions tests/Dump/DumpOrchestratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
*/
final class DumpOrchestratorTest extends TestCase
{
public function testDumpCannotBeCreated(): void
public function testDumpCannotBeCreatedWithException(): void
{
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects(self::never())->method('dispatch');

$logger = $this->createMock(LoggerInterface::class);
$logger->expects(self::once())->method('critical');
$logger->expects(self::once())->method('critical')->with(self::equalTo('An error occurred when trying to create a new dump'), [
'error' => 'An error occurred',
]);

$client = $this->createMock(Client::class);
$client->expects(self::once())->method('createDump')
Expand All @@ -34,6 +36,7 @@ public function testDumpCannotBeCreated(): void

static::expectException(RuntimeException::class);
static::expectExceptionMessage('An error occurred');
static::expectExceptionCode(0);
$orchestrator->create();
}

Expand Down Expand Up @@ -87,13 +90,15 @@ public function testDumpCanBeCreated(): void
], $dump);
}

public function testDumpStatusCannotBeRetrieved(): void
public function testDumpStatusCannotBeRetrievedWithException(): void
{
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects(self::never())->method('dispatch');

$logger = $this->createMock(LoggerInterface::class);
$logger->expects(self::once())->method('critical');
$logger->expects(self::once())->method('critical')->with(self::equalTo('An error occurred when trying to fetch the dump status'), [
'error' => 'An error occurred',
]);

$client = $this->createMock(Client::class);
$client->expects(self::once())->method('getDumpStatus')->with(self::equalTo('foo'))
Expand All @@ -104,6 +109,7 @@ public function testDumpStatusCannotBeRetrieved(): void

static::expectException(RuntimeException::class);
static::expectExceptionMessage('An error occurred');
static::expectExceptionCode(0);
$orchestrator->getStatus('foo');
}

Expand Down
168 changes: 166 additions & 2 deletions tests/Index/IndexOrchestratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Tests\MeiliSearchBundle\Index;

use Generator;
use MeiliSearch\Client;
use MeiliSearch\Endpoints\Indexes;
use MeiliSearchBundle\Event\Index\IndexCreatedEvent;
use MeiliSearchBundle\Index\IndexOrchestrator;
use MeiliSearchBundle\Exception\RuntimeException as MeiliSeachBundleRuntimeException;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -38,6 +40,33 @@ public function testIndexCannotBeAddedWithException(): void
}

public function testIndexCanBeAdded(): void
{
$logger = $this->createMock(LoggerInterface::class);
$logger->expects(self::never())->method('error');

$index = $this->createMock(Indexes::class);
$index->expects(self::never())->method('getUid')->willReturn('test');
$index->expects(self::never())->method('getPrimaryKey')->willReturn('test');

$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects(self::once())->method('dispatch')->with(new IndexCreatedEvent([
'uid' => 'test',
'primaryKey' => 'test',
], $index));

$client = $this->createMock(Client::class);
$client->expects(self::once())->method('createIndex')->with(self::equalTo('test'), [
'primaryKey' => 'test',
])->willReturn($index);

$orchestrator = new IndexOrchestrator($client, $eventDispatcher, $logger);
$orchestrator->addIndex('test', 'test');
}

/**
* @dataProvider provideConfiguration
*/
public function testIndexCanBeAddedWithConfiguration(array $configuration, string $method): void
{
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects(self::once())->method('dispatch');
Expand All @@ -48,14 +77,118 @@ public function testIndexCanBeAdded(): void
$index = $this->createMock(Indexes::class);
$index->expects(self::never())->method('getUid')->willReturn('test');
$index->expects(self::never())->method('getPrimaryKey')->willReturn('test');
$index->expects(self::once())->method($method);

$client = $this->createMock(Client::class);
$client->expects(self::once())->method('createIndex')->willReturn($index);
$client->expects(self::once())->method('createIndex')->with(self::equalTo('test'), [
'primaryKey' => 'test',
])->willReturn($index);

$orchestrator = new IndexOrchestrator($client, $eventDispatcher, $logger);
$orchestrator->addIndex('test', 'test', $configuration);
}

public function testIndexCanBeAddedWithoutEventDispatcher(): void
{
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects(self::never())->method('dispatch');

$logger = $this->createMock(LoggerInterface::class);
$logger->expects(self::never())->method('error');

$index = $this->createMock(Indexes::class);
$index->expects(self::never())->method('getUid')->willReturn('test');
$index->expects(self::never())->method('getPrimaryKey')->willReturn('test');

$client = $this->createMock(Client::class);
$client->expects(self::once())->method('createIndex')->willReturn($index);

$orchestrator = new IndexOrchestrator($client, null, $logger);
$orchestrator->addIndex('test', 'test');
}

public function testIndexCannotBeUpdatedWithException(): void
{
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects(self::never())->method('dispatch');

$logger = $this->createMock(LoggerInterface::class);
$logger->expects(self::exactly(2))->method('error');

$client = $this->createMock(Client::class);
$client->expects(self::once())->method('getIndex')->willThrowException(new RuntimeException('An error occurred'));

$orchestrator = new IndexOrchestrator($client, $eventDispatcher, $logger);

static::expectException(RuntimeException::class);
static::expectExceptionCode(0);
static::expectExceptionMessage('An error occurred');
$orchestrator->update('foo', ['synonyms' => ['xmen' => ['wolverine']]]);
}

public function testIndexCanBeUpdated(): void
{
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects(self::once())->method('dispatch');

$logger = $this->createMock(LoggerInterface::class);
$logger->expects(self::once())->method('info');
$logger->expects(self::never())->method('error');

$index = $this->createMock(Indexes::class);
$index->expects(self::once())->method('updateSynonyms');
$index->expects(self::once())->method('getPrimaryKey')->willReturn('id');

$client = $this->createMock(Client::class);
$client->expects(self::once())->method('getIndex')->willReturn($index);

$orchestrator = new IndexOrchestrator($client, $eventDispatcher, $logger);
$orchestrator->update('test', ['synonyms' => ['xmen' => ['wolverine']]]);
}

public function testIndexCanBeUpdatedWithoutPrimaryKey(): void
{
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects(self::once())->method('dispatch');

$logger = $this->createMock(LoggerInterface::class);
$logger->expects(self::once())->method('info');
$logger->expects(self::never())->method('error');

$index = $this->createMock(Indexes::class);
$index->expects(self::once())->method('getPrimaryKey')->willReturn(null);
$index->expects(self::once())->method('update')->with(['primaryKey' => 'id']);

$client = $this->createMock(Client::class);
$client->expects(self::once())->method('getIndex')->willReturn($index);

$orchestrator = new IndexOrchestrator($client, $eventDispatcher, $logger);
$orchestrator->update('test', ['primaryKey' => 'id']);
}

/**
* @dataProvider provideConfiguration
*/
public function testIndexCanBeUpdatedWithConfiguration(array $configuration, string $method): void
{
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher->expects(self::once())->method('dispatch');

$logger = $this->createMock(LoggerInterface::class);
$logger->expects(self::once())->method('info');
$logger->expects(self::never())->method('error');

$index = $this->createMock(Indexes::class);
$index->expects(self::once())->method($method);
$index->expects(self::once())->method('getPrimaryKey')->willReturn('id');

$client = $this->createMock(Client::class);
$client->expects(self::once())->method('getIndex')->willReturn($index);

$orchestrator = new IndexOrchestrator($client, $eventDispatcher, $logger);
$orchestrator->update('test', $configuration);
}

public function testIndexesCannotBeRetrievedWithException(): void
{
$logger = $this->createMock(LoggerInterface::class);
Expand Down Expand Up @@ -151,7 +284,9 @@ public function testIndexCanBeDeleted(): void
{
$logger = $this->createMock(LoggerInterface::class);
$logger->expects(self::never())->method('error');
$logger->expects(self::once())->method('info');
$logger->expects(self::once())->method('info')->with(self::equalTo('An index has been deleted'), [
'uid' => 'test',
]);

$client = $this->createMock(Client::class);
$client->expects(self::once())->method('deleteIndex');
Expand All @@ -173,6 +308,7 @@ public function testIndexesCannotBeDeletedWithException(): void

static::expectException(RuntimeException::class);
static::expectExceptionMessage('An error occurred');
static::expectExceptionCode(0);
$orchestrator->removeIndexes();
}

Expand All @@ -188,4 +324,32 @@ public function testIndexesCanBeDeleted(): void
$orchestrator = new IndexOrchestrator($client, null, $logger);
$orchestrator->removeIndexes();
}

public function provideConfiguration(): Generator
{
yield 'Displayed attributes' => [
['displayedAttributes' => ['id', 'title']],
'updateDisplayedAttributes',
];
yield 'Distinct attribute' => [
['distinctAttribute' => 'movie_id'],
'updateDistinctAttribute',
];
yield 'Attributes for faceting' => [
['facetedAttributes' => ['id', 'title']],
'updateAttributesForFaceting',
];
yield 'Ranking rules' => [
['rankingRulesAttributes' => ['typo', 'words', 'proximity', 'attribute', 'wordsPosition', 'exactness', 'asc(release_date)', 'desc(rank)']],
'updateRankingRules',
];
yield 'Stop words' => [
['stopWords' => ['the', 'of', 'to']],
'updateStopWords',
];
yield 'Searchable attributes' => [
['searchableAttributes' => ['title', 'description', 'uid']],
'updateSearchableAttributes',
];
}
}

0 comments on commit 1bfbd07

Please sign in to comment.