Skip to content

Commit

Permalink
Added tests and implemented exists functionality for elasticsearch se…
Browse files Browse the repository at this point in the history
…rvice
  • Loading branch information
zakhenry committed Dec 21, 2015
1 parent fe645f4 commit 3cb4b01
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 7 deletions.
19 changes: 19 additions & 0 deletions api/app/Services/ElasticSearch.php
Expand Up @@ -91,6 +91,25 @@ public function deleteIndex(IndexedModel $model = null)
return $this->getClient()->indices()->delete($config);
}

/**
* @param IndexedModel|null $model
* @return array
*/
public function indexExists(IndexedModel $model = null)
{
$indexName = self::getDefaultIndexName();

if ($model) {
$indexName = $model->getIndexName();
}

$config = [
'index' => $indexName,
];

return $this->getClient()->indices()->exists($config);
}

/**
* @param $indexName
* @return array
Expand Down
6 changes: 1 addition & 5 deletions api/bootstrap/app.php
Expand Up @@ -58,12 +58,8 @@
);

$app->singleton(App\Services\ElasticSearch::class, function ($app) {
$config = [];
if (config()->has('elasticquent.config')) {
$config = config()->get('elasticquent.config');
}

$client = new \Elasticsearch\Client($config);
$client = new \Elasticsearch\Client(App\Services\ElasticSearch::getConfig());

return new App\Services\ElasticSearch($client);
});
Expand Down
Expand Up @@ -29,7 +29,9 @@ public function __construct()
*/
public function up()
{
$this->elasticSearch->createIndex();
if (!$this->elasticSearch->indexExists()) {
$this->elasticSearch->createIndex();
}
}

/**
Expand All @@ -39,6 +41,8 @@ public function up()
*/
public function down()
{
$this->elasticSearch->deleteIndex();
if ($this->elasticSearch->indexExists()){
$this->elasticSearch->deleteIndex();
}
}
}
122 changes: 122 additions & 0 deletions api/tests/ElasticSearchTest.php
@@ -0,0 +1,122 @@
<?php

/*
* This file is part of the Spira framework.
*
* @link https://github.com/spira/spira
*
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
*/

/**
* Class ElasticSearchTest.
*/
class ElasticSearchTest extends TestCase
{
public function testCreateIndex()
{
$elasticSearchMock = Mockery::mock(\Elasticsearch\Client::class)->makePartial();
$indicesMock = Mockery::mock(\Elasticsearch\Namespaces\IndicesNamespace::class);

$elasticSearchMock->shouldReceive('indices')->andReturn($indicesMock);
$indicesMock->shouldReceive('create')->with(\Mockery::subset(['index' => config()->get('elasticquent.default_index')]));

$elasticSearchService = new \App\Services\ElasticSearch($elasticSearchMock);

$elasticSearchService->createIndex();
}

public function testCreateIndexForModel()
{
$elasticSearchMock = Mockery::mock(\Elasticsearch\Client::class)->makePartial();
$indicesMock = Mockery::mock(\Elasticsearch\Namespaces\IndicesNamespace::class);

$elasticSearchMock->shouldReceive('indices')->andReturn($indicesMock);
$indicesMock->shouldReceive('create')->with(\Mockery::subset(['index' => 'foo']));

$elasticSearchService = new \App\Services\ElasticSearch($elasticSearchMock);

$elasticSearchService->createIndex(new class extends \Spira\Core\Model\Model\IndexedModel {

public function getIndexName()
{
return 'foo';
}

});
}

public function testDeleteIndex()
{
$elasticSearchMock = Mockery::mock(\Elasticsearch\Client::class)->makePartial();
$indicesMock = Mockery::mock(\Elasticsearch\Namespaces\IndicesNamespace::class);

$elasticSearchMock->shouldReceive('indices')->andReturn($indicesMock);
$indicesMock->shouldReceive('delete')->with(\Mockery::subset(['index' => config()->get('elasticquent.default_index')]));

$elasticSearchService = new \App\Services\ElasticSearch($elasticSearchMock);

$elasticSearchService->deleteIndex();
}

public function testDeleteIndexForModel()
{
$elasticSearchMock = Mockery::mock(\Elasticsearch\Client::class)->makePartial();
$indicesMock = Mockery::mock(\Elasticsearch\Namespaces\IndicesNamespace::class);

$elasticSearchMock->shouldReceive('indices')->andReturn($indicesMock);
$indicesMock->shouldReceive('delete')->with(\Mockery::subset(['index' => 'foo']));

$elasticSearchService = new \App\Services\ElasticSearch($elasticSearchMock);

$elasticSearchService->deleteIndex(new class extends \Spira\Core\Model\Model\IndexedModel {

public function getIndexName()
{
return 'foo';
}

});
}

public function testIndexExists()
{
$elasticSearchMock = Mockery::mock(\Elasticsearch\Client::class)->makePartial();
$indicesMock = Mockery::mock(\Elasticsearch\Namespaces\IndicesNamespace::class);

$elasticSearchMock->shouldReceive('indices')->andReturn($indicesMock);
$indicesMock->shouldReceive('exists')->with(\Mockery::subset(['index' => 'foo']));

$elasticSearchService = new \App\Services\ElasticSearch($elasticSearchMock);

$elasticSearchService->indexExists(new class extends \Spira\Core\Model\Model\IndexedModel {

public function getIndexName()
{
return 'foo';
}

});
}

public function testIndexExistsForModel()
{
$elasticSearchMock = Mockery::mock(\Elasticsearch\Client::class)->makePartial();
$indicesMock = Mockery::mock(\Elasticsearch\Namespaces\IndicesNamespace::class);

$elasticSearchMock->shouldReceive('indices')->andReturn($indicesMock);
$indicesMock->shouldReceive('exists')->with(\Mockery::subset(['index' => 'foo']));

$elasticSearchService = new \App\Services\ElasticSearch($elasticSearchMock);

$elasticSearchService->indexExists(new class extends \Spira\Core\Model\Model\IndexedModel {

public function getIndexName()
{
return 'foo';
}

});
}

}

0 comments on commit 3cb4b01

Please sign in to comment.