From 434d19c15a90ce8c2e0dace98b98a56dc38f631e Mon Sep 17 00:00:00 2001 From: Karel Souffriau Date: Mon, 14 Jan 2019 16:51:30 +0100 Subject: [PATCH 01/13] Increase vendors --- .travis.yml | 16 +++------------- composer.json | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6972a30a6..709e50500 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,25 +15,18 @@ matrix: fast_finish: true include: # Minimum supported dependencies with min and max PHP version - - php: 5.6 - env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" - - php: 7.2 - env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" - - php: 7.3 + - php: 7.1 env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" # Latest supported dependencies with each PHP version - - php: 5.6 - - php: 7.0 - php: 7.1 - php: 7.2 - env: COVERAGE=true PHPUNIT_FLAGS="-v --coverage-clover=coverage.clover" - php: 7.3 env: COVERAGE=true PHPUNIT_FLAGS="-v --coverage-clover=coverage.clover" # Install all SF components in the same major version, see https://github.com/dunglas/symfony-lock - - php: 7.2 - env: SYMFONY_VERSION="^3" + - php: 7.3 + env: SYMFONY_VERSION="^4" before_install: - if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi @@ -51,9 +44,6 @@ install: before_script: - phpenv config-add .travis/php.ini - - if [ "$TRAVIS_PHP_VERSION" = "5.6" ]; then echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi; - - if [ "$TRAVIS_PHP_VERSION" = "5.6" ]; then composer require --dev --no-update doctrine/mongodb-odm=^1; fi; - - if [ "$TRAVIS_PHP_VERSION" = "7.0" ]; then echo "extension = mongodb.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi; - if [ "$TRAVIS_PHP_VERSION" = "7.1" ]; then echo "extension = mongodb.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi; script: diff --git a/composer.json b/composer.json index 9532d9a0b..63ed911c3 100644 --- a/composer.json +++ b/composer.json @@ -12,30 +12,30 @@ { "name": "Jeremy Mikola", "email": "jmikola@gmail.com" } ], "require": { - "php": "^5.6.0|^7.0", - "symfony/framework-bundle": "^3.2|^4", - "symfony/console": "^3.2|^4", - "symfony/dependency-injection": "^3.3|^4", - "symfony/property-access": "^3.2|^4", + "php": "^7.1", + "symfony/framework-bundle": "^3.4|^4", + "symfony/console": "^3.4|^4", + "symfony/dependency-injection": "^3.4|^4", + "symfony/property-access": "^3.4|^4", "pagerfanta/pagerfanta": "^1.0.5|^2.0", "psr/log": "^1.0", - "ruflin/elastica": "^5.2.1|^6.0" + "ruflin/elastica": "^5.3|^6.1" }, "require-dev": { "doctrine/orm": "^2.5", "doctrine/doctrine-bundle": "^1.6", - "doctrine/phpcr-bundle": "^1.3", + "doctrine/phpcr-bundle": "^1.3|^2.0", "doctrine/phpcr-odm": "^1.4", "jackalope/jackalope-doctrine-dbal": "^1.2", "jms/serializer-bundle": "^2.2", "phpunit/phpunit": "^5.7.11|^6.5", "knplabs/knp-components": "^1.2", - "symfony/expression-language" : "^3.2|^4", - "symfony/twig-bundle": "^3.2|^4", - "symfony/serializer": "^3.2|^4", - "symfony/yaml": "^3.2|^4", + "symfony/expression-language" : "^3.4|^4", + "symfony/twig-bundle": "^3.4|^4", + "symfony/serializer": "^3.4|^4", + "symfony/yaml": "^3.4|^4", "friendsofphp/php-cs-fixer": "^2.2", - "symfony/web-profiler-bundle": "^3.0|^4.0" + "symfony/web-profiler-bundle": "^3.4|^4.0" }, "suggest": { "enqueue/elastica-bundle": "The bundle adds extra features to FOSElasticaBundle bundle. Aimed to improve performance." From 01a0bf728621960ba6e7f8ab3923d7378592bf47 Mon Sep 17 00:00:00 2001 From: Jan Domanski Date: Sun, 13 Jan 2019 15:35:00 +0000 Subject: [PATCH 02/13] Implement simple unit tests - src/Event/ - src/Exception/ - src/Unit/EventListener --- tests/Unit/Event/IndexEventTest.php | 33 ++++++++++ tests/Unit/Event/IndexPopulateEventTest.php | 43 +++++++++++++ tests/Unit/Event/IndexResetEventTest.php | 36 +++++++++++ tests/Unit/Event/TransformEventTest.php | 56 +++++++++++++++++ tests/Unit/Event/TypePopulateEventTest.php | 24 ++++++++ tests/Unit/Event/TypeResetEventTest.php | 24 ++++++++ .../EventListener/PopulateListenerTest.php | 60 +++++++++++++++++++ .../Exception/AliasIsIndexExceptionTest.php | 23 +++++++ .../InvalidArgumentTypeExceptionTest.php | 23 +++++++ 9 files changed, 322 insertions(+) create mode 100644 tests/Unit/Event/IndexEventTest.php create mode 100644 tests/Unit/Event/IndexPopulateEventTest.php create mode 100644 tests/Unit/Event/IndexResetEventTest.php create mode 100644 tests/Unit/Event/TransformEventTest.php create mode 100644 tests/Unit/Event/TypePopulateEventTest.php create mode 100644 tests/Unit/Event/TypeResetEventTest.php create mode 100644 tests/Unit/EventListener/PopulateListenerTest.php create mode 100644 tests/Unit/Exception/AliasIsIndexExceptionTest.php create mode 100644 tests/Unit/Exception/InvalidArgumentTypeExceptionTest.php diff --git a/tests/Unit/Event/IndexEventTest.php b/tests/Unit/Event/IndexEventTest.php new file mode 100644 index 000000000..f22d4e9ed --- /dev/null +++ b/tests/Unit/Event/IndexEventTest.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Tests\Unit\Event; + +use FOS\ElasticaBundle\Event\IndexEvent; +use PHPUnit\Framework\TestCase; + +class IndexEventTest extends TestCase +{ + /** + * @var IndexEvent + */ + private $event; + + protected function setUp() + { + $this->event = new IndexEvent('index'); + } + + public function testIndex() + { + $this->assertEquals('index', $this->event->getIndex()); + } +} diff --git a/tests/Unit/Event/IndexPopulateEventTest.php b/tests/Unit/Event/IndexPopulateEventTest.php new file mode 100644 index 000000000..28cd58ce8 --- /dev/null +++ b/tests/Unit/Event/IndexPopulateEventTest.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Tests\Unit\Event; + +use FOS\ElasticaBundle\Event\IndexPopulateEvent; +use PHPUnit\Framework\TestCase; + +class IndexPopulateEventTest extends TestCase +{ + /** + * @var IndexPopulateEvent + */ + private $event; + + protected function setUp() + { + $this->event = new IndexPopulateEvent('index', false, []); + } + + public function testReset() + { + $this->assertFalse($this->event->isReset()); + $this->event->setReset(true); + $this->assertTrue($this->event->isReset()); + } + + public function testOption() + { + $this->expectException(\InvalidArgumentException::class); + $this->event->getOption('name'); + $this->event->setOption('name', 'value'); + $this->assertEquals('value', $this->event->getOption('name')); + } +} diff --git a/tests/Unit/Event/IndexResetEventTest.php b/tests/Unit/Event/IndexResetEventTest.php new file mode 100644 index 000000000..9f306e629 --- /dev/null +++ b/tests/Unit/Event/IndexResetEventTest.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Tests\Unit\Event; + +use FOS\ElasticaBundle\Event\IndexResetEvent; +use PHPUnit\Framework\TestCase; + +class IndexResetEventTest extends TestCase +{ + public function testForce() + { + $event = new IndexResetEvent('index', false, true); + $this->assertTrue($event->isForce()); + + $event = new IndexResetEvent('index', false, false); + $this->assertFalse($event->isForce()); + } + + public function testPopulating() + { + $event = new IndexResetEvent('index', true, false); + $this->assertTrue($event->isPopulating()); + + $event = new IndexResetEvent('index', false, false); + $this->assertFalse($event->isPopulating()); + } +} diff --git a/tests/Unit/Event/TransformEventTest.php b/tests/Unit/Event/TransformEventTest.php new file mode 100644 index 000000000..5aae506c8 --- /dev/null +++ b/tests/Unit/Event/TransformEventTest.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Tests\Unit\Event; + +use Elastica\Document; +use FOS\ElasticaBundle\Event\TransformEvent; +use PHPUnit\Framework\TestCase; + +class TransformEventTest extends TestCase +{ + /** + * @var TransformEvent + */ + private $event; + + protected function setUp() + { + $document = new Document(); + $object = (object) []; + $this->event = new TransformEvent($document, [], $object); + } + + public function testDocument() + { + $this->assertNotNull($this->event->getDocument()); + $document = new Document(); + $this->event->setDocument($document); + $this->assertEquals($document, $this->event->getDocument()); + } + + public function testFields() + { + $document = new Document(); + $object = (object) []; + $fields = ['abc', '123']; + $event = new TransformEvent($document, $fields, $object); + $this->assertEquals($fields, $event->getFields()); + } + + public function testObject() + { + $document = new Document(); + $object = (object) ['abc', '123']; + $event = new TransformEvent($document, [], $object); + $this->assertEquals($object, $event->getObject()); + } +} diff --git a/tests/Unit/Event/TypePopulateEventTest.php b/tests/Unit/Event/TypePopulateEventTest.php new file mode 100644 index 000000000..d8536a62f --- /dev/null +++ b/tests/Unit/Event/TypePopulateEventTest.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Tests\Unit\Event; + +use FOS\ElasticaBundle\Event\TypePopulateEvent; +use PHPUnit\Framework\TestCase; + +class TypePopulateEventTest extends TestCase +{ + public function testType() + { + $event = new TypePopulateEvent('index', 'type', false, []); + $this->assertEquals('type', $event->getType()); + } +} diff --git a/tests/Unit/Event/TypeResetEventTest.php b/tests/Unit/Event/TypeResetEventTest.php new file mode 100644 index 000000000..197a38d26 --- /dev/null +++ b/tests/Unit/Event/TypeResetEventTest.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Tests\Unit\Event; + +use FOS\ElasticaBundle\Event\TypeResetEvent; +use PHPUnit\Framework\TestCase; + +class TypeResetEventTest extends TestCase +{ + public function testType() + { + $event = new TypeResetEvent('index', 'type'); + $this->assertEquals('type', $event->getType()); + } +} diff --git a/tests/Unit/EventListener/PopulateListenerTest.php b/tests/Unit/EventListener/PopulateListenerTest.php new file mode 100644 index 000000000..f5260d6c8 --- /dev/null +++ b/tests/Unit/EventListener/PopulateListenerTest.php @@ -0,0 +1,60 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Tests\Unit\Event; + +use FOS\ElasticaBundle\EventListener\PopulateListener; +use FOS\ElasticaBundle\Event\IndexPopulateEvent; +use FOS\ElasticaBundle\Index\Resetter; +use PHPUnit\Framework\TestCase; + +class PopulateListenerTest extends TestCase +{ + private function mockResetter($numberOfCalls, $indexName, $deleteOption) + { + $stub = $this + ->getMockBuilder(Resetter::class) + ->disableOriginalConstructor() + ->getMock() + ; + + $stub + ->expects($this->exactly($numberOfCalls)) + ->method('switchIndexAlias') + ->with($indexName, $deleteOption); + + return $stub; + } + + public function testOnPostIndexPopulateWithReset() + { + $indexName = 'index'; + $deleteOption = true; + + $stub = $this->mockResetter(1, $indexName, $deleteOption); + $listener = new PopulateListener($stub); + + $event = new IndexPopulateEvent($indexName, true, ['delete' => $deleteOption]); + $listener->onPostIndexPopulate($event); + } + + public function testOnPostIndexPopulateWithoutReset() + { + $indexName = 'index'; + $deleteOption = true; + + $stub = $this->mockResetter(0, $indexName, $deleteOption); + $listener = new PopulateListener($stub); + + $event = new IndexPopulateEvent($indexName, false, ['delete' => $deleteOption]); + $listener->onPostIndexPopulate($event); + } +} diff --git a/tests/Unit/Exception/AliasIsIndexExceptionTest.php b/tests/Unit/Exception/AliasIsIndexExceptionTest.php new file mode 100644 index 000000000..6eaf30d49 --- /dev/null +++ b/tests/Unit/Exception/AliasIsIndexExceptionTest.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Tests\Unit\Event; + +use FOS\ElasticaBundle\Exception\AliasIsIndexException; +use PHPUnit\Framework\TestCase; + +class AliasIsIndexExceptionTest extends TestCase +{ + public function testConstruct() + { + $exception = new AliasIsIndexException('indexName'); + } +} diff --git a/tests/Unit/Exception/InvalidArgumentTypeExceptionTest.php b/tests/Unit/Exception/InvalidArgumentTypeExceptionTest.php new file mode 100644 index 000000000..6aecdc800 --- /dev/null +++ b/tests/Unit/Exception/InvalidArgumentTypeExceptionTest.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Tests\Unit\Event; + +use FOS\ElasticaBundle\Exception\InvalidArgumentTypeException; +use PHPUnit\Framework\TestCase; + +class InvalidArgumentTypeExceptionTest extends TestCase +{ + public function testConstruct() + { + $exception = new InvalidArgumentTypeException('value', 'expectedType'); + } +} From 2afbf9d1d17316b603941719d94437626bb7f163 Mon Sep 17 00:00:00 2001 From: Jan Domanski Date: Sun, 13 Jan 2019 20:50:19 +0000 Subject: [PATCH 03/13] Adding unit test coverage for Paginator classes --- .../Paginator/FantaPaginatorAdapterTest.php | 99 +++++++++++++ .../Paginator/RawPaginatorAdapterTest.php | 130 ++++++++++++++++++ 2 files changed, 229 insertions(+) create mode 100644 tests/Unit/Paginator/FantaPaginatorAdapterTest.php create mode 100644 tests/Unit/Paginator/RawPaginatorAdapterTest.php diff --git a/tests/Unit/Paginator/FantaPaginatorAdapterTest.php b/tests/Unit/Paginator/FantaPaginatorAdapterTest.php new file mode 100644 index 000000000..36d954731 --- /dev/null +++ b/tests/Unit/Paginator/FantaPaginatorAdapterTest.php @@ -0,0 +1,99 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Tests\Unit\Event; + +use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter; +use FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface; +use FOS\ElasticaBundle\Paginator\PartialResultsInterface; +use PHPUnit\Framework\TestCase; + +class FantaPaginatorAdapterTest extends TestCase +{ + private function mockPartialResults($results) + { + $mock = $this + ->getMockBuilder(PartialResultsInterface::class) + ->getMock(); + $mock + ->expects($this->exactly(1)) + ->method('toArray') + ->willReturn($results); + return $mock; + } + + private function mockPaginatorAdapter() + { + $mock = $this + ->getMockBuilder(PaginatorAdapterInterface::class) + ->getMock(); + return $mock; + } + + public function testGetNbResults() + { + $mock = $this->mockPaginatorAdapter(); + $mock + ->expects($this->exactly(1)) + ->method('getTotalHits') + ->willReturn(123); + $adapter = new FantaPaginatorAdapter($mock); + $this->assertEquals(123, $adapter->getNbResults()); + } + + public function testGetAggregations() + { + $mock = $this->mockPaginatorAdapter(); + $mock + ->expects($this->exactly(1)) + ->method('getAggregations') + ->willReturn([]); + $adapter = new FantaPaginatorAdapter($mock); + $this->assertEquals([], $adapter->getAggregations()); + } + + public function testGetSuggests() + { + $mock = $this->mockPaginatorAdapter(); + $mock + ->expects($this->exactly(1)) + ->method('getSuggests') + ->willReturn([]); + $adapter = new FantaPaginatorAdapter($mock); + $this->assertEquals([], $adapter->getSuggests()); + } + + public function testGetGetSlice() + { + $results = []; + $resultsMock = $this->mockPartialResults($results); + + $mock = $this->mockPaginatorAdapter(); + $mock + ->expects($this->exactly(1)) + ->method('getResults') + ->with(1, 10) + ->willReturn($resultsMock); + $adapter = new FantaPaginatorAdapter($mock); + $this->assertEquals($results, $adapter->getSlice(1, 10)); + } + + public function testGetMaxScore() + { + $mock = $this->mockPaginatorAdapter(); + $mock + ->expects($this->exactly(1)) + ->method('getMaxScore') + ->willReturn(123); + $adapter = new FantaPaginatorAdapter($mock); + $this->assertEquals(123, $adapter->getMaxScore()); + } +} diff --git a/tests/Unit/Paginator/RawPaginatorAdapterTest.php b/tests/Unit/Paginator/RawPaginatorAdapterTest.php new file mode 100644 index 000000000..13e3e2893 --- /dev/null +++ b/tests/Unit/Paginator/RawPaginatorAdapterTest.php @@ -0,0 +1,130 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Tests\Unit\Event; + +use FOS\ElasticaBundle\Paginator\RawPaginatorAdapter; +use Elastica\Query; +use Elastica\ResultSet; +use Elastica\SearchableInterface; +use InvalidArgumentException; +use PHPUnit\Framework\TestCase; + +class RawPaginatorAdapterTest extends TestCase +{ + private function mockResultSet() + { + $methods = ['getTotalHits', 'getAggregations', 'getSuggests', 'getMaxScore']; + $mock = $this + ->getMockBuilder(ResultSet::class) + ->disableOriginalConstructor() + ->setMethods($methods) + ->getMock(); + return $mock; + } + + private function mockSearchable() + { + $mock = $this + ->getMockBuilder(SearchableInterface::class) + ->getMock(); + return $mock; + } + + private function createAdapterWithSearch($methodName, $value) + { + $resultSet = $this->mockResultSet(); + $resultSet + ->expects($this->exactly(1)) + ->method($methodName) + ->willReturn($value); + + $query = new Query(); + $options = []; + $searchable = $this->mockSearchable(); + $searchable + ->expects($this->exactly(1)) + ->method('search') + ->with($query) + ->willReturn($resultSet); + + $adapter = new RawPaginatorAdapter($searchable, $query, $options); + return $adapter; + } + + private function createAdapterWithCount($totalHits, $querySize = null) + { + $query = new Query(); + if ($querySize) { + $query->setParam('size', $querySize); + } + $options = []; + $searchable = $this->mockSearchable(); + $searchable + ->expects($this->exactly(1)) + ->method('count') + ->willReturn($totalHits); + + $adapter = new RawPaginatorAdapter($searchable, $query, $options); + return $adapter; + } + + public function testGetTotalHits() + { + $adapter = $this->createAdapterWithCount(123); + $this->assertEquals(123, $adapter->getTotalHits()); + + $adapter = $this->createAdapterWithCount(123, 100); + $this->assertEquals(100, $adapter->getTotalHits()); + } + + public function testGetTotalHitsGenuineTotal() + { + $adapter = $this->createAdapterWithCount(123); + $this->assertEquals(123, $adapter->getTotalHits(true)); + + $adapter = $this->createAdapterWithCount(123, 100); + $this->assertEquals(123, $adapter->getTotalHits(true)); + } + + public function testGetAggregations() + { + $value = []; + $adapter = $this->createAdapterWithSearch('getAggregations', $value); + $this->assertEquals($value, $adapter->getAggregations()); + } + + public function testGetSuggests() + { + $value = []; + $adapter = $this->createAdapterWithSearch('getSuggests', $value); + $this->assertEquals($value, $adapter->getSuggests()); + } + + public function testGetMaxScore() + { + $value = 1.0; + $adapter = $this->createAdapterWithSearch('getMaxScore', $value); + $this->assertEquals($value, $adapter->getMaxScore()); + } + + public function testGetQuery() + { + $resultSet = $this->mockResultSet(); + + $query = new Query(); + $options = []; + $searchable = $this->mockSearchable($query); + + $adapter = new RawPaginatorAdapter($searchable, $query, $options); + $this->assertEquals($query, $adapter->getQuery()); + } +} From 0e8f74ef837c75d429812c7c868f002f92d6f583 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Tue, 15 Jan 2019 09:08:03 +0100 Subject: [PATCH 04/13] Fix deprecation for symfony/config 4.2+ --- src/DependencyInjection/Configuration.php | 47 +++++++++++++---------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 8bc31f827..82d79a0bf 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -77,8 +77,7 @@ public function getConfigTreeBuilder() */ public function getDynamicTemplateNode() { - $builder = new TreeBuilder(); - $node = $builder->root('dynamic_templates'); + $node = $this->createTreeBuilderNode('dynamic_templates'); $node ->prototype('array') @@ -108,8 +107,7 @@ public function getDynamicTemplateNode() */ protected function getTypesNode() { - $builder = new TreeBuilder(); - $node = $builder->root('types'); + $node = $this->createTreeBuilderNode('types'); $node ->useAttributeAsKey('name') @@ -168,8 +166,7 @@ protected function getTypesNode() */ protected function getPropertiesNode() { - $builder = new TreeBuilder(); - $node = $builder->root('properties'); + $node = $this->createTreeBuilderNode('properties'); $node ->useAttributeAsKey('name') @@ -184,8 +181,7 @@ protected function getPropertiesNode() */ protected function getIdNode() { - $builder = new TreeBuilder(); - $node = $builder->root('_id'); + $node = $this->createTreeBuilderNode('_id'); $node ->children() @@ -201,8 +197,7 @@ protected function getIdNode() */ protected function getSourceNode() { - $builder = new TreeBuilder(); - $node = $builder->root('_source'); + $node = $this->createTreeBuilderNode('_source'); $node ->children() @@ -228,8 +223,7 @@ protected function getSourceNode() */ protected function getRoutingNode() { - $builder = new TreeBuilder(); - $node = $builder->root('_routing'); + $node = $this->createTreeBuilderNode('_routing'); $node ->children() @@ -246,8 +240,7 @@ protected function getRoutingNode() */ protected function getParentNode() { - $builder = new TreeBuilder(); - $node = $builder->root('_parent'); + $node = $this->createTreeBuilderNode('_parent'); $node ->children() @@ -265,8 +258,7 @@ protected function getParentNode() */ protected function getAllNode() { - $builder = new TreeBuilder(); - $node = $builder->root('_all'); + $node = $this->createTreeBuilderNode('_all'); $node ->children() @@ -283,8 +275,7 @@ protected function getAllNode() */ protected function getPersistenceNode() { - $builder = new TreeBuilder(); - $node = $builder->root('persistence'); + $node = $this->createTreeBuilderNode('persistence'); $node ->validate() @@ -387,8 +378,7 @@ protected function getPersistenceNode() */ protected function getSerializerNode() { - $builder = new TreeBuilder(); - $node = $builder->root('serializer'); + $node = $this->createTreeBuilderNode('serializer'); $node ->addDefaultsIfNotSet() @@ -538,4 +528,21 @@ private function addIndexesSection(ArrayNodeDefinition $rootNode) ->end() ; } + + /** + * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition + */ + private function createTreeBuilderNode($name) + { + $builder = new TreeBuilder($name); + + if (method_exists($builder, 'getRootNode')) { + $node = $builder->getRootNode(); + } else { + // BC layer for symfony/config 4.1 and older + $node = $builder->root($name); + } + + return $node; + } } From 67003c2c819ac14a160cc1fcbbf993353da762ff Mon Sep 17 00:00:00 2001 From: Karel Date: Tue, 15 Jan 2019 18:03:49 +0100 Subject: [PATCH 05/13] Update CHANGELOG-5.0.md --- CHANGELOG-5.0.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG-5.0.md b/CHANGELOG-5.0.md index cc6538403..4b7b83709 100644 --- a/CHANGELOG-5.0.md +++ b/CHANGELOG-5.0.md @@ -4,6 +4,10 @@ CHANGELOG for 5.0.x This changelog references the relevant changes (bug and security fixes) done in 5.0 versions. +### 5.1.0 (2019-xx-xx) + +* Added compatibility with Symfony 4.2. + ### 5.0.3 (2018-02-07) * Fixed support for decorating object persister services. The refactoring done in 5.0.0 was breaking in such case. From 6e8213bcf89cbda089e8af2582896d56f22dda9e Mon Sep 17 00:00:00 2001 From: Karel Date: Tue, 15 Jan 2019 18:12:34 +0100 Subject: [PATCH 06/13] Update README.md --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6cd94259f..37b6832e7 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,10 @@ The following table shows the compatibilities of different versions of the bundl | FOSElasticaBundle | Elastica | Elasticsearch | Symfony | PHP | | --------------------------------------------------------------------------------------- | ---------| ------------- | ---------- | ----- | -| [5.x](https://github.com/FriendsOfSymfony/FOSElasticaBundle/tree/master) | ^5.2\|^6 | 5.\*\|6.\* | ^3.2\|^4 | >=5.6 | -| [4.x](https://github.com/FriendsOfSymfony/FOSElasticaBundle/tree/4.x) (unmaintained) | 3.2.\* | 2.\* | ^2.8\|^3.2 | >=5.5 | -| [3.2.x](https://github.com/FriendsOfSymfony/FOSElasticaBundle/tree/3.2.x) (unmaintained)| ^2.1 | 1.\* | ^2.3\|^3 | >=5.3 | +| [5.1] (master) | ^5.3\|^6 | 5.\*\|6.\* | ^3.4\|^4 | >=7.1 | +| [5.0] (unmaintained) | ^5.2\|^6 | 5.\*\|6.\* | ^3.2\|^4 | >=5.6 | +| [4.x] (unmaintained) | 3.2.\* | 2.\* | ^2.8\|^3.2 | >=5.5 | +| [3.2.x] (unmaintained) | ^2.1 | 1.\* | ^2.3\|^3 | >=5.3 | License ------- From 72725197f48e7ac45cee394b3a0e74cff40c9ded Mon Sep 17 00:00:00 2001 From: Karel Date: Tue, 15 Jan 2019 18:13:57 +0100 Subject: [PATCH 07/13] Create CHANGELOG-5.1.md --- CHANGELOG-5.1.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 CHANGELOG-5.1.md diff --git a/CHANGELOG-5.1.md b/CHANGELOG-5.1.md new file mode 100644 index 000000000..71806bf1c --- /dev/null +++ b/CHANGELOG-5.1.md @@ -0,0 +1,9 @@ +CHANGELOG for 5.1.x +=================== + +This changelog references the relevant changes (bug and security fixes) done +in 5.1 versions. + +### 5.1.0 (2019-xx-xx) + +* Added compatibility with Symfony 4.2. From c7595f2aa9baf96dedd8af7b84ba1e7cb038c1fe Mon Sep 17 00:00:00 2001 From: XWB Date: Tue, 15 Jan 2019 18:23:59 +0100 Subject: [PATCH 08/13] Revert "Update CHANGELOG-5.0.md" This reverts commit 67003c2c819ac14a160cc1fcbbf993353da762ff. --- CHANGELOG-5.0.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/CHANGELOG-5.0.md b/CHANGELOG-5.0.md index 4b7b83709..cc6538403 100644 --- a/CHANGELOG-5.0.md +++ b/CHANGELOG-5.0.md @@ -4,10 +4,6 @@ CHANGELOG for 5.0.x This changelog references the relevant changes (bug and security fixes) done in 5.0 versions. -### 5.1.0 (2019-xx-xx) - -* Added compatibility with Symfony 4.2. - ### 5.0.3 (2018-02-07) * Fixed support for decorating object persister services. The refactoring done in 5.0.0 was breaking in such case. From 4b08b200bc8aa95eb27996a5176b55104f6cddb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Doma=C5=84ski?= Date: Fri, 18 Jan 2019 13:00:16 +0000 Subject: [PATCH 09/13] Improving unit test code coverage for PagerProviderRegistry (#1486) * Improving unit test code coverage for PagerProviderRegistry * Remove duplicate headers as per XWB's review --- tests/Functional/TypeObj.php | 9 --- tests/Functional/TypeObject.php | 9 --- tests/Functional/app/ORM/IndexableService.php | 9 --- tests/Unit/Provider/IndexableTest.php | 9 --- .../Provider/PagerProviderRegistryTest.php | 81 +++++++++++++++++++ 5 files changed, 81 insertions(+), 36 deletions(-) create mode 100644 tests/Unit/Provider/PagerProviderRegistryTest.php diff --git a/tests/Functional/TypeObj.php b/tests/Functional/TypeObj.php index b047604b5..4daec76ae 100644 --- a/tests/Functional/TypeObj.php +++ b/tests/Functional/TypeObj.php @@ -9,15 +9,6 @@ * file that was distributed with this source code. */ -/** - * This file is part of the FOSElasticaBundle project. - * - * (c) Infinite Networks Pty Ltd - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace FOS\ElasticaBundle\Tests\Functional; class TypeObj diff --git a/tests/Functional/TypeObject.php b/tests/Functional/TypeObject.php index 02ae9538a..b663d12f0 100644 --- a/tests/Functional/TypeObject.php +++ b/tests/Functional/TypeObject.php @@ -9,15 +9,6 @@ * file that was distributed with this source code. */ -/** - * This file is part of the FOSElasticaBundle project. - * - * (c) Infinite Networks Pty Ltd - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace FOS\ElasticaBundle\Tests\Functional; class TypeObject diff --git a/tests/Functional/app/ORM/IndexableService.php b/tests/Functional/app/ORM/IndexableService.php index 5aecf0c50..cef885c11 100644 --- a/tests/Functional/app/ORM/IndexableService.php +++ b/tests/Functional/app/ORM/IndexableService.php @@ -9,15 +9,6 @@ * file that was distributed with this source code. */ -/** - * This file is part of the FOSElasticaBundle project. - * - * (c) Infinite Networks Pty Ltd - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace FOS\ElasticaBundle\Tests\Functional\app\ORM; class IndexableService diff --git a/tests/Unit/Provider/IndexableTest.php b/tests/Unit/Provider/IndexableTest.php index 5aac883bd..99f241936 100644 --- a/tests/Unit/Provider/IndexableTest.php +++ b/tests/Unit/Provider/IndexableTest.php @@ -9,15 +9,6 @@ * file that was distributed with this source code. */ -/** - * This file is part of the FOSElasticaBundle project. - * - * (c) Infinite Networks Pty Ltd - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace FOS\ElasticaBundle\Tests\Unit\Provider; use FOS\ElasticaBundle\Provider\Indexable; diff --git a/tests/Unit/Provider/PagerProviderRegistryTest.php b/tests/Unit/Provider/PagerProviderRegistryTest.php new file mode 100644 index 000000000..70a08fe65 --- /dev/null +++ b/tests/Unit/Provider/PagerProviderRegistryTest.php @@ -0,0 +1,81 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Tests\Unit\Provider; + +use FOS\ElasticaBundle\Provider\PagerProviderRegistry; +use Symfony\Component\DependencyInjection\Container; +use PHPUnit\Framework\TestCase; + +class PagerProviderRegistryTest extends TestCase +{ + protected function mockPagerProviderRegistry(array $providers, $service = null) + { + $container = new Container(); + $container->set('the_service_id', $service); + + $registry = new PagerProviderRegistry($providers); + $registry->setContainer($container); + return $registry; + } + + public function testGetAllProviders() + { + $providers = [ + 'index' => [ + 'type' => 'the_service_id', + ], + ]; + $service = new \stdClass(); + $registry = $this->mockPagerProviderRegistry($providers, $service); + $this->assertEquals(['index/type' => $service], $registry->getAllProviders('index', 'type')); + } + + public function testGetIndexProvidersValid() + { + $providers = [ + 'index' => [ + 'type' => 'the_service_id', + ], + ]; + $service = new \stdClass(); + $registry = $this->mockPagerProviderRegistry($providers, $service); + $this->assertEquals(['type' => $service], $registry->getIndexProviders('index', 'type')); + } + + public function testGetIndexProvidersInvalid() + { + $registry = $this->mockPagerProviderRegistry([]); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('No providers were registered for index "index".'); + $registry->getIndexProviders('index'); + } + + public function testGetProviderValid() + { + $providers = [ + 'index' => [ + 'type' => 'the_service_id', + ], + ]; + $service = new \stdClass(); + $registry = $this->mockPagerProviderRegistry($providers, $service); + $this->assertEquals($service, $registry->getProvider('index', 'type')); + } + + public function testGetProviderInvalid() + { + $registry = $this->mockPagerProviderRegistry([]); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('No provider was registered for index "index" and type "type".'); + $registry->getProvider('index', 'type'); + } +} From eaa3f646dd46a6bf5b524ddff6cea5288660b513 Mon Sep 17 00:00:00 2001 From: Jan Domanski Date: Fri, 18 Jan 2019 21:14:56 +0000 Subject: [PATCH 10/13] Improve unit test coverage for Repository - More opinionated than previous contributions - Relies on more recent phpunit features (withConsecutive) --- tests/Unit/RepositoryTest.php | 36 +++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/tests/Unit/RepositoryTest.php b/tests/Unit/RepositoryTest.php index 93601bdf5..163911b46 100644 --- a/tests/Unit/RepositoryTest.php +++ b/tests/Unit/RepositoryTest.php @@ -20,59 +20,67 @@ */ class RepositoryTest extends TestCase { - public function testThatFindCallsFindOnFinder() + public function testFind() { $testQuery = 'Test Query'; - $finderMock = $this->getFinderMock($testQuery); + $finderMock = $this->mockTransformedFinder('find', [$testQuery]); $repository = new Repository($finderMock); $repository->find($testQuery); } - public function testThatFindCallsFindOnFinderWithLimit() + public function testFindWithLimit() { $testQuery = 'Test Query'; $testLimit = 20; - $finderMock = $this->getFinderMock($testQuery, $testLimit); + $finderMock = $this->mockTransformedFinder('find', [$testQuery, $testLimit]); $repository = new Repository($finderMock); $repository->find($testQuery, $testLimit); } - public function testThatFindPaginatedCallsFindPaginatedOnFinder() + public function testFindPaginated() { $testQuery = 'Test Query'; - $finderMock = $this->getFinderMock($testQuery, [], 'findPaginated'); + $finderMock = $this->mockTransformedFinder('findPaginated', [$testQuery, []]); $repository = new Repository($finderMock); $repository->findPaginated($testQuery); } - public function testThatCreatePaginatorCreatesAPaginatorViaFinder() + public function testCreatePagitatorAdapter() { $testQuery = 'Test Query'; - $finderMock = $this->getFinderMock($testQuery, [], 'createPaginatorAdapter'); + $finderMock = $this->mockTransformedFinder('createPaginatorAdapter', [$testQuery, []]); $repository = new Repository($finderMock); $repository->createPaginatorAdapter($testQuery); } - public function testThatFindHybridCallsFindHybridOnFinder() + public function testCreateHybridPaginatorAdapter() { $testQuery = 'Test Query'; - $finderMock = $this->getFinderMock($testQuery, null, 'findHybrid'); + $finderMock = $this->mockTransformedFinder('createHybridPaginatorAdapter', [$testQuery]); + $repository = new Repository($finderMock); + $repository->createHybridPaginatorAdapter($testQuery); + } + + public function testFindHybrid() + { + $testQuery = 'Test Query'; + + $finderMock = $this->mockTransformedFinder('findHybrid', [$testQuery, null, []]); $repository = new Repository($finderMock); $repository->findHybrid($testQuery); } - private function getFinderMock($testQuery, $testLimit = null, $method = 'find') + private function mockTransformedFinder($name, $arguments) { $finderMock = $this->createMock(TransformedFinder::class); $finderMock->expects($this->once()) - ->method($method) - ->with($this->equalTo($testQuery), $this->equalTo($testLimit)); - + ->method($name) + ->withConsecutive($arguments); return $finderMock; } } From 7dbe69b390534f09027b731f6b089606d8dafa0b Mon Sep 17 00:00:00 2001 From: Jan Domanski Date: Fri, 18 Jan 2019 21:43:10 +0000 Subject: [PATCH 11/13] Testing AbstractElasticaToModelTransformer Remove getSortingClosure, as it is not used anywhere Introduce a UnitTestHelper class for some shared functionality --- .../AbstractElasticaToModelTransformer.php | 18 -------- ...AbstractElasticaToModelTransformerTest.php | 41 +++++++++++++++++++ tests/Unit/UnitTestHelper.php | 32 +++++++++++++++ 3 files changed, 73 insertions(+), 18 deletions(-) create mode 100644 tests/Unit/Transformer/AbstractElasticaToModelTransformerTest.php create mode 100644 tests/Unit/UnitTestHelper.php diff --git a/src/Transformer/AbstractElasticaToModelTransformer.php b/src/Transformer/AbstractElasticaToModelTransformer.php index 6f3103ca1..c811bce27 100644 --- a/src/Transformer/AbstractElasticaToModelTransformer.php +++ b/src/Transformer/AbstractElasticaToModelTransformer.php @@ -31,22 +31,4 @@ public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor) { $this->propertyAccessor = $propertyAccessor; } - - /** - * Returns a sorting closure to be used with usort() to put retrieved objects - * back in the order that they were returned by ElasticSearch. - * - * @param array $idPos - * @param string $identifierPath - * - * @return callable - */ - protected function getSortingClosure(array $idPos, $identifierPath) - { - $propertyAccessor = $this->propertyAccessor; - - return function ($a, $b) use ($idPos, $identifierPath, $propertyAccessor) { - return $idPos[(string) $propertyAccessor->getValue($a, $identifierPath)] > $idPos[(string) $propertyAccessor->getValue($b, $identifierPath)]; - }; - } } diff --git a/tests/Unit/Transformer/AbstractElasticaToModelTransformerTest.php b/tests/Unit/Transformer/AbstractElasticaToModelTransformerTest.php new file mode 100644 index 000000000..f9e462e26 --- /dev/null +++ b/tests/Unit/Transformer/AbstractElasticaToModelTransformerTest.php @@ -0,0 +1,41 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Tests\Unit\Transformer; + +use FOS\ElasticaBundle\Transformer\AbstractElasticaToModelTransformer; +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; +use FOS\ElasticaBundle\Tests\Unit\UnitTestHelper; + +class AbstractElasticaToModelTransformerTest extends UnitTestHelper +{ + public function testSetPropertyAccessor() + { + $propertyAccessor = $this->mockPropertyAccesor(); + $transformer = $this->mockAbstractElasticaToModelTransformer(); + $transformer->setPropertyAccessor($propertyAccessor); + $this->assertEquals($propertyAccessor, $this->getProtectedProperty($transformer, 'propertyAccessor')); + } + + protected function mockAbstractElasticaToModelTransformer() + { + $mock = $this + ->getMockBuilder(AbstractElasticaToModelTransformer::class) + ->getMockForAbstractClass(); + return $mock; + } + + protected function mockPropertyAccesor() + { + $mock = $this->createMock(PropertyAccessorInterface::class); + return $mock; + } +} diff --git a/tests/Unit/UnitTestHelper.php b/tests/Unit/UnitTestHelper.php new file mode 100644 index 000000000..0519a9d6a --- /dev/null +++ b/tests/Unit/UnitTestHelper.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Tests\Unit; + +use PHPUnit\Framework\TestCase; + +class UnitTestHelper extends TestCase +{ + /** + * Gets a protected property on a given object via reflection. + * + * @param object $object instance in which protected value is being modified + * @param string $property property on instance being modified + */ + protected function getProtectedProperty($object, string $property) + { + $reflection = new \ReflectionClass($object); + $reflectionProperty = $reflection->getProperty($property); + $reflectionProperty->setAccessible(true); + + return $reflectionProperty->getValue($object); + } +} From 8d785e77fb2e589cc598ad8d12b35006034dcea7 Mon Sep 17 00:00:00 2001 From: Jan Domanski Date: Tue, 22 Jan 2019 21:20:01 +0000 Subject: [PATCH 12/13] Improve ElasticaDataCollector coverage --- .../DataCollector/ElasticaDataCollectorTest.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/Unit/DataCollector/ElasticaDataCollectorTest.php b/tests/Unit/DataCollector/ElasticaDataCollectorTest.php index 79ba03500..f288f9370 100644 --- a/tests/Unit/DataCollector/ElasticaDataCollectorTest.php +++ b/tests/Unit/DataCollector/ElasticaDataCollectorTest.php @@ -13,14 +13,14 @@ use FOS\ElasticaBundle\DataCollector\ElasticaDataCollector; use FOS\ElasticaBundle\Logger\ElasticaLogger; -use PHPUnit\Framework\TestCase; +use FOS\ElasticaBundle\Tests\Unit\UnitTestHelper; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; /** * @author Richard Miller */ -class ElasticaDataCollectorTest extends TestCase +class ElasticaDataCollectorTest extends UnitTestHelper { public function testCorrectAmountOfQueries() { @@ -103,4 +103,17 @@ public function testName() $this->assertSame('elastica', $elasticaDataCollector->getName()); } + + public function testReset() + { + /** @var $loggerMock \PHPUnit_Framework_MockObject_MockObject|ElasticaLogger */ + $loggerMock = $this->createMock(ElasticaLogger::class); + $loggerMock->expects($this->once()) + ->method('reset') + ->willReturn('foo'); + + $elasticaDataCollector = new ElasticaDataCollector($loggerMock); + $elasticaDataCollector->reset(); + $this->assertSame([], $this->getProtectedProperty($elasticaDataCollector, 'data')); + } } From 6edcd71019aa7eea34220ad40b40201a56e0d2ae Mon Sep 17 00:00:00 2001 From: Jan Domanski Date: Tue, 22 Jan 2019 21:24:42 +0000 Subject: [PATCH 13/13] Improve coverage of Events --- tests/Unit/Event/IndexPopulateEventTest.php | 14 +++++++++++++- tests/Unit/Event/IndexResetEventTest.php | 3 +++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/Unit/Event/IndexPopulateEventTest.php b/tests/Unit/Event/IndexPopulateEventTest.php index 28cd58ce8..c400800f4 100644 --- a/tests/Unit/Event/IndexPopulateEventTest.php +++ b/tests/Unit/Event/IndexPopulateEventTest.php @@ -33,7 +33,19 @@ public function testReset() $this->assertTrue($this->event->isReset()); } - public function testOption() + public function testOptions() + { + $this->event->setOption('name', 'value'); + $this->assertEquals(['name' => 'value'], $this->event->getOptions()); + } + + public function testOptionValid() + { + $this->event->setOption('name', 'value'); + $this->assertEquals('value', $this->event->getOption('name')); + } + + public function testOptionInvalid() { $this->expectException(\InvalidArgumentException::class); $this->event->getOption('name'); diff --git a/tests/Unit/Event/IndexResetEventTest.php b/tests/Unit/Event/IndexResetEventTest.php index 9f306e629..5dfac08c0 100644 --- a/tests/Unit/Event/IndexResetEventTest.php +++ b/tests/Unit/Event/IndexResetEventTest.php @@ -23,6 +23,9 @@ public function testForce() $event = new IndexResetEvent('index', false, false); $this->assertFalse($event->isForce()); + + $event->setForce(true); + $this->assertTrue($event->isForce()); } public function testPopulating()