Skip to content
Snippets Groups Projects
Commit 1ca173ae authored by Aivaras Gotovskis's avatar Aivaras Gotovskis
Browse files

Refactor endpoints to allow retrieving set queries or filters.

parent 9582224e
No related branches found
No related tags found
No related merge requests found
Showing
with 563 additions and 115 deletions
...@@ -18,4 +18,5 @@ use ONGR\ElasticsearchDSL\Serializer\Normalizer\AbstractNormalizable; ...@@ -18,4 +18,5 @@ use ONGR\ElasticsearchDSL\Serializer\Normalizer\AbstractNormalizable;
*/ */
abstract class AbstractSearchEndpoint extends AbstractNormalizable implements SearchEndpointInterface abstract class AbstractSearchEndpoint extends AbstractNormalizable implements SearchEndpointInterface
{ {
use BuilderContainerAwareTrait;
} }
...@@ -21,17 +21,19 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface; ...@@ -21,17 +21,19 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
*/ */
class AggregationsEndpoint implements SearchEndpointInterface class AggregationsEndpoint implements SearchEndpointInterface
{ {
use BuilderContainerAwareTrait;
/** /**
* @var NamedBuilderBag * @var NamedBuilderBag
*/ */
private $bag; private $builderContainer;
/** /**
* Initialized aggregations bag. * Initialized aggregations bag.
*/ */
public function __construct() public function __construct()
{ {
$this->bag = new NamedBuilderBag(); $this->builderContainer = new NamedBuilderBag();
} }
/** /**
...@@ -39,9 +41,11 @@ class AggregationsEndpoint implements SearchEndpointInterface ...@@ -39,9 +41,11 @@ class AggregationsEndpoint implements SearchEndpointInterface
*/ */
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{ {
if (count($this->bag->all()) > 0) { if (count($this->builderContainer->all()) > 0) {
return $this->bag->toArray(); return $this->builderContainer->toArray();
} }
return null;
} }
/** /**
...@@ -49,16 +53,38 @@ class AggregationsEndpoint implements SearchEndpointInterface ...@@ -49,16 +53,38 @@ class AggregationsEndpoint implements SearchEndpointInterface
*/ */
public function addBuilder(BuilderInterface $builder, $parameters = []) public function addBuilder(BuilderInterface $builder, $parameters = [])
{ {
if ($builder instanceof NamedBuilderInterface) { if (!($builder instanceof NamedBuilderInterface)) {
$this->bag->add($builder); throw new \InvalidArgumentException('Builder must be named builder');
} }
$this->builderContainer->add($builder);
return $builder->getName();
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getBuilder() public function getBuilders()
{ {
return $this->bag->all(); return $this->builderContainer->all();
}
/**
* {@inheritdoc}
*/
public function getBuilder($key)
{
return $this->builderContainer->get($key);
}
/**
* {@inheritdoc}
*/
public function removeBuilder($key)
{
$this->builderContainer->remove($key);
return $this;
} }
} }
<?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\SearchEndpoint;
use ONGR\ElasticsearchDSL\BuilderInterface;
/**
* Trait to implement SearchEndpointInterface builder methods.
*/
trait BuilderContainerAwareTrait
{
/**
* @var BuilderInterface[]
*/
private $builderContainer = [];
/**
* @var array
*/
private $parameterContainer = [];
/**
* {@inheritdoc}
*/
public function addBuilder(BuilderInterface $builder, $parameters = [])
{
$this->builderContainer[] = $builder;
end($this->builderContainer);
$key = key($this->builderContainer);
$this->parameterContainer[$key] = $parameters;
return $key;
}
/**
* {@inheritdoc}
*/
public function removeBuilder($key)
{
if (array_key_exists($key, $this->builderContainer)) {
unset($this->builderContainer[$key]);
unset($this->parameterContainer[$key]);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function getBuilder($key)
{
if (array_key_exists($key, $this->builderContainer)) {
return $this->builderContainer[$key];
}
return null;
}
/**
* {@inheritdoc}
*/
public function getBuilders()
{
return $this->builderContainer;
}
/**
* {@inheritdoc}
*/
public function getBuilderParameters($key)
{
if (array_key_exists($key, $this->parameterContainer)) {
return $this->parameterContainer[$key];
}
return [];
}
/**
* {@inheritdoc}
*/
public function setBuilderParameters($key, $parameters)
{
$this->parameterContainer[$key] = $parameters;
return $this;
}
}
...@@ -25,12 +25,14 @@ class FilterEndpoint extends QueryEndpoint ...@@ -25,12 +25,14 @@ class FilterEndpoint extends QueryEndpoint
*/ */
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{ {
if ($this->getBuilder()) { $builder = $this->getBuilderForNormalization();
$query = new FilteredQuery(); if (empty($builder)) {
!$this->isBool() ? : $this->getBuilder()->setParameters($this->getParameters()); return;
$query->setFilter($this->getBuilder());
$this->addReference('filtered_query', $query);
} }
$query = new FilteredQuery();
$query->setFilter($builder);
$this->addReference('filtered_query', $query);
} }
/** /**
......
...@@ -19,8 +19,12 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface; ...@@ -19,8 +19,12 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
*/ */
class HighlightEndpoint implements SearchEndpointInterface class HighlightEndpoint implements SearchEndpointInterface
{ {
use BuilderContainerAwareTrait {
addBuilder as private traitAddBuilder;
}
/** /**
* @var BuilderInterface * @var int
*/ */
private $highlight; private $highlight;
...@@ -29,9 +33,11 @@ class HighlightEndpoint implements SearchEndpointInterface ...@@ -29,9 +33,11 @@ class HighlightEndpoint implements SearchEndpointInterface
*/ */
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{ {
if ($this->getBuilder()) { if (!$this->getBuilder($this->highlight)) {
return $this->getBuilder()->toArray(); return null;
} }
return $this->getBuilder($this->highlight)->toArray();
} }
/** /**
...@@ -39,14 +45,12 @@ class HighlightEndpoint implements SearchEndpointInterface ...@@ -39,14 +45,12 @@ class HighlightEndpoint implements SearchEndpointInterface
*/ */
public function addBuilder(BuilderInterface $builder, $parameters = []) public function addBuilder(BuilderInterface $builder, $parameters = [])
{ {
$this->highlight = $builder; if ($this->getBuilders()) {
} throw new \OverflowException('Only one highlight is expected');
}
$this->highlight = $this->traitAddBuilder($builder, $parameters);
/**
* {@inheritdoc}
*/
public function getBuilder()
{
return $this->highlight; return $this->highlight;
} }
} }
...@@ -24,14 +24,15 @@ class PostFilterEndpoint extends FilterEndpoint ...@@ -24,14 +24,15 @@ class PostFilterEndpoint extends FilterEndpoint
*/ */
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{ {
if ($this->getBuilder()) { $builder = $this->getBuilderForNormalization();
$postFilter = new PostFilter();
!$this->isBool() ? : $this->getBuilder()->setParameters($this->getParameters());
$postFilter->setFilter($this->getBuilder());
return $postFilter->toArray(); if (empty($builder)) {
return null;
} }
return null; $postFilter = new PostFilter();
$postFilter->setFilter($builder);
return $postFilter->toArray();
} }
} }
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
namespace ONGR\ElasticsearchDSL\SearchEndpoint; namespace ONGR\ElasticsearchDSL\SearchEndpoint;
use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\Filter\BoolFilter;
use ONGR\ElasticsearchDSL\ParametersTrait; use ONGR\ElasticsearchDSL\ParametersTrait;
use ONGR\ElasticsearchDSL\Query\BoolQuery; use ONGR\ElasticsearchDSL\Query\BoolQuery;
use ONGR\ElasticsearchDSL\Query\FilteredQuery; use ONGR\ElasticsearchDSL\Query\FilteredQuery;
...@@ -26,11 +27,6 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI ...@@ -26,11 +27,6 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI
{ {
use ParametersTrait; use ParametersTrait;
/**
* @var BuilderInterface|BoolQuery
*/
private $query;
/** /**
* @var OptionsResolver * @var OptionsResolver
*/ */
...@@ -45,73 +41,57 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI ...@@ -45,73 +41,57 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI
$this->configureResolver($this->resolver); $this->configureResolver($this->resolver);
} }
/**
* {@inheritdoc}
*/
public function addBuilder(BuilderInterface $builder, $parameters = [])
{
if (!$this->query && !(array_key_exists('bool_type', $parameters) && !empty($parameters['bool_type']))) {
$this->setBuilder($builder);
} else {
$parameters = $this->resolver->resolve(array_filter($parameters));
$this->isBool() ? : $this->convertToBool();
$this->query->add($builder, $parameters['bool_type']);
}
return $this;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{ {
$isRelevant = false;
if ($this->hasReference('filtered_query')) { if ($this->hasReference('filtered_query')) {
/** @var FilteredQuery $query */ /** @var FilteredQuery $query */
$query = $this->getReference('filtered_query'); $query = $this->getReference('filtered_query');
$this->addBuilder($query); $this->addBuilder($query);
$isRelevant = true;
} }
if ($this->getBuilder()) { $builder = $this->getBuilderForNormalization();
if (method_exists($this->getBuilder(), 'setParameters') && count($this->getParameters()) > 0) {
$this
->getBuilder()
->setParameters($this->processArray($this->getBuilder()->getParameters()));
}
$isRelevant = true; if (empty($builder)) {
return null;
} }
return $isRelevant ? [$this->getBuilder()->getType() => $this->getBuilder()->toArray()] : null; return [$builder->getType() => $builder->toArray()];
} }
/** /**
* {@inheritdoc} * Return builder that is ready to be normalized.
*
* @return BuilderInterface|null
*/ */
public function getOrder() protected function getBuilderForNormalization()
{ {
return 2; $builders = $this->getbuilders();
} if (empty($builders)) {
return null;
}
/** if (count($builders) > 1) {
* {@inheritdoc} $builder = $this->buildBool();
*/ } else {
public function getBuilder() $builder = end($builders);
{ }
return $this->query;
if (method_exists($builder, 'setParameters') && count($this->getParameters()) > 0) {
$builder->setParameters($this->processArray($builder->getParameters()));
}
return $builder;
} }
/** /**
* Sets builder. * {@inheritdoc}
*
* @param BuilderInterface $builder
*/ */
protected function setBuilder(BuilderInterface $builder) public function getOrder()
{ {
$this->query = $builder; return 2;
} }
/** /**
...@@ -127,20 +107,10 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI ...@@ -127,20 +107,10 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI
); );
} }
/**
* Returns true if query is bool.
*
* @return bool
*/
protected function isBool()
{
return $this->getBuilder() instanceof BoolQuery;
}
/** /**
* Returns bool instance for this endpoint case. * Returns bool instance for this endpoint case.
* *
* @return BoolQuery * @return BoolFilter|BoolQuery
*/ */
protected function getBoolInstance() protected function getBoolInstance()
{ {
...@@ -148,16 +118,19 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI ...@@ -148,16 +118,19 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI
} }
/** /**
* Converts query to bool. * Returns bool instance with builders set.
*
* @return BoolFilter|BoolQuery
*/ */
private function convertToBool() protected function buildBool()
{ {
$bool = $this->getBoolInstance(); $boolInstance = $this->getBoolInstance();
if ($this->query !== null) { foreach ($this->getBuilders() as $key => $builder) {
$bool->add($this->query); $parameters = $this->resolver->resolve(array_filter($this->getBuilderParameters($key)));
$boolInstance->add($builder, $parameters['bool_type']);
} }
$this->query = $bool; return $boolInstance;
} }
} }
...@@ -25,14 +25,49 @@ interface SearchEndpointInterface extends NormalizableInterface ...@@ -25,14 +25,49 @@ interface SearchEndpointInterface extends NormalizableInterface
* @param BuilderInterface $builder Builder to add. * @param BuilderInterface $builder Builder to add.
* @param array $parameters Additional parameters relevant to builder. * @param array $parameters Additional parameters relevant to builder.
* *
* @return SearchEndpointInterface * @return int Returns builder key.
*/ */
public function addBuilder(BuilderInterface $builder, $parameters = []); public function addBuilder(BuilderInterface $builder, $parameters = []);
/** /**
* Returns contained builder. * Removes contained builder.
* *
* @return BuilderInterface|BuilderInterface[] * @param int $key
*
* @return $this
*/
public function removeBuilder($key);
/**
* Returns contained builder or null if Builder is not found.
*
* @param int $key
*
* @return BuilderInterface|null
*/
public function getBuilder($key);
/**
* Returns all contained builders.
*
* @return BuilderInterface[]
*/
public function getBuilders();
/**
* Returns parameters for contained builder or empty array if parameters are not found.
*
* @param int $key
*
* @return array
*/
public function getBuilderParameters($key);
/**
* @param int $key
* @param array $parameters
*
* @return $this
*/ */
public function getBuilder(); public function setBuilderParameters($key, $parameters);
} }
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
namespace ONGR\ElasticsearchDSL\SearchEndpoint; namespace ONGR\ElasticsearchDSL\SearchEndpoint;
use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\Sort\AbstractSort;
use ONGR\ElasticsearchDSL\Sort\Sorts; use ONGR\ElasticsearchDSL\Sort\Sorts;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
...@@ -20,17 +21,8 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface; ...@@ -20,17 +21,8 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
*/ */
class SortEndpoint implements SearchEndpointInterface class SortEndpoint implements SearchEndpointInterface
{ {
/** use BuilderContainerAwareTrait {
* @var Sorts addBuilder as private traitAddBuilder;
*/
protected $sorts;
/**
* Initializes Sorts object.
*/
public function __construct()
{
$this->sorts = new Sorts();
} }
/** /**
...@@ -38,24 +30,40 @@ class SortEndpoint implements SearchEndpointInterface ...@@ -38,24 +30,40 @@ class SortEndpoint implements SearchEndpointInterface
*/ */
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{ {
if ($this->sorts->isRelevant()) { $sorts = $this->buildSorts();
return $this->sorts->toArray();
if ($sorts->isRelevant()) {
return $sorts->toArray();
} }
return null;
} }
/** /**
* {@inheritdoc} * Builds sorts object.
*
* @return Sorts
*/ */
public function addBuilder(BuilderInterface $builder, $parameters = []) protected function buildSorts()
{ {
$this->sorts->addSort($builder); $sorts = new Sorts();
/** @var AbstractSort $builder */
foreach ($this->getBuilders() as $builder) {
$sorts->addSort($builder);
}
return $sorts;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getBuilder() public function addBuilder(BuilderInterface $builder, $parameters = [])
{ {
return $this->sorts; if (!($builder instanceof AbstractSort)) {
throw new \InvalidArgumentException('Sort must must a subtype of AbstractSort');
}
return $this->traitAddBuilder($builder, $parameters);
} }
} }
<?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\SearchEndpoint\AggregationsEndpoint;
/**
* Class AggregationsEndpointTest.
*/
class AggregationsEndpointTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests constructor.
*/
public function testItCanBeInstantiated()
{
$this->assertInstanceOf(
'ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint',
new AggregationsEndpoint()
);
}
}
<?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\BuilderContainerAwareTrait;
/**
* Class BuilderContainerAwareTraitTest.
*/
class BuilderContainerAwareTraitTest extends \PHPUnit_Framework_TestCase
{
/**
* @var BuilderContainerAwareTrait|\PHPUnit_Framework_MockObject_MockObject
*/
private $instance;
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->instance = $this->getMockForTrait('ONGR\\ElasticsearchDSL\\SearchEndpoint\\BuilderContainerAwareTrait');
}
/**
* Tests builder parameters.
*/
public function testBuilderParameters()
{
$this->assertSame([], $this->instance->getBuilderParameters('non_existing_builder'));
$this->assertSame(
$this->instance,
$this->instance->setBuilderParameters('key', [ 'builder' => 'parameter' ])
);
$this->assertSame([ 'builder' => 'parameter' ], $this->instance->getBuilderParameters('key'));
}
/**
* Tests interactions with builders.
*/
public function testBuilders()
{
$this->assertSame([], $this->instance->getBuilders());
$this->assertNull($this->instance->getBuilder('non_existing_builder'));
$this->assertSame($this->instance, $this->instance->removeBuilder('non_existing_builder'));
/** @var BuilderInterface $builder1 */
$builder1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface');
$key1 = $this->instance->addBuilder($builder1, ['parameter' => 'value']);
$this->assertSame($builder1, $this->instance->getBuilder($key1));
$this->assertSame(['parameter' => 'value'], $this->instance->getBuilderParameters($key1));
$builders = $this->instance->getBuilders();
$this->assertCount(1, $builders);
$this->assertArrayHasKey($key1, $builders);
$this->assertSame($builder1, $builders[$key1]);
/** @var BuilderInterface $builder2 */
$builder2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface');
$key2 = $this->instance->addBuilder($builder2, ['parameter2' => 'value2']);
$this->assertSame($builder2, $this->instance->getBuilder($key2));
$this->assertSame(['parameter2' => 'value2'], $this->instance->getBuilderParameters($key2));
$builders = $this->instance->getBuilders();
$this->assertCount(2, $builders);
$this->assertArrayHasKey($key1, $builders);
$this->assertArrayHasKey($key2, $builders);
$this->assertSame($builder1, $builders[$key1]);
$this->assertSame($builder2, $builders[$key2]);
$this->instance->removeBuilder($key2);
$this->assertNull($this->instance->getBuilder($key2));
$this->assertSame([], $this->instance->getBuilderParameters($key2));
$builders = $this->instance->getBuilders();
$this->assertCount(1, $builders);
$this->assertArrayHasKey($key1, $builders);
$this->assertArrayNotHasKey($key2, $builders);
$this->assertSame($builder1, $builders[$key1]);
$this->assertSame($builder1, ($this->instance->getBuilder($key1)));
$this->instance->removeBuilder($key1);
$this->assertNull($this->instance->getBuilder($key1));
$this->assertSame([], $this->instance->getBuilderParameters($key1));
$this->assertSame([], $this->instance->getBuilders());
}
}
<?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\SearchEndpoint\FilterEndpoint;
/**
* Class FilterEndpointTest.
*/
class FilterEndpointTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests constructor.
*/
public function testItCanBeInstantiated()
{
$this->assertInstanceOf(
'ONGR\ElasticsearchDSL\SearchEndpoint\FilterEndpoint',
new FilterEndpoint()
);
}
}
<?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\SearchEndpoint\HighlightEndpoint;
/**
* Class HighlightEndpointTest.
*/
class HighlightEndpointTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests constructor.
*/
public function testItCanBeInstantiated()
{
$this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\HighlightEndpoint', new HighlightEndpoint());
}
}
<?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\SearchEndpoint\PostFilterEndpoint;
/**
* Class PostFilterEndpointTest.
*/
class PostFilterEndpointTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests constructor.
*/
public function testItCanBeInstantiated()
{
$this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\PostFilterEndpoint', new PostFilterEndpoint());
}
}
...@@ -11,7 +11,11 @@ ...@@ -11,7 +11,11 @@
namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\Query\BoolQuery;
use ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/** /**
* Unit test class for the QueryEndpoint. * Unit test class for the QueryEndpoint.
...@@ -25,4 +29,77 @@ class QueryEndpointTest extends \PHPUnit_Framework_TestCase ...@@ -25,4 +29,77 @@ class QueryEndpointTest extends \PHPUnit_Framework_TestCase
{ {
$this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint', new QueryEndpoint()); $this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint', new QueryEndpoint());
} }
/**
* Tests if correct order is returned.
*/
public function testGetOrder()
{
$instance = new QueryEndpoint();
$this->assertEquals(2, $instance->getOrder());
}
/**
* Tests if endpoint return correct normalized data.
*/
public function testEndpoint()
{
$instance = new QueryEndpoint();
/** @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(3))->method('toArray')->willReturn(['array' => 'data']);
$builderInterface1->expects($this->exactly(3))->method('getType')->willReturn('test');
$instance->addBuilder($builderInterface1);
$data = $instance->normalize($normalizerInterface);
$this->assertEquals(['test' => ['array' => 'data']], $data);
/** @var BuilderInterface|MockObject $builderInterface2 */
$builderInterface2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface');
$builderInterface2->expects($this->exactly(2))->method('toArray')->willReturn(['array2' => 'data2']);
$builderInterface2->expects($this->exactly(2))->method('getType')->willReturn('test2');
$instance->addBuilder($builderInterface2);
$data = $instance->normalize($normalizerInterface);
$this->assertEquals(
[
'bool' => [
'must' => [
[ 'test' => [ 'array' => 'data' ] ],
[ 'test2' => [ 'array2' => 'data2' ] ],
],
],
],
$data
);
/** @var BuilderInterface|MockObject $builderInterface3 */
$builderInterface3 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface');
$builderInterface3->expects($this->once())->method('toArray')->willReturn(['array3' => 'data3']);
$builderInterface3->expects($this->once())->method('getType')->willReturn('test3');
$instance->addBuilder($builderInterface3, ['bool_type' => BoolQuery::SHOULD]);
$instance->setParameters(['some' => 'parameter']);
$data = $instance->normalize($normalizerInterface);
$this->assertEquals(
[
'bool' => [
'must' => [
[ 'test' => [ 'array' => 'data' ] ],
[ 'test2' => [ 'array2' => 'data2' ] ],
],
'should' => [
[ 'test3' => [ 'array3' => 'data3' ] ],
],
'some' => 'parameter',
],
],
$data
);
}
} }
...@@ -27,4 +27,12 @@ class SearchEndpointFactoryTest extends \PHPUnit_Framework_TestCase ...@@ -27,4 +27,12 @@ class SearchEndpointFactoryTest extends \PHPUnit_Framework_TestCase
{ {
SearchEndpointFactory::get('foo'); SearchEndpointFactory::get('foo');
} }
/**
* Tests if factory can create endpoint.
*/
public function testFactory()
{
SearchEndpointFactory::get('aggregations');
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment