Skip to content
50 changes: 15 additions & 35 deletions .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
2 changes: 0 additions & 2 deletions DependencyInjection/ONGRElasticsearchExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 7 additions & 6 deletions Event/PreCreateManagerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

namespace ONGR\ElasticsearchBundle\Event;

use Elasticsearch\ClientBuilder;
use Elastic\Elasticsearch\ClientBuilder;
use Elasticsearch\ClientBuilder as LegacyClientBuilder;

class PreCreateManagerEvent extends BaseEvent
{
Expand All @@ -28,27 +29,27 @@ 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()
{
return $this->client;
}

/**
* @param ClientBuilder $client
* @param ClientBuilder|LegacyClientBuilder $client
*/
public function setClient(ClientBuilder $client)
public function setClient($client)
{
$this->client = $client;
}
Expand Down
12 changes: 0 additions & 12 deletions Profiler/ElasticsearchProfiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 0 additions & 2 deletions Service/ExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 18 additions & 7 deletions Service/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@

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;
use ONGR\ElasticsearchBundle\Event\PrePersistEvent;
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;
Expand All @@ -40,7 +43,7 @@ class Manager
private $config = [];

/**
* @var Client
* @var Client|LegacyClient
*/
private $client;

Expand Down Expand Up @@ -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
Expand All @@ -133,7 +136,7 @@ public function __construct(
/**
* Returns Elasticsearch connection.
*
* @return Client
* @return Client|LegacyClient
*/
public function getClient()
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
31 changes: 23 additions & 8 deletions Service/ManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@

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;
use ONGR\ElasticsearchBundle\Mapping\MetadataCollector;
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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Test/AbstractElasticsearchTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 2 additions & 12 deletions Tests/Functional/Mapping/DocumentFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
}
Expand All @@ -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();
}
}
10 changes: 8 additions & 2 deletions Tests/Functional/Profiler/ElasticsearchProfilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,7 +102,6 @@ public function testGetQueries()
'method' => 'GET',
'httpParameters' => [],
'scheme' => 'http',
'port' => 9200,
],
$lastQuery,
'Logged data did not match expected data.'
Expand Down Expand Up @@ -140,7 +140,6 @@ public function testGetTermQuery()
'method' => 'POST',
'httpParameters' => [],
'scheme' => 'http',
'port' => 9200,
],
$lastQuery,
'Logged data did not match expected data.'
Expand All @@ -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']);
}

/**
Expand Down
9 changes: 7 additions & 2 deletions Tests/Functional/Service/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
Loading