Loading phpunit.xml.dist +1 −0 Original line number Diff line number Diff line Loading @@ -20,5 +20,6 @@ </coverage> <php> <env name="IQ_ES_PRODUCT_SEARCH_INDEX" value="product-test"/> <env name="IQ_ES_PRODUCT_SEARCH_INDEX_CHANGING_STATE" value="product-test-changing-state"/> </php> </phpunit> No newline at end of file src/ElasticSearch/Helper/ArrayHelper.php +3 −1 Original line number Diff line number Diff line Loading @@ -15,12 +15,14 @@ class ArrayHelper public static function array_filter_recursive(array $array, ?callable $callback = null): array { $array = is_callable($callback) ? array_filter($array, $callback) : array_filter($array); foreach ($array as &$value) { foreach ($array as $key => &$value) { if (is_array($value)) { $value = call_user_func([__CLASS__, __FUNCTION__], $value, $callback); if (!empty($value)) { $value = self::array_filter_recursive($value, $callback); } else { unset($array[$key]); } } } Loading src/ElasticSearch/Indexer/IndexRunner.php +6 −2 Original line number Diff line number Diff line Loading @@ -74,11 +74,15 @@ final class IndexRunner continue; } if (!($index instanceof AddIndex) && !($index instanceof UpdateIndex)) { if ($index instanceof AddIndex) { $this->esClient->index($index->es()); continue; } $this->esClient->index($index->es()); if ($index instanceof UpdateIndex) { $this->esClient->update($index->es()); continue; } } } } Loading tests/Config/ChangingStateConfiguration.php 0 → 100644 +13 −0 Original line number Diff line number Diff line <?php namespace IQDEV\ElasticSearchTests\Config; use IQDEV\ElasticSearch\Config\BaseConfiguration; class ChangingStateConfiguration extends BaseConfiguration { public function getIndexName(): string { return $_ENV['IQ_ES_PRODUCT_SEARCH_INDEX_CHANGING_STATE']; } } No newline at end of file tests/FIlter/IndexesTest.php 0 → 100644 +83 −0 Original line number Diff line number Diff line <?php namespace IQDEV\ElasticSearchTests\Filter; use Elastic\Elasticsearch\Client; use IQDEV\ElasticSearch\Configuration; use IQDEV\ElasticSearch\Converter\EsResponseToResult; use IQDEV\ElasticSearch\Indexer\IndexRunner; use IQDEV\ElasticSearchTests\AbstractTestCase; use IQDEV\ElasticSearchTests\Config\ChangingStateConfiguration; use IQDEV\ElasticSearchTests\Factory\ClientFactory; use IQDEV\ElasticSearchTests\Helpers\FormatData; use IQDEV\ElasticSearchTests\Helpers\TestIndexProvider; use Psr\Log\Test\TestLogger; class IndexesTest extends AbstractTestCase { private array $product = [ 'id' => 'test', 'name' => 'Test товар', 'category' => 'indexes', 'properties' => [ 'prop1' => 'value1', 'prop2' => 'value2', 'prop3' => 'value3', ] ]; private IndexRunner $indexRunner; private Client $esClient; private Configuration $configuration; public function __construct(?string $name = null, array $data = [], $dataName = '') { parent::__construct($name, $data, $dataName); $this->configuration = new ChangingStateConfiguration(); $this->esClient = ClientFactory::create(); $this->indexRunner = new IndexRunner( $this->esClient, $this->configuration, new TestLogger() ); } public function testUpdate() { $indexProvider = new TestIndexProvider($this->configuration, [$this->product]); $this->indexRunner->run($indexProvider); $updateData = [ 'id' => $this->product['id'], 'category' => $this->product['category'], 'type' => 'update', 'name' => 'Обновленный элемент' ]; $indexProvider = new TestIndexProvider($this->configuration, [$updateData]); $this->indexRunner->run($indexProvider); $response = $this->esClient->search([ 'index' => $this->configuration->getIndexName(), 'body' => [ 'query' => [ 'match' => [ '_id' => $this->product['id'] ], ] ] ]); $esResponseToResult = new EsResponseToResult(); $result = $esResponseToResult->fromResponse($response); unset($updateData['type']); $expected = [ 'products' => [ array_merge($this->product, $updateData) ] ]; $this->assertEqualsCanonicalizing($expected, FormatData::formatDataProducts($result)); } } No newline at end of file Loading
phpunit.xml.dist +1 −0 Original line number Diff line number Diff line Loading @@ -20,5 +20,6 @@ </coverage> <php> <env name="IQ_ES_PRODUCT_SEARCH_INDEX" value="product-test"/> <env name="IQ_ES_PRODUCT_SEARCH_INDEX_CHANGING_STATE" value="product-test-changing-state"/> </php> </phpunit> No newline at end of file
src/ElasticSearch/Helper/ArrayHelper.php +3 −1 Original line number Diff line number Diff line Loading @@ -15,12 +15,14 @@ class ArrayHelper public static function array_filter_recursive(array $array, ?callable $callback = null): array { $array = is_callable($callback) ? array_filter($array, $callback) : array_filter($array); foreach ($array as &$value) { foreach ($array as $key => &$value) { if (is_array($value)) { $value = call_user_func([__CLASS__, __FUNCTION__], $value, $callback); if (!empty($value)) { $value = self::array_filter_recursive($value, $callback); } else { unset($array[$key]); } } } Loading
src/ElasticSearch/Indexer/IndexRunner.php +6 −2 Original line number Diff line number Diff line Loading @@ -74,11 +74,15 @@ final class IndexRunner continue; } if (!($index instanceof AddIndex) && !($index instanceof UpdateIndex)) { if ($index instanceof AddIndex) { $this->esClient->index($index->es()); continue; } $this->esClient->index($index->es()); if ($index instanceof UpdateIndex) { $this->esClient->update($index->es()); continue; } } } } Loading
tests/Config/ChangingStateConfiguration.php 0 → 100644 +13 −0 Original line number Diff line number Diff line <?php namespace IQDEV\ElasticSearchTests\Config; use IQDEV\ElasticSearch\Config\BaseConfiguration; class ChangingStateConfiguration extends BaseConfiguration { public function getIndexName(): string { return $_ENV['IQ_ES_PRODUCT_SEARCH_INDEX_CHANGING_STATE']; } } No newline at end of file
tests/FIlter/IndexesTest.php 0 → 100644 +83 −0 Original line number Diff line number Diff line <?php namespace IQDEV\ElasticSearchTests\Filter; use Elastic\Elasticsearch\Client; use IQDEV\ElasticSearch\Configuration; use IQDEV\ElasticSearch\Converter\EsResponseToResult; use IQDEV\ElasticSearch\Indexer\IndexRunner; use IQDEV\ElasticSearchTests\AbstractTestCase; use IQDEV\ElasticSearchTests\Config\ChangingStateConfiguration; use IQDEV\ElasticSearchTests\Factory\ClientFactory; use IQDEV\ElasticSearchTests\Helpers\FormatData; use IQDEV\ElasticSearchTests\Helpers\TestIndexProvider; use Psr\Log\Test\TestLogger; class IndexesTest extends AbstractTestCase { private array $product = [ 'id' => 'test', 'name' => 'Test товар', 'category' => 'indexes', 'properties' => [ 'prop1' => 'value1', 'prop2' => 'value2', 'prop3' => 'value3', ] ]; private IndexRunner $indexRunner; private Client $esClient; private Configuration $configuration; public function __construct(?string $name = null, array $data = [], $dataName = '') { parent::__construct($name, $data, $dataName); $this->configuration = new ChangingStateConfiguration(); $this->esClient = ClientFactory::create(); $this->indexRunner = new IndexRunner( $this->esClient, $this->configuration, new TestLogger() ); } public function testUpdate() { $indexProvider = new TestIndexProvider($this->configuration, [$this->product]); $this->indexRunner->run($indexProvider); $updateData = [ 'id' => $this->product['id'], 'category' => $this->product['category'], 'type' => 'update', 'name' => 'Обновленный элемент' ]; $indexProvider = new TestIndexProvider($this->configuration, [$updateData]); $this->indexRunner->run($indexProvider); $response = $this->esClient->search([ 'index' => $this->configuration->getIndexName(), 'body' => [ 'query' => [ 'match' => [ '_id' => $this->product['id'] ], ] ] ]); $esResponseToResult = new EsResponseToResult(); $result = $esResponseToResult->fromResponse($response); unset($updateData['type']); $expected = [ 'products' => [ array_merge($this->product, $updateData) ] ]; $this->assertEqualsCanonicalizing($expected, FormatData::formatDataProducts($result)); } } No newline at end of file