diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index 62d90df0a..cc56b7367 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -9,56 +9,36 @@ 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: 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: '^3.4' elasticsearch-package-constraint: '^5.0' - php-version: '7.4' - job-name-prefix: 'Allow to fail: ' elasticsearch-version: '7.11.1' - lint: true - symfony-version: '^5.0' - elasticsearch-package-constraint: '^5.0' + lint: false + symfony-version: '^3.4' + elasticsearch-package-constraint: '7.11.*' - php-version: '8.0' - job-name-prefix: 'Allow to fail: ' elasticsearch-version: '7.11.1' - lint: true - symfony-version: '^5.0' - elasticsearch-package-constraint: '^7.0' + lint: false + symfony-version: '^4.4' + elasticsearch-package-constraint: '7.11.*' + + - php-version: '8.1' + elasticsearch-version: '8.0.1' + lint: false + symfony-version: '^5.4' + elasticsearch-package-constraint: '^8.0' services: elasticsearch: @@ -103,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 638b88571..2777b29ed 100644 --- a/DependencyInjection/ONGRElasticsearchExtension.php +++ b/DependencyInjection/ONGRElasticsearchExtension.php @@ -18,8 +18,6 @@ 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. diff --git a/Event/PreCreateManagerEvent.php b/Event/PreCreateManagerEvent.php index cb03faaa5..441e34cde 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/Profiler/ElasticsearchProfiler.php b/Profiler/ElasticsearchProfiler.php index 3a67fd733..f870771df 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/ExportService.php b/Service/ExportService.php index a3d1eb493..424905131 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 00ae5a78c..920b660c6 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; @@ -20,6 +22,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; @@ -40,7 +43,7 @@ class Manager private $config = []; /** - * @var Client + * @var Client|LegacyClient */ private $client; @@ -109,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 @@ -133,7 +136,7 @@ public function __construct( /** * Returns Elasticsearch connection. * - * @return Client + * @return Client|LegacyClient */ public function getClient() { @@ -646,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; } @@ -734,7 +743,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 711772997..3603ac05a 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; @@ -20,6 +22,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; @@ -103,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() : LecacyClientBuilder::create(); $client->setHosts($connection['hosts']); - if ($this->tracer) { + if ($this->tracer && method_exists($client, 'setTracer')) { $client->setTracer($this->tracer); } @@ -131,9 +134,19 @@ public function createManager($managerName, $connection, $analysis, $managerConf ]; // set elasticsearch specific settings - $elasticSearchVersion = defined(Client::class . '::VERSION') ? Client::VERSION : '5.0'; - - if (version_compare($elasticSearchVersion, '7.0.0', '>=')) { + $elasticSearchVersion = + defined(Client::class . '::VERSION') + ? Client::VERSION + : ( + defined(LecacyClient::class . '::VERSION') + ? LecacyClient::VERSION + : '5.0' + ); + + if ( + version_compare($elasticSearchVersion, '7.0.0', '>=') + && version_compare($elasticSearchVersion, '8.0.0', '<') + ) { $indexSettings['include_type_name'] = true; } @@ -169,7 +182,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/Test/AbstractElasticsearchTestCase.php b/Test/AbstractElasticsearchTestCase.php index 116136c2d..d5f18d8bc 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/Tests/Functional/Mapping/DocumentFinderTest.php b/Tests/Functional/Mapping/DocumentFinderTest.php index 614fd2996..b1f692e89 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,17 +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'); } - - /** - * Returns service container. - * - * @return ContainerInterface - */ - public function getContainer() - { - return $this->createClient()->getContainer(); - } } diff --git a/Tests/Functional/Profiler/ElasticsearchProfilerTest.php b/Tests/Functional/Profiler/ElasticsearchProfilerTest.php index a2fcefabd..96db7aee4 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,13 @@ 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']); } /** diff --git a/Tests/Functional/Service/RepositoryTest.php b/Tests/Functional/Service/RepositoryTest.php index c823c3b8f..cb7aa1a02 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 45e4edbdb..d313aca48 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/Tests/app/AppKernel.php b/Tests/app/AppKernel.php index c05683690..ff4da7ba8 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 f22f7749e..db61f9d2a 100644 --- a/composer.json +++ b/composer.json @@ -15,31 +15,33 @@ } ], "require": { - "php": "^5.6|^7.0|^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", - "doctrine/annotations": "~1.2", + "php": "^7.4|^8.0", + "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", "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" + "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" : "^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/validator": "^3.4|^4.4|^5.4", + "symfony/options-resolver": "^3.4|^4.4|^5.4" }, "autoload": { "psr-4": { "ONGR\\ElasticsearchBundle\\": "" },