Skip to content

Commit

Permalink
Add support for Symfony 6 with Elasticsearch 7 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
joanna-bak-sourceability authored Jul 25, 2022
1 parent 80c890f commit 6c41255
Show file tree
Hide file tree
Showing 38 changed files with 152 additions and 126 deletions.
18 changes: 13 additions & 5 deletions .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,33 @@ jobs:
fail-fast: false
matrix:
include:
- php-version: '7.1'
- php-version: '8.0'
lint: false
symfony-version: '^2.8'

- php-version: '7.2'
- php-version: '8.0'
lint: false
symfony-version: '^3.4'

- php-version: '7.3'
- php-version: '8.0'
lint: false
symfony-version: '^4.4'

- php-version: '7.4'
- php-version: '8.0'
lint: true
symfony-version: '^5.0'

- php-version: '8.1'
lint: true
symfony-version: '^5.4'

- php-version: '8.0'
lint: true
symfony-version: '^5.0'
symfony-version: '^6.0'

- php-version: '8.1'
lint: true
symfony-version: '^6.0'

services:
elasticsearch:
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# ElasticsearchDSL

This is a fork of the [ongr/elasticsearch-dsl](https://github.com/ongr-io/elasticsearchbundle).
This is a fork of the [ongr/elasticsearch-dsl](https://github.com/ongr-io/ElasticsearchDSL).
With some basic changes to support wider range of Symfony Versions.

| Version | Supported Elasticsearch Version | Supported Symfony Version |
|---------|---------------------------------|---------------------------|
| 7.x | ^7.0 | ^5.0, ^4.0, ^3.4, ^2.8 |
| 6.x | ^6.0 | ^5.0, ^4.0, ^3.4, ^2.8 |
| 5.x | ^5.0 | ^5.0, ^4.0, ^3.4, ^2.8 |
| 2.x | ^2.0 | ^3.0, ^2.7 |
| 1.x | ^1.0 | ^3.0, ^2.7 |
| Version | Supported Elasticsearch Version | Supported Symfony Version |
|---------|---------------------------------|---------------------------------|
| 7.x | ^7.0 | ^6.0, ^5.0, ^4.0, ^3.4, ^2.8 |
| 6.x | ^6.0 | ^5.0, ^4.0, ^3.4, ^2.8 |
| 5.x | ^5.0 | ^5.0, ^4.0, ^3.4, ^2.8 |
| 2.x | ^2.0 | ^3.0, ^2.7 |
| 1.x | ^1.0 | ^3.0, ^2.7 |

## Documentation

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
}
],
"require": {
"php": "^7.1|^8.0",
"symfony/serializer": "^2.8 || ^3.4 || ^4.0 || ^5.0",
"ext-json": "*",
"php": "^8.0",
"symfony/serializer": "^2.8 || ^3.4 || ^4.0 || ^5.0 || ^6.0",
"elasticsearch/elasticsearch": "^7.0"
},
"require-dev": {
Expand Down
7 changes: 5 additions & 2 deletions src/SearchEndpoint/AggregationsEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ class AggregationsEndpoint extends AbstractSearchEndpoint
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
public function normalize(
NormalizerInterface $normalizer,
$format = null,
array $context = []
): array|string|int|float|bool {
$output = [];
if (count($this->getAll()) > 0) {
/** @var AbstractAggregation $aggregation */
Expand Down
9 changes: 6 additions & 3 deletions src/SearchEndpoint/HighlightEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ class HighlightEndpoint extends AbstractSearchEndpoint
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
public function normalize(
NormalizerInterface $normalizer,
$format = null,
array $context = []
): array|string|int|float|bool {
if ($this->highlight) {
return $this->highlight->toArray();
}

return null;
return false;
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/SearchEndpoint/InnerHitsEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ class InnerHitsEndpoint extends AbstractSearchEndpoint
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
public function normalize(
NormalizerInterface $normalizer,
$format = null,
array $context = []
): array|string|int|float|bool {
$output = [];
if (count($this->getAll()) > 0) {
/** @var NestedInnerHit $innerHit */
Expand Down
9 changes: 6 additions & 3 deletions src/SearchEndpoint/PostFilterEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ class PostFilterEndpoint extends QueryEndpoint
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
public function normalize(
NormalizerInterface $normalizer,
$format = null,
array $context = []
): array|string|int|float|bool {
if (!$this->getBool()) {
return null;
return false;
}

return $this->getBool()->toArray();
Expand Down
9 changes: 6 additions & 3 deletions src/SearchEndpoint/QueryEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
public function normalize(
NormalizerInterface $normalizer,
$format = null,
array $context = []
): array|string|int|float|bool {
if (!$this->filtersSet && $this->hasReference('filter_query')) {
/** @var BuilderInterface $filter */
$filter = $this->getReference('filter_query');
Expand All @@ -49,7 +52,7 @@ public function normalize(NormalizerInterface $normalizer, $format = null, array
}

if (!$this->bool) {
return null;
return false;
}

return $this->bool->toArray();
Expand Down
7 changes: 5 additions & 2 deletions src/SearchEndpoint/SortEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ class SortEndpoint extends AbstractSearchEndpoint
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
public function normalize(
NormalizerInterface $normalizer,
$format = null,
array $context = []
): array|string|int|float|bool {
$output = [];

foreach ($this->getAll() as $sort) {
Expand Down
7 changes: 5 additions & 2 deletions src/SearchEndpoint/SuggestEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ class SuggestEndpoint extends AbstractSearchEndpoint
/**
* {@inheritdoc}
*/
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{
public function normalize(
NormalizerInterface $normalizer,
$format = null,
array $context = []
): array|string|int|float|bool {
$output = [];
if (count($this->getAll()) > 0) {
/** @var TermSuggest $suggest */
Expand Down
15 changes: 5 additions & 10 deletions src/Serializer/Normalizer/CustomReferencedNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,15 @@ class CustomReferencedNormalizer extends CustomNormalizer
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = [])
{
public function normalize(
mixed $object,
$format = null,
array $context = []
): array|string|int|float|bool|\ArrayObject|null {
$object->setReferences($this->references);
$data = parent::normalize($object, $format, $context);
$this->references = array_merge($this->references, $object->getReferences());

return $data;
}

/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof AbstractNormalizable;
}
}
37 changes: 34 additions & 3 deletions src/Serializer/OrderedSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,34 @@
namespace ONGR\ElasticsearchDSL\Serializer;

use ONGR\ElasticsearchDSL\Serializer\Normalizer\OrderedNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Serializer;

/**
* Custom serializer which orders data before normalization.
*/
class OrderedSerializer extends Serializer
class OrderedSerializer implements NormalizerInterface, DenormalizerInterface
{
/**
* @var Serializer
*/
private $serializer;

/**
* @param array<NormalizerInterface|DenormalizerInterface> $normalizers
*/
public function __construct(array $normalizers = [])
{
$this->serializer = new Serializer($normalizers);
}

/**
* {@inheritdoc}
*/
public function normalize($data, $format = null, array $context = [])
{
return parent::normalize(
return $this->serializer->normalize(
is_array($data) ? $this->order($data) : $data,
$format,
$context
Expand All @@ -36,7 +51,7 @@ public function normalize($data, $format = null, array $context = [])
*/
public function denormalize($data, $type, $format = null, array $context = [])
{
return parent::denormalize(
return $this->serializer->denormalize(
is_array($data) ? $this->order($data) : $data,
$type,
$format,
Expand Down Expand Up @@ -85,4 +100,20 @@ function ($value) {
}
);
}

/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return $this->serializer->supportsDenormalization($data, $format);
}

/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $this->serializer->supportsNormalization($data, $format);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ class AdjacencyMatrixAggregationTest extends \PHPUnit\Framework\TestCase
{
// /**
// * Test if exception is thrown when not anonymous filter is without name.
// *
// * @expectedException \LogicException
// * @expectedExceptionMessage In not anonymous filters filter name must be set.
// */
// public function testIfExceptionIsThrown()
// {
// $this->expectException(\LogicException::class);
// $this->expectExceptionMessage('In not anonymous filters filter name must be set.');
// $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock();
// $aggregation = new FiltersAggregation('test_agg');
// $aggregation->addFilter($mock);
Expand Down
3 changes: 1 addition & 2 deletions tests/Unit/Aggregation/Bucketing/ChildrenAggregationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ class ChildrenAggregationTest extends \PHPUnit\Framework\TestCase
{
/**
* Tests if ChildrenAggregation#getArray throws exception when expected.
*
* @expectedException \LogicException
*/
public function testGetArrayException()
{
$this->expectException(\LogicException::class);
$aggregation = new ChildrenAggregation('foo');
$aggregation->getArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ class DateHistogramAggregationTest extends \PHPUnit\Framework\TestCase
{
/**
* Tests if ChildrenAggregation#getArray throws exception when expected.
*
* @expectedException \LogicException
*/
public function testGetArrayException()
{
$this->expectException(\LogicException::class);
$aggregation = new DateHistogramAggregation('foo');
$aggregation->getArray();
}
Expand Down
10 changes: 4 additions & 6 deletions tests/Unit/Aggregation/Bucketing/DateRangeAggregationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,22 @@ class DateRangeAggregationTest extends \PHPUnit\Framework\TestCase
{
/**
* Test if exception is thrown.
*
* @expectedException \LogicException
* @expectedExceptionMessage Date range aggregation must have field, format set and range added.
*/
public function testIfExceptionIsThrownWhenNoParametersAreSet()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Date range aggregation must have field, format set and range added.');
$agg = new DateRangeAggregation('test_agg');
$agg->getArray();
}

/**
* Test if exception is thrown when both range parameters are null.
*
* @expectedException \LogicException
* @expectedExceptionMessage Either from or to must be set. Both cannot be null.
*/
public function testIfExceptionIsThrownWhenBothRangesAreNull()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Either from or to must be set. Both cannot be null.');
$agg = new DateRangeAggregation('test_agg');
$agg->addRange(null, null);
}
Expand Down
10 changes: 4 additions & 6 deletions tests/Unit/Aggregation/Bucketing/FilterAggregationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,22 @@ public function testToArray($aggregation, $expectedResult)

/**
* Test for setField().
*
* @expectedException \LogicException
* @expectedExceptionMessage doesn't support `field` parameter
*/
public function testSetField()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('doesn\'t support `field` parameter');
$aggregation = new FilterAggregation('test_agg');
$aggregation->setField('test_field');
}

/**
* Test for toArray() without setting a filter.
*
* @expectedException \LogicException
* @expectedExceptionMessage has no filter added
*/
public function testToArrayNoFilter()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('has no filter added');
$aggregation = new FilterAggregation('test_agg');
$result = $aggregation->toArray();

Expand Down
5 changes: 2 additions & 3 deletions tests/Unit/Aggregation/Bucketing/FiltersAggregationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ class FiltersAggregationTest extends \PHPUnit\Framework\TestCase
{
/**
* Test if exception is thrown when not anonymous filter is without name.
*
* @expectedException \LogicException
* @expectedExceptionMessage In not anonymous filters filter name must be set.
*/
public function testIfExceptionIsThrown()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('In not anonymous filters filter name must be set.');
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock();
$aggregation = new FiltersAggregation('test_agg');
$aggregation->addFilter($mock);
Expand Down
Loading

0 comments on commit 6c41255

Please sign in to comment.