From 70b3559ac629c42ebe86a79557726fc631ffef7b Mon Sep 17 00:00:00 2001 From: Aivaras Gotovskis <aivaras.gotovskis@ongr.io> Date: Fri, 3 Jul 2015 12:22:07 +0300 Subject: [PATCH] Add missing tests for endpoints. --- src/SearchEndpoint/AggregationsEndpoint.php | 20 ++-- .../AggregationsEndpointTest.php | 94 +++++++++++++++++++ tests/SearchEndpoint/FilterEndpointTest.php | 54 +++++++++++ .../SearchEndpoint/HighlightEndpointTest.php | 42 +++++++++ .../SearchEndpoint/PostFilterEndpointTest.php | 24 +++++ tests/SearchEndpoint/QueryEndpointTest.php | 39 ++++++++ tests/SearchEndpoint/SortEndpointTest.php | 78 +++++++++++++++ 7 files changed, 343 insertions(+), 8 deletions(-) create mode 100644 tests/SearchEndpoint/SortEndpointTest.php diff --git a/src/SearchEndpoint/AggregationsEndpoint.php b/src/SearchEndpoint/AggregationsEndpoint.php index a92bd55..e9f9ae7 100644 --- a/src/SearchEndpoint/AggregationsEndpoint.php +++ b/src/SearchEndpoint/AggregationsEndpoint.php @@ -26,14 +26,14 @@ class AggregationsEndpoint implements SearchEndpointInterface /** * @var NamedBuilderBag */ - private $builderContainer; + private $bag; /** * Initialized aggregations bag. */ public function __construct() { - $this->builderContainer = new NamedBuilderBag(); + $this->bag = new NamedBuilderBag(); } /** @@ -41,8 +41,8 @@ class AggregationsEndpoint implements SearchEndpointInterface */ public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) { - if (count($this->builderContainer->all()) > 0) { - return $this->builderContainer->toArray(); + if (count($this->bag->all()) > 0) { + return $this->bag->toArray(); } return null; @@ -57,7 +57,7 @@ class AggregationsEndpoint implements SearchEndpointInterface throw new \InvalidArgumentException('Builder must be named builder'); } - $this->builderContainer->add($builder); + $this->bag->add($builder); return $builder->getName(); } @@ -67,7 +67,7 @@ class AggregationsEndpoint implements SearchEndpointInterface */ public function getBuilders() { - return $this->builderContainer->all(); + return $this->bag->all(); } /** @@ -75,7 +75,11 @@ class AggregationsEndpoint implements SearchEndpointInterface */ public function getBuilder($key) { - return $this->builderContainer->get($key); + if (!$this->bag->has($key)) { + return null; + } + + return $this->bag->get($key); } /** @@ -83,7 +87,7 @@ class AggregationsEndpoint implements SearchEndpointInterface */ public function removeBuilder($key) { - $this->builderContainer->remove($key); + $this->bag->remove($key); return $this; } diff --git a/tests/SearchEndpoint/AggregationsEndpointTest.php b/tests/SearchEndpoint/AggregationsEndpointTest.php index 078d56f..616d872 100644 --- a/tests/SearchEndpoint/AggregationsEndpointTest.php +++ b/tests/SearchEndpoint/AggregationsEndpointTest.php @@ -11,7 +11,11 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\NamedBuilderInterface; use ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint; +use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; /** * Class AggregationsEndpointTest. @@ -28,4 +32,94 @@ class AggregationsEndpointTest extends \PHPUnit_Framework_TestCase new AggregationsEndpoint() ); } + + /** + * Tests AddBuilder. + */ + public function testAddBuilder() + { + $instance = new AggregationsEndpoint(); + + /** @var NamedBuilderInterface|MockObject $builderInterface1 */ + $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\NamedBuilderInterface'); + $builderInterface1->expects($this->any())->method('getName')->willReturn('namedBuilder'); + $key = $instance->addBuilder($builderInterface1); + $this->assertSame('namedBuilder', $key); + $this->assertSame($builderInterface1, $instance->getBuilder($key)); + + /** @var BuilderInterface|MockObject $builderInterface2 */ + $builderInterface2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); + + $this->setExpectedException('InvalidArgumentException', 'Builder must be named builder'); + $instance->addBuilder($builderInterface2); + } + + /** + * Tests removing builders. + */ + public function testRemoveBuilder() + { + $instance = new AggregationsEndpoint(); + /** @var NamedBuilderInterface|MockObject $builderInterface1 */ + $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\NamedBuilderInterface'); + $builderInterface1->expects($this->any())->method('getName')->willReturn('namedBuilder'); + $key = $instance->addBuilder($builderInterface1); + + $this->assertNotNull($instance->getBuilder($key)); + $this->assertSame($instance, $instance->removeBuilder($key)); + $this->assertNull($instance->getBuilder($key)); + } + + /** + * Tests getting all builders. + */ + public function testGetBuilders() + { + $instance = new AggregationsEndpoint(); + $this->assertSame([], $instance->getBuilders()); + + /** @var NamedBuilderInterface|MockObject $builderInterface1 */ + $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\NamedBuilderInterface'); + $builderInterface1->expects($this->any())->method('getName')->willReturn('namedBuilder'); + + $instance->addBuilder($builderInterface1); + $this->assertSame(['namedBuilder' => $builderInterface1], $instance->getBuilders()); + + /** @var NamedBuilderInterface|MockObject $builderInterface2 */ + $builderInterface2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\NamedBuilderInterface'); + $builderInterface2->expects($this->any())->method('getName')->willReturn('namedBuilder2'); + + $instance->addBuilder($builderInterface2); + $this->assertSame( + [ + 'namedBuilder' => $builderInterface1, + 'namedBuilder2' => $builderInterface2, + ], + $instance->getBuilders() + ); + } + + /** + * Tests normalization builder. + */ + public function testNormalization() + { + $instance = new AggregationsEndpoint(); + /** @var NormalizerInterface|MockObject $normalizerInterface */ + $normalizerInterface = $this->getMockForAbstractClass( + 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' + ); + + $this->assertNull($instance->normalize($normalizerInterface)); + + /** @var NamedBuilderInterface|MockObject $builderInterface1 */ + $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\NamedBuilderInterface'); + $builderInterface1->expects($this->any())->method('getName')->willReturn('namedBuilder'); + $builderInterface1->expects($this->exactly(1))->method('toArray')->willReturn(['array' => 'data']); + $builderInterface1->expects($this->exactly(0))->method('getType')->willReturn('test'); + + $instance->addBuilder($builderInterface1); + + $this->assertSame(['array' => 'data'], $instance->normalize($normalizerInterface)); + } } diff --git a/tests/SearchEndpoint/FilterEndpointTest.php b/tests/SearchEndpoint/FilterEndpointTest.php index 33faf0d..3de6d22 100644 --- a/tests/SearchEndpoint/FilterEndpointTest.php +++ b/tests/SearchEndpoint/FilterEndpointTest.php @@ -11,7 +11,11 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\Query\FilteredQuery; use ONGR\ElasticsearchDSL\SearchEndpoint\FilterEndpoint; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; +use PHPUnit_Framework_MockObject_MockObject as MockObject; /** * Class FilterEndpointTest. @@ -28,4 +32,54 @@ class FilterEndpointTest extends \PHPUnit_Framework_TestCase new FilterEndpoint() ); } + + /** + * Tests if correct order is returned. + */ + public function testGetOrder() + { + $instance = new FilterEndpoint(); + $this->assertEquals(1, $instance->getOrder()); + } + + /** + * Test normalization. + */ + public function testNormalization() + { + $instance = new FilterEndpoint(); + /** @var NormalizerInterface|MockObject $normalizerInterface */ + $normalizerInterface = $this->getMockForAbstractClass( + 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' + ); + $this->assertNull($instance->normalize($normalizerInterface)); + $this->assertFalse($instance->hasReference('filtered_query')); + + /** @var BuilderInterface|MockObject $builderInterface1 */ + $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); + $builderInterface1->expects($this->exactly(1))->method('toArray')->willReturn(['array' => 'data']); + $builderInterface1->expects($this->exactly(1))->method('getType')->willReturn('test'); + $instance->addBuilder($builderInterface1); + + $this->assertNull($instance->normalize($normalizerInterface)); + $this->assertTrue($instance->hasReference('filtered_query')); + /** @var FilteredQuery $reference */ + $reference = $instance->getReference('filtered_query'); + $this->assertInstanceOf('ONGR\ElasticsearchDSL\Query\FilteredQuery', $reference); + $this->assertSame($builderInterface1, $reference->getFilter()); + + $instance = new FilterEndpoint(); + /** @var BuilderInterface|MockObject $builderInterface2 */ + $builderInterface2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); + $builderInterface2->expects($this->exactly(1))->method('toArray')->willReturn(['array2' => 'data2']); + $builderInterface2->expects($this->exactly(1))->method('getType')->willReturn('test2'); + $instance->addBuilder($builderInterface1); + $instance->addBuilder($builderInterface2); + + $this->assertNull($instance->normalize($normalizerInterface)); + $this->assertTrue($instance->hasReference('filtered_query')); + $reference = $instance->getReference('filtered_query'); + $this->assertInstanceOf('ONGR\ElasticsearchDSL\Query\FilteredQuery', $reference); + $this->assertInstanceOf('ONGR\ElasticsearchDSL\Filter\BoolFilter', $reference->getFilter()); + } } diff --git a/tests/SearchEndpoint/HighlightEndpointTest.php b/tests/SearchEndpoint/HighlightEndpointTest.php index e1b5934..90967c6 100644 --- a/tests/SearchEndpoint/HighlightEndpointTest.php +++ b/tests/SearchEndpoint/HighlightEndpointTest.php @@ -11,7 +11,10 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; +use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\SearchEndpoint\HighlightEndpoint; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; +use PHPUnit_Framework_MockObject_MockObject as MockObject; /** * Class HighlightEndpointTest. @@ -25,4 +28,43 @@ class HighlightEndpointTest extends \PHPUnit_Framework_TestCase { $this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\HighlightEndpoint', new HighlightEndpoint()); } + + /** + * Tests adding builder. + */ + public function testAddBuilder() + { + $instance = new HighlightEndpoint(); + + /** @var BuilderInterface|MockObject $builderInterface1 */ + $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); + $key = $instance->addBuilder($builderInterface1); + $this->assertNotNull($key); + $this->assertSame($builderInterface1, $instance->getBuilder($key)); + + $this->setExpectedException('OverflowException', 'Only one highlight is expected'); + $instance->addBuilder($builderInterface1); + } + + /** + * Tests adding builder. + */ + public function testNormalization() + { + $instance = new HighlightEndpoint(); + /** @var NormalizerInterface|MockObject $normalizerInterface */ + $normalizerInterface = $this->getMockForAbstractClass( + 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' + ); + + $this->assertNull($instance->normalize($normalizerInterface)); + + /** @var BuilderInterface|MockObject $builderInterface1 */ + $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); + $builderInterface1->expects($this->exactly(1))->method('toArray')->willReturn(['array' => 'data']); + $builderInterface1->expects($this->exactly(0))->method('getType')->willReturn('test'); + + $instance->addBuilder($builderInterface1); + $this->assertSame(['array' => 'data'], $instance->normalize($normalizerInterface)); + } } diff --git a/tests/SearchEndpoint/PostFilterEndpointTest.php b/tests/SearchEndpoint/PostFilterEndpointTest.php index 08e0593..857cdbc 100644 --- a/tests/SearchEndpoint/PostFilterEndpointTest.php +++ b/tests/SearchEndpoint/PostFilterEndpointTest.php @@ -11,7 +11,10 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; +use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\SearchEndpoint\PostFilterEndpoint; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; +use PHPUnit_Framework_MockObject_MockObject as MockObject; /** * Class PostFilterEndpointTest. @@ -25,4 +28,25 @@ class PostFilterEndpointTest extends \PHPUnit_Framework_TestCase { $this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\PostFilterEndpoint', new PostFilterEndpoint()); } + + /** + * Test normalization. + */ + public function testNormalization() + { + $instance = new PostFilterEndpoint(); + /** @var NormalizerInterface|MockObject $normalizerInterface */ + $normalizerInterface = $this->getMockForAbstractClass( + 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' + ); + $this->assertNull($instance->normalize($normalizerInterface)); + + /** @var BuilderInterface|MockObject $builderInterface1 */ + $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); + $builderInterface1->expects($this->exactly(1))->method('toArray')->willReturn(['array' => 'data']); + $builderInterface1->expects($this->exactly(1))->method('getType')->willReturn('test'); + + $instance->addBuilder($builderInterface1); + $this->assertSame(['test' => ['array' => 'data']], $instance->normalize($normalizerInterface)); + } } diff --git a/tests/SearchEndpoint/QueryEndpointTest.php b/tests/SearchEndpoint/QueryEndpointTest.php index 4ac1da1..e22cf4a 100644 --- a/tests/SearchEndpoint/QueryEndpointTest.php +++ b/tests/SearchEndpoint/QueryEndpointTest.php @@ -13,6 +13,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\Query\BoolQuery; +use ONGR\ElasticsearchDSL\Query\FilteredQuery; use ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint; use PHPUnit_Framework_MockObject_MockObject as MockObject; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; @@ -102,4 +103,42 @@ class QueryEndpointTest extends \PHPUnit_Framework_TestCase $data ); } + + /** + * Tests filtered query reference. + */ + public function testFilteredQuery() + { + $instance = new QueryEndpoint(); + /** @var NormalizerInterface|MockObject $normalizerInterface */ + $normalizerInterface = $this->getMockForAbstractClass( + 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' + ); + /** @var BuilderInterface|MockObject $builderInterface1 */ + $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); + $builderInterface1->expects($this->exactly(1))->method('toArray')->willReturn(['array' => 'data']); + $builderInterface1->expects($this->exactly(1))->method('getType')->willReturn('test'); + + /** @var BuilderInterface|MockObject $builderInterface2 */ + $builderInterface2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); + $builderInterface2->expects($this->exactly(1))->method('toArray')->willReturn(['array2' => 'data2']); + $builderInterface2->expects($this->exactly(1))->method('getType')->willReturn('test2'); + + $filteredQuery = new FilteredQuery($builderInterface1, $builderInterface2); + $instance->addReference('filtered_query', $filteredQuery); + + $this->assertSame( + [ + 'filtered' => [ + 'filter' => [ + 'test2' => ['array2' => 'data2'], + ], + 'query' => [ + 'test' => ['array' => 'data'], + ], + ], + ], + $instance->normalize($normalizerInterface) + ); + } } diff --git a/tests/SearchEndpoint/SortEndpointTest.php b/tests/SearchEndpoint/SortEndpointTest.php new file mode 100644 index 0000000..7ae8f11 --- /dev/null +++ b/tests/SearchEndpoint/SortEndpointTest.php @@ -0,0 +1,78 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint; +use ONGR\ElasticsearchDSL\Sort\AbstractSort; +use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; + +/** + * Class SortEndpointTest. + */ +class SortEndpointTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests constructor. + */ + public function testItCanBeInstantiated() + { + $this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint', new SortEndpoint()); + } + + /** + * Tests AddBuilder. + */ + public function testAddBuilder() + { + $instance = new SortEndpoint(); + + /** @var AbstractSort|MockObject $builderInterface1 */ + $builderInterface1 = $this->getMockBuilder('ONGR\ElasticsearchDSL\Sort\AbstractSort') + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $key = $instance->addBuilder($builderInterface1); + $this->assertNotNull($key); + $this->assertSame($builderInterface1, $instance->getBuilder($key)); + + /** @var BuilderInterface|MockObject $builderInterface2 */ + $builderInterface2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); + + $this->setExpectedException('InvalidArgumentException', 'Sort must must a subtype of AbstractSort'); + $instance->addBuilder($builderInterface2); + } + + /** + * Tests if endpoint return correct normalized data. + */ + public function testEndpoint() + { + $instance = new SortEndpoint(); + /** @var NormalizerInterface|MockObject $normalizerInterface */ + $normalizerInterface = $this->getMockForAbstractClass( + 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' + ); + $this->assertNull($instance->normalize($normalizerInterface)); + + /** @var AbstractSort|MockObject $builderInterface1 */ + $builderInterface1 = $this->getMockBuilder('ONGR\ElasticsearchDSL\Sort\AbstractSort') + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + $builderInterface1->expects($this->exactly(0))->method('toArray')->willReturn(['array' => 'data']); + $builderInterface1->expects($this->exactly(2))->method('getType')->willReturn('test'); + $instance->addBuilder($builderInterface1); + + $this->assertSame(['test' => ['order' => 'asc']], $instance->normalize($normalizerInterface)); + } +} -- GitLab