From c7f1bf46cb8b9b5c57ad03f454c11e8f388da3f9 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Fri, 25 Mar 2022 22:05:35 +0100 Subject: [PATCH 01/10] Add support for elasticsearch 8 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f22f7749..a5b0f364 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "doctrine/cache": "~1.4", "doctrine/collections": "~1.4", "monolog/monolog": "^1.10 || ^2.0", - "handcraftedinthealps/elasticsearch-dsl": "^5.0.7.1|^6.2.0.1|^7.2.0.1" + "handcraftedinthealps/elasticsearch-dsl": "^5.0.7.1|^6.2.0.1|^7.2.0.1|^8.0" }, "require-dev": { "mikey179/vfsstream": "~1.4", From 9603e7d6b6930765c66e778cb9caf722aece92aa Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Fri, 25 Mar 2022 22:10:24 +0100 Subject: [PATCH 02/10] Add test cases for php 8.1 and elasticsearch 8 --- .github/workflows/test-application.yaml | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index 62d90df0..9b171b69 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -9,7 +9,7 @@ on: jobs: php: - name: '${{ matrix.job-name-prefix }}PHP ${{ matrix.php-version }}, ES ${{ matrix.elasticsearch-version }}' + name: 'PHP ${{ matrix.php-version }}, ES ${{ matrix.elasticsearch-version }}' runs-on: ubuntu-latest strategy: @@ -47,18 +47,22 @@ jobs: elasticsearch-package-constraint: '^5.0' - php-version: '7.4' - job-name-prefix: 'Allow to fail: ' elasticsearch-version: '7.11.1' - lint: true + lint: false symfony-version: '^5.0' - elasticsearch-package-constraint: '^5.0' + elasticsearch-package-constraint: '7.11.*' - php-version: '8.0' - job-name-prefix: 'Allow to fail: ' elasticsearch-version: '7.11.1' - lint: true + lint: false + symfony-version: '^5.0' + elasticsearch-package-constraint: '7.11.*' + + - php-version: '8.1' + elasticsearch-version: '8.0.1' + lint: false symfony-version: '^5.0' - elasticsearch-package-constraint: '^7.0' + elasticsearch-package-constraint: '^8.0' services: elasticsearch: From 0b3b64f71bcf2c35a1185633f126a2b8030a61d2 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Fri, 25 Mar 2022 22:40:18 +0100 Subject: [PATCH 03/10] Drop support for < PHP 7.4 --- .github/workflows/test-application.yaml | 32 ++++--------------------- Test/AbstractElasticsearchTestCase.php | 2 +- composer.json | 2 +- 3 files changed, 6 insertions(+), 30 deletions(-) diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index 9b171b69..8bacaf6f 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -16,52 +16,28 @@ jobs: fail-fast: false matrix: include: - - php-version: '5.6' - elasticsearch-version: '5.6.14' - lint: false - symfony-version: '^2.8' - elasticsearch-package-constraint: '^5.0' - - - php-version: '7.0' - elasticsearch-version: '5.6.14' - lint: false - symfony-version: '^3.4' - elasticsearch-package-constraint: '^5.0' - - - php-version: '7.2' - elasticsearch-version: '5.6.14' - lint: false - symfony-version: '^3.4' - elasticsearch-package-constraint: '^5.0' - - - php-version: '7.3' - elasticsearch-version: '5.6.14' - lint: false - symfony-version: '^4.4' - elasticsearch-package-constraint: '^5.0' - - php-version: '7.4' elasticsearch-version: '5.6.14' lint: true - symfony-version: '^5.0' + symfony-version: '^2.8' elasticsearch-package-constraint: '^5.0' - php-version: '7.4' elasticsearch-version: '7.11.1' lint: false - symfony-version: '^5.0' + symfony-version: '^3.4' elasticsearch-package-constraint: '7.11.*' - php-version: '8.0' elasticsearch-version: '7.11.1' lint: false - symfony-version: '^5.0' + symfony-version: '^4.4' elasticsearch-package-constraint: '7.11.*' - php-version: '8.1' elasticsearch-version: '8.0.1' lint: false - symfony-version: '^5.0' + symfony-version: '^5.4' elasticsearch-package-constraint: '^8.0' services: diff --git a/Test/AbstractElasticsearchTestCase.php b/Test/AbstractElasticsearchTestCase.php index 116136c2..d5f18d8b 100644 --- a/Test/AbstractElasticsearchTestCase.php +++ b/Test/AbstractElasticsearchTestCase.php @@ -172,7 +172,7 @@ protected function doTearDown() * * @return ContainerInterface */ - protected function getContainer($kernelOptions = []) + protected static function getContainer($kernelOptions = []): ContainerInterface { if (null === self::$container) { self::bootKernel($kernelOptions); diff --git a/composer.json b/composer.json index a5b0f364..c6033f14 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } ], "require": { - "php": "^5.6|^7.0|^8.0", + "php": "^7.4|^8.0", "symfony/framework-bundle": "^2.8|^3.0|^4|^5", "symfony/console": "^2.8|^3.0|^4|^5", "symfony/stopwatch": "^2.8|^3.0|^4|^5", From 9dd9dfd9945a21c8d01edd89cb06df335a7d32b9 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Wed, 20 Apr 2022 23:54:20 +0200 Subject: [PATCH 04/10] Drop support for Symfony 2.8 --- .github/workflows/test-application.yaml | 2 +- composer.json | 28 ++++++++++++------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index 8bacaf6f..16a0e6a8 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -19,7 +19,7 @@ jobs: - php-version: '7.4' elasticsearch-version: '5.6.14' lint: true - symfony-version: '^2.8' + symfony-version: '^3.4' elasticsearch-package-constraint: '^5.0' - php-version: '7.4' diff --git a/composer.json b/composer.json index c6033f14..cecb7ac6 100644 --- a/composer.json +++ b/composer.json @@ -16,11 +16,11 @@ ], "require": { "php": "^7.4|^8.0", - "symfony/framework-bundle": "^2.8|^3.0|^4|^5", - "symfony/console": "^2.8|^3.0|^4|^5", - "symfony/stopwatch": "^2.8|^3.0|^4|^5", - "symfony/templating": "^2.8|^3.0|^4|^5", - "symfony/asset": "^2.8|^3.0|^4|^5", + "symfony/framework-bundle": "^3.4|^4.4|^5.4", + "symfony/console": "^3.4|^4.4|^5.4", + "symfony/stopwatch": "^3.4|^4.4|^5.4", + "symfony/templating": "^3.4|^4.4|^5.4", + "symfony/asset": "^3.4|^4.4|^5.4", "doctrine/annotations": "~1.2", "doctrine/inflector": "^1.0 || ^2.0", "doctrine/cache": "~1.4", @@ -31,15 +31,15 @@ "require-dev": { "mikey179/vfsstream": "~1.4", "squizlabs/php_codesniffer": "^2.0|^3.0", - "symfony/browser-kit" : "^2.8|^3.4|^4|^5", - "symfony/expression-language" : "^2.8|^3.4|^4|^5", - "symfony/twig-bundle": "^2.8|^3.4|^4|^5", - "symfony/serializer": "^2.8|^3.4|^4|^5", - "symfony/yaml": "^2.8|^3.4|^4|^5", - "symfony/phpunit-bridge": "^5.1", - "symfony/dependency-injection": "^2.8|^3.4|^4|^5", - "symfony/validator": "^2.8|^3.4|^4|^5", - "symfony/options-resolver": "^2.8|^3.4|^4|^5" + "symfony/browser-kit" : "^3.4|^4.4|^5.4", + "symfony/expression-language" : "^3.4|^4.4|^5.4", + "symfony/twig-bundle": "^3.4|^4.4|^5.4", + "symfony/serializer": "^3.4|^4.4|^5.4", + "symfony/yaml": "^3.4|^4.4|^5.4", + "symfony/phpunit-bridge": "^5.4", + "symfony/dependency-injection": "^3.4|^4.4|^5.4", + "symfony/validator": "^3.4|^4.4|^5.4", + "symfony/options-resolver": "^3.4|^4.4|^5.4" }, "autoload": { "psr-4": { "ONGR\\ElasticsearchBundle\\": "" }, From bae4800845eabcf5238711ef022da28213e9ebfd Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Thu, 21 Apr 2022 00:17:37 +0200 Subject: [PATCH 05/10] Define some dependencies and fix compatibility to event dispatcher --- Profiler/ElasticsearchProfiler.php | 12 ------------ Service/Manager.php | 5 ++++- Service/ManagerFactory.php | 5 ++++- Tests/Functional/Mapping/DocumentFinderTest.php | 10 ---------- Tests/app/AppKernel.php | 2 +- composer.json | 4 +++- 6 files changed, 12 insertions(+), 26 deletions(-) diff --git a/Profiler/ElasticsearchProfiler.php b/Profiler/ElasticsearchProfiler.php index 3a67fd73..f870771d 100644 --- a/Profiler/ElasticsearchProfiler.php +++ b/Profiler/ElasticsearchProfiler.php @@ -115,18 +115,6 @@ public function getQueries() return $this->cloneVar($this->data['queries']); } - /** - * Backward compatibility Layer can be removed when Symfony 2.8 is dropped. - */ - protected function cloneVar($var) - { - if (class_exists(Kernel::class) && Kernel::VERSION_ID < 30000) { - return $var; - } - - return parent::cloneVar($var); - } - /** * @return array */ diff --git a/Service/Manager.php b/Service/Manager.php index 00ae5a78..3bb836c1 100644 --- a/Service/Manager.php +++ b/Service/Manager.php @@ -20,6 +20,7 @@ use ONGR\ElasticsearchBundle\Exception\BulkWithErrorsException; use ONGR\ElasticsearchBundle\Mapping\MetadataCollector; use ONGR\ElasticsearchBundle\Result\Converter; +use Symfony\Component\EventDispatcher\Attribute\AsEventListener; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy; use Symfony\Component\Stopwatch\Stopwatch; @@ -734,7 +735,9 @@ private function stopwatch($action, $name) private function dispatch($eventName, $event) { - if (class_exists(LegacyEventDispatcherProxy::class)) { + if (class_exists(LegacyEventDispatcherProxy::class) + || class_exists(AsEventListener::class) + ) { return $this->eventDispatcher->dispatch($event, $eventName); } else { return $this->eventDispatcher->dispatch($eventName, $event); diff --git a/Service/ManagerFactory.php b/Service/ManagerFactory.php index 71177299..3f41d20c 100644 --- a/Service/ManagerFactory.php +++ b/Service/ManagerFactory.php @@ -20,6 +20,7 @@ use ONGR\ElasticsearchBundle\Result\Converter; use PackageVersions\Versions; use Psr\Log\LoggerInterface; +use Symfony\Component\EventDispatcher\Attribute\AsEventListener; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy; use Symfony\Component\Stopwatch\Stopwatch; @@ -169,7 +170,9 @@ public function createManager($managerName, $connection, $analysis, $managerConf private function dispatch($eventName, $event) { - if (class_exists(LegacyEventDispatcherProxy::class)) { + if (class_exists(LegacyEventDispatcherProxy::class) + || class_exists(AsEventListener::class) + ) { return $this->eventDispatcher->dispatch($event, $eventName); } else { return $this->eventDispatcher->dispatch($eventName, $event); diff --git a/Tests/Functional/Mapping/DocumentFinderTest.php b/Tests/Functional/Mapping/DocumentFinderTest.php index 614fd299..4156ff97 100644 --- a/Tests/Functional/Mapping/DocumentFinderTest.php +++ b/Tests/Functional/Mapping/DocumentFinderTest.php @@ -38,14 +38,4 @@ public function testGetBundleClassException() $finder = new DocumentFinder($this->getContainer()->getParameter('kernel.bundles')); $finder->getBundleClass('NotExistingBundle'); } - - /** - * Returns service container. - * - * @return ContainerInterface - */ - public function getContainer() - { - return $this->createClient()->getContainer(); - } } diff --git a/Tests/app/AppKernel.php b/Tests/app/AppKernel.php index c0568369..ff4da7ba 100644 --- a/Tests/app/AppKernel.php +++ b/Tests/app/AppKernel.php @@ -22,7 +22,7 @@ class AppKernel extends Kernel * * @return array */ - public function registerBundles() + public function registerBundles(): iterable { return [ new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), diff --git a/composer.json b/composer.json index cecb7ac6..53ff9446 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,9 @@ "symfony/framework-bundle": "^3.4|^4.4|^5.4", "symfony/console": "^3.4|^4.4|^5.4", "symfony/stopwatch": "^3.4|^4.4|^5.4", + "symfony/event-dispatcher": "^3.4|^4.4|^5.4", + "symfony/dependency-injection": "^3.4|^4.4|^5.4", + "symfony/config": "^3.4|^4.4|^5.4", "symfony/templating": "^3.4|^4.4|^5.4", "symfony/asset": "^3.4|^4.4|^5.4", "doctrine/annotations": "~1.2", @@ -37,7 +40,6 @@ "symfony/serializer": "^3.4|^4.4|^5.4", "symfony/yaml": "^3.4|^4.4|^5.4", "symfony/phpunit-bridge": "^5.4", - "symfony/dependency-injection": "^3.4|^4.4|^5.4", "symfony/validator": "^3.4|^4.4|^5.4", "symfony/options-resolver": "^3.4|^4.4|^5.4" }, From f8586007766c993a4fe10d2eb414c906cb37a2ea Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Thu, 21 Apr 2022 01:15:59 +0200 Subject: [PATCH 06/10] Add compatibility layer to new namespace --- .../ONGRElasticsearchExtension.php | 18 ++++++++++++++++- Event/PreCreateManagerEvent.php | 13 ++++++------ Service/ExportService.php | 2 -- Service/Manager.php | 20 +++++++++++++------ Service/ManagerFactory.php | 19 +++++++++++++----- .../Functional/Mapping/DocumentFinderTest.php | 4 ++-- Tests/Functional/Service/RepositoryTest.php | 9 +++++++-- Tests/Unit/Service/ManagerTest.php | 18 +++++++++++------ composer.json | 8 ++++---- 9 files changed, 77 insertions(+), 34 deletions(-) diff --git a/DependencyInjection/ONGRElasticsearchExtension.php b/DependencyInjection/ONGRElasticsearchExtension.php index 638b8857..cd6f3bf3 100644 --- a/DependencyInjection/ONGRElasticsearchExtension.php +++ b/DependencyInjection/ONGRElasticsearchExtension.php @@ -14,6 +14,7 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Loader; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\HttpKernel\DependencyInjection\Extension; @@ -24,8 +25,23 @@ /** * This is the class that loads and manages bundle configuration. */ -class ONGRElasticsearchExtension extends Extension +class ONGRElasticsearchExtension extends Extension implements PrependExtensionInterface { + public function prepend(ContainerBuilder $container): void + { + $container->prependExtensionConfig('framework', + [ + 'cache' => [ + 'pools' => [ + 'es.cache_engine_pool' => [ + 'adapter' => 'cache.system', + ], + ], + ], + ] + ); + } + /** * {@inheritdoc} */ diff --git a/Event/PreCreateManagerEvent.php b/Event/PreCreateManagerEvent.php index cb03faaa..441e34cd 100644 --- a/Event/PreCreateManagerEvent.php +++ b/Event/PreCreateManagerEvent.php @@ -11,7 +11,8 @@ namespace ONGR\ElasticsearchBundle\Event; -use Elasticsearch\ClientBuilder; +use Elastic\Elasticsearch\ClientBuilder; +use Elasticsearch\ClientBuilder as LegacyClientBuilder; class PreCreateManagerEvent extends BaseEvent { @@ -28,17 +29,17 @@ class PreCreateManagerEvent extends BaseEvent /** * CreateManagerEvent constructor. * - * @param ClientBuilder $client + * @param ClientBuilder|LegacyClientBuilder $client * @param $indexSettings array */ - public function __construct(ClientBuilder $client, &$indexSettings) + public function __construct($client, &$indexSettings) { $this->client = $client; $this->indexSettings = $indexSettings; } /** - * @return ClientBuilder + * @return ClientBuilder|LegacyClientBuilder */ public function getClient() { @@ -46,9 +47,9 @@ public function getClient() } /** - * @param ClientBuilder $client + * @param ClientBuilder|LegacyClientBuilder $client */ - public function setClient(ClientBuilder $client) + public function setClient($client) { $this->client = $client; } diff --git a/Service/ExportService.php b/Service/ExportService.php index a3d1eb49..42490513 100644 --- a/Service/ExportService.php +++ b/Service/ExportService.php @@ -11,8 +11,6 @@ namespace ONGR\ElasticsearchBundle\Service; -use Elasticsearch\Helper\Iterators\SearchHitIterator; -use Elasticsearch\Helper\Iterators\SearchResponseIterator; use ONGR\ElasticsearchBundle\Result\RawIterator; use ONGR\ElasticsearchBundle\Service\Json\JsonWriter; use ONGR\ElasticsearchDSL\Query\MatchAllQuery; diff --git a/Service/Manager.php b/Service/Manager.php index 3bb836c1..920b660c 100644 --- a/Service/Manager.php +++ b/Service/Manager.php @@ -11,8 +11,10 @@ namespace ONGR\ElasticsearchBundle\Service; -use Elasticsearch\Client; -use Elasticsearch\Common\Exceptions\Missing404Exception; +use Elastic\Elasticsearch\Client; +use Elastic\Elasticsearch\Exception\ClientResponseException; +use Elasticsearch\Client as LegacyClient; +use Elasticsearch\Common\Exceptions\Missing404Exception as LegacyMissing404Exception; use ONGR\ElasticsearchBundle\Event\Events; use ONGR\ElasticsearchBundle\Event\BulkEvent; use ONGR\ElasticsearchBundle\Event\CommitEvent; @@ -41,7 +43,7 @@ class Manager private $config = []; /** - * @var Client + * @var Client|LegacyClient */ private $client; @@ -110,7 +112,7 @@ class Manager /** * @param string $name Manager name * @param array $config Manager configuration - * @param Client $client + * @param Client|LegacyClient $client * @param array $indexSettings * @param MetadataCollector $metadataCollector * @param Converter $converter @@ -134,7 +136,7 @@ public function __construct( /** * Returns Elasticsearch connection. * - * @return Client + * @return Client|LegacyClient */ public function getClient() { @@ -647,7 +649,13 @@ public function find($className, $id, $routing = null) try { $result = $this->getClient()->get($params); - } catch (Missing404Exception $e) { + } catch (LegacyMissing404Exception $e) { + return null; + } catch (ClientResponseException $e) { + if ($e->getResponse()->getStatusCode() !== 404) { + throw $e; + } + return null; } diff --git a/Service/ManagerFactory.php b/Service/ManagerFactory.php index 3f41d20c..f4218f25 100644 --- a/Service/ManagerFactory.php +++ b/Service/ManagerFactory.php @@ -11,8 +11,10 @@ namespace ONGR\ElasticsearchBundle\Service; -use Elasticsearch\Client; -use Elasticsearch\ClientBuilder; +use Elastic\Elasticsearch\ClientBuilder; +use Elastic\Elasticsearch\Client; +use Elasticsearch\Client as LecacyClient; +use Elasticsearch\ClientBuilder as LecacyClientBuilder; use ONGR\ElasticsearchBundle\Event\Events; use ONGR\ElasticsearchBundle\Event\PostCreateManagerEvent; use ONGR\ElasticsearchBundle\Event\PreCreateManagerEvent; @@ -104,10 +106,10 @@ public function createManager($managerName, $connection, $analysis, $managerConf { $mappings = $this->metadataCollector->getClientMapping($managerConfig['mappings']); - $client = ClientBuilder::create(); + $client = class_exists(ClientBuilder::class) ? ClientBuilder::create() : LegacyClientBuilder::create(); $client->setHosts($connection['hosts']); - if ($this->tracer) { + if ($this->tracer && method_exists($client, 'setTracer')) { $client->setTracer($this->tracer); } @@ -132,7 +134,14 @@ public function createManager($managerName, $connection, $analysis, $managerConf ]; // set elasticsearch specific settings - $elasticSearchVersion = defined(Client::class . '::VERSION') ? Client::VERSION : '5.0'; + $elasticSearchVersion = + defined(Client::class . '::VERSION') + ? Client::VERSION + : ( + defined(LegacyClient::class . '::VERSION') + ? LegacyClient::VERSION + : '5.0' + ); if (version_compare($elasticSearchVersion, '7.0.0', '>=')) { $indexSettings['include_type_name'] = true; diff --git a/Tests/Functional/Mapping/DocumentFinderTest.php b/Tests/Functional/Mapping/DocumentFinderTest.php index 4156ff97..b1f692e8 100644 --- a/Tests/Functional/Mapping/DocumentFinderTest.php +++ b/Tests/Functional/Mapping/DocumentFinderTest.php @@ -22,7 +22,7 @@ class DocumentFinderTest extends WebTestCase */ public function testGetBundleDocumentClasses() { - $finder = new DocumentFinder($this->getContainer()->getParameter('kernel.bundles')); + $finder = new DocumentFinder($this->createClient()->getContainer()->getParameter('kernel.bundles')); $this->assertGreaterThan(0, count($finder->getBundleDocumentClasses('TestBundle'))); $this->assertEquals(0, count($finder->getBundleDocumentClasses('FrameworkBundle'))); } @@ -35,7 +35,7 @@ public function testGetBundleClassException() $this->expectException(\LogicException::class); $this->expectExceptionMessage('Bundle \'NotExistingBundle\' does not exist.'); - $finder = new DocumentFinder($this->getContainer()->getParameter('kernel.bundles')); + $finder = new DocumentFinder($this->createClient()->getContainer()->getParameter('kernel.bundles')); $finder->getBundleClass('NotExistingBundle'); } } diff --git a/Tests/Functional/Service/RepositoryTest.php b/Tests/Functional/Service/RepositoryTest.php index c823c3b8..cb7aa1a0 100644 --- a/Tests/Functional/Service/RepositoryTest.php +++ b/Tests/Functional/Service/RepositoryTest.php @@ -11,7 +11,8 @@ namespace ONGR\ElasticsearchBundle\Tests\Functional; -use Elasticsearch\Common\Exceptions\Missing404Exception; +use Elastic\Elasticsearch\Exception\ClientResponseException; +use Elasticsearch\Common\Exceptions\Missing404Exception as LegacyMissing404Exception; use ONGR\ElasticsearchBundle\Result\DocumentIterator; use ONGR\ElasticsearchBundle\Tests\app\fixture\TestBundle\Document\Product; use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery; @@ -333,7 +334,11 @@ public function testRemove() */ public function testRemoveException() { - $this->expectException(Missing404Exception::class); + if (class_exists(ClientResponseException::class)) { + $this->expectException(ClientResponseException::class); + } else { + $this->expectException(LegacyMissing404Exception::class); + } $manager = $this->getManager(); diff --git a/Tests/Unit/Service/ManagerTest.php b/Tests/Unit/Service/ManagerTest.php index 45e4edbd..d313aca4 100644 --- a/Tests/Unit/Service/ManagerTest.php +++ b/Tests/Unit/Service/ManagerTest.php @@ -11,6 +11,10 @@ namespace ONGR\ElasticsearchBundle\Tests\Unit\Service; +use Elastic\Elasticsearch\Client; +use Elastic\Elasticsearch\Endpoints\Indices; +use Elasticsearch\Namespaces\IndicesNamespace as LegacyIndicesNamespace; +use Elasticsearch\Client as LegacyClient; use ONGR\ElasticsearchBundle\Service\Manager; use ONGR\ElasticsearchDSL\Query\MatchAllQuery; use ONGR\ElasticsearchDSL\Search; @@ -185,9 +189,9 @@ public function getTestBulkData() */ public function testBulk($expected, $calls) { - $indices = $this->createMock('Elasticsearch\Namespaces\IndicesNamespace'); + $indices = $this->createMock(class_exists(Indices::class) ? Indices::class : LegacyIndicesNamespace::class); - $esClient = $this->createMock('Elasticsearch\Client'); + $esClient = $this->createMock(class_exists(Client::class) ? Client::class : LegacyClient::class); $esClient->expects($this->once())->method('bulk')->with($expected); $esClient->expects($this->any())->method('indices')->will($this->returnValue($indices)); @@ -221,9 +225,11 @@ public function testBulkWithCommitModeSet() $expected = $this->getTestBulkData()['update_script']['expected']; $expected['refresh'] = true; $calls = $this->getTestBulkData()['update_script']['calls']; - $indices = $this->createMock('Elasticsearch\Namespaces\IndicesNamespace'); - $esClient = $this->createMock('Elasticsearch\Client'); + $indices = $this->createMock(class_exists(Indices::class) ? Indices::class : LegacyIndicesNamespace::class); + + $esClient = $this->createMock(class_exists(Client::class) ? Client::class : LegacyClient::class); + $esClient->expects($this->any())->method('bulk')->with($expected)->willReturn(['errors' => false]); $esClient->expects($this->any())->method('indices')->will($this->returnValue($indices)); @@ -265,7 +271,7 @@ public function testBulkWithCommitModeSet() public function testClearScroll() { $esClient = $this - ->getMockBuilder('Elasticsearch\Client') + ->getMockBuilder(class_exists(Client::class) ? Client::class : LegacyClient::class) ->setMethods(['clearScroll']) ->disableOriginalConstructor() ->getMock(); @@ -306,7 +312,7 @@ public function getPreparedConfiguration() { $search = new Search(); $search->addQuery(new MatchAllQuery()); - $client = $this->getMockBuilder('Elasticsearch\Client') + $client = $this->getMockBuilder(class_exists(Client::class) ? Client::class : LegacyClient::class) ->disableOriginalConstructor() ->getMock(); $manager = new Manager( diff --git a/composer.json b/composer.json index 53ff9446..db61f9d2 100644 --- a/composer.json +++ b/composer.json @@ -24,15 +24,15 @@ "symfony/config": "^3.4|^4.4|^5.4", "symfony/templating": "^3.4|^4.4|^5.4", "symfony/asset": "^3.4|^4.4|^5.4", - "doctrine/annotations": "~1.2", + "doctrine/annotations": "^1.2", "doctrine/inflector": "^1.0 || ^2.0", - "doctrine/cache": "~1.4", - "doctrine/collections": "~1.4", + "doctrine/cache": "^1.4", + "doctrine/collections": "^1.4", "monolog/monolog": "^1.10 || ^2.0", "handcraftedinthealps/elasticsearch-dsl": "^5.0.7.1|^6.2.0.1|^7.2.0.1|^8.0" }, "require-dev": { - "mikey179/vfsstream": "~1.4", + "mikey179/vfsstream": "^1.4", "squizlabs/php_codesniffer": "^2.0|^3.0", "symfony/browser-kit" : "^3.4|^4.4|^5.4", "symfony/expression-language" : "^3.4|^4.4|^5.4", From 93bd41ee13bc7c29c241421cd75753ec3a932c7e Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Thu, 21 Apr 2022 01:19:32 +0200 Subject: [PATCH 07/10] Fix code style --- .github/workflows/test-application.yaml | 2 +- .../ONGRElasticsearchExtension.php | 20 +------------------ 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index 16a0e6a8..cc56b736 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -83,7 +83,7 @@ jobs: - name: Fix code style if: ${{ matrix.lint }} - run: vendor/bin/phpcs -p --standard=PSR2 --ignore=vendor/,Tests/app/,Profiler/Handler/BackwardCompatibilityWriteTrait.php ./ + run: vendor/bin/phpcs -p --standard=PSR2 --ignore=vendor/,Tests/app/,var/,Profiler/Handler/BackwardCompatibilityWriteTrait.php ./ - name: Run tests run: vendor/bin/simple-phpunit diff --git a/DependencyInjection/ONGRElasticsearchExtension.php b/DependencyInjection/ONGRElasticsearchExtension.php index cd6f3bf3..2777b29e 100644 --- a/DependencyInjection/ONGRElasticsearchExtension.php +++ b/DependencyInjection/ONGRElasticsearchExtension.php @@ -14,34 +14,16 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Loader; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\Reference; -use Symfony\Component\HttpKernel\Kernel; -use Symfony\Component\HttpKernel\KernelEvents; /** * This is the class that loads and manages bundle configuration. */ -class ONGRElasticsearchExtension extends Extension implements PrependExtensionInterface +class ONGRElasticsearchExtension extends Extension { - public function prepend(ContainerBuilder $container): void - { - $container->prependExtensionConfig('framework', - [ - 'cache' => [ - 'pools' => [ - 'es.cache_engine_pool' => [ - 'adapter' => 'cache.system', - ], - ], - ], - ] - ); - } - /** * {@inheritdoc} */ From 3adeb3a040db4f917fd78b187d34ad0dac402107 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Thu, 21 Apr 2022 01:22:25 +0200 Subject: [PATCH 08/10] Fix legacy class usages --- Service/ManagerFactory.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Service/ManagerFactory.php b/Service/ManagerFactory.php index f4218f25..d4dc873e 100644 --- a/Service/ManagerFactory.php +++ b/Service/ManagerFactory.php @@ -106,7 +106,7 @@ public function createManager($managerName, $connection, $analysis, $managerConf { $mappings = $this->metadataCollector->getClientMapping($managerConfig['mappings']); - $client = class_exists(ClientBuilder::class) ? ClientBuilder::create() : LegacyClientBuilder::create(); + $client = class_exists(ClientBuilder::class) ? ClientBuilder::create() : LecacyClientBuilder::create(); $client->setHosts($connection['hosts']); if ($this->tracer && method_exists($client, 'setTracer')) { @@ -138,8 +138,8 @@ public function createManager($managerName, $connection, $analysis, $managerConf defined(Client::class . '::VERSION') ? Client::VERSION : ( - defined(LegacyClient::class . '::VERSION') - ? LegacyClient::VERSION + defined(LecacyClient::class . '::VERSION') + ? LecacyClient::VERSION : '5.0' ); From 2deb3c3d6a1c36464cbce68db6d6abfbdf9e2560 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Thu, 21 Apr 2022 01:28:17 +0200 Subject: [PATCH 09/10] Do not check for port in profiler test --- Tests/Functional/Profiler/ElasticsearchProfilerTest.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Tests/Functional/Profiler/ElasticsearchProfilerTest.php b/Tests/Functional/Profiler/ElasticsearchProfilerTest.php index a2fcefab..23c0b111 100644 --- a/Tests/Functional/Profiler/ElasticsearchProfilerTest.php +++ b/Tests/Functional/Profiler/ElasticsearchProfilerTest.php @@ -11,6 +11,7 @@ namespace ONGR\ElasticsearchBundle\Tests\Functional\Profiler; +use Elasticsearch\Client as LegacyClient; use ONGR\ElasticsearchBundle\Profiler\ElasticsearchProfiler; use ONGR\ElasticsearchBundle\Tests\app\fixture\TestBundle\Document\Product; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\GlobalAggregation; @@ -101,7 +102,6 @@ public function testGetQueries() 'method' => 'GET', 'httpParameters' => [], 'scheme' => 'http', - 'port' => 9200, ], $lastQuery, 'Logged data did not match expected data.' @@ -140,7 +140,6 @@ public function testGetTermQuery() 'method' => 'POST', 'httpParameters' => [], 'scheme' => 'http', - 'port' => 9200, ], $lastQuery, 'Logged data did not match expected data.' @@ -165,6 +164,12 @@ public function checkQueryParameters(&$query) $this->assertArrayHasKey('path', $query, 'Query should have host path set.'); $this->assertNotEmpty($query['path'], 'Path should not be empty.'); unset($query['path']); + + if (class_exists(LegacyClient::class)) { + $this->assertArrayHasKey('port', $query, 'Query should have port set.'); + $this->assertNotEmpty($query['port'], 'Port should not be empty.'); + unset($query['port']); + } } /** From 0445f63b25e7e0c2b7a6f79ec291edf8b012e627 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Thu, 21 Apr 2022 01:55:12 +0200 Subject: [PATCH 10/10] Check for port only in LegacyClient --- Service/ManagerFactory.php | 5 ++++- Tests/Functional/Profiler/ElasticsearchProfilerTest.php | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Service/ManagerFactory.php b/Service/ManagerFactory.php index d4dc873e..3603ac05 100644 --- a/Service/ManagerFactory.php +++ b/Service/ManagerFactory.php @@ -143,7 +143,10 @@ public function createManager($managerName, $connection, $analysis, $managerConf : '5.0' ); - if (version_compare($elasticSearchVersion, '7.0.0', '>=')) { + if ( + version_compare($elasticSearchVersion, '7.0.0', '>=') + && version_compare($elasticSearchVersion, '8.0.0', '<') + ) { $indexSettings['include_type_name'] = true; } diff --git a/Tests/Functional/Profiler/ElasticsearchProfilerTest.php b/Tests/Functional/Profiler/ElasticsearchProfilerTest.php index 23c0b111..96db7aee 100644 --- a/Tests/Functional/Profiler/ElasticsearchProfilerTest.php +++ b/Tests/Functional/Profiler/ElasticsearchProfilerTest.php @@ -168,8 +168,9 @@ public function checkQueryParameters(&$query) if (class_exists(LegacyClient::class)) { $this->assertArrayHasKey('port', $query, 'Query should have port set.'); $this->assertNotEmpty($query['port'], 'Port should not be empty.'); - unset($query['port']); } + + unset($query['port']); } /**