diff --git a/README.md b/README.md index 935ed1a..e443079 100644 --- a/README.md +++ b/README.md @@ -320,8 +320,12 @@ This library supports specifying a prefix for index names, similarly to how many order to allow multiple applications to share the same cache. This means you can use a single Elasticsearch cluster for multiple projects (or for example a shared one for the "dev" and "staging" environment). -The prefix used is specified by the `ELASTICSEARCH_INDEX_PREFIX` environment variable. If you have an index named -`content` and you specify `foo` as a prefix, the index will be named `foo_content`. +The prefix used is specified by the configuration file (`config/elasticsearch.php`). The default behavior is to read the + prefix from the `ELASTICSEARCH_INDEX_PREFIX` environment variable. + + If you have an index named +`content` and you specify `foo` as a prefix, the index will be named `foo_content`. If you need custom logic you +can override `getPrefixedIndexName()` in `ElasticsearchService`. Prefixing is supported for index migrations too, in which case the both the indices and the aliases created are prefixed. diff --git a/config/elasticsearch.php b/config/elasticsearch.php index 71b971c..2f8c62f 100644 --- a/config/elasticsearch.php +++ b/config/elasticsearch.php @@ -10,6 +10,10 @@ | */ - 'hosts' => ['localhost:9200'], + 'hosts' => ['localhost:9200'], + /* + * The prefix to use for index names + */ + 'index_prefix' => env('ELASTICSEARCH_INDEX_PREFIX'), ]; diff --git a/src/Console/ApplyMigrationCommand.php b/src/Console/ApplyMigrationCommand.php index e369721..3a2698f 100644 --- a/src/Console/ApplyMigrationCommand.php +++ b/src/Console/ApplyMigrationCommand.php @@ -4,7 +4,6 @@ use League\Pipeline\Pipeline; use Nord\Lumen\Elasticsearch\Exceptions\IndexExistsException; -use Nord\Lumen\Elasticsearch\IndexNamePrefixer; use Nord\Lumen\Elasticsearch\Pipelines\Payloads\ApplyMigrationPayload; use Nord\Lumen\Elasticsearch\Pipelines\Stages\CheckIndexExistsStage; use Nord\Lumen\Elasticsearch\Pipelines\Stages\CreateIndexStage; @@ -59,8 +58,9 @@ public function handle() try { $pipeline->process($payload); - $this->output->writeln(sprintf('Migrated %s to %s', $payload->getPrefixedIndexName(), - $payload->getPrefixedTargetVersionName())); + $this->output->writeln(sprintf('Migrated %s to %s', + $this->elasticsearchService->getPrefixedIndexName($payload->getIndexName()), + $this->elasticsearchService->getPrefixedIndexName($payload->getTargetVersionName()))); } catch (IndexExistsException $e) { $this->output->writeln('No migration required'); } diff --git a/src/Console/CreateCommand.php b/src/Console/CreateCommand.php index 35b614f..91044c6 100644 --- a/src/Console/CreateCommand.php +++ b/src/Console/CreateCommand.php @@ -1,7 +1,5 @@ elasticsearchService->getPrefixedIndexParameters($params); $this->info(sprintf("Creating index '%s' ...", $params['index'])); diff --git a/src/Console/DeleteCommand.php b/src/Console/DeleteCommand.php index 9937c0b..2ea22b3 100644 --- a/src/Console/DeleteCommand.php +++ b/src/Console/DeleteCommand.php @@ -1,7 +1,5 @@ argument('index')); + $index = $this->elasticsearchService->getPrefixedIndexName((string)$this->argument('index')); $this->info(sprintf("Deleting index '%s' ...", $index)); diff --git a/src/Console/IndexCommand.php b/src/Console/IndexCommand.php index 2907ad5..003f98a 100644 --- a/src/Console/IndexCommand.php +++ b/src/Console/IndexCommand.php @@ -3,7 +3,6 @@ use Nord\Lumen\Elasticsearch\Documents\Bulk\BulkAction; use Nord\Lumen\Elasticsearch\Documents\Bulk\BulkQuery; use Nord\Lumen\Elasticsearch\Documents\Bulk\BulkResponseAggregator; -use Nord\Lumen\Elasticsearch\IndexNamePrefixer; abstract class IndexCommand extends AbstractCommand { @@ -62,8 +61,8 @@ public function handle() */ protected function indexData(string $indexName): void { - $indexName = IndexNamePrefixer::getPrefixedIndexName($indexName); - + $indexName = $this->elasticsearchService->getPrefixedIndexName($indexName); + $this->info(sprintf('Indexing data of type "%s" into "%s"', $this->getType(), $indexName)); $data = $this->getData(); diff --git a/src/Console/UpdateIndexSettingsCommand.php b/src/Console/UpdateIndexSettingsCommand.php index e0e35a5..9b4722e 100644 --- a/src/Console/UpdateIndexSettingsCommand.php +++ b/src/Console/UpdateIndexSettingsCommand.php @@ -2,15 +2,11 @@ namespace Nord\Lumen\Elasticsearch\Console; -use Illuminate\Console\Command; -use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract; -use Nord\Lumen\Elasticsearch\IndexNamePrefixer; - /** * Class SetIndexSettingsCommand * @package namespace Nord\Lumen\Elasticsearch\Console */ -class UpdateIndexSettingsCommand extends Command +class UpdateIndexSettingsCommand extends AbstractCommand { /** @@ -26,26 +22,9 @@ class UpdateIndexSettingsCommand extends Command */ protected $description = 'Updates specified dynamic index settings for the specified index'; - /** - * @var ElasticsearchServiceContract - */ - private $elasticsearchService; - - /** - * SetIndexSettingsCommand constructor. - * - * @param ElasticsearchServiceContract $elasticsearchService - */ - public function __construct(ElasticsearchServiceContract $elasticsearchService) - { - parent::__construct(); - - $this->elasticsearchService = $elasticsearchService; - } - public function handle(): void { - $index = IndexNamePrefixer::getPrefixedIndexName($this->input->getArgument('index')); + $index = $this->elasticsearchService->getPrefixedIndexName($this->input->getArgument('index')); $numReplicas = $this->input->getOption('numReplicas'); $refreshInterval = $this->input->getOption('refreshInterval'); diff --git a/src/Contracts/ElasticsearchServiceContract.php b/src/Contracts/ElasticsearchServiceContract.php index 84ef383..2cd1974 100644 --- a/src/Contracts/ElasticsearchServiceContract.php +++ b/src/Contracts/ElasticsearchServiceContract.php @@ -15,7 +15,6 @@ interface ElasticsearchServiceContract */ public function search(array $params = []); - /** * @param array $params * @@ -23,7 +22,6 @@ public function search(array $params = []); */ public function index(array $params = []); - /** * @param array $params * @@ -31,7 +29,6 @@ public function index(array $params = []); */ public function reindex(array $params = []); - /** * @param array $params * @@ -39,7 +36,6 @@ public function reindex(array $params = []); */ public function updateByQuery(array $params = []); - /** * @param array $params * @@ -47,7 +43,6 @@ public function updateByQuery(array $params = []); */ public function bulk(array $params = []); - /** * @param array $params * @@ -55,7 +50,6 @@ public function bulk(array $params = []); */ public function delete(array $params = []); - /** * @param array $params * @@ -63,7 +57,6 @@ public function delete(array $params = []); */ public function deleteByQuery(array $params = []); - /** * @param array $params * @@ -71,7 +64,6 @@ public function deleteByQuery(array $params = []); */ public function create(array $params = []); - /** * @param array $params * @@ -79,33 +71,29 @@ public function create(array $params = []); */ public function exists(array $params = []); - /** * @return TasksNamespace */ public function tasks(); - /** * @return IndicesNamespace */ public function indices(); - /** * @return Search */ public function createSearch(); - /** * @return Sort */ public function createSort(); - /** * @param Search $search + * * @return array */ public function execute(Search $search); @@ -116,4 +104,18 @@ public function execute(Search $search); * @return int */ public function count(Search $search): int; + + /** + * @param string $indexName + * + * @return string + */ + public function getPrefixedIndexName(string $indexName): string; + + /** + * @param array $parameters + * + * @return array + */ + public function getPrefixedIndexParameters(array $parameters): array; } diff --git a/src/ElasticsearchService.php b/src/ElasticsearchService.php index d28a10d..75e6561 100644 --- a/src/ElasticsearchService.php +++ b/src/ElasticsearchService.php @@ -13,14 +13,21 @@ class ElasticsearchService implements ElasticsearchServiceContract */ private $client; + /** + * @var string|null + */ + private $indexPrefix; + /** * ElasticsearchService constructor. * - * @param Client $client + * @param Client $client + * @param string|null $indexPrefix */ - public function __construct(Client $client) + public function __construct(Client $client, ?string $indexPrefix = null) { - $this->client = $client; + $this->client = $client; + $this->indexPrefix = $indexPrefix; } /** @@ -28,9 +35,7 @@ public function __construct(Client $client) */ public function search(array $params = []) { - $params = IndexNamePrefixer::getPrefixedIndexParameters($params); - - return $this->client->search($params); + return $this->client->search($this->getPrefixedIndexParameters($params)); } /** @@ -38,9 +43,7 @@ public function search(array $params = []) */ public function index(array $params = []) { - $params = IndexNamePrefixer::getPrefixedIndexParameters($params); - - return $this->client->index($params); + return $this->client->index($this->getPrefixedIndexParameters($params)); } /** @@ -48,6 +51,7 @@ public function index(array $params = []) */ public function reindex(array $params = []) { + // Index prefixing omitted on purpose here return $this->client->reindex($params); } @@ -56,9 +60,7 @@ public function reindex(array $params = []) */ public function updateByQuery(array $params = []) { - $params = IndexNamePrefixer::getPrefixedIndexParameters($params); - - return $this->client->updateByQuery($params); + return $this->client->updateByQuery($this->getPrefixedIndexParameters($params)); } /** @@ -66,9 +68,7 @@ public function updateByQuery(array $params = []) */ public function bulk(array $params = []) { - $params = IndexNamePrefixer::getPrefixedIndexParameters($params); - - return $this->client->bulk($params); + return $this->client->bulk($this->getPrefixedIndexParameters($params)); } /** @@ -76,9 +76,7 @@ public function bulk(array $params = []) */ public function delete(array $params = []) { - $params = IndexNamePrefixer::getPrefixedIndexParameters($params); - - return $this->client->delete($params); + return $this->client->delete($this->getPrefixedIndexParameters($params)); } /** @@ -86,9 +84,7 @@ public function delete(array $params = []) */ public function deleteByQuery(array $params = []) { - $params = IndexNamePrefixer::getPrefixedIndexParameters($params); - - return $this->client->deleteByQuery($params); + return $this->client->deleteByQuery($this->getPrefixedIndexParameters($params)); } /** @@ -104,9 +100,7 @@ public function tasks() */ public function create(array $params = []) { - $params = IndexNamePrefixer::getPrefixedIndexParameters($params); - - return $this->client->create($params); + return $this->client->create($this->getPrefixedIndexParameters($params)); } /** @@ -114,9 +108,7 @@ public function create(array $params = []) */ public function exists(array $params = []) { - $params = IndexNamePrefixer::getPrefixedIndexParameters($params); - - return $this->client->exists($params); + return $this->client->exists($this->getPrefixedIndexParameters($params)); } /** @@ -148,10 +140,8 @@ public function createSort() */ public function execute(Search $search) { - $index = IndexNamePrefixer::getPrefixedIndexName($search->getIndex()); - return $this->search([ - 'index' => $index, + 'index' => $this->getPrefixedIndexName($search->getIndex()), 'type' => $search->getType(), 'body' => $search->buildBody(), ]); @@ -162,12 +152,34 @@ public function execute(Search $search) */ public function count(Search $search): int { - $index = IndexNamePrefixer::getPrefixedIndexName($search->getIndex()); - return $this->client->count([ - 'index' => $index, + 'index' => $this->getPrefixedIndexName($search->getIndex()), 'type' => $search->getType(), 'body' => $search->buildBody(), ])['count']; } + + /** + * @inheritdoc + */ + public function getPrefixedIndexName(string $indexName): string + { + if ($this->indexPrefix) { + return \sprintf('%s_%s', $this->indexPrefix, $indexName); + } + + return $indexName; + } + + /** + * @inheritdoc + */ + public function getPrefixedIndexParameters(array $parameters): array + { + if (isset($parameters['index'])) { + $parameters['index'] = $this->getPrefixedIndexName($parameters['index']); + } + + return $parameters; + } } diff --git a/src/ElasticsearchServiceProvider.php b/src/ElasticsearchServiceProvider.php index f7e29b8..3e1dc7d 100644 --- a/src/ElasticsearchServiceProvider.php +++ b/src/ElasticsearchServiceProvider.php @@ -8,7 +8,6 @@ class ElasticsearchServiceProvider extends ServiceProvider { protected const CONFIG_KEY = 'elasticsearch'; - /** * @inheritdoc */ @@ -20,7 +19,6 @@ public function register() $this->registerBindings(); } - /** * Register bindings. */ @@ -28,9 +26,14 @@ protected function registerBindings() { /** @noinspection PhpUndefinedMethodInspection */ $config = $this->app['config']->get('elasticsearch', []); - - $this->app->/** @scrutinizer ignore-call */ singleton(ElasticsearchServiceContract::class, function () use ($config) { - return new ElasticsearchService(ClientBuilder::fromConfig($config)); - }); + + // Extract the index_prefix parameter if present + $indexPrefix = $config['index_prefix']; + unset($config['index_prefix']); + + $this->app->/** @scrutinizer ignore-call */ singleton(ElasticsearchServiceContract::class, + static function () use ($config, $indexPrefix) { + return new ElasticsearchService(ClientBuilder::fromConfig($config), $indexPrefix); + }); } } diff --git a/src/IndexNamePrefixer.php b/src/IndexNamePrefixer.php deleted file mode 100644 index 331a9e7..0000000 --- a/src/IndexNamePrefixer.php +++ /dev/null @@ -1,26 +0,0 @@ -getTargetConfiguration()); - } - /** * @return string */ @@ -92,14 +82,6 @@ public function getTargetVersionName() return $this->getTargetConfiguration()['index']; } - /** - * @return string - */ - public function getPrefixedTargetVersionName(): string - { - return IndexNamePrefixer::getPrefixedIndexName($this->getTargetVersionName()); - } - /** * @return int */ diff --git a/src/Pipelines/Payloads/MigrationPayload.php b/src/Pipelines/Payloads/MigrationPayload.php index 8547cc5..726e75f 100644 --- a/src/Pipelines/Payloads/MigrationPayload.php +++ b/src/Pipelines/Payloads/MigrationPayload.php @@ -2,8 +2,6 @@ namespace Nord\Lumen\Elasticsearch\Pipelines\Payloads; -use Nord\Lumen\Elasticsearch\IndexNamePrefixer; - /** * Class MigrationPayload * @package Nord\Lumen\Elasticsearch\Pipelines\Payloads @@ -58,14 +56,6 @@ public function getIndexName() return $this->getConfiguration()['index']; } - /** - * @return string - */ - public function getPrefixedIndexName(): string - { - return IndexNamePrefixer::getPrefixedIndexName($this->getIndexName()); - } - /** * @return string */ diff --git a/src/Pipelines/Stages/CheckIndexExistsStage.php b/src/Pipelines/Stages/CheckIndexExistsStage.php index 2116ff7..671fc7a 100644 --- a/src/Pipelines/Stages/CheckIndexExistsStage.php +++ b/src/Pipelines/Stages/CheckIndexExistsStage.php @@ -37,7 +37,7 @@ public function __construct(ElasticsearchServiceContract $elasticsearchService) public function __invoke($payload) { /** @var ApplyMigrationPayload $payload */ - $index = $payload->getPrefixedTargetVersionName(); + $index = $this->elasticsearchService->getPrefixedIndexName($payload->getTargetVersionName()); $response = $this->elasticsearchService->indices()->exists(['index' => $index]); if ($response) { diff --git a/src/Pipelines/Stages/CreateIndexStage.php b/src/Pipelines/Stages/CreateIndexStage.php index e067b47..049d6d7 100644 --- a/src/Pipelines/Stages/CreateIndexStage.php +++ b/src/Pipelines/Stages/CreateIndexStage.php @@ -34,7 +34,7 @@ public function __construct(ElasticsearchServiceContract $elasticsearchService) public function __invoke($payload) { /** @var ApplyMigrationPayload $payload */ - $params = $payload->getPrefixedTargetConfiguration(); + $params = $this->elasticsearchService->getPrefixedIndexParameters($payload->getTargetConfiguration()); $this->elasticsearchService->indices()->create($params); diff --git a/src/Pipelines/Stages/ReIndexStage.php b/src/Pipelines/Stages/ReIndexStage.php index c8e16bf..9184f6c 100644 --- a/src/Pipelines/Stages/ReIndexStage.php +++ b/src/Pipelines/Stages/ReIndexStage.php @@ -38,8 +38,8 @@ public function __invoke($payload) { /** @var ApplyMigrationPayload $payload */ // Reindex data from the old index to the new, but only if the old index exists (not true on brand new setups) - $oldIndex = $payload->getPrefixedIndexName(); - $newIndex = $payload->getPrefixedTargetVersionName(); + $oldIndex = $this->elasticsearchService->getPrefixedIndexName($payload->getIndexName()); + $newIndex = $this->elasticsearchService->getPrefixedIndexName($payload->getTargetVersionName()); if ($this->elasticsearchService->indices()->exists(['index' => $oldIndex])) { // Temporarily change some index settings to speed up the process diff --git a/src/Pipelines/Stages/StoreIndexSettingsStage.php b/src/Pipelines/Stages/StoreIndexSettingsStage.php index 31393cb..192bac8 100644 --- a/src/Pipelines/Stages/StoreIndexSettingsStage.php +++ b/src/Pipelines/Stages/StoreIndexSettingsStage.php @@ -35,7 +35,7 @@ public function __invoke($payload) { /** @var ApplyMigrationPayload $payload */ // Store the current number_of_replicas setting value in the payload - $index = $payload->getPrefixedTargetVersionName(); + $index = $this->elasticsearchService->getPrefixedIndexName($payload->getTargetVersionName()); $settings = $this->elasticsearchService->indices()->getSettings(['index' => $index]); diff --git a/src/Pipelines/Stages/UpdateIndexAliasStage.php b/src/Pipelines/Stages/UpdateIndexAliasStage.php index 77e0ab9..825c3d1 100644 --- a/src/Pipelines/Stages/UpdateIndexAliasStage.php +++ b/src/Pipelines/Stages/UpdateIndexAliasStage.php @@ -37,7 +37,7 @@ public function __invoke($payload) /** @var ApplyMigrationPayload $payload */ $indices = $this->elasticsearchService->indices(); - $alias = $payload->getPrefixedIndexName(); + $alias = $this->elasticsearchService->getPrefixedIndexName($payload->getIndexName()); $orphanedIndices = []; // If we already have an alias in place we store the indices it points to right now @@ -55,8 +55,10 @@ public function __invoke($payload) } // Revert temporary index settings + $index = $this->elasticsearchService->getPrefixedIndexName($payload->getTargetVersionName()); + $indices->putSettings([ - 'index' => $payload->getPrefixedTargetVersionName(), + 'index' => $index, 'body' => [ 'refresh_interval' => '1s', 'number_of_replicas' => $payload->getNumberOfReplicas(), @@ -69,7 +71,7 @@ public function __invoke($payload) 'actions' => [ [ 'add' => [ - 'index' => $payload->getPrefixedTargetVersionName(), + 'index' => $index, 'alias' => $alias, ], ], diff --git a/tests/IndexNamePrefixerTest.php b/tests/IndexNamePrefixerTest.php deleted file mode 100644 index c89f5a4..0000000 --- a/tests/IndexNamePrefixerTest.php +++ /dev/null @@ -1,24 +0,0 @@ -assertEquals('foo', IndexNamePrefixer::getPrefixedIndexName('foo')); - $this->assertEquals(['index' => 'foo'], IndexNamePrefixer::getPrefixedIndexParameters(['index' => 'foo'])); - } - - public function testPrefixDefined(): void - { - putenv('ELASTICSEARCH_INDEX_PREFIX=dev'); - - $this->assertEquals('dev_foo', IndexNamePrefixer::getPrefixedIndexName('foo')); - $this->assertEquals(['index' => 'dev_foo'], IndexNamePrefixer::getPrefixedIndexParameters(['index' => 'foo'])); - } -} diff --git a/tests/Pipelines/Stages/AbstractStageTestCase.php b/tests/Pipelines/Stages/AbstractStageTestCase.php index 25194d4..2eab6d6 100644 --- a/tests/Pipelines/Stages/AbstractStageTestCase.php +++ b/tests/Pipelines/Stages/AbstractStageTestCase.php @@ -5,6 +5,7 @@ use Elasticsearch\Namespaces\IndicesNamespace; use Elasticsearch\Namespaces\TasksNamespace; use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract; +use Nord\Lumen\Elasticsearch\ElasticsearchService; use Nord\Lumen\Elasticsearch\Tests\TestCase; /** @@ -27,7 +28,6 @@ protected function getMockedIndices($methods = []) ->getMock(); } - /** * @param array $methods * @@ -43,14 +43,16 @@ protected function getMockedTasks($methods = []) /** * @param IndicesNamespace|\PHPUnit_Framework_MockObject_MockObject $mockedIndices + * @param string|null $indexPrefix * * @return ElasticsearchServiceContract|\PHPUnit_Framework_MockObject_MockObject */ - protected function getMockedSearchService($mockedIndices) + protected function getMockedSearchService($mockedIndices, ?string $indexPrefix = null) { - $searchService = $this->getMockBuilder(ElasticsearchServiceContract::class) - ->setMethods(['indices', 'reindex']) - ->getMockForAbstractClass(); + $searchService = $this->getMockBuilder(ElasticsearchService::class) + ->setConstructorArgs([$this->createDummyClient(), $indexPrefix]) + ->setMethods(['indices', 'reindex', 'tasks']) + ->getMock(); $searchService->method('indices') ->willReturn($mockedIndices); diff --git a/tests/Pipelines/Stages/UpdateIndexAliasStageTest.php b/tests/Pipelines/Stages/UpdateIndexAliasStageTest.php index 2e00027..333f856 100644 --- a/tests/Pipelines/Stages/UpdateIndexAliasStageTest.php +++ b/tests/Pipelines/Stages/UpdateIndexAliasStageTest.php @@ -4,16 +4,14 @@ use Elasticsearch\Common\Exceptions\Missing404Exception; use Elasticsearch\Namespaces\IndicesNamespace; -use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract; use Nord\Lumen\Elasticsearch\Pipelines\Payloads\ApplyMigrationPayload; use Nord\Lumen\Elasticsearch\Pipelines\Stages\UpdateIndexAliasStage; -use Nord\Lumen\Elasticsearch\Tests\TestCase; /** * Class UpdateIndexAliasStageTest * @package Nord\Lumen\Elasticsearch\Tests\Pipelines\Stages */ -class UpdateIndexAliasStageTest extends TestCase +class UpdateIndexAliasStageTest extends AbstractStageTestCase { /** @@ -46,14 +44,7 @@ public function testNormalOperation() ->method('delete') ->with(['index' => 'content_3']); - /** @var ElasticsearchServiceContract|\PHPUnit_Framework_MockObject_MockObject $service */ - $service = $this->getMockBuilder(ElasticsearchServiceContract::class) - ->setMethods(['indices']) - ->getMockForAbstractClass(); - - $service->expects($this->once()) - ->method('indices') - ->willReturn($indices); + $service = $this->getMockedSearchService($indices); $stage = new UpdateIndexAliasStage($service); $payload = new ApplyMigrationPayload($this->getResourcesBasePath() . '/content.php', 100); @@ -67,8 +58,6 @@ public function testNormalOperation() */ public function testNormalOperationWithPrefix() { - putenv('ELASTICSEARCH_INDEX_PREFIX=test'); - /** @var IndicesNamespace|\PHPUnit_Framework_MockObject_MockObject $indices */ $indices = $this->getMockBuilder(IndicesNamespace::class) ->disableOriginalConstructor() @@ -94,14 +83,8 @@ public function testNormalOperationWithPrefix() ->method('delete') ->with(['index' => 'test_content_3']); - /** @var ElasticsearchServiceContract|\PHPUnit_Framework_MockObject_MockObject $service */ - $service = $this->getMockBuilder(ElasticsearchServiceContract::class) - ->setMethods(['indices']) - ->getMockForAbstractClass(); - - $service->expects($this->once()) - ->method('indices') - ->willReturn($indices); + $indexPrefix = 'test'; + $service = $this->getMockedSearchService($indices, $indexPrefix); $stage = new UpdateIndexAliasStage($service); $payload = new ApplyMigrationPayload($this->getResourcesBasePath() . '/content.php', 100); @@ -135,14 +118,7 @@ public function testMissingAliasMissingIndex() $indices->expects($this->never()) ->method('delete'); - /** @var ElasticsearchServiceContract|\PHPUnit_Framework_MockObject_MockObject $service */ - $service = $this->getMockBuilder(ElasticsearchServiceContract::class) - ->setMethods(['indices']) - ->getMockForAbstractClass(); - - $service->expects($this->once()) - ->method('indices') - ->willReturn($indices); + $service = $this->getMockedSearchService($indices); $stage = new UpdateIndexAliasStage($service); $payload = new ApplyMigrationPayload($this->getResourcesBasePath() . '/content.php', 100); @@ -177,14 +153,7 @@ public function testMissingAlias() ->method('delete') ->with(['index' => 'content']); - /** @var ElasticsearchServiceContract|\PHPUnit_Framework_MockObject_MockObject $service */ - $service = $this->getMockBuilder(ElasticsearchServiceContract::class) - ->setMethods(['indices']) - ->getMockForAbstractClass(); - - $service->expects($this->once()) - ->method('indices') - ->willReturn($indices); + $service = $this->getMockedSearchService($indices); $stage = new UpdateIndexAliasStage($service); $payload = new ApplyMigrationPayload($this->getResourcesBasePath() . '/content.php', 100); diff --git a/tests/ServiceTest.php b/tests/ServiceTest.php index 49946e5..fb3fcfa 100644 --- a/tests/ServiceTest.php +++ b/tests/ServiceTest.php @@ -4,6 +4,7 @@ use Elasticsearch\Client; use Nord\Lumen\Elasticsearch\ElasticsearchService; +use Nord\Lumen\Elasticsearch\IndexNamePrefixer; /** * Class ServiceTest @@ -282,4 +283,18 @@ public function testMethodIndices() $this->assertEquals($output, $this->service->indices()); } + + public function testNoPrefixDefined(): void + { + $this->assertEquals('foo', $this->service->getPrefixedIndexName('foo')); + $this->assertEquals(['index' => 'foo'], $this->service->getPrefixedIndexParameters(['index' => 'foo'])); + } + + public function testPrefixDefined(): void + { + $this->service = new ElasticsearchService($this->client, 'dev'); + + $this->assertEquals('dev_foo', $this->service->getPrefixedIndexName('foo')); + $this->assertEquals(['index' => 'dev_foo'], $this->service->getPrefixedIndexParameters(['index' => 'foo'])); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 6032b10..3aaa381 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,7 @@ namespace Nord\Lumen\Elasticsearch\Tests; +use Elasticsearch\Client; use Elasticsearch\ClientBuilder; use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract; use Nord\Lumen\Elasticsearch\ElasticsearchService; @@ -25,15 +26,15 @@ protected function setUp() { parent::setUp(); - $this->service = new ElasticsearchService(ClientBuilder::fromConfig([])); + $this->service = new ElasticsearchService($this->createDummyClient()); } /** - * @inheritDoc + * @return Client */ - protected function tearDown() + protected function createDummyClient(): Client { - putenv('ELASTICSEARCH_INDEX_PREFIX='); + return ClientBuilder::fromConfig([]); } /**