From 338267cdc580110c61813f2c2ed1e875b54081e6 Mon Sep 17 00:00:00 2001 From: Mantas <marc.mantas@gmail.com> Date: Fri, 17 Jun 2016 12:03:02 +0300 Subject: [PATCH] added SpanContaining query --- src/Query/Span/SpanContainingQuery.php | 61 ++++++++++++++++++++ tests/Query/Span/SpanContainingQueryTest.php | 57 ++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 src/Query/Span/SpanContainingQuery.php create mode 100644 tests/Query/Span/SpanContainingQueryTest.php diff --git a/src/Query/Span/SpanContainingQuery.php b/src/Query/Span/SpanContainingQuery.php new file mode 100644 index 0000000..233d272 --- /dev/null +++ b/src/Query/Span/SpanContainingQuery.php @@ -0,0 +1,61 @@ +<?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\Query\Span; + +/** + * Elasticsearch span containing query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-containing-query.html + */ +class SpanContainingQuery implements SpanQueryInterface +{ + /** + * @param SpanQueryInterface + */ + private $big; + + /** + * @param SpanQueryInterface + */ + private $little; + + /** + * @param SpanQueryInterface $big + * @param SpanQueryInterface $little + */ + public function __construct(SpanQueryInterface $big, SpanQueryInterface $little) + { + $this->big = $big; + $this->little = $little; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'span_containing'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $out = [ + 'little' => $this->little->toArray(), + 'big' => $this->big->toArray(), + ]; + + return [$this->getType() => $out]; + } +} diff --git a/tests/Query/Span/SpanContainingQueryTest.php b/tests/Query/Span/SpanContainingQueryTest.php new file mode 100644 index 0000000..328ffc3 --- /dev/null +++ b/tests/Query/Span/SpanContainingQueryTest.php @@ -0,0 +1,57 @@ +<?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\Query\Span; + +use ONGR\ElasticsearchDSL\Query\Span\SpanContainingQuery; + +/** + * Unit test for SpanContainingQuery. + */ +class SpanContainingQueryTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests for toArray(). + */ + public function testToArray() + { + $query = new SpanContainingQuery( + $this->getSpanQueryMack('foo'), + $this->getSpanQueryMack('bar') + ); + $result = [ + 'span_containing' => [ + 'little' => [ + 'span_term' => ['user' => 'bar'], + ], + 'big' => [ + 'span_term' => ['user' => 'foo'], + ], + ], + ]; + $this->assertEquals($result, $query->toArray()); + } + + /** + * @param string $value + * + * @returns \PHPUnit_Framework_MockObject_MockObject + */ + private function getSpanQueryMack($value) + { + $mock = $this->getMock('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface'); + $mock + ->expects($this->once()) + ->method('toArray') + ->willReturn(['span_term' => ['user' => $value]]); + return $mock; + } +} -- GitLab