Skip to content

Commit

Permalink
Added test for Finder::get() when an id is duplicated in a read-only …
Browse files Browse the repository at this point in the history
…index

Suppress debug messages in travis test runs
  • Loading branch information
pmishev committed Jul 29, 2019
1 parent f81e39f commit d274466
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ install:
- composer install --dev --no-interaction

script:
- vendor/bin/simple-phpunit --coverage-clover=Tests/App/build/clover.xml
- vendor/bin/simple-phpunit --coverage-clover=Tests/App/build/clover.xml 2>/dev/null
- vendor/bin/phpcs -np --standard=PSR2 --ignore=vendor/,Tests/App/,var/ ./

after_script:
Expand Down
2 changes: 1 addition & 1 deletion Finder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function get($documentClass, $id, $resultType = self::RESULTS_OBJECT)
];
$results = $this->getConnection([$documentClass])->getClient()->search($search);

// The document id should not be duplicated across the indices pointed by the read alias,
// The document id must not be duplicated across the indices pointed by the read alias,
// but in case it is, just return the first one we get
$rawDoc = $results['hits']['hits'][0] ?? null;

Expand Down
2 changes: 1 addition & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ services:
class: Monolog\Logger
arguments: ['sfes_trace']
calls:
- [pushHandler, ['@sfes.logger.collection_handler']]
- [pushHandler, ['@Sineflow\ElasticsearchBundle\Profiler\Handler\CollectionHandler']]

sfes.logger.log_handler:
class: Monolog\Handler\RotatingFileHandler
Expand Down
66 changes: 64 additions & 2 deletions Tests/Functional/Finder/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Sineflow\ElasticsearchBundle\Result\DocumentIterator;
use Sineflow\ElasticsearchBundle\Tests\AbstractElasticsearchTestCase;
use Sineflow\ElasticsearchBundle\Tests\App\fixture\Acme\BarBundle\Document\Product;
use Sineflow\ElasticsearchBundle\Tests\App\fixture\Acme\FooBundle\Document\Customer;

/**
* Class FinderTest
Expand Down Expand Up @@ -48,6 +49,24 @@ protected function getDataArray()
];
}

private $readOnlyIndexName = 'sineflow-customer-read-only-index-123';

/**
* {@inheritdoc}
*/
protected function tearDown()
{
parent::tearDown();

try {
// Delete the read-only index we manually created
$im = $this->getIndexManager('customer', false);
$im->getConnection()->getClient()->indices()->delete(['index' => $this->readOnlyIndexName]);
} catch (\Exception $e) {
// Do nothing.
}
}

/**
* {@inheritdoc}
*/
Expand All @@ -56,11 +75,44 @@ protected function setUp()
parent::setUp();

// Create and populate indices just once for all tests in this class
$this->getIndexManager('customer', !$this->hasCreatedIndexManager('customer'));
$this->getIndexManager('bar', !$this->hasCreatedIndexManager('bar'));
$im = $this->getIndexManager('customer', !$this->hasCreatedIndexManager('customer'));

// Create a second read-only index for 'customer' index manager, which uses aliases
$settings = $im->getIndexMapping();
$settings['index'] = $this->readOnlyIndexName;
$im->getConnection()->getClient()->indices()->create($settings);
$setAliasParams = [
'body' => [
'actions' => [
['add' => ['index' => $this->readOnlyIndexName, 'alias' => $im->getReadAlias()]],
],
],
];
$im->getConnection()->getClient()->indices()->updateAliases($setAliasParams);

// Enter data in the read-only index
$im->getConnection()->getClient()->index([
'index' => $this->readOnlyIndexName,
'type' => 'customer',
'id' => 111,
'refresh' => true,
'body' => [
'name' => 'Another Jane',
],
]);
$im->getConnection()->getClient()->index([
'index' => $this->readOnlyIndexName,
'type' => 'customer',
'id' => 123,
'refresh' => true,
'body' => [
'name' => 'Jason Bourne',
],
]);
}

public function testGet()
public function testGetById()
{
$finder = $this->getContainer()->get('sfes.finder');

Expand All @@ -84,6 +136,16 @@ public function testGet()
$this->assertInstanceOf(Product::class, $docAsObjectKNP);
}

public function testGetByIdWhenHavingAnotherReadOnlyIndex()
{
$finder = $this->getContainer()->get('sfes.finder');

$doc = $finder->get(Customer::class, 111);

// Make sure a document is returned for the duplicated id, whichever it is
$this->assertInstanceOf(Customer::class, $doc);
}

public function testFindInMultipleTypesAndIndices()
{
$finder = $this->getContainer()->get('sfes.finder');
Expand Down

0 comments on commit d274466

Please sign in to comment.