From f477c67173336ac53bee671a88dcce87b417fe22 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 10:34:28 +0200 Subject: [PATCH 01/57] Added shouldNotInstantiateWithoutConfigArray test --- composer.json | 14 ++++++++++++-- src/ElasticSearch.php | 5 +++-- tests/ElasticSearchTest.php | 29 +++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 tests/ElasticSearchTest.php diff --git a/composer.json b/composer.json index 66993d3..5f5ddd6 100644 --- a/composer.json +++ b/composer.json @@ -1,19 +1,29 @@ { - "name": "jotweh/codeception-elasticsearch", + "name": "willemnviljoen/codeception-elasticsearch", "description": "Elastic Search Module for Codeception", "license": "gpl", "authors": [ { "name": "Jan Wyszynski", "email": "jan.wyszynki@gmail.com" + }, + { + "name": "Willem Viljoen", + "email": "willemnviljoen@gmail.com" } ], "require": { "elasticsearch/elasticsearch": "~1.0" }, + "require-dev": { + "phpunit/phpunit": "~4.8", + "codeception/codeception": "*", + "mockery/mockery": "~0.9" + }, "autoload": { "psr-4": { - "Codeception\\Module\\": "src" + "Codeception\\Module\\": "src", + "Tests\\Codeception\\Module\\": "tests" } } } diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 7049895..a6256e1 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -5,6 +5,7 @@ namespace Codeception\Module; +use Codeception\Lib\ModuleContainer; use Codeception\Module; use Elasticsearch\Client; @@ -13,7 +14,7 @@ class ElasticSearch extends Module /** @var \Elasticsearch\Client */ private $elasticSearch; - public function __construct($config = null) + public function __construct(ModuleContainer $moduleContainer, $config = null) { // terminology: see = isXyz => true/false, have = create, grab = get => data @@ -26,7 +27,7 @@ public function __construct($config = null) } $this->config = (array)$config; - parent::__construct(); + parent::__construct($moduleContainer); } public function _initialize() diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php new file mode 100644 index 0000000..8b4b0c5 --- /dev/null +++ b/tests/ElasticSearchTest.php @@ -0,0 +1,29 @@ + Date: Tue, 24 Nov 2015 10:37:42 +0200 Subject: [PATCH 02/57] Added test shouldNotInstantiateWithoutHostsArrayInConfigArray --- tests/ElasticSearchTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index 8b4b0c5..ade7e51 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -26,4 +26,15 @@ public function shouldNotInstantiateWithoutConfigArray() $container = m::mock('\Codeception\Lib\ModuleContainer'); new ElasticSearch($container, null); } + + /** + * @test + * @expectedException \Exception + */ + public function shouldNotInstantiateWithoutHostsArrayInConfigArray() + { + /** @var ModuleContainer | m\Mock $container */ + $container = m::mock('\Codeception\Lib\ModuleContainer'); + new ElasticSearch($container, ['hosts' => null]); + } } From c8658962e523a021c71db1ff0df40da3409572bf Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 10:38:59 +0200 Subject: [PATCH 03/57] Extracted setUp and tairDown methods --- tests/ElasticSearchTest.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index ade7e51..74df5a2 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -15,6 +15,11 @@ class ElasticSearchTest extends \PHPUnit_Framework_TestCase { + /** + * @var ModuleContainer | m\Mock + */ + private $container; + /** * @test * @expectedException \Exception @@ -22,9 +27,7 @@ class ElasticSearchTest extends \PHPUnit_Framework_TestCase */ public function shouldNotInstantiateWithoutConfigArray() { - /** @var ModuleContainer | m\Mock $container */ - $container = m::mock('\Codeception\Lib\ModuleContainer'); - new ElasticSearch($container, null); + new ElasticSearch($this->container, null); } /** @@ -33,8 +36,16 @@ public function shouldNotInstantiateWithoutConfigArray() */ public function shouldNotInstantiateWithoutHostsArrayInConfigArray() { - /** @var ModuleContainer | m\Mock $container */ - $container = m::mock('\Codeception\Lib\ModuleContainer'); - new ElasticSearch($container, ['hosts' => null]); + new ElasticSearch($this->container, ['hosts' => null]); + } + + public function setUp() + { + $this->container = m::mock('\Codeception\Lib\ModuleContainer'); + } + + public function tearDown() + { + m::close(); } } From 75947d6e79611b9544fed12fb2471f300cf307cd Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 10:47:21 +0200 Subject: [PATCH 04/57] Added ability to inject client (needed for testing) --- src/ElasticSearch.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index a6256e1..86966f1 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -11,10 +11,12 @@ class ElasticSearch extends Module { - /** @var \Elasticsearch\Client */ - private $elasticSearch; + /** + * @var Client + */ + private $elasticSearch = null; - public function __construct(ModuleContainer $moduleContainer, $config = null) + public function __construct(ModuleContainer $moduleContainer, $config = null, Client $client = null) { // terminology: see = isXyz => true/false, have = create, grab = get => data @@ -27,18 +29,18 @@ public function __construct(ModuleContainer $moduleContainer, $config = null) } $this->config = (array)$config; + if (!is_null($client)) { + $this->elasticSearch = $client; + } + parent::__construct($moduleContainer); } public function _initialize() { - /* - * elastic search config - * hosts - array of ES hosts - * dic - ES dictionary - */ - - $this->elasticSearch = new Client($this->config); + if (!is_null($this->$this->elasticSearch)) { + $this->elasticSearch = new Client($this->config); + } } /** From b77ad9b996a0fd44a6cf6b41711db619742c8e3a Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:00:30 +0200 Subject: [PATCH 05/57] Added tests for initialise and seeItemExistsInElasticsearchShouldPassIndexNameToClient --- src/ElasticSearch.php | 2 +- tests/ElasticSearchTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 86966f1..e0da73a 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -38,7 +38,7 @@ public function __construct(ModuleContainer $moduleContainer, $config = null, Cl public function _initialize() { - if (!is_null($this->$this->elasticSearch)) { + if (is_null($this->elasticSearch)) { $this->elasticSearch = new Client($this->config); } } diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index 74df5a2..d94a571 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -9,6 +9,7 @@ namespace Tests\Codeception\Module; +use Elasticsearch\Client; use Mockery as m; use Codeception\Lib\ModuleContainer; use Codeception\Module\ElasticSearch; @@ -39,6 +40,33 @@ public function shouldNotInstantiateWithoutHostsArrayInConfigArray() new ElasticSearch($this->container, ['hosts' => null]); } + /** + * @test + * @expectedException \Exception + * @expectedExceptionMessage Could not resolve host: test.3.1415.nonexistent-host.com + */ + public function initializeShouldCreateClientWithConfiguredHostsIfNoClientIsPassedToConstructor() + { + $module = new ElasticSearch($this->container, ['hosts' => ['test.3.1415.nonexistent-host.com']]); + $module->_initialize(); + $module->seeItemExistsInElasticsearch('any-indexname', 'any-type', 'any-id'); + } + + /** + * @test + */ + public function seeItemExistsInElasticsearchShouldPassIndexNameToClient() + { + $client = m::mock('\Elasticsearch\Client'); + $client->shouldIgnoreMissing(); + + $module = new ElasticSearch($this->container, ['hosts' => []], $client); + $module->_initialize(); + $module->seeItemExistsInElasticsearch('index-name', null, null); + + $client->shouldHaveReceived('exists')->with(m::subset(['index' => 'index-name']))->once(); + } + public function setUp() { $this->container = m::mock('\Codeception\Lib\ModuleContainer'); From 7aab601c725ca8f8ddfdb4ee40a6904535e9f419 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:01:13 +0200 Subject: [PATCH 06/57] Extracted class level client --- tests/ElasticSearchTest.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index d94a571..1e0fbca 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -16,6 +16,10 @@ class ElasticSearchTest extends \PHPUnit_Framework_TestCase { + /** + * @var Client | m\Mock + */ + private $client; /** * @var ModuleContainer | m\Mock */ @@ -57,19 +61,18 @@ public function initializeShouldCreateClientWithConfiguredHostsIfNoClientIsPasse */ public function seeItemExistsInElasticsearchShouldPassIndexNameToClient() { - $client = m::mock('\Elasticsearch\Client'); - $client->shouldIgnoreMissing(); - - $module = new ElasticSearch($this->container, ['hosts' => []], $client); + $module = new ElasticSearch($this->container, ['hosts' => []], $this->client); $module->_initialize(); $module->seeItemExistsInElasticsearch('index-name', null, null); - $client->shouldHaveReceived('exists')->with(m::subset(['index' => 'index-name']))->once(); + $this->client->shouldHaveReceived('exists')->with(m::subset(['index' => 'index-name']))->once(); } public function setUp() { $this->container = m::mock('\Codeception\Lib\ModuleContainer'); + $this->client = m::mock('\Elasticsearch\Client'); + $this->client->shouldIgnoreMissing(); } public function tearDown() From 58d85360057de54b5ddf98f212ac3771d61b687a Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:02:40 +0200 Subject: [PATCH 07/57] Added test seeItemExistsInElasticsearchShouldPassTypeToClient --- tests/ElasticSearchTest.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index 1e0fbca..ea7e5b7 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -62,12 +62,20 @@ public function initializeShouldCreateClientWithConfiguredHostsIfNoClientIsPasse public function seeItemExistsInElasticsearchShouldPassIndexNameToClient() { $module = new ElasticSearch($this->container, ['hosts' => []], $this->client); - $module->_initialize(); $module->seeItemExistsInElasticsearch('index-name', null, null); - $this->client->shouldHaveReceived('exists')->with(m::subset(['index' => 'index-name']))->once(); } + /** + * @test + */ + public function seeItemExistsInElasticsearchShouldPassTypeToClient() + { + $module = new ElasticSearch($this->container, ['hosts' => []], $this->client); + $module->seeItemExistsInElasticsearch(null, 'document-type', null); + $this->client->shouldHaveReceived('exists')->with(m::subset(['type' => 'document-type']))->once(); + } + public function setUp() { $this->container = m::mock('\Codeception\Lib\ModuleContainer'); From 925472a64d51079febe52a9ca13b9c8e5abfddd6 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:03:30 +0200 Subject: [PATCH 08/57] Added seeItemExistsInElasticsearchShouldPassIdToClient --- tests/ElasticSearchTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index ea7e5b7..5a483da 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -76,6 +76,16 @@ public function seeItemExistsInElasticsearchShouldPassTypeToClient() $this->client->shouldHaveReceived('exists')->with(m::subset(['type' => 'document-type']))->once(); } + /** + * @test + */ + public function seeItemExistsInElasticsearchShouldPassIdToClient() + { + $module = new ElasticSearch($this->container, ['hosts' => []], $this->client); + $module->seeItemExistsInElasticsearch(null, null, 'document-id'); + $this->client->shouldHaveReceived('exists')->with(m::subset(['id' => 'document-id']))->once(); + } + public function setUp() { $this->container = m::mock('\Codeception\Lib\ModuleContainer'); From 1f95bb798c94aeb069180a4cd57df8ad4658f4f3 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:03:57 +0200 Subject: [PATCH 09/57] Renamed seeItemExistsInElasticsearchShouldCallExistsWithIndexNameOnClient --- tests/ElasticSearchTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index 5a483da..18ec259 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -59,7 +59,7 @@ public function initializeShouldCreateClientWithConfiguredHostsIfNoClientIsPasse /** * @test */ - public function seeItemExistsInElasticsearchShouldPassIndexNameToClient() + public function seeItemExistsInElasticsearchShouldCallExistsWithIndexNameOnClient() { $module = new ElasticSearch($this->container, ['hosts' => []], $this->client); $module->seeItemExistsInElasticsearch('index-name', null, null); From cbf451946bbf3d41823a38d9bbd4ed9e329c479f Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:04:13 +0200 Subject: [PATCH 10/57] Renamed seeItemExistsInElasticsearchShouldCallExistsWithTypeOnClient --- tests/ElasticSearchTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index 18ec259..7d8c5b0 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -69,7 +69,7 @@ public function seeItemExistsInElasticsearchShouldCallExistsWithIndexNameOnClien /** * @test */ - public function seeItemExistsInElasticsearchShouldPassTypeToClient() + public function seeItemExistsInElasticsearchShouldCallExistsWithTypeOnClient() { $module = new ElasticSearch($this->container, ['hosts' => []], $this->client); $module->seeItemExistsInElasticsearch(null, 'document-type', null); From 3309832270569056afca8063d7cba9e24d67e95e Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:04:35 +0200 Subject: [PATCH 11/57] Renamed seeItemExistsInElasticsearchShouldCallExistsWithIdOnClient --- tests/ElasticSearchTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index 7d8c5b0..90ae12f 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -79,7 +79,7 @@ public function seeItemExistsInElasticsearchShouldCallExistsWithTypeOnClient() /** * @test */ - public function seeItemExistsInElasticsearchShouldPassIdToClient() + public function seeItemExistsInElasticsearchShouldCallExistsWithIdOnClient() { $module = new ElasticSearch($this->container, ['hosts' => []], $this->client); $module->seeItemExistsInElasticsearch(null, null, 'document-id'); From 5ba074fb882c114630564245233ecd1a3a6fb266 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:15:21 +0200 Subject: [PATCH 12/57] Extracted class level module for happy path tests --- tests/ElasticSearchTest.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index 90ae12f..f11816b 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -16,6 +16,10 @@ class ElasticSearchTest extends \PHPUnit_Framework_TestCase { + /** + * @var ElasticSearch + */ + private $module; /** * @var Client | m\Mock */ @@ -61,8 +65,7 @@ public function initializeShouldCreateClientWithConfiguredHostsIfNoClientIsPasse */ public function seeItemExistsInElasticsearchShouldCallExistsWithIndexNameOnClient() { - $module = new ElasticSearch($this->container, ['hosts' => []], $this->client); - $module->seeItemExistsInElasticsearch('index-name', null, null); + $this->module->seeItemExistsInElasticsearch('index-name', null, null); $this->client->shouldHaveReceived('exists')->with(m::subset(['index' => 'index-name']))->once(); } @@ -71,8 +74,7 @@ public function seeItemExistsInElasticsearchShouldCallExistsWithIndexNameOnClien */ public function seeItemExistsInElasticsearchShouldCallExistsWithTypeOnClient() { - $module = new ElasticSearch($this->container, ['hosts' => []], $this->client); - $module->seeItemExistsInElasticsearch(null, 'document-type', null); + $this->module->seeItemExistsInElasticsearch(null, 'document-type', null); $this->client->shouldHaveReceived('exists')->with(m::subset(['type' => 'document-type']))->once(); } @@ -81,16 +83,17 @@ public function seeItemExistsInElasticsearchShouldCallExistsWithTypeOnClient() */ public function seeItemExistsInElasticsearchShouldCallExistsWithIdOnClient() { - $module = new ElasticSearch($this->container, ['hosts' => []], $this->client); - $module->seeItemExistsInElasticsearch(null, null, 'document-id'); + $this->module->seeItemExistsInElasticsearch(null, null, 'document-id'); $this->client->shouldHaveReceived('exists')->with(m::subset(['id' => 'document-id']))->once(); } public function setUp() { + /** @var ModuleContainer container */ $this->container = m::mock('\Codeception\Lib\ModuleContainer'); $this->client = m::mock('\Elasticsearch\Client'); $this->client->shouldIgnoreMissing(); + $this->module = new ElasticSearch($this->container, ['hosts' => []], $this->client); } public function tearDown() From 17846d5c447e078e6f7b14b1e3be3d8806a713a2 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:17:14 +0200 Subject: [PATCH 13/57] Added test grabAnItemFromElasticsearchShouldCallSearchWithIndexOnClient --- tests/ElasticSearchTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index f11816b..4f8a7b4 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -87,6 +87,15 @@ public function seeItemExistsInElasticsearchShouldCallExistsWithIdOnClient() $this->client->shouldHaveReceived('exists')->with(m::subset(['id' => 'document-id']))->once(); } + /** + * @test + */ + public function grabAnItemFromElasticsearchShouldCallSearchWithIndexOnClient() + { + $this->module->grabAnItemFromElasticsearch('index-name'); + $this->client->shouldHaveReceived('search')->with(m::subset(['index' => 'index-name'])); + } + public function setUp() { /** @var ModuleContainer container */ From a4624dba38d407ae6bba7cc2d95af82fbb7e1e23 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:21:20 +0200 Subject: [PATCH 14/57] Added test grabAnItemFromElasticsearchShouldCallSearchWithTypeOnClient --- tests/ElasticSearchTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index 4f8a7b4..108cb69 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -96,6 +96,15 @@ public function grabAnItemFromElasticsearchShouldCallSearchWithIndexOnClient() $this->client->shouldHaveReceived('search')->with(m::subset(['index' => 'index-name'])); } + /** + * @test + */ + public function grabAnItemFromElasticsearchShouldCallSearchWithTypeOnClient() + { + $this->module->grabAnItemFromElasticsearch(null, 'some-document-type'); + $this->client->shouldHaveReceived('search')->with(m::subset(['type' => 'some-document-type'])); + } + public function setUp() { /** @var ModuleContainer container */ From f30ec0df5710e8577a35e3f5ac7accfaa1754691 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:25:23 +0200 Subject: [PATCH 15/57] Added test grabAnItemFromElasticsearchShouldCallSearchWithQueryStringOnClient --- tests/ElasticSearchTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index 108cb69..dbd1f7b 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -105,6 +105,15 @@ public function grabAnItemFromElasticsearchShouldCallSearchWithTypeOnClient() $this->client->shouldHaveReceived('search')->with(m::subset(['type' => 'some-document-type'])); } + /** + * @test + */ + public function grabAnItemFromElasticsearchShouldCallSearchWithQueryStringOnClient() + { + $this->module->grabAnItemFromElasticsearch(null, null, 'something'); + $this->client->shouldHaveReceived('search')->with(m::subset(['q' => 'something'])); + } + public function setUp() { /** @var ModuleContainer container */ From 3a8d2faf3c35a24d36e0c6b4376a705bfe42ea3b Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:28:01 +0200 Subject: [PATCH 16/57] Added test grabAnItemFromElasticsearchShouldCallSearchWithSizeOneOnClient --- tests/ElasticSearchTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index dbd1f7b..6423b5a 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -114,6 +114,15 @@ public function grabAnItemFromElasticsearchShouldCallSearchWithQueryStringOnClie $this->client->shouldHaveReceived('search')->with(m::subset(['q' => 'something'])); } + /** + * @test + */ + public function grabAnItemFromElasticsearchShouldCallSearchWithSizeOneOnClient() + { + $this->module->grabAnItemFromElasticsearch(null, null, null); + $this->client->shouldHaveReceived('search')->with(m::subset(['size' => 1])); + } + public function setUp() { /** @var ModuleContainer container */ From 08459642330fb67831397ad3439e657220a693ec Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:31:43 +0200 Subject: [PATCH 17/57] Added grabAnItemFromElasticsearchShouldReturnEmptyArrayIfThereAreNoHits --- tests/ElasticSearchTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index 6423b5a..a2b2ac2 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -123,6 +123,16 @@ public function grabAnItemFromElasticsearchShouldCallSearchWithSizeOneOnClient() $this->client->shouldHaveReceived('search')->with(m::subset(['size' => 1])); } + /** + * @test + */ + public function grabAnItemFromElasticsearchShouldReturnEmptyArrayIfThereAreNoHits() + { + $this->client->shouldReceive('search')->andReturn(['hits' => ['hits' => []]]); + $item = $this->module->grabAnItemFromElasticsearch(null, null, null); + $this->assertEmpty($item); + } + public function setUp() { /** @var ModuleContainer container */ From 89bc996b374c55df2540f5b9d8cde87bd6402eb0 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:38:47 +0200 Subject: [PATCH 18/57] Marked dangerous test with fixme --- tests/ElasticSearchTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index a2b2ac2..9eb214d 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -52,6 +52,7 @@ public function shouldNotInstantiateWithoutHostsArrayInConfigArray() * @test * @expectedException \Exception * @expectedExceptionMessage Could not resolve host: test.3.1415.nonexistent-host.com + * @fixme Find a way to test this without having to rely on an exception from across the boundary */ public function initializeShouldCreateClientWithConfiguredHostsIfNoClientIsPassedToConstructor() { From 68924c034bc7a3bf03a473e0aa21714f4b5be4eb Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:41:39 +0200 Subject: [PATCH 19/57] Added test grabAnItemFromElasticsearchShouldReturnFirstSourceArrayIfThereIsAHits --- tests/ElasticSearchTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index 9eb214d..100699d 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -134,6 +134,17 @@ public function grabAnItemFromElasticsearchShouldReturnEmptyArrayIfThereAreNoHit $this->assertEmpty($item); } + /** + * @test + */ + public function grabAnItemFromElasticsearchShouldReturnFirstSourceArrayIfThereIsAHits() + { + $expectedItem = ['apples' => 1, 'oranges' => 2]; + $this->client->shouldReceive('search')->andReturn(['hits' => ['hits' => [['_source' => $expectedItem]]]]); + $actualItem = $this->module->grabAnItemFromElasticsearch(null, null, null); + $this->assertEquals($expectedItem, $actualItem); + } + public function setUp() { /** @var ModuleContainer container */ From 36614c395703f400661ff08a9b0f7e11a0ae8351 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:53:17 +0200 Subject: [PATCH 20/57] Added tests and exposed getHosts to let us get 100% test coverage on existing codebase --- src/ElasticSearch.php | 5 ++++- tests/ElasticSearchTest.php | 7 ++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index e0da73a..337ef06 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -89,5 +89,8 @@ public function grabAnItemFromElasticsearch($index = null, $type = null, $queryS : array(); } - + public function getHosts() + { + return $this->config['hosts']; + } } \ No newline at end of file diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index 100699d..3706f62 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -41,11 +41,12 @@ public function shouldNotInstantiateWithoutConfigArray() /** * @test - * @expectedException \Exception */ - public function shouldNotInstantiateWithoutHostsArrayInConfigArray() + public function shouldEncapsulateHostsInArrayIfNotEncapsulated() { - new ElasticSearch($this->container, ['hosts' => null]); + $module = new ElasticSearch($this->container, ['hosts' => 'test.host.com']); + $hosts = $module->getHosts(); + $this->assertEquals('test.host.com', $hosts[0]); } /** From eabc09febf787da1445267951d062e3b32024762 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:57:20 +0200 Subject: [PATCH 21/57] Removed noisy comment --- src/ElasticSearch.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 337ef06..28540e9 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -18,8 +18,6 @@ class ElasticSearch extends Module public function __construct(ModuleContainer $moduleContainer, $config = null, Client $client = null) { - // terminology: see = isXyz => true/false, have = create, grab = get => data - if (!isset($config['hosts'])) { throw new \Exception('please configure hosts for ElasticSearch codeception module'); } From 626417ac38243a9cf0aa7e1b33e73102508b4174 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 11:59:22 +0200 Subject: [PATCH 22/57] Extracted configureModuleWithSettingsInArray --- src/ElasticSearch.php | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 28540e9..0e52ab6 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -18,14 +18,7 @@ class ElasticSearch extends Module public function __construct(ModuleContainer $moduleContainer, $config = null, Client $client = null) { - if (!isset($config['hosts'])) { - throw new \Exception('please configure hosts for ElasticSearch codeception module'); - } - - if (isset($config['hosts']) && !is_array($config['hosts'])) { - $config['hosts'] = array($config['hosts']); - } - $this->config = (array)$config; + $this->configureModuleWithSettingsInArray($config); if (!is_null($client)) { $this->elasticSearch = $client; @@ -91,4 +84,20 @@ public function getHosts() { return $this->config['hosts']; } + + /** + * @param $configArray + * @throws \Exception + */ + private function configureModuleWithSettingsInArray($configArray) + { + if (!isset($configArray['hosts'])) { + throw new \Exception('please configure hosts for ElasticSearch codeception module'); + } + + if (isset($configArray['hosts']) && !is_array($configArray['hosts'])) { + $configArray['hosts'] = array($configArray['hosts']); + } + $this->config = (array)$configArray; + } } \ No newline at end of file From c550da90de559bada1f9f7a5dfb74ced9bd523f3 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:01:51 +0200 Subject: [PATCH 23/57] Extracted setElasticSearchClientIfInjected --- src/ElasticSearch.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 0e52ab6..440a4b9 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -19,10 +19,7 @@ class ElasticSearch extends Module public function __construct(ModuleContainer $moduleContainer, $config = null, Client $client = null) { $this->configureModuleWithSettingsInArray($config); - - if (!is_null($client)) { - $this->elasticSearch = $client; - } + $this->setElasticSearchClientIfInjected($client); parent::__construct($moduleContainer); } @@ -100,4 +97,14 @@ private function configureModuleWithSettingsInArray($configArray) } $this->config = (array)$configArray; } + + /** + * @param Client $client + */ + private function setElasticSearchClientIfInjected($client) + { + if (!is_null($client)) { + $this->elasticSearch = $client; + } + } } \ No newline at end of file From 43bd45b588544cae561f15de643e52abce2ee21c Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:02:11 +0200 Subject: [PATCH 24/57] Extracted class level constructorConfiguration --- src/ElasticSearch.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 440a4b9..f1ca70c 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -11,6 +11,7 @@ class ElasticSearch extends Module { + private $constructorConfiguration; /** * @var Client */ @@ -88,7 +89,8 @@ public function getHosts() */ private function configureModuleWithSettingsInArray($configArray) { - if (!isset($configArray['hosts'])) { + $this->constructorConfiguration = $configArray; + if (!isset($this->constructorConfiguration['hosts'])) { throw new \Exception('please configure hosts for ElasticSearch codeception module'); } From b0514f9f6b84d020f36fa249c97768f98ef6337c Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:05:53 +0200 Subject: [PATCH 25/57] Got rid of unneeded exta configuration array --- src/ElasticSearch.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index f1ca70c..d1ab082 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -11,7 +11,6 @@ class ElasticSearch extends Module { - private $constructorConfiguration; /** * @var Client */ @@ -89,15 +88,12 @@ public function getHosts() */ private function configureModuleWithSettingsInArray($configArray) { - $this->constructorConfiguration = $configArray; - if (!isset($this->constructorConfiguration['hosts'])) { - throw new \Exception('please configure hosts for ElasticSearch codeception module'); - } + $this->config = $configArray; + $this->guardThatConstructorConfigurationHasHosts(); - if (isset($configArray['hosts']) && !is_array($configArray['hosts'])) { - $configArray['hosts'] = array($configArray['hosts']); + if (!is_array($this->config['hosts'])) { + $this->config['hosts'] = array($this->config['hosts']); } - $this->config = (array)$configArray; } /** @@ -109,4 +105,11 @@ private function setElasticSearchClientIfInjected($client) $this->elasticSearch = $client; } } + + private function guardThatConstructorConfigurationHasHosts() + { + if (!isset($this->config['hosts'])) { + throw new \Exception('please configure hosts for ElasticSearch codeception module'); + } + } } \ No newline at end of file From 2d39178a5c502e0331d79db25600ca6c3f117c8d Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:06:26 +0200 Subject: [PATCH 26/57] Extracted wrapConfiguredHostsInArrayIfNeeded --- src/ElasticSearch.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index d1ab082..d0b64ba 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -91,9 +91,7 @@ private function configureModuleWithSettingsInArray($configArray) $this->config = $configArray; $this->guardThatConstructorConfigurationHasHosts(); - if (!is_array($this->config['hosts'])) { - $this->config['hosts'] = array($this->config['hosts']); - } + $this->wrapConfiguredHostsInArrayIfNeeded(); } /** @@ -112,4 +110,11 @@ private function guardThatConstructorConfigurationHasHosts() throw new \Exception('please configure hosts for ElasticSearch codeception module'); } } + + private function wrapConfiguredHostsInArrayIfNeeded() + { + if (!is_array($this->config['hosts'])) { + $this->config['hosts'] = array($this->config['hosts']); + } + } } \ No newline at end of file From aedd6f90ef2a8754d7057276f82f61688565387e Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:07:20 +0200 Subject: [PATCH 27/57] Renamed guardThatConfigurationHasHosts --- src/ElasticSearch.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index d0b64ba..a8f5f29 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -89,8 +89,7 @@ public function getHosts() private function configureModuleWithSettingsInArray($configArray) { $this->config = $configArray; - $this->guardThatConstructorConfigurationHasHosts(); - + $this->guardThatConfigurationHasHosts(); $this->wrapConfiguredHostsInArrayIfNeeded(); } @@ -104,7 +103,7 @@ private function setElasticSearchClientIfInjected($client) } } - private function guardThatConstructorConfigurationHasHosts() + private function guardThatConfigurationHasHosts() { if (!isset($this->config['hosts'])) { throw new \Exception('please configure hosts for ElasticSearch codeception module'); From 76c8ae2847b4cfce61660e301ca671f296a6f61b Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:08:04 +0200 Subject: [PATCH 28/57] Inlined configureModuleWithSettingsInArray --- src/ElasticSearch.php | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index a8f5f29..faa3281 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -18,7 +18,9 @@ class ElasticSearch extends Module public function __construct(ModuleContainer $moduleContainer, $config = null, Client $client = null) { - $this->configureModuleWithSettingsInArray($config); + $this->config = $config; + $this->guardThatConfigurationHasHosts(); + $this->wrapConfiguredHostsInArrayIfNeeded(); $this->setElasticSearchClientIfInjected($client); parent::__construct($moduleContainer); @@ -82,17 +84,6 @@ public function getHosts() return $this->config['hosts']; } - /** - * @param $configArray - * @throws \Exception - */ - private function configureModuleWithSettingsInArray($configArray) - { - $this->config = $configArray; - $this->guardThatConfigurationHasHosts(); - $this->wrapConfiguredHostsInArrayIfNeeded(); - } - /** * @param Client $client */ From 762385d2b057a48ea4db03ae7d05b2d07e5ba2c4 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:09:16 +0200 Subject: [PATCH 29/57] Extracted sanitizeModuleConfiguration --- src/ElasticSearch.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index faa3281..6a19c67 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -18,9 +18,8 @@ class ElasticSearch extends Module public function __construct(ModuleContainer $moduleContainer, $config = null, Client $client = null) { - $this->config = $config; - $this->guardThatConfigurationHasHosts(); - $this->wrapConfiguredHostsInArrayIfNeeded(); + $this->setModuleConfiguration($config); + $this->sanitizeModuleConfiguration(); $this->setElasticSearchClientIfInjected($client); parent::__construct($moduleContainer); @@ -107,4 +106,18 @@ private function wrapConfiguredHostsInArrayIfNeeded() $this->config['hosts'] = array($this->config['hosts']); } } + + /** + * @param $config + */ + private function setModuleConfiguration($config) + { + $this->config = $config; + } + + private function sanitizeModuleConfiguration() + { + $this->guardThatConfigurationHasHosts(); + $this->wrapConfiguredHostsInArrayIfNeeded(); + } } \ No newline at end of file From c346035455f6683d0f5a95e2b87828a16f387e56 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:10:02 +0200 Subject: [PATCH 30/57] Rearranged code --- src/ElasticSearch.php | 77 +++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 6a19c67..dc45059 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -25,6 +25,44 @@ public function __construct(ModuleContainer $moduleContainer, $config = null, Cl parent::__construct($moduleContainer); } + /** + * @param $config + */ + private function setModuleConfiguration($config) + { + $this->config = $config; + } + + private function sanitizeModuleConfiguration() + { + $this->guardThatConfigurationHasHosts(); + $this->wrapConfiguredHostsInArrayIfNeeded(); + } + + private function guardThatConfigurationHasHosts() + { + if (!isset($this->config['hosts'])) { + throw new \Exception('please configure hosts for ElasticSearch codeception module'); + } + } + + private function wrapConfiguredHostsInArrayIfNeeded() + { + if (!is_array($this->config['hosts'])) { + $this->config['hosts'] = array($this->config['hosts']); + } + } + + /** + * @param Client $client + */ + private function setElasticSearchClientIfInjected($client) + { + if (!is_null($client)) { + $this->elasticSearch = $client; + } + } + public function _initialize() { if (is_null($this->elasticSearch)) { @@ -52,7 +90,6 @@ public function seeItemExistsInElasticsearch($index, $type, $id) ); } - /** * grab an item from search index * @@ -82,42 +119,4 @@ public function getHosts() { return $this->config['hosts']; } - - /** - * @param Client $client - */ - private function setElasticSearchClientIfInjected($client) - { - if (!is_null($client)) { - $this->elasticSearch = $client; - } - } - - private function guardThatConfigurationHasHosts() - { - if (!isset($this->config['hosts'])) { - throw new \Exception('please configure hosts for ElasticSearch codeception module'); - } - } - - private function wrapConfiguredHostsInArrayIfNeeded() - { - if (!is_array($this->config['hosts'])) { - $this->config['hosts'] = array($this->config['hosts']); - } - } - - /** - * @param $config - */ - private function setModuleConfiguration($config) - { - $this->config = $config; - } - - private function sanitizeModuleConfiguration() - { - $this->guardThatConfigurationHasHosts(); - $this->wrapConfiguredHostsInArrayIfNeeded(); - } } \ No newline at end of file From 35f99afab77c057a4da371147dc33062c1e686bf Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:10:21 +0200 Subject: [PATCH 31/57] Extracted buildElasticSearchClientIfNotInjected --- src/ElasticSearch.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index dc45059..a93658b 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -65,9 +65,7 @@ private function setElasticSearchClientIfInjected($client) public function _initialize() { - if (is_null($this->elasticSearch)) { - $this->elasticSearch = new Client($this->config); - } + $this->buildElasticSearchClientIfNotInjected(); } /** @@ -119,4 +117,11 @@ public function getHosts() { return $this->config['hosts']; } + + private function buildElasticSearchClientIfNotInjected() + { + if (is_null($this->elasticSearch)) { + $this->elasticSearch = new Client($this->config); + } + } } \ No newline at end of file From 83136955ceec84414f51285528bff8a830c9f336 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:10:35 +0200 Subject: [PATCH 32/57] Rearranged code --- src/ElasticSearch.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index a93658b..32afd6a 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -68,6 +68,13 @@ public function _initialize() $this->buildElasticSearchClientIfNotInjected(); } + private function buildElasticSearchClientIfNotInjected() + { + if (is_null($this->elasticSearch)) { + $this->elasticSearch = new Client($this->config); + } + } + /** * check if an item exists in a given index * @@ -117,11 +124,4 @@ public function getHosts() { return $this->config['hosts']; } - - private function buildElasticSearchClientIfNotInjected() - { - if (is_null($this->elasticSearch)) { - $this->elasticSearch = new Client($this->config); - } - } } \ No newline at end of file From 3705507561323ce32266d203ea33d1c2c1f9d8d1 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:11:48 +0200 Subject: [PATCH 33/57] Extracted isEmptyResult --- src/ElasticSearch.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 32afd6a..bda8ec0 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -115,7 +115,7 @@ public function grabAnItemFromElasticsearch($index = null, $type = null, $queryS ] ); - return !empty($result['hits']['hits']) + return $this->isEmptyResult($result) ? $result['hits']['hits'][0]['_source'] : array(); } @@ -124,4 +124,13 @@ public function getHosts() { return $this->config['hosts']; } + + /** + * @param $result + * @return bool + */ + private function isEmptyResult($result) + { + return !empty($result['hits']['hits']); + } } \ No newline at end of file From 8fc6a22cac54d7b1e6ab4e6b3325b1b3e15c9b3e Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:13:46 +0200 Subject: [PATCH 34/57] Extracted isEmptyResult --- src/ElasticSearch.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index bda8ec0..600475b 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -115,9 +115,11 @@ public function grabAnItemFromElasticsearch($index = null, $type = null, $queryS ] ); - return $this->isEmptyResult($result) - ? $result['hits']['hits'][0]['_source'] - : array(); + if ($this->isEmptyResult($result)) { + return array(); + } + + return $result['hits']['hits'][0]['_source']; } public function getHosts() @@ -131,6 +133,6 @@ public function getHosts() */ private function isEmptyResult($result) { - return !empty($result['hits']['hits']); + return empty($result['hits']['hits']); } } \ No newline at end of file From 9d3bfac592e6113bc622911874f71f56d6038af9 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:14:51 +0200 Subject: [PATCH 35/57] Extracted getFirstItemFromResult --- src/ElasticSearch.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 600475b..b7fc230 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -119,7 +119,7 @@ public function grabAnItemFromElasticsearch($index = null, $type = null, $queryS return array(); } - return $result['hits']['hits'][0]['_source']; + return $this->getFirstItemFromResult($result); } public function getHosts() @@ -135,4 +135,13 @@ private function isEmptyResult($result) { return empty($result['hits']['hits']); } + + /** + * @param $result + * @return mixed + */ + private function getFirstItemFromResult($result) + { + return $result['hits']['hits'][0]['_source']; + } } \ No newline at end of file From 2e2bc6e7dfb54af49a825f8a87584ec6c2d0e6d7 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:17:48 +0200 Subject: [PATCH 36/57] Rearranged code --- src/ElasticSearch.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index b7fc230..408817e 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -122,11 +122,6 @@ public function grabAnItemFromElasticsearch($index = null, $type = null, $queryS return $this->getFirstItemFromResult($result); } - public function getHosts() - { - return $this->config['hosts']; - } - /** * @param $result * @return bool @@ -144,4 +139,9 @@ private function getFirstItemFromResult($result) { return $result['hits']['hits'][0]['_source']; } + + public function getHosts() + { + return $this->config['hosts']; + } } \ No newline at end of file From a596936f977efd59d4d6373ee26b33a171273065 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:18:24 +0200 Subject: [PATCH 37/57] Inlined buildElasticSearchClientIfNotInjected --- src/ElasticSearch.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 408817e..5dec08e 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -64,11 +64,6 @@ private function setElasticSearchClientIfInjected($client) } public function _initialize() - { - $this->buildElasticSearchClientIfNotInjected(); - } - - private function buildElasticSearchClientIfNotInjected() { if (is_null($this->elasticSearch)) { $this->elasticSearch = new Client($this->config); From 2dd28167b799cc21f433220a2f04e9b188fb7bcb Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:19:47 +0200 Subject: [PATCH 38/57] Extracted doesNotHaveElasticSearchClient --- src/ElasticSearch.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 5dec08e..d8ac87b 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -65,7 +65,7 @@ private function setElasticSearchClientIfInjected($client) public function _initialize() { - if (is_null($this->elasticSearch)) { + if ($this->doesNotHaveElasticSearchClient()) { $this->elasticSearch = new Client($this->config); } } @@ -139,4 +139,12 @@ public function getHosts() { return $this->config['hosts']; } + + /** + * @return bool + */ + private function doesNotHaveElasticSearchClient() + { + return is_null($this->elasticSearch); + } } \ No newline at end of file From 39261b6a907657460bb39570909c521cb24e2380 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:20:09 +0200 Subject: [PATCH 39/57] Extracted buildElasticSearchClient --- src/ElasticSearch.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index d8ac87b..b8219ba 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -66,7 +66,7 @@ private function setElasticSearchClientIfInjected($client) public function _initialize() { if ($this->doesNotHaveElasticSearchClient()) { - $this->elasticSearch = new Client($this->config); + $this->buildElasticSearchClient(); } } @@ -147,4 +147,9 @@ private function doesNotHaveElasticSearchClient() { return is_null($this->elasticSearch); } + + private function buildElasticSearchClient() + { + $this->elasticSearch = new Client($this->config); + } } \ No newline at end of file From b68912e10856b4137d145cb55b7ef9d9510fae2b Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:20:46 +0200 Subject: [PATCH 40/57] Renamed and cleaned up setElasticSearchClient --- src/ElasticSearch.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index b8219ba..0a40d21 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -20,7 +20,7 @@ public function __construct(ModuleContainer $moduleContainer, $config = null, Cl { $this->setModuleConfiguration($config); $this->sanitizeModuleConfiguration(); - $this->setElasticSearchClientIfInjected($client); + $this->setElasticSearchClient($client); parent::__construct($moduleContainer); } @@ -56,11 +56,9 @@ private function wrapConfiguredHostsInArrayIfNeeded() /** * @param Client $client */ - private function setElasticSearchClientIfInjected($client) + private function setElasticSearchClient($client) { - if (!is_null($client)) { - $this->elasticSearch = $client; - } + $this->elasticSearch = $client; } public function _initialize() From 36e04a6a37a28979c189593f0a32e329b974905d Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 12:22:29 +0200 Subject: [PATCH 41/57] Rearranged code --- src/ElasticSearch.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 0a40d21..67be807 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -68,6 +68,19 @@ public function _initialize() } } + /** + * @return bool + */ + private function doesNotHaveElasticSearchClient() + { + return is_null($this->elasticSearch); + } + + private function buildElasticSearchClient() + { + $this->elasticSearch = new Client($this->config); + } + /** * check if an item exists in a given index * @@ -137,17 +150,4 @@ public function getHosts() { return $this->config['hosts']; } - - /** - * @return bool - */ - private function doesNotHaveElasticSearchClient() - { - return is_null($this->elasticSearch); - } - - private function buildElasticSearchClient() - { - $this->elasticSearch = new Client($this->config); - } } \ No newline at end of file From 7d979dde69959ceb9e0aed4c64905d191568893b Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 14:12:04 +0200 Subject: [PATCH 42/57] Extracted ElasticSearchConfigurationTest and ElasticSearchTestCase --- tests/ElasticSearchConfigurationTest.php | 34 ++++++++++++++++ tests/ElasticSearchTest.php | 51 +----------------------- tests/ElasticSearchTestCase.php | 44 ++++++++++++++++++++ 3 files changed, 79 insertions(+), 50 deletions(-) create mode 100644 tests/ElasticSearchConfigurationTest.php create mode 100644 tests/ElasticSearchTestCase.php diff --git a/tests/ElasticSearchConfigurationTest.php b/tests/ElasticSearchConfigurationTest.php new file mode 100644 index 0000000..cd45e35 --- /dev/null +++ b/tests/ElasticSearchConfigurationTest.php @@ -0,0 +1,34 @@ +container, null); + } + + /** + * @test + */ + public function shouldEncapsulateHostsInArrayIfNotEncapsulated() + { + $module = new ElasticSearch($this->container, ['hosts' => 'test.host.com']); + $hosts = $module->getHosts(); + $this->assertEquals('test.host.com', $hosts[0]); + } +} \ No newline at end of file diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index 3706f62..5c07087 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -9,46 +9,11 @@ namespace Tests\Codeception\Module; -use Elasticsearch\Client; use Mockery as m; -use Codeception\Lib\ModuleContainer; use Codeception\Module\ElasticSearch; -class ElasticSearchTest extends \PHPUnit_Framework_TestCase +class ElasticSearchTest extends ElasticSearchTestCase { - /** - * @var ElasticSearch - */ - private $module; - /** - * @var Client | m\Mock - */ - private $client; - /** - * @var ModuleContainer | m\Mock - */ - private $container; - - /** - * @test - * @expectedException \Exception - * @expectedExceptionMessage please configure hosts for ElasticSearch codeception module - */ - public function shouldNotInstantiateWithoutConfigArray() - { - new ElasticSearch($this->container, null); - } - - /** - * @test - */ - public function shouldEncapsulateHostsInArrayIfNotEncapsulated() - { - $module = new ElasticSearch($this->container, ['hosts' => 'test.host.com']); - $hosts = $module->getHosts(); - $this->assertEquals('test.host.com', $hosts[0]); - } - /** * @test * @expectedException \Exception @@ -145,18 +110,4 @@ public function grabAnItemFromElasticsearchShouldReturnFirstSourceArrayIfThereIs $actualItem = $this->module->grabAnItemFromElasticsearch(null, null, null); $this->assertEquals($expectedItem, $actualItem); } - - public function setUp() - { - /** @var ModuleContainer container */ - $this->container = m::mock('\Codeception\Lib\ModuleContainer'); - $this->client = m::mock('\Elasticsearch\Client'); - $this->client->shouldIgnoreMissing(); - $this->module = new ElasticSearch($this->container, ['hosts' => []], $this->client); - } - - public function tearDown() - { - m::close(); - } } diff --git a/tests/ElasticSearchTestCase.php b/tests/ElasticSearchTestCase.php new file mode 100644 index 0000000..e4b64c1 --- /dev/null +++ b/tests/ElasticSearchTestCase.php @@ -0,0 +1,44 @@ +container = m::mock('\Codeception\Lib\ModuleContainer'); + $this->client = m::mock('\Elasticsearch\Client'); + $this->client->shouldIgnoreMissing(); + $this->module = new ElasticSearch($this->container, ['hosts' => []], $this->client); + } + + public function tearDown() + { + m::close(); + } +} \ No newline at end of file From 371388c3459fe554c63910fcd1761eabc7640942 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 14:12:57 +0200 Subject: [PATCH 43/57] Moved initialize test --- tests/ElasticSearchConfigurationTest.php | 13 +++++++++++++ tests/ElasticSearchTest.php | 14 -------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/tests/ElasticSearchConfigurationTest.php b/tests/ElasticSearchConfigurationTest.php index cd45e35..12f6dd9 100644 --- a/tests/ElasticSearchConfigurationTest.php +++ b/tests/ElasticSearchConfigurationTest.php @@ -31,4 +31,17 @@ public function shouldEncapsulateHostsInArrayIfNotEncapsulated() $hosts = $module->getHosts(); $this->assertEquals('test.host.com', $hosts[0]); } + + /** + * @test + * @expectedException \Exception + * @expectedExceptionMessage Could not resolve host: test.3.1415.nonexistent-host.com + * @fixme Find a way to test this without having to rely on an exception from across the boundary + */ + public function initializeShouldCreateClientWithConfiguredHostsIfNoClientIsPassedToConstructor() + { + $module = new ElasticSearch($this->container, ['hosts' => ['test.3.1415.nonexistent-host.com']]); + $module->_initialize(); + $module->seeItemExistsInElasticsearch('any-indexname', 'any-type', 'any-id'); + } } \ No newline at end of file diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index 5c07087..5c502e9 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -10,23 +10,9 @@ use Mockery as m; -use Codeception\Module\ElasticSearch; class ElasticSearchTest extends ElasticSearchTestCase { - /** - * @test - * @expectedException \Exception - * @expectedExceptionMessage Could not resolve host: test.3.1415.nonexistent-host.com - * @fixme Find a way to test this without having to rely on an exception from across the boundary - */ - public function initializeShouldCreateClientWithConfiguredHostsIfNoClientIsPassedToConstructor() - { - $module = new ElasticSearch($this->container, ['hosts' => ['test.3.1415.nonexistent-host.com']]); - $module->_initialize(); - $module->seeItemExistsInElasticsearch('any-indexname', 'any-type', 'any-id'); - } - /** * @test */ From bb3af042e4a615ee36660ebb7914e52a56e3b66a Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 14:14:49 +0200 Subject: [PATCH 44/57] Extracted ElasticSearch_seeItemExistsInElasticsearchTest --- tests/ElasticSearchTest.php | 27 ------------ ...earch_seeItemExistsInElasticsearchTest.php | 42 +++++++++++++++++++ 2 files changed, 42 insertions(+), 27 deletions(-) create mode 100644 tests/ElasticSearch_seeItemExistsInElasticsearchTest.php diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearchTest.php index 5c502e9..4525c54 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearchTest.php @@ -13,33 +13,6 @@ class ElasticSearchTest extends ElasticSearchTestCase { - /** - * @test - */ - public function seeItemExistsInElasticsearchShouldCallExistsWithIndexNameOnClient() - { - $this->module->seeItemExistsInElasticsearch('index-name', null, null); - $this->client->shouldHaveReceived('exists')->with(m::subset(['index' => 'index-name']))->once(); - } - - /** - * @test - */ - public function seeItemExistsInElasticsearchShouldCallExistsWithTypeOnClient() - { - $this->module->seeItemExistsInElasticsearch(null, 'document-type', null); - $this->client->shouldHaveReceived('exists')->with(m::subset(['type' => 'document-type']))->once(); - } - - /** - * @test - */ - public function seeItemExistsInElasticsearchShouldCallExistsWithIdOnClient() - { - $this->module->seeItemExistsInElasticsearch(null, null, 'document-id'); - $this->client->shouldHaveReceived('exists')->with(m::subset(['id' => 'document-id']))->once(); - } - /** * @test */ diff --git a/tests/ElasticSearch_seeItemExistsInElasticsearchTest.php b/tests/ElasticSearch_seeItemExistsInElasticsearchTest.php new file mode 100644 index 0000000..c32e6bc --- /dev/null +++ b/tests/ElasticSearch_seeItemExistsInElasticsearchTest.php @@ -0,0 +1,42 @@ +module->seeItemExistsInElasticsearch('index-name', null, null); + $this->client->shouldHaveReceived('exists')->with(m::subset(['index' => 'index-name']))->once(); + } + + /** + * @test + */ + public function seeItemExistsInElasticsearchShouldCallExistsWithTypeOnClient() + { + $this->module->seeItemExistsInElasticsearch(null, 'document-type', null); + $this->client->shouldHaveReceived('exists')->with(m::subset(['type' => 'document-type']))->once(); + } + + /** + * @test + */ + public function seeItemExistsInElasticsearchShouldCallExistsWithIdOnClient() + { + $this->module->seeItemExistsInElasticsearch(null, null, 'document-id'); + $this->client->shouldHaveReceived('exists')->with(m::subset(['id' => 'document-id']))->once(); + } +} \ No newline at end of file From f5cca8c872ae0835e109e35cb5f4a3b20020f073 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 14:15:37 +0200 Subject: [PATCH 45/57] Extracted ElasticSearch_grabAnItemFromElasticsearchTest --- ...st.php => ElasticSearch_grabAnItemFromElasticsearchTest.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/{ElasticSearchTest.php => ElasticSearch_grabAnItemFromElasticsearchTest.php} (96%) diff --git a/tests/ElasticSearchTest.php b/tests/ElasticSearch_grabAnItemFromElasticsearchTest.php similarity index 96% rename from tests/ElasticSearchTest.php rename to tests/ElasticSearch_grabAnItemFromElasticsearchTest.php index 4525c54..acf020f 100644 --- a/tests/ElasticSearchTest.php +++ b/tests/ElasticSearch_grabAnItemFromElasticsearchTest.php @@ -11,7 +11,7 @@ use Mockery as m; -class ElasticSearchTest extends ElasticSearchTestCase +class ElasticSearch_grabAnItemFromElasticsearchTest extends ElasticSearchTestCase { /** * @test From 27825557e7acef3bb8ad8966bfa0c7341990dca9 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 14:31:53 +0200 Subject: [PATCH 46/57] Create Elastic Search index --- src/ElasticSearch.php | 5 ++++ ...cSearch_createIndexInElasticsearchTest.php | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 tests/ElasticSearch_createIndexInElasticsearchTest.php diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 67be807..20027fe 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -150,4 +150,9 @@ public function getHosts() { return $this->config['hosts']; } + + public function createIndexInElasticsearch($indexName) + { + $this->elasticSearch->indices()->create(['index' => $indexName]); + } } \ No newline at end of file diff --git a/tests/ElasticSearch_createIndexInElasticsearchTest.php b/tests/ElasticSearch_createIndexInElasticsearchTest.php new file mode 100644 index 0000000..1fe60b8 --- /dev/null +++ b/tests/ElasticSearch_createIndexInElasticsearchTest.php @@ -0,0 +1,29 @@ +shouldIgnoreMissing(); + $this->client->shouldReceive('indices')->andReturn($indicesNamespace); + + $this->module->createIndexInElasticsearch('index-name'); + + $indicesNamespace->shouldHaveReceived('create')->with(m::subset(['index' => 'index-name'])); + } +} From af652b17768c0bc3255cf58ead7536d624ee7d87 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 14:33:53 +0200 Subject: [PATCH 47/57] Extracted setUp function --- ...icSearch_createIndexInElasticsearchTest.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tests/ElasticSearch_createIndexInElasticsearchTest.php b/tests/ElasticSearch_createIndexInElasticsearchTest.php index 1fe60b8..0d7ceb7 100644 --- a/tests/ElasticSearch_createIndexInElasticsearchTest.php +++ b/tests/ElasticSearch_createIndexInElasticsearchTest.php @@ -9,21 +9,29 @@ namespace Tests\Codeception\Module; +use Elasticsearch\Namespaces\IndicesNamespace; use Mockery as m; class ElasticSearch_createIndexInElasticsearchTest extends ElasticSearchTestCase { + /** @var IndicesNamespace | m\Mock */ + private $indicesNamespace; + /** * @test */ public function createIndexInElasticsearchShouldCallIndicesCreateOnClientWithIndexName() { - $indicesNamespace = m::mock('\Elasticsearch\Namespaces\IndicesNamespace'); - $indicesNamespace->shouldIgnoreMissing(); - $this->client->shouldReceive('indices')->andReturn($indicesNamespace); - + $this->client->shouldReceive('indices')->andReturn($this->indicesNamespace); $this->module->createIndexInElasticsearch('index-name'); + $this->indicesNamespace->shouldHaveReceived('create')->with(m::subset(['index' => 'index-name'])); + } + + public function setUp() + { + parent::setUp(); - $indicesNamespace->shouldHaveReceived('create')->with(m::subset(['index' => 'index-name'])); + $this->indicesNamespace = m::mock('\Elasticsearch\Namespaces\IndicesNamespace'); + $this->indicesNamespace->shouldIgnoreMissing(); } } From 8f7acabb9f308705d62eb356ef4eded6523396b2 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 14:35:29 +0200 Subject: [PATCH 48/57] Renamed ElasticSearchIndexManagementTest --- ...asticsearchTest.php => ElasticSearchIndexManagementTest.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/{ElasticSearch_createIndexInElasticsearchTest.php => ElasticSearchIndexManagementTest.php} (91%) diff --git a/tests/ElasticSearch_createIndexInElasticsearchTest.php b/tests/ElasticSearchIndexManagementTest.php similarity index 91% rename from tests/ElasticSearch_createIndexInElasticsearchTest.php rename to tests/ElasticSearchIndexManagementTest.php index 0d7ceb7..5f50f3a 100644 --- a/tests/ElasticSearch_createIndexInElasticsearchTest.php +++ b/tests/ElasticSearchIndexManagementTest.php @@ -12,7 +12,7 @@ use Elasticsearch\Namespaces\IndicesNamespace; use Mockery as m; -class ElasticSearch_createIndexInElasticsearchTest extends ElasticSearchTestCase +class ElasticSearchIndexManagementTest extends ElasticSearchTestCase { /** @var IndicesNamespace | m\Mock */ private $indicesNamespace; From dbed8b1aa33cb36816c83e02306409b384a29e89 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 14:35:50 +0200 Subject: [PATCH 49/57] Moved some instrumentation to setup --- tests/ElasticSearchIndexManagementTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ElasticSearchIndexManagementTest.php b/tests/ElasticSearchIndexManagementTest.php index 5f50f3a..1bc659d 100644 --- a/tests/ElasticSearchIndexManagementTest.php +++ b/tests/ElasticSearchIndexManagementTest.php @@ -22,7 +22,6 @@ class ElasticSearchIndexManagementTest extends ElasticSearchTestCase */ public function createIndexInElasticsearchShouldCallIndicesCreateOnClientWithIndexName() { - $this->client->shouldReceive('indices')->andReturn($this->indicesNamespace); $this->module->createIndexInElasticsearch('index-name'); $this->indicesNamespace->shouldHaveReceived('create')->with(m::subset(['index' => 'index-name'])); } @@ -33,5 +32,6 @@ public function setUp() $this->indicesNamespace = m::mock('\Elasticsearch\Namespaces\IndicesNamespace'); $this->indicesNamespace->shouldIgnoreMissing(); + $this->client->shouldReceive('indices')->andReturn($this->indicesNamespace)->byDefault(); } } From 702cde0676da23f017ee7a0a08f13e832780b7f7 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 14:36:43 +0200 Subject: [PATCH 50/57] Renamed createIndexInElasticsearchShouldCallCreateOnClientIndicesWithIndexName --- tests/ElasticSearchIndexManagementTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ElasticSearchIndexManagementTest.php b/tests/ElasticSearchIndexManagementTest.php index 1bc659d..115143a 100644 --- a/tests/ElasticSearchIndexManagementTest.php +++ b/tests/ElasticSearchIndexManagementTest.php @@ -20,7 +20,7 @@ class ElasticSearchIndexManagementTest extends ElasticSearchTestCase /** * @test */ - public function createIndexInElasticsearchShouldCallIndicesCreateOnClientWithIndexName() + public function createIndexInElasticsearchShouldCallCreateOnClientIndicesWithIndexName() { $this->module->createIndexInElasticsearch('index-name'); $this->indicesNamespace->shouldHaveReceived('create')->with(m::subset(['index' => 'index-name'])); From ecef7125b556f246ba8d0869b9ca5efad4791e87 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 14:38:12 +0200 Subject: [PATCH 51/57] Delete index in ElasticSearch --- src/ElasticSearch.php | 5 +++++ tests/ElasticSearchIndexManagementTest.php | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 20027fe..f6ee759 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -155,4 +155,9 @@ public function createIndexInElasticsearch($indexName) { $this->elasticSearch->indices()->create(['index' => $indexName]); } + + public function deleteIndexInElasticsearch($indexName) + { + $this->elasticSearch->indices()->delete(['index' => $indexName]); + } } \ No newline at end of file diff --git a/tests/ElasticSearchIndexManagementTest.php b/tests/ElasticSearchIndexManagementTest.php index 115143a..50d1048 100644 --- a/tests/ElasticSearchIndexManagementTest.php +++ b/tests/ElasticSearchIndexManagementTest.php @@ -26,6 +26,15 @@ public function createIndexInElasticsearchShouldCallCreateOnClientIndicesWithInd $this->indicesNamespace->shouldHaveReceived('create')->with(m::subset(['index' => 'index-name'])); } + /** + * @test + */ + public function deleteIndexInElasticsearchShouldCallDeleteOnClientIndicesWithIndexName() + { + $this->module->deleteIndexInElasticsearch('index-name'); + $this->indicesNamespace->shouldHaveReceived('delete')->with(m::subset(['index' => 'index-name'])); + } + public function setUp() { parent::setUp(); From 8fb1c821609797ae73c293a26e0e1b4f01d2885f Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 14:54:14 +0200 Subject: [PATCH 52/57] See if index exists --- src/ElasticSearch.php | 5 ++++ tests/ElasticSearchIndexManagementTest.php | 29 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index f6ee759..36f3741 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -160,4 +160,9 @@ public function deleteIndexInElasticsearch($indexName) { $this->elasticSearch->indices()->delete(['index' => $indexName]); } + + public function seeIndexExistsInElasticsearch($indexName) + { + $this->assertTrue($this->elasticSearch->indices()->exists(['index' => $indexName])); + } } \ No newline at end of file diff --git a/tests/ElasticSearchIndexManagementTest.php b/tests/ElasticSearchIndexManagementTest.php index 50d1048..88102ed 100644 --- a/tests/ElasticSearchIndexManagementTest.php +++ b/tests/ElasticSearchIndexManagementTest.php @@ -35,11 +35,40 @@ public function deleteIndexInElasticsearchShouldCallDeleteOnClientIndicesWithInd $this->indicesNamespace->shouldHaveReceived('delete')->with(m::subset(['index' => 'index-name'])); } + /** + * @test + */ + public function seeIndexExistsInElasticsearchShouldCallExistsOnClientIndicesWithIndexName() + { + $this->module->seeIndexExistsInElasticsearch('index-name'); + $this->indicesNamespace->shouldHaveReceived('exists')->with(m::subset(['index' => 'index-name'])); + } + + /** + * @test + */ + public function seeIndexExistsInElasticsearchShouldDoNothingIfIndexExists() + { + $this->indicesNamespace->shouldReceive('exists')->andReturn(true); + $this->module->seeIndexExistsInElasticsearch('index-name'); + } + + /** + * @test + * @expectedException \PHPUnit_Framework_ExpectationFailedException + */ + public function seeIndexExistsInElasticsearchShouldHaveFailingAssertionIfIndexDoesNot() + { + $this->indicesNamespace->shouldReceive('exists')->andReturn(false); + $this->module->seeIndexExistsInElasticsearch('index-name'); + } + public function setUp() { parent::setUp(); $this->indicesNamespace = m::mock('\Elasticsearch\Namespaces\IndicesNamespace'); + $this->indicesNamespace->shouldReceive('exists')->andReturn(true)->byDefault(); $this->indicesNamespace->shouldIgnoreMissing(); $this->client->shouldReceive('indices')->andReturn($this->indicesNamespace)->byDefault(); } From a19aa618e23bf2cf598b90dd6ab669244d46bb62 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 15:01:34 +0200 Subject: [PATCH 53/57] Check that index does not exist in ElasticSearch --- src/ElasticSearch.php | 5 +++++ tests/ElasticSearchIndexManagementTest.php | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 36f3741..aa7ebf6 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -165,4 +165,9 @@ public function seeIndexExistsInElasticsearch($indexName) { $this->assertTrue($this->elasticSearch->indices()->exists(['index' => $indexName])); } + + public function dontSeeIndexExistsInElasticsearch($indexName) + { + $this->assertFalse($this->elasticSearch->indices()->exists(['index' => $indexName])); + } } \ No newline at end of file diff --git a/tests/ElasticSearchIndexManagementTest.php b/tests/ElasticSearchIndexManagementTest.php index 88102ed..603795c 100644 --- a/tests/ElasticSearchIndexManagementTest.php +++ b/tests/ElasticSearchIndexManagementTest.php @@ -63,6 +63,25 @@ public function seeIndexExistsInElasticsearchShouldHaveFailingAssertionIfIndexDo $this->module->seeIndexExistsInElasticsearch('index-name'); } + /** + * @test + */ + public function dontSeeIndexExistsInElasticsearchShouldDoNothingIfIndexDoesNotExist() + { + $this->indicesNamespace->shouldReceive('exists')->andReturn(false); + $this->module->dontSeeIndexExistsInElasticsearch('index-name'); + } + + /** + * @test + * @expectedException \PHPUnit_Framework_ExpectationFailedException + */ + public function dontSeeIndexExistsInElasticsearchShouldHaveFailingAssertionIfIndexExists() + { + $this->indicesNamespace->shouldReceive('exists')->andReturn(true); + $this->module->dontSeeIndexExistsInElasticsearch('index-name'); + } + public function setUp() { parent::setUp(); From 7fd29098d4043f8369e7972340fa4b7a569a1988 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 15:04:55 +0200 Subject: [PATCH 54/57] Fixed composer requires to enforce right version of codeception --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 5f5ddd6..8724b73 100644 --- a/composer.json +++ b/composer.json @@ -13,11 +13,11 @@ } ], "require": { - "elasticsearch/elasticsearch": "~1.0" + "elasticsearch/elasticsearch": "~1.0", + "codeception/codeception": "^2.1" }, "require-dev": { "phpunit/phpunit": "~4.8", - "codeception/codeception": "*", "mockery/mockery": "~0.9" }, "autoload": { From 79139dc08a5765d5671df6ec493294458bb4aa70 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 15:19:49 +0200 Subject: [PATCH 55/57] Index an item in ElasticSearch --- src/ElasticSearch.php | 15 +++++++ tests/ElasticSearchIndexingTest.php | 67 +++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 tests/ElasticSearchIndexingTest.php diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index aa7ebf6..9add197 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -170,4 +170,19 @@ public function dontSeeIndexExistsInElasticsearch($indexName) { $this->assertFalse($this->elasticSearch->indices()->exists(['index' => $indexName])); } + + public function indexAnItemInElasticSearch($indexName, $documentType, $documentBody, $id = null) + { + $params = [ + 'index' => $indexName, + 'type' => $documentType, + 'body' => $documentBody + ]; + + if (!is_null($id)) { + $params['id'] = $id; + } + + $this->elasticSearch->index($params); + } } \ No newline at end of file diff --git a/tests/ElasticSearchIndexingTest.php b/tests/ElasticSearchIndexingTest.php new file mode 100644 index 0000000..8082fca --- /dev/null +++ b/tests/ElasticSearchIndexingTest.php @@ -0,0 +1,67 @@ +module->indexAnItemInElasticSearch('index-name', null, null); + $this->client->shouldHaveReceived('index')->with(m::subset(['index' => 'index-name'])); + } + + /** + * @test + */ + public function indexAnItemInElasticsearchShouldCallIndexOnClientWithType() + { + $this->module->indexAnItemInElasticSearch(null, 'document-type', null); + $this->client->shouldHaveReceived('index')->with(m::subset(['type' => 'document-type'])); + } + + /** + * @test + */ + public function indexAnItemInElasticsearchShouldCallIndexOnClientWithIdIfSpecified() + { + $this->module->indexAnItemInElasticSearch(null, null, null, 123); + $this->client->shouldHaveReceived('index')->with(m::subset(['id' => 123])); + } + + /** + * @test + */ + public function indexAnItemInElasticsearchShouldCallIndexOnClientWithoutIdIfNotSpecified() + { + $this->module->indexAnItemInElasticSearch(null, null, null); + $this->client->shouldHaveReceived('index')->with(m::on(function ($actual) { + return !array_key_exists('id', $actual); + })); + } + + /** + * @test + */ + public function indexAnItemInElasticsearchShouldCallIndexOnClientWithDocumentBody() + { + $documentBody = [ + 'apples' => 1, + 'oranges' => 2 + ]; + + $this->module->indexAnItemInElasticSearch(null, null, $documentBody); + $this->client->shouldHaveReceived('index')->with(m::subset(['body' => $documentBody])); + } +} From 94c366ed938eb7802067cfd2dfcc4bec559977d1 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 15:22:00 +0200 Subject: [PATCH 56/57] Improved spelling --- src/ElasticSearch.php | 12 ++++++------ tests/ElasticSearchIndexingTest.php | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 9add197..34fa9e8 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -90,7 +90,7 @@ private function buildElasticSearchClient() * * @return array */ - public function seeItemExistsInElasticsearch($index, $type, $id) + public function seeItemExistsInElasticSearch($index, $type, $id) { return $this->elasticSearch->exists( [ @@ -110,7 +110,7 @@ public function seeItemExistsInElasticsearch($index, $type, $id) * * @return array */ - public function grabAnItemFromElasticsearch($index = null, $type = null, $queryString = '*') + public function grabAnItemFromElasticSearch($index = null, $type = null, $queryString = '*') { $result = $this->elasticSearch->search( [ @@ -151,22 +151,22 @@ public function getHosts() return $this->config['hosts']; } - public function createIndexInElasticsearch($indexName) + public function createIndexInElasticSearch($indexName) { $this->elasticSearch->indices()->create(['index' => $indexName]); } - public function deleteIndexInElasticsearch($indexName) + public function deleteIndexInElasticSearch($indexName) { $this->elasticSearch->indices()->delete(['index' => $indexName]); } - public function seeIndexExistsInElasticsearch($indexName) + public function seeIndexExistsInElasticSearch($indexName) { $this->assertTrue($this->elasticSearch->indices()->exists(['index' => $indexName])); } - public function dontSeeIndexExistsInElasticsearch($indexName) + public function dontSeeIndexExistsInElasticSearch($indexName) { $this->assertFalse($this->elasticSearch->indices()->exists(['index' => $indexName])); } diff --git a/tests/ElasticSearchIndexingTest.php b/tests/ElasticSearchIndexingTest.php index 8082fca..fe8e03d 100644 --- a/tests/ElasticSearchIndexingTest.php +++ b/tests/ElasticSearchIndexingTest.php @@ -16,7 +16,7 @@ class ElasticSearchIndexingTest extends ElasticSearchTestCase /** * @test */ - public function indexAnItemInElasticsearchShouldCallIndexOnClientWithIndexName() + public function indexAnItemInElasticSearchShouldCallIndexOnClientWithIndexName() { $this->module->indexAnItemInElasticSearch('index-name', null, null); $this->client->shouldHaveReceived('index')->with(m::subset(['index' => 'index-name'])); @@ -25,7 +25,7 @@ public function indexAnItemInElasticsearchShouldCallIndexOnClientWithIndexName() /** * @test */ - public function indexAnItemInElasticsearchShouldCallIndexOnClientWithType() + public function indexAnItemInElasticSearchShouldCallIndexOnClientWithType() { $this->module->indexAnItemInElasticSearch(null, 'document-type', null); $this->client->shouldHaveReceived('index')->with(m::subset(['type' => 'document-type'])); @@ -34,7 +34,7 @@ public function indexAnItemInElasticsearchShouldCallIndexOnClientWithType() /** * @test */ - public function indexAnItemInElasticsearchShouldCallIndexOnClientWithIdIfSpecified() + public function indexAnItemInElasticSearchShouldCallIndexOnClientWithIdIfSpecified() { $this->module->indexAnItemInElasticSearch(null, null, null, 123); $this->client->shouldHaveReceived('index')->with(m::subset(['id' => 123])); @@ -43,7 +43,7 @@ public function indexAnItemInElasticsearchShouldCallIndexOnClientWithIdIfSpecifi /** * @test */ - public function indexAnItemInElasticsearchShouldCallIndexOnClientWithoutIdIfNotSpecified() + public function indexAnItemInElasticSearchShouldCallIndexOnClientWithoutIdIfNotSpecified() { $this->module->indexAnItemInElasticSearch(null, null, null); $this->client->shouldHaveReceived('index')->with(m::on(function ($actual) { @@ -54,7 +54,7 @@ public function indexAnItemInElasticsearchShouldCallIndexOnClientWithoutIdIfNotS /** * @test */ - public function indexAnItemInElasticsearchShouldCallIndexOnClientWithDocumentBody() + public function indexAnItemInElasticSearchShouldCallIndexOnClientWithDocumentBody() { $documentBody = [ 'apples' => 1, From c5f64bad46a92c34723e0edbb3fec852ce81b202 Mon Sep 17 00:00:00 2001 From: willemv Date: Tue, 24 Nov 2015 15:30:10 +0200 Subject: [PATCH 57/57] Fixed name in composer.json to enable VCS repository override in dependant projects --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8724b73..34c1c67 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "willemnviljoen/codeception-elasticsearch", + "name": "jotweh/codeception-elasticsearch", "description": "Elastic Search Module for Codeception", "license": "gpl", "authors": [