diff --git a/Query/Span/SpanFirstQueryTest.php b/Query/Span/SpanFirstQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..6805ed4936c320dc722831e7daed2eab0905a2b4 --- /dev/null +++ b/Query/Span/SpanFirstQueryTest.php @@ -0,0 +1,77 @@ +<?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\ElasticsearchBundle\Tests\Unit\DSL\Query\Span; + +use ONGR\ElasticsearchBundle\DSL\Query\Span\SpanFirstQuery; +use ONGR\ElasticsearchBundle\DSL\Query\Span\SpanQueryInterface; + +/** + * Unit test for SpanFirstQuery. + */ +class SpanFirstQueryTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Object. + */ + protected $mock; + + /** + * Create mock object. + */ + protected function setUp() + { + $this->mock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Query\Span\SpanQueryInterface')->getMock(); + $this->mock->expects($this->atMost(1)) + ->method('getType') + ->will($this->returnValue('span_or')); + $this->mock->expects($this->atMost(1)) + ->method('toArray') + ->will($this->returnValue(['key' => 'value'])); + } + + /** + * Reset mock object. + */ + public function tearDown() + { + $this->mock = null; + } + + /** + * Tests toArray method. + */ + public function testSpanFirstQueryToArray() + { + /** @var SpanQueryInterface $mock */ + $mock = $this->mock; + $query = new SpanFirstQuery($mock, 5); + $result = [ + 'match' => [ + 'span_or' => [ 'key' => 'value'], + ], + 'end' => 5, + ]; + $this->assertEquals($result, $query->toArray()); + } + + /** + * Tests get Type method. + */ + public function testSpanFirstQueryGetType() + { + /** @var SpanQueryInterface $mock */ + $mock = $this->mock; + $query = new SpanFirstQuery($mock, 5); + $result = $query->getType(); + $this->assertEquals('span_first', $result); + } +} diff --git a/Query/Span/SpanMultiTermQueryTest.php b/Query/Span/SpanMultiTermQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..d692a26b9f5e95ca43bbeb28661d9790f276f7e1 --- /dev/null +++ b/Query/Span/SpanMultiTermQueryTest.php @@ -0,0 +1,114 @@ +<?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\ElasticsearchBundle\Tests\Unit\DSL\Query\Span; + +use ONGR\ElasticsearchBundle\DSL\BuilderInterface; +use ONGR\ElasticsearchBundle\DSL\Query\Span\SpanMultiTermQuery; + +/** + * Unit test for SpanMultiTermQuery. + */ +class SpanMultiTermQueryTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var array. + */ + protected $mock; + + /** + * Create mock object. + */ + protected function setUp() + { + $allowedQueries = ['\FuzzyQuery', '\PrefixQuery', '\TermQuery', '\WildcardQuery', '\RegexpQuery']; + // Same constructors for all of these queries. + foreach ($allowedQueries as $query) { + $this->mock[$query] = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Query' . "{$query}") + ->setConstructorArgs(['field', 'value']) + ->getMock(); + $this->mock[$query]->expects($this->atMost(1)) + ->method('getType') + ->will($this->returnValue('span')); + $this->mock[$query]->expects($this->atMost(1)) + ->method('toArray') + ->will($this->returnValue(['field' => 'value'])); + } + } + + /** + * Reset mock object. + */ + public function tearDown() + { + $this->mock = null; + } + + /** + * Tests toArray method using these queries: Fuzzy, Prefix, Term, Wildcard, Regexp. + */ + public function testSpanMultiTermQueryToArray() + { + /** @var BuilderInterface $mock */ + $mock = $this->mock; + + foreach ($mock as $mocked) { + $query = new SpanMultiTermQuery($mocked); + $result = [ + 'match' => [ + 'span' => [ + 'field' => 'value', + ], + ], + ]; + $this->assertEquals($result, $query->toArray()); + } + } + + /** + * Tests toArray method using this query: Range. + */ + public function testSpanMultiTermQueryToArrayNext() + { + /** @var BuilderInterface $mock */ + $mock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Query\RangeQuery') + ->setConstructorArgs(['field', ['gte']]) + ->getMock(); + $mock->expects($this->once()) + ->method('getType') + ->will($this->returnValue('range')); + $mock->expects($this->once()) + ->method('toArray') + ->will($this->returnValue(['field' => ['gte']])); + + $query = new SpanMultiTermQuery($mock); + $result = [ + 'match' => [ + 'range' => [ + 'field' => ['gte'], + ], + ], + ]; + $this->assertEquals($result, $query->toArray()); + } + + /** + * Tests get Type method. + */ + public function testSpanMultiTermQueryGetType() + { + /** @var BuilderInterface $mock */ + $mock = $this->mock['\FuzzyQuery']; + $query = new SpanMultiTermQuery($mock); + $result = $query->getType(); + $this->assertEquals('span_multi', $result); + } +} diff --git a/Query/Span/SpanNearQueryTest.php b/Query/Span/SpanNearQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..0962be4d40742448d35c25752f32fc74bd2548e8 --- /dev/null +++ b/Query/Span/SpanNearQueryTest.php @@ -0,0 +1,82 @@ +<?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\ElasticsearchBundle\Tests\Unit\DSL\Query\Span; + +use ONGR\ElasticsearchBundle\DSL\Query\Span\SpanNearQuery; +use ONGR\ElasticsearchBundle\DSL\Query\Span\SpanQueryInterface; + +/** + * Unit test for SpanNearQuery. + */ +class SpanNearQueryTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Object. + */ + protected $mock; + + /** + * Create mock object. + */ + protected function setUp() + { + $this->mock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Query\Span\SpanQueryInterface')->getMock(); + $this->mock->expects($this->atMost(1)) + ->method('getType') + ->will($this->returnValue('span_or')); + $this->mock->expects($this->atMost(1)) + ->method('toArray') + ->will($this->returnValue(['key' => 'value'])); + } + + /** + * Reset mock object. + */ + public function tearDown() + { + $this->mock = null; + } + + /** + * Tests toArray method. + */ + public function testSpanMultiTermQueryToArray() + { + /** @var SpanQueryInterface $mock */ + $mock = $this->mock; + $query = new SpanNearQuery(['name']); + $query->setSlop(5); + $query->addQuery($mock); + $result = [ + 'clauses' => [ + 0 => [ + 'span_or' => [ + 'key' => 'value', + ], + ], + ], + 'slop' => 5, + 0 => 'name', + ]; + $this->assertEquals($result, $query->toArray()); + } + + /** + * Tests get Type method. + */ + public function testSpanNearQueryGetType() + { + $query = new SpanNearQuery(['name']); + $result = $query->getType(); + $this->assertEquals('span_near', $result); + } +} diff --git a/Query/Span/SpanNotQueryTest.php b/Query/Span/SpanNotQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..891b80e48150399a076e8e4eebb3be4d4b6027c5 --- /dev/null +++ b/Query/Span/SpanNotQueryTest.php @@ -0,0 +1,79 @@ +<?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\ElasticsearchBundle\Tests\Unit\DSL\Query\Span; + +use ONGR\ElasticsearchBundle\DSL\Query\Span\SpanNotQuery; +use ONGR\ElasticsearchBundle\DSL\Query\Span\SpanQueryInterface; + +/** + * Unit test for SpanNotQuery. + */ +class SpanNotQueryTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Object. + */ + protected $mock; + + /** + * Create mock object. + */ + protected function setUp() + { + $this->mock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Query\Span\SpanQueryInterface')->getMock(); + $this->mock->expects($this->atMost(2)) + ->method('getType') + ->will($this->returnValue('span_or')); + $this->mock->expects($this->atMost(2)) + ->method('toArray') + ->will($this->returnValue(['key' => 'value'])); + } + + /** + * Reset mock object. + */ + public function tearDown() + { + $this->mock = null; + } + + /** + * Tests get Type method. + */ + public function testSpanNotQueryGetType() + { + /** @var SpanQueryInterface $mock */ + $mock = $this->mock; + $query = new SpanNotQuery($mock, $mock); + $result = $query->getType(); + $this->assertEquals('span_not', $result); + } + + /** + * Tests toArray method. + */ + public function testSpanNotQueryToArray() + { + /** @var SpanQueryInterface $mock */ + $mock = $this->mock; + $query = new SpanNotQuery($mock, $mock); + $result = [ + 'include' => [ + 'span_or' => ['key' => 'value'], + ], + 'exclude' => [ + 'span_or' => ['key' => 'value'], + ], + ]; + $this->assertEquals($result, $query->toArray()); + } +} diff --git a/Query/Span/SpanOrQueryTest.php b/Query/Span/SpanOrQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..67aeb81780a29c3a8fa67fd8b2fa7263e64294e3 --- /dev/null +++ b/Query/Span/SpanOrQueryTest.php @@ -0,0 +1,81 @@ +<?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\ElasticsearchBundle\Tests\Unit\DSL\Query\Span; + +use ONGR\ElasticsearchBundle\DSL\Query\Span\SpanOrQuery; +use ONGR\ElasticsearchBundle\DSL\Query\Span\SpanQueryInterface; + +/** + * Unit test for SpanOrQuery. + */ +class SpanOrQueryTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Object. + */ + protected $mock; + + /** + * Create mock object. + */ + protected function setUp() + { + $this->mock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Query\Span\SpanQueryInterface')->getMock(); + $this->mock->expects($this->atMost(1)) + ->method('getType') + ->will($this->returnValue('span_or')); + $this->mock->expects($this->atMost(1)) + ->method('toArray') + ->will($this->returnValue(['key' => 'value'])); + } + + /** + * Reset mock object. + */ + public function tearDown() + { + $this->mock = null; + } + + /** + * Tests get Type method. + */ + public function testSpanOrQueryGetType() + { + $query = new SpanOrQuery(); + $result = $query->getType(); + $this->assertEquals('span_or', $result); + } + + /** + * Tests toArray method. + */ + public function testSpanOrQueryToArray() + { + /** @var SpanQueryInterface $mock */ + $mock = $this->mock; + $query = new SpanOrQuery(); + $query->addQuery($mock); + $result = [ + 'clauses' => [ + 0 => [ + 'span_or' => ['key' => 'value'], + ], + ], + ]; + $this->assertEquals($result, $query->toArray()); + + $result = $query->getQueries(); + $this->assertInternalType('array',$result); + $this->assertEquals(1,count($result)); + } +} diff --git a/Query/Span/SpanTermQueryTest.php b/Query/Span/SpanTermQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..1af7971f32461096cc088393ff849edf6ecca69f --- /dev/null +++ b/Query/Span/SpanTermQueryTest.php @@ -0,0 +1,30 @@ +<?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\ElasticsearchBundle\Tests\Unit\DSL\Query\Span; + +use ONGR\ElasticsearchBundle\DSL\Query\Span\SpanTermQuery; + +/** + * Unit test for SpanTermQuery. + */ +class SpanTermQueryTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests get Type method. + */ + public function testSpanTermQueryGetType() + { + $query = new SpanTermQuery('field', 'value'); + $result = $query->getType(); + $this->assertEquals('span_term', $result); + } +}