From b3bd64c4681e9d91ce8867598749d24210acacee Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Tue, 19 Apr 2022 07:44:43 +0300 Subject: [PATCH 1/5] elasticsearch 8 upgrade --- composer.json | 4 ++-- src/Console/Commands/FlushCommand.php | 1 + src/ElasticSearch/Params/Bulk.php | 2 -- src/ElasticSearchServiceProvider.php | 4 ++-- src/Engines/ElasticSearchEngine.php | 16 ++++++++-------- src/Jobs/Import.php | 2 +- src/Jobs/Stages/CleanUp.php | 9 ++++----- src/Jobs/Stages/CreateWriteIndex.php | 2 +- src/Jobs/Stages/RefreshIndex.php | 2 +- src/Jobs/Stages/SwitchToNewAndRemoveOldIndex.php | 4 ++-- src/ScoutElasticSearchServiceProvider.php | 2 +- tests/Feature/FlushCommandTest.php | 1 + tests/Feature/ImportCommandTest.php | 2 +- .../ScoutElasticSearchServiceProviderTest.php | 2 +- tests/Integration/Jobs/Stages/CleanUpTest.php | 4 ++-- .../Jobs/Stages/CreateWriteIndexTest.php | 5 +++-- .../Integration/Jobs/Stages/RefreshIndexTest.php | 3 +-- .../Stages/SwitchToNewAndRemoveOldIndexTest.php | 6 +++--- tests/IntegrationTestCase.php | 2 +- tests/Unit/ElasticSearch/Params/BulkTest.php | 10 +++++----- tests/Unit/Engines/ElasticSearchEngineTest.php | 2 +- 21 files changed, 42 insertions(+), 43 deletions(-) diff --git a/composer.json b/composer.json index f1432e9f..993594b4 100644 --- a/composer.json +++ b/composer.json @@ -17,9 +17,9 @@ ], "require": { "php": "^8.0.12|^8.1", - "elasticsearch/elasticsearch": "^7.2", + "elasticsearch/elasticsearch": "^8.0", + "handcraftedinthealps/elasticsearch-dsl": "^8.0", "laravel/scout": "^8.0|^9.0", - "ongr/elasticsearch-dsl": "^7.0", "roave/better-reflection": "^4.3|^5.0" }, "require-dev": { diff --git a/src/Console/Commands/FlushCommand.php b/src/Console/Commands/FlushCommand.php index 2fafa0c7..a8a38baf 100644 --- a/src/Console/Commands/FlushCommand.php +++ b/src/Console/Commands/FlushCommand.php @@ -4,6 +4,7 @@ namespace Matchish\ScoutElasticSearch\Console\Commands; +use Elastic\Elasticsearch\Exception\ClientResponseException; use Illuminate\Console\Command; use Matchish\ScoutElasticSearch\Searchable\SearchableListFactory; diff --git a/src/ElasticSearch/Params/Bulk.php b/src/ElasticSearch/Params/Bulk.php index c4dca02b..f8b739b5 100644 --- a/src/ElasticSearch/Params/Bulk.php +++ b/src/ElasticSearch/Params/Bulk.php @@ -50,7 +50,6 @@ function ($payload, $model) { 'index' => [ '_index' => $model->searchableAs(), '_id' => $scoutKey, - '_type' => '_doc', 'routing' => false === empty($routing) ? $routing : $scoutKey, ], ]; @@ -73,7 +72,6 @@ function ($payload, $model) { 'delete' => [ '_index' => $model->searchableAs(), '_id' => $scoutKey, - '_type' => '_doc', 'routing' => false === empty($routing) ? $routing : $scoutKey, ], ]; diff --git a/src/ElasticSearchServiceProvider.php b/src/ElasticSearchServiceProvider.php index ef338ed1..ddee3e7e 100644 --- a/src/ElasticSearchServiceProvider.php +++ b/src/ElasticSearchServiceProvider.php @@ -4,8 +4,8 @@ namespace Matchish\ScoutElasticSearch; -use Elasticsearch\Client; -use Elasticsearch\ClientBuilder; +use Elastic\Elasticsearch\Client; +use Elastic\Elasticsearch\ClientBuilder; use Illuminate\Support\ServiceProvider; use Matchish\ScoutElasticSearch\ElasticSearch\Config\Config; diff --git a/src/Engines/ElasticSearchEngine.php b/src/Engines/ElasticSearchEngine.php index eebebdc4..6caec734 100644 --- a/src/Engines/ElasticSearchEngine.php +++ b/src/Engines/ElasticSearchEngine.php @@ -2,7 +2,7 @@ namespace Matchish\ScoutElasticSearch\Engines; -use Elasticsearch\Common\Exceptions\ServerErrorResponseException; +use Elastic\Elasticsearch\Exception\ServerResponseException; use Illuminate\Database\Eloquent\Collection; use Laravel\Scout\Builder; use Laravel\Scout\Builder as BaseBuilder; @@ -21,17 +21,17 @@ final class ElasticSearchEngine extends Engine /** * The ElasticSearch client. * - * @var \Elasticsearch\Client + * @var \Elastic\Elasticsearch\Client */ protected $elasticsearch; /** * Create a new engine instance. * - * @param \Elasticsearch\Client $elasticsearch + * @param \Elastic\Elasticsearch\Client $elasticsearch * @return void */ - public function __construct(\Elasticsearch\Client $elasticsearch) + public function __construct(\Elastic\Elasticsearch\Client $elasticsearch) { $this->elasticsearch = $elasticsearch; } @@ -43,9 +43,9 @@ public function update($models) { $params = new Bulk(); $params->index($models); - $response = $this->elasticsearch->bulk($params->toArray()); + $response = $this->elasticsearch->bulk($params->toArray())->asArray(); if (array_key_exists('errors', $response) && $response['errors']) { - $error = new ServerErrorResponseException(json_encode($response, JSON_PRETTY_PRINT)); + $error = new ServerResponseException(json_encode($response, JSON_PRETTY_PRINT)); throw new \Exception('Bulk update error', $error->getCode(), $error); } } @@ -66,7 +66,7 @@ public function delete($models) public function flush($model) { $indexName = $model->searchableAs(); - $exist = $this->elasticsearch->indices()->exists(['index' => $indexName]); + $exist = $this->elasticsearch->indices()->exists(['index' => $indexName])->asBool(); if ($exist) { $body = (new Search())->addQuery(new MatchAllQuery())->toArray(); $params = new SearchParams($indexName, $body); @@ -185,6 +185,6 @@ private function performSearch(BaseBuilder $builder, $options = []) $indexName = $builder->index ?: $model->searchableAs(); $params = new SearchParams($indexName, $searchBody->toArray()); - return $this->elasticsearch->search($params->toArray()); + return $this->elasticsearch->search($params->toArray())->asArray(); } } diff --git a/src/Jobs/Import.php b/src/Jobs/Import.php index 4f090af6..7aa440f0 100644 --- a/src/Jobs/Import.php +++ b/src/Jobs/Import.php @@ -2,7 +2,7 @@ namespace Matchish\ScoutElasticSearch\Jobs; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; use Illuminate\Bus\Queueable; use Illuminate\Support\Collection; use Matchish\ScoutElasticSearch\ProgressReportable; diff --git a/src/Jobs/Stages/CleanUp.php b/src/Jobs/Stages/CleanUp.php index 16d560c6..2f6af041 100644 --- a/src/Jobs/Stages/CleanUp.php +++ b/src/Jobs/Stages/CleanUp.php @@ -2,8 +2,8 @@ namespace Matchish\ScoutElasticSearch\Jobs\Stages; -use Elasticsearch\Client; -use Elasticsearch\Common\Exceptions\Missing404Exception; +use Elastic\Elasticsearch\Client; +use Elastic\Elasticsearch\Exception\ClientResponseException; use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Alias\Get as GetAliasParams; use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Delete as DeleteIndexParams; use Matchish\ScoutElasticSearch\Searchable\ImportSource; @@ -31,9 +31,8 @@ public function handle(Client $elasticsearch): void $source = $this->source; $params = GetAliasParams::anyIndex($source->searchableAs()); try { - /** @var array $response */ - $response = $elasticsearch->indices()->getAlias($params->toArray()); - } catch (Missing404Exception $e) { + $response = $elasticsearch->indices()->getAlias($params->toArray())->asArray(); + } catch (ClientResponseException $e) { $response = []; } foreach ($response as $indexName => $data) { diff --git a/src/Jobs/Stages/CreateWriteIndex.php b/src/Jobs/Stages/CreateWriteIndex.php index b74ebf27..a19dd442 100644 --- a/src/Jobs/Stages/CreateWriteIndex.php +++ b/src/Jobs/Stages/CreateWriteIndex.php @@ -2,7 +2,7 @@ namespace Matchish\ScoutElasticSearch\Jobs\Stages; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; use Matchish\ScoutElasticSearch\ElasticSearch\DefaultAlias; use Matchish\ScoutElasticSearch\ElasticSearch\Index; use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Create; diff --git a/src/Jobs/Stages/RefreshIndex.php b/src/Jobs/Stages/RefreshIndex.php index 32153253..86a46d7e 100644 --- a/src/Jobs/Stages/RefreshIndex.php +++ b/src/Jobs/Stages/RefreshIndex.php @@ -2,7 +2,7 @@ namespace Matchish\ScoutElasticSearch\Jobs\Stages; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; use Matchish\ScoutElasticSearch\ElasticSearch\Index; use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Refresh; diff --git a/src/Jobs/Stages/SwitchToNewAndRemoveOldIndex.php b/src/Jobs/Stages/SwitchToNewAndRemoveOldIndex.php index d4b18661..8602af4d 100644 --- a/src/Jobs/Stages/SwitchToNewAndRemoveOldIndex.php +++ b/src/Jobs/Stages/SwitchToNewAndRemoveOldIndex.php @@ -2,7 +2,7 @@ namespace Matchish\ScoutElasticSearch\Jobs\Stages; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; use Matchish\ScoutElasticSearch\ElasticSearch\Index; use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Alias\Get; use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Alias\Update; @@ -36,7 +36,7 @@ public function handle(Client $elasticsearch): void { $source = $this->source; $params = Get::anyIndex($source->searchableAs()); - $response = $elasticsearch->indices()->getAlias($params->toArray()); + $response = $elasticsearch->indices()->getAlias($params->toArray())->asArray(); $params = new Update(); foreach ($response as $indexName => $alias) { diff --git a/src/ScoutElasticSearchServiceProvider.php b/src/ScoutElasticSearchServiceProvider.php index 4abbbb0c..2fd7f755 100644 --- a/src/ScoutElasticSearchServiceProvider.php +++ b/src/ScoutElasticSearchServiceProvider.php @@ -4,7 +4,7 @@ namespace Matchish\ScoutElasticSearch; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; use Illuminate\Support\ServiceProvider; use Laravel\Scout\EngineManager; use Laravel\Scout\ScoutServiceProvider; diff --git a/tests/Feature/FlushCommandTest.php b/tests/Feature/FlushCommandTest.php index 0b3a9d93..7d67249a 100644 --- a/tests/Feature/FlushCommandTest.php +++ b/tests/Feature/FlushCommandTest.php @@ -5,6 +5,7 @@ namespace Tests\Feature; use App\Product; +use Elastic\Elasticsearch\Exception\ClientResponseException; use Illuminate\Support\Facades\Artisan; use Matchish\ScoutElasticSearch\ElasticSearch\Params\Bulk; use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Refresh; diff --git a/tests/Feature/ImportCommandTest.php b/tests/Feature/ImportCommandTest.php index 49cf3ba3..9b0c9147 100644 --- a/tests/Feature/ImportCommandTest.php +++ b/tests/Feature/ImportCommandTest.php @@ -142,7 +142,7 @@ public function test_remove_old_index_after_switching_to_new(): void Artisan::call('scout:import'); - $this->assertFalse($this->elasticsearch->indices()->exists(['index' => 'products_old']), 'Old index must be deleted'); + $this->assertFalse($this->elasticsearch->indices()->exists(['index' => 'products_old'])->asBool(), 'Old index must be deleted'); } public function test_progress_report() diff --git a/tests/Feature/ScoutElasticSearchServiceProviderTest.php b/tests/Feature/ScoutElasticSearchServiceProviderTest.php index d4c4cde3..60a02816 100644 --- a/tests/Feature/ScoutElasticSearchServiceProviderTest.php +++ b/tests/Feature/ScoutElasticSearchServiceProviderTest.php @@ -2,7 +2,7 @@ namespace Matchish\ScoutElasticSearch; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; use Tests\TestCase; class ScoutElasticSearchServiceProviderTest extends TestCase diff --git a/tests/Integration/Jobs/Stages/CleanUpTest.php b/tests/Integration/Jobs/Stages/CleanUpTest.php index fca0d015..bcb3cdca 100644 --- a/tests/Integration/Jobs/Stages/CleanUpTest.php +++ b/tests/Integration/Jobs/Stages/CleanUpTest.php @@ -27,8 +27,8 @@ public function test_remove_write_index() $stage = new CleanUp(DefaultImportSourceFactory::from(Product::class)); $stage->handle($this->elasticsearch); - $writeIndexExist = $this->elasticsearch->indices()->exists(['index' => 'products_new']); - $readIndexExist = $this->elasticsearch->indices()->exists(['index' => 'products_old']); + $writeIndexExist = $this->elasticsearch->indices()->exists(['index' => 'products_new'])->asBool(); + $readIndexExist = $this->elasticsearch->indices()->exists(['index' => 'products_old'])->asBool(); $this->assertFalse($writeIndexExist); $this->assertTrue($readIndexExist); diff --git a/tests/Integration/Jobs/Stages/CreateWriteIndexTest.php b/tests/Integration/Jobs/Stages/CreateWriteIndexTest.php index cf9325d5..50b42001 100644 --- a/tests/Integration/Jobs/Stages/CreateWriteIndexTest.php +++ b/tests/Integration/Jobs/Stages/CreateWriteIndexTest.php @@ -5,7 +5,7 @@ namespace Tests\Integration\Jobs\Stages; use App\Product; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; use Matchish\ScoutElasticSearch\ElasticSearch\Index; use Matchish\ScoutElasticSearch\Jobs\Stages\CreateWriteIndex; use Matchish\ScoutElasticSearch\Searchable\DefaultImportSourceFactory; @@ -15,10 +15,11 @@ final class CreateWriteIndexTest extends IntegrationTestCase { public function test_create_write_index(): void { + /** @var Client $elasticsearch */ $elasticsearch = $this->app->make(Client::class); $stage = new CreateWriteIndex(DefaultImportSourceFactory::from(Product::class), Index::fromSource(DefaultImportSourceFactory::from(Product::class))); $stage->handle($elasticsearch); - $response = $elasticsearch->indices()->getAlias(['index' => '*', 'name' => 'products']); + $response = $elasticsearch->indices()->getAlias(['index' => '*', 'name' => 'products'])->asArray(); $this->assertTrue($this->containsWriteIndex($response, 'products')); } diff --git a/tests/Integration/Jobs/Stages/RefreshIndexTest.php b/tests/Integration/Jobs/Stages/RefreshIndexTest.php index deaec211..902f62a5 100644 --- a/tests/Integration/Jobs/Stages/RefreshIndexTest.php +++ b/tests/Integration/Jobs/Stages/RefreshIndexTest.php @@ -21,7 +21,6 @@ public function test_refresh_index(): void ['index' => [ '_index' => 'products', '_id' => 'id', - '_type' => '_doc', ]], [ 'id' => 1, @@ -40,7 +39,7 @@ public function test_refresh_index(): void ], ], ]; - $response = $this->elasticsearch->search($params); + $response = $this->elasticsearch->search($params)->asArray(); $this->assertEquals(1, $response['hits']['total']['value']); } } diff --git a/tests/Integration/Jobs/Stages/SwitchToNewAndRemoveOldIndexTest.php b/tests/Integration/Jobs/Stages/SwitchToNewAndRemoveOldIndexTest.php index fd23af70..fb906ca9 100644 --- a/tests/Integration/Jobs/Stages/SwitchToNewAndRemoveOldIndexTest.php +++ b/tests/Integration/Jobs/Stages/SwitchToNewAndRemoveOldIndexTest.php @@ -27,9 +27,9 @@ public function test_switch_to_new_and_remove_old_index(): void $stage = new SwitchToNewAndRemoveOldIndex(DefaultImportSourceFactory::from(Product::class), new Index('products_new')); $stage->handle($this->elasticsearch); - $newIndexExist = $this->elasticsearch->indices()->exists(['index' => 'products_new']); - $oldIndexExist = $this->elasticsearch->indices()->exists(['index' => 'products_old']); - $alias = $this->elasticsearch->indices()->getAlias(['index' => 'products_new']); + $newIndexExist = $this->elasticsearch->indices()->exists(['index' => 'products_new'])->asBool(); + $oldIndexExist = $this->elasticsearch->indices()->exists(['index' => 'products_old'])->asBool(); + $alias = $this->elasticsearch->indices()->getAlias(['index' => 'products_new'])->asArray(); $this->assertTrue($newIndexExist); $this->assertFalse($oldIndexExist); diff --git a/tests/IntegrationTestCase.php b/tests/IntegrationTestCase.php index 3f981ca2..0929f84e 100644 --- a/tests/IntegrationTestCase.php +++ b/tests/IntegrationTestCase.php @@ -2,7 +2,7 @@ namespace Tests; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; /** * Class IntegrationTestCase. diff --git a/tests/Unit/ElasticSearch/Params/BulkTest.php b/tests/Unit/ElasticSearch/Params/BulkTest.php index f4e1ff6e..d6055c26 100644 --- a/tests/Unit/ElasticSearch/Params/BulkTest.php +++ b/tests/Unit/ElasticSearch/Params/BulkTest.php @@ -17,7 +17,7 @@ public function test_delete() $params = $bulk->toArray(); $this->assertEquals([ - 'body' => [['delete' => ['_index' => 'products', '_type' => '_doc', '_id' => 2, 'routing' => 2]]], + 'body' => [['delete' => ['_index' => 'products', '_id' => 2, 'routing' => 2]]], ], $params); } @@ -31,7 +31,7 @@ public function test_delete_with_custom_key_name() $params = $bulk->toArray(); $this->assertEquals([ - 'body' => [['delete' => ['_index' => 'products', '_type' => '_doc', '_id' => 'Scout', 'routing' => 'Scout']]], + 'body' => [['delete' => ['_index' => 'products', '_id' => 'Scout', 'routing' => 'Scout']]], ], $params); } @@ -45,7 +45,7 @@ public function test_index() $this->assertEquals([ 'body' => [ - ['index' => ['_index' => 'products', '_type' => '_doc', '_id' => 2, 'routing' => 2]], + ['index' => ['_index' => 'products', '_id' => 2, 'routing' => 2]], ['title' => 'Scout', 'id' => 2, '__class_name' => 'App\Product'], ], ], $params); @@ -62,7 +62,7 @@ public function test_index_with_custom_key_name() $this->assertEquals([ 'body' => [ - ['index' => ['_index' => 'products', '_type' => '_doc', '_id' => 'Scout', 'routing' => 'Scout']], + ['index' => ['_index' => 'products', '_id' => 'Scout', 'routing' => 'Scout']], ['title' => 'Scout', 'id' => 2, '__class_name' => 'App\Product'], ], ], $params); @@ -78,7 +78,7 @@ public function test_push_soft_delete_meta_data() $params = $bulk->toArray(); $this->assertEquals([ 'body' => [ - ['index' => ['_index' => 'products', '_type' => '_doc', '_id' => 2, 'routing' => 2]], + ['index' => ['_index' => 'products', '_id' => 2, 'routing' => 2]], ['title' => 'Scout', '__soft_deleted' => 0, 'id' => 2, '__class_name' => 'App\Product'], ], ], $params); diff --git a/tests/Unit/Engines/ElasticSearchEngineTest.php b/tests/Unit/Engines/ElasticSearchEngineTest.php index 989bec6a..b7e671e6 100644 --- a/tests/Unit/Engines/ElasticSearchEngineTest.php +++ b/tests/Unit/Engines/ElasticSearchEngineTest.php @@ -3,7 +3,7 @@ namespace Tests\Unit\Engines; use App\Product; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; use Laravel\Scout\Builder; use Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine; use Mockery as m; From 355f8f07a81f00dbbd6d03cc9bc753f0f44ea252 Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Tue, 19 Apr 2022 17:17:31 +0300 Subject: [PATCH 2/5] some tests removed because they are mocking elasticsearch client which is final after upgrade --- .../Unit/Engines/ElasticSearchEngineTest.php | 25 +------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/tests/Unit/Engines/ElasticSearchEngineTest.php b/tests/Unit/Engines/ElasticSearchEngineTest.php index b7e671e6..ba0e541b 100644 --- a/tests/Unit/Engines/ElasticSearchEngineTest.php +++ b/tests/Unit/Engines/ElasticSearchEngineTest.php @@ -4,6 +4,7 @@ use App\Product; use Elastic\Elasticsearch\Client; +use Elastic\Elasticsearch\ClientBuilder; use Laravel\Scout\Builder; use Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine; use Mockery as m; @@ -19,30 +20,6 @@ public function test_map_ids() $this->assertEquals([1, 15], $ids->all()); } - public function test_pass_client_to_callback() - { - $client = m::mock(Client::class); - $engine = new ElasticSearchEngine($client); - $query = 'zonda'; - $client->shouldReceive('search')->once()->withNoArgs(); - $builder = new Builder(new Product(), $query, function ($client, $query) { - return $client->search(); - }); - $engine->search($builder); - } - - public function test_pass_search_builder_to_callback() - { - $client = m::mock(Client::class); - $engine = new ElasticSearchEngine($client); - $client->shouldReceive('search')->once()->with(m::type('array')); - $query = 'zonda'; - $builder = new Builder(new Product(), $query, function ($client, $query) { - return $client->search($query->toArray()); - }); - $engine->search($builder); - } - public function test_pass_query_to_callback_before_executing() { $builder = new Builder(new Product(), 'zonga'); From 6f89a0684a010df87c54efdd3a29702478d22149 Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Tue, 19 Apr 2022 17:19:03 +0300 Subject: [PATCH 3/5] unused namespaces removed --- src/Console/Commands/FlushCommand.php | 1 - tests/Feature/FlushCommandTest.php | 1 - tests/Unit/Engines/ElasticSearchEngineTest.php | 2 -- 3 files changed, 4 deletions(-) diff --git a/src/Console/Commands/FlushCommand.php b/src/Console/Commands/FlushCommand.php index a8a38baf..2fafa0c7 100644 --- a/src/Console/Commands/FlushCommand.php +++ b/src/Console/Commands/FlushCommand.php @@ -4,7 +4,6 @@ namespace Matchish\ScoutElasticSearch\Console\Commands; -use Elastic\Elasticsearch\Exception\ClientResponseException; use Illuminate\Console\Command; use Matchish\ScoutElasticSearch\Searchable\SearchableListFactory; diff --git a/tests/Feature/FlushCommandTest.php b/tests/Feature/FlushCommandTest.php index 7d67249a..0b3a9d93 100644 --- a/tests/Feature/FlushCommandTest.php +++ b/tests/Feature/FlushCommandTest.php @@ -5,7 +5,6 @@ namespace Tests\Feature; use App\Product; -use Elastic\Elasticsearch\Exception\ClientResponseException; use Illuminate\Support\Facades\Artisan; use Matchish\ScoutElasticSearch\ElasticSearch\Params\Bulk; use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Refresh; diff --git a/tests/Unit/Engines/ElasticSearchEngineTest.php b/tests/Unit/Engines/ElasticSearchEngineTest.php index ba0e541b..db5e5433 100644 --- a/tests/Unit/Engines/ElasticSearchEngineTest.php +++ b/tests/Unit/Engines/ElasticSearchEngineTest.php @@ -4,10 +4,8 @@ use App\Product; use Elastic\Elasticsearch\Client; -use Elastic\Elasticsearch\ClientBuilder; use Laravel\Scout\Builder; use Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine; -use Mockery as m; use Tests\TestCase; class ElasticSearchEngineTest extends TestCase From 45c9f698fb08ae883bcaf38243410f6ffa0fc6aa Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Wed, 20 Apr 2022 11:40:19 +0300 Subject: [PATCH 4/5] ElasticSearchEngine feature tests improved --- tests/Feature/ElasticSearchEngineTest.php | 48 +++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/Feature/ElasticSearchEngineTest.php diff --git a/tests/Feature/ElasticSearchEngineTest.php b/tests/Feature/ElasticSearchEngineTest.php new file mode 100644 index 00000000..0905d351 --- /dev/null +++ b/tests/Feature/ElasticSearchEngineTest.php @@ -0,0 +1,48 @@ +states(['iphone'])->create(); + Product::setEventDispatcher($dispatcher); + + Artisan::call('scout:import'); + + $results = Product::search('Quia', static function($client, $body) { + return $client->search(['index' => 'products', 'body' => $body->toArray()]); + })->raw(); + + $this->assertEmpty($results['hits']['hits']); + } + + public function test_pass_with_response(): void + { + $dispatcher = Product::getEventDispatcher(); + Product::unsetEventDispatcher(); + + $productsAmount = random_int(1, 5); + factory(Product::class, $productsAmount)->states(['iphone'])->create(); + Product::setEventDispatcher($dispatcher); + + Artisan::call('scout:import'); + + $results = Product::search('iphone', static function($client, $body) { + return $client->search(['index' => 'products', 'body' => $body->toArray()]); + })->raw(); + + $this->assertNotEmpty($results['hits']['hits']); + } +} From 2c450ccb2a222ab6c3329d39a42265d84c1c7334 Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Wed, 20 Apr 2022 11:43:30 +0300 Subject: [PATCH 5/5] styleci fix for the Feature\ElasticSearchEngineTest --- tests/Feature/ElasticSearchEngineTest.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/Feature/ElasticSearchEngineTest.php b/tests/Feature/ElasticSearchEngineTest.php index 0905d351..edb258ae 100644 --- a/tests/Feature/ElasticSearchEngineTest.php +++ b/tests/Feature/ElasticSearchEngineTest.php @@ -16,15 +16,14 @@ public function test_pass_empty_response(): void Product::unsetEventDispatcher(); $productsAmount = random_int(1, 5); - $products = factory(Product::class, $productsAmount)->states(['iphone'])->create(); + factory(Product::class, $productsAmount)->states(['iphone'])->create(); Product::setEventDispatcher($dispatcher); Artisan::call('scout:import'); - $results = Product::search('Quia', static function($client, $body) { + $results = Product::search('Quia', static function ($client, $body) { return $client->search(['index' => 'products', 'body' => $body->toArray()]); })->raw(); - $this->assertEmpty($results['hits']['hits']); } @@ -39,7 +38,7 @@ public function test_pass_with_response(): void Artisan::call('scout:import'); - $results = Product::search('iphone', static function($client, $body) { + $results = Product::search('iphone', static function ($client, $body) { return $client->search(['index' => 'products', 'body' => $body->toArray()]); })->raw();