diff --git a/tests/Aggregation/CardinalityAggregationTest.php b/tests/Aggregation/CardinalityAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..75b9d19b53f5a8c5887eb2ecfbb25d3dd607cf61 --- /dev/null +++ b/tests/Aggregation/CardinalityAggregationTest.php @@ -0,0 +1,74 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\CardinalityAggregation; + +/** + * Unit test for cardinality aggregation. + */ +class CardinalityAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests getArray method. + */ + public function testGetArray() + { + $aggregation = new CardinalityAggregation('bar'); + + $aggregation->setScript('foo'); + $result = $aggregation->getArray(); + + $this->assertArrayHasKey('script', $result, 'key=script when script is set'); + $this->assertEquals('foo', $result['script'], 'script=foo when scripts name=foo'); + + $aggregation->setField('foo'); + $result = $aggregation->getArray(); + + $this->assertArrayHasKey('field', $result, 'key=field when field is set'); + $this->assertEquals('foo', $result['field'], 'field=foo when fields name=foo'); + + $aggregation->setPrecisionThreshold(10); + $result = $aggregation->getArray(); + + $this->assertArrayHasKey('precision_threshold', $result, 'key=precision_threshold when is set'); + $this->assertEquals(10, $result['precision_threshold'], 'precision_threshold=10 when is set'); + + $aggregation->setRehash(true); + $result = $aggregation->getArray(); + + $this->assertArrayHasKey('rehash', $result, 'key=rehash when rehash is set'); + $this->assertEquals(true, $result['rehash'], 'rehash=true when rehash is set to true'); + } + + /** + * Tests if CardinalityAggregation#getArray throws exception when expected. + * + * @expectedException \LogicException + * @expectedExceptionMessage Cardinality aggregation must have field or script set. + */ + public function testGetArrayException() + { + $aggregation = new CardinalityAggregation('bar'); + $aggregation->getArray(); + } + + /** + * Tests getType method. + */ + public function testCardinallyAggregationGetType() + { + $aggregation = new CardinalityAggregation('foo'); + $result = $aggregation->getType(); + $this->assertEquals('cardinality', $result); + } +} diff --git a/tests/Aggregation/ChildrenAggregationTest.php b/tests/Aggregation/ChildrenAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..11e4446099eb0c48fb110edcc80205535cb1d9bc --- /dev/null +++ b/tests/Aggregation/ChildrenAggregationTest.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\ElasticsearchBundle\Tests\Unit\DSL\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\ChildrenAggregation; + +/** + * Unit test for children aggregation. + */ +class ChildrenAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests if ChildrenAggregation#getArray throws exception when expected. + * + * @expectedException \LogicException + */ + public function testGetArrayException() + { + $aggregation = new ChildrenAggregation('foo'); + $aggregation->getArray(); + } + + /** + * Tests getType method. + */ + public function testChildrenAggregationGetType() + { + $aggregation = new ChildrenAggregation('foo'); + $result = $aggregation->getType(); + $this->assertEquals('children', $result); + } + + /** + * Tests getArray method. + */ + public function testChildrenAggregationGetArray() + { + $mock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Aggregation\AbstractAggregation') + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + $aggregation = new ChildrenAggregation('foo'); + $aggregation->addAggregation($mock); + $aggregation->setChildren('question'); + $result = $aggregation->getArray(); + $expected = ['type' => 'question']; + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Aggregation/DateRangeAggregationTest.php b/tests/Aggregation/DateRangeAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..f2643adaaf0d311824f8245c51fea4bc0aac5c98 --- /dev/null +++ b/tests/Aggregation/DateRangeAggregationTest.php @@ -0,0 +1,69 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\DateRangeAggregation; + +class DateRangeAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test if exception is thrown. + * + * @expectedException \LogicException + * @expectedExceptionMessage Date range aggregation must have field, format set and range added. + */ + public function testIfExceptionIsThrownWhenNoParametersAreSet() + { + $agg = new DateRangeAggregation('test_agg'); + $agg->getArray(); + } + + /** + * Test if exception is thrown when both range parameters are null. + * + * @expectedException \LogicException + * @expectedExceptionMessage Either from or to must be set. Both cannot be null. + */ + public function testIfExceptionIsThrownWhenBothRangesAreNull() + { + $agg = new DateRangeAggregation('test_agg'); + $agg->addRange(null, null); + } + + /** + * Test getArray method. + */ + public function testDateRangeAggregationGetArray() + { + $agg = new DateRangeAggregation('foo'); + $agg->addRange(10, 20); + $agg->setFormat('bar'); + $agg->setField('baz'); + $result = $agg->getArray(); + $expected = [ + 'format' => 'bar', + 'field' => 'baz', + 'ranges' => [['from' => 10, 'to' => 20]], + ]; + $this->assertEquals($expected, $result); + } + + /** + * Tests getType method. + */ + public function testDateRangeAggregationGetType() + { + $aggregation = new DateRangeAggregation('foo'); + $result = $aggregation->getType(); + $this->assertEquals('date_range', $result); + } +} diff --git a/tests/Aggregation/FilterAggregationTest.php b/tests/Aggregation/FilterAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..520c18bb767def7cbbc7794d8678e3b65244f04d --- /dev/null +++ b/tests/Aggregation/FilterAggregationTest.php @@ -0,0 +1,146 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\FilterAggregation; +use ONGR\ElasticsearchBundle\DSL\Filter\AndFilter; +use ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter; + +class FilterAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Data provider for testToArray. + * + * @return array + */ + public function getToArrayData() + { + $out = []; + + // Case #0 filter aggregation. + $aggregation = new FilterAggregation('test_agg'); + + $filter = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->setMethods(['toArray', 'getType']) + ->getMockForAbstractClass(); + $filter->expects($this->any()) + ->method('getType') + ->willReturn('test_filter'); + $filter->expects($this->any()) + ->method('toArray') + ->willReturn(['test_field' => ['test_value' => 'test']]); + + $aggregation->setFilter($filter); + + $result = [ + 'agg_test_agg' => [ + 'filter' => [ + 'test_filter' => [ + 'test_field' => ['test_value' => 'test'], + ], + ], + ], + ]; + + $out[] = [ + $aggregation, + $result, + ]; + + // Case #1 nested filter aggregation. + $aggregation = new FilterAggregation('test_agg'); + $aggregation->setFilter($filter); + + $aggregation2 = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Aggregation\AbstractAggregation') + ->disableOriginalConstructor() + ->setMethods(['toArray', 'getName']) + ->getMockForAbstractClass(); + $aggregation2->expects($this->any()) + ->method('toArray') + ->willReturn(['agg_test_agg2' => ['avg' => []]]); + $aggregation2->expects($this->any()) + ->method('getName') + ->willReturn('agg_test_agg2'); + + $aggregation->addAggregation($aggregation2); + + $result = [ + 'agg_test_agg' => [ + 'filter' => [ + 'test_filter' => [ + 'test_field' => ['test_value' => 'test'], + ], + ], + 'aggregations' => [ + 'agg_test_agg2' => [ + 'avg' => [], + ], + ], + ], + ]; + + $out[] = [ + $aggregation, + $result, + ]; + + return $out; + } + + /** + * Test for filter aggregation toArray() method. + * + * @param FilterAggregation $aggregation + * @param array $expectedResult + * + * @dataProvider getToArrayData + */ + public function testToArray($aggregation, $expectedResult) + { + $this->assertEquals($expectedResult, $aggregation->toArray()); + } + + /** + * Test for setField(). + * + * @expectedException \LogicException + * @expectedExceptionMessage doesn't support `field` parameter + */ + public function testSetField() + { + $aggregation = new FilterAggregation('test_agg'); + $aggregation->setField('test_field'); + } + + /** + * Test for toArray() without setting a filter. + * + * @expectedException \LogicException + * @expectedExceptionMessage has no filter added + */ + public function testToArrayNoFilter() + { + $aggregation = new FilterAggregation('test_agg'); + $aggregation->toArray(); + } + + /** + * Test for toArray() with setting a filter. + */ + public function testToArrayWithFilter() + { + $aggregation = new FilterAggregation('test_agg'); + + $aggregation->setFilter(new MissingFilter('test')); + $aggregation->toArray(); + } +} diff --git a/tests/Aggregation/FiltersAggregationTest.php b/tests/Aggregation/FiltersAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..38c9cae2ab577436b14891ea8b8bdbaa27862297 --- /dev/null +++ b/tests/Aggregation/FiltersAggregationTest.php @@ -0,0 +1,100 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\FiltersAggregation; + +/** + * Unit test for filters aggregation. + */ +class FiltersAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test if exception is thrown when not anonymous filter is without name. + * + * @expectedException \LogicException + * @expectedExceptionMessage In not anonymous filters filter name must be set. + */ + public function testIfExceptionIsThrown() + { + $mock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface')->getMock(); + $aggregation = new FiltersAggregation('test_agg'); + $aggregation->addFilter($mock); + } + + /** + * Test GetArray method. + */ + public function testFiltersAggregationGetArray() + { + $mock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface')->getMock(); + $aggregation = new FiltersAggregation('test_agg'); + $aggregation->setAnonymous(true); + $aggregation->addFilter($mock, 'name'); + $result = $aggregation->getArray(); + $this->assertArrayHasKey('filters', $result); + } + + /** + * Tests getType method. + */ + public function testFiltersAggregationGetType() + { + $aggregation = new FiltersAggregation('foo'); + $result = $aggregation->getType(); + $this->assertEquals('filters', $result); + } + + /** + * Test for filter aggregation toArray() method. + */ + public function testToArray() + { + $aggregation = new FiltersAggregation('test_agg'); + $filter = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->setMethods(['toArray', 'getType']) + ->getMockForAbstractClass(); + $filter->expects($this->any()) + ->method('getType') + ->willReturn('test_filter'); + $filter->expects($this->any()) + ->method('toArray') + ->willReturn(['test_field' => ['test_value' => 'test']]); + + $aggregation->addFilter($filter, 'first'); + $aggregation->addFilter($filter, 'second'); + $results = $aggregation->toArray(); + $expected = [ + 'agg_test_agg' => [ + 'filters' => [ + 'filters' => [ + 'first' => [ + 'test_filter' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + 'second' => [ + 'test_filter' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + ], + ], + ], + ]; + $this->assertEquals($expected, $results); + } +} diff --git a/tests/Aggregation/GeoBoundsAggregationTest.php b/tests/Aggregation/GeoBoundsAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..829f371ef59cff8252d6d1f15b44de6d737ff42a --- /dev/null +++ b/tests/Aggregation/GeoBoundsAggregationTest.php @@ -0,0 +1,71 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\GeoBoundsAggregation; + +/** + * Unit test for geo bounds aggregation. + */ +class GeoBoundsAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test if exception is thrown. + * + * @expectedException \LogicException + */ + public function testGeoBoundsAggregationException() + { + $agg = new GeoBoundsAggregation('test_agg'); + $agg->getArray(); + } + + /** + * Tests getType method. + */ + public function testGeoBoundsAggregationGetType() + { + $agg = new GeoBoundsAggregation('foo'); + $result = $agg->getType(); + $this->assertEquals('geo_bounds', $result); + } + + /** + * Tests getArray method. + */ + public function testGeoBoundsAggregationGetArray() + { + $agg = new GeoBoundsAggregation('foo'); + $agg->setField('bar'); + $agg->setWrapLongitude(true); + $result = [ + 'agg_foo' => [ + 'geo_bounds' => [ + 'field' => 'bar', + 'wrap_longitude' => true, + ], + ], + ]; + $this->assertEquals($result, $agg->toArray(), 'when wraplongitude is true'); + + $agg->setWrapLongitude(false); + $result = [ + 'agg_foo' => [ + 'geo_bounds' => [ + 'field' => 'bar', + 'wrap_longitude' => false, + ], + ], + ]; + $this->assertEquals($result, $agg->toArray(), 'when wraplongitude is false'); + } +} diff --git a/tests/Aggregation/GeoDistanceAggregationTest.php b/tests/Aggregation/GeoDistanceAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..4dd3e0ece475d757f7a795f3df2257bd988adf95 --- /dev/null +++ b/tests/Aggregation/GeoDistanceAggregationTest.php @@ -0,0 +1,115 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\GeoDistanceAggregation; + +class GeoDistanceAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test if exception is thrown when field is not set. + * + * @expectedException \LogicException + * @expectedExceptionMessage Geo distance aggregation must have a field set. + */ + public function testGeoDistanceAggregationExceptionWhenFieldIsNotSet() + { + $agg = new GeoDistanceAggregation('test_agg'); + $agg->setOrigin('50, 70'); + $agg->getArray(); + } + + /** + * Test if exception is thrown when origin is not set. + * + * @expectedException \LogicException + * @expectedExceptionMessage Geo distance aggregation must have an origin set. + */ + public function testGeoDistanceAggregationExceptionWhenOriginIsNotSet() + { + $agg = new GeoDistanceAggregation('test_agg'); + $agg->setField('location'); + $agg->getArray(); + } + + /** + * Test if exception is thrown when field is not set. + * + * @expectedException \LogicException + * @expectedExceptionMessage Either from or to must be set. Both cannot be null. + */ + public function testGeoDistanceAggregationAddRangeException() + { + $agg = new GeoDistanceAggregation('test_agg'); + $agg->addRange(); + } + + /** + * Data provider for testGeoDistanceAggregationGetArray(). + * + * @return array + */ + public function testGeoDistanceAggregationGetArrayDataProvider() + { + $out = []; + $filterData = [ + 'field' => 'location', + 'origin' => '52.3760, 4.894', + 'unit' => 'mi', + 'distance_type' => 'plane', + 'ranges' => [100, 300], + ]; + + $expectedResults = [ + 'field' => 'location', + 'origin' => '52.3760, 4.894', + 'unit' => 'mi', + 'distance_type' => 'plane', + 'ranges' => [['from' => 100, 'to' => 300]], + ]; + + $out[] = [$filterData, $expectedResults]; + + return $out; + } + + /** + * Tests getArray method. + * + * @param array $filterData + * @param array $expected + * + * @dataProvider testGeoDistanceAggregationGetArrayDataProvider + */ + public function testGeoDistanceAggregationGetArray($filterData, $expected) + { + $aggregation = new GeoDistanceAggregation('foo'); + $aggregation->setOrigin($filterData['origin']); + $aggregation->setField($filterData['field']); + $aggregation->setUnit($filterData['unit']); + $aggregation->setDistanceType($filterData['distance_type']); + $aggregation->addRange($filterData['ranges'][0], $filterData['ranges'][1]); + + $result = $aggregation->getArray(); + $this->assertEquals($result, $expected); + } + + /** + * Tests getType method. + */ + public function testGeoDistanceAggregationGetType() + { + $aggregation = new GeoDistanceAggregation('foo'); + $result = $aggregation->getType(); + $this->assertEquals('geo_distance', $result); + } +} diff --git a/tests/Aggregation/GeoHashGridAggregationTest.php b/tests/Aggregation/GeoHashGridAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..aac572646b759121e1621439afaa6e8b4d749e4c --- /dev/null +++ b/tests/Aggregation/GeoHashGridAggregationTest.php @@ -0,0 +1,89 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\GeoHashGridAggregation; + +/** + * Unit test for geohash grid aggregation. + */ +class GeoHashGridAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test if exception is thrown. + * + * @expectedException \LogicException + */ + public function testGeoHashGridAggregationException() + { + $agg = new GeoHashGridAggregation('test_agg'); + $agg->getArray(); + } + + /** + * Data provider for testGeoHashGridAggregationGetArray(). + * + * @return array + */ + public function getArrayDataProvider() + { + $out = []; + + $filterData = [ + 'field' => 'location', + 'precision' => 3, + 'size' => 10, + 'shard_size' => 10, + ]; + + $expectedResults = [ + 'field' => 'location', + 'precision' => 3, + 'size' => 10, + 'shard_size' => 10, + ]; + + $out[] = [$filterData, $expectedResults]; + + return $out; + } + + /** + * Tests getArray method. + * + * @param array $filterData + * @param array $expected + * + * @dataProvider getArrayDataProvider + */ + public function testGeoHashGridAggregationGetArray($filterData, $expected) + { + $aggregation = new GeoHashGridAggregation('foo'); + $aggregation->setPrecision($filterData['precision']); + $aggregation->setSize($filterData['size']); + $aggregation->setShardSize($filterData['shard_size']); + $aggregation->setField($filterData['field']); + + $result = $aggregation->getArray(); + $this->assertEquals($result, $expected); + } + + /** + * Tests getType method. + */ + public function testGeoHashGridAggregationGetType() + { + $aggregation = new GeoHashGridAggregation('foo'); + $result = $aggregation->getType(); + $this->assertEquals('geohash_grid', $result); + } +} diff --git a/tests/Aggregation/GlobalAggregationTest.php b/tests/Aggregation/GlobalAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..351c27e2b932718dbad4965e40ebc8e509ba7c7d --- /dev/null +++ b/tests/Aggregation/GlobalAggregationTest.php @@ -0,0 +1,88 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\GlobalAggregation; + +class GlobalAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Data provider for testToArray(). + * + * @return array + */ + public function getToArrayData() + { + $out = []; + + // Case #0 global aggregation. + $aggregation = new GlobalAggregation('test_agg'); + + $result = [ + 'agg_test_agg' => [ + 'global' => new \stdClass(), + ], + ]; + + $out[] = [ + $aggregation, + $result, + ]; + + // Case #1 nested global aggregation. + $aggregation = new GlobalAggregation('test_agg'); + $aggregation2 = new GlobalAggregation('test_agg_2'); + $aggregation->addAggregation($aggregation2); + + $result = [ + 'agg_test_agg' => [ + 'global' => new \stdClass(), + 'aggregations' => [ + 'agg_test_agg_2' => [ + 'global' => new \stdClass(), + ], + ], + ], + ]; + + $out[] = [ + $aggregation, + $result, + ]; + + return $out; + } + + /** + * Test for global aggregation toArray() method. + * + * @param GlobalAggregation $aggregation + * @param array $expectedResult + * + * @dataProvider getToArrayData + */ + public function testToArray($aggregation, $expectedResult) + { + $this->assertEquals($expectedResult, $aggregation->toArray()); + } + + /** + * Test for setField method on global aggregation. + * + * @expectedException \LogicException + */ + public function testSetField() + { + $aggregation = new GlobalAggregation('test_agg'); + $aggregation->setField('test_field'); + } +} diff --git a/tests/Aggregation/Ipv4RangeAggregationTest.php b/tests/Aggregation/Ipv4RangeAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..00111227d908d76e257d57466499c7fadf3566e4 --- /dev/null +++ b/tests/Aggregation/Ipv4RangeAggregationTest.php @@ -0,0 +1,28 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\Ipv4RangeAggregation; + +class Ipv4RangeAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test exception when field and range are not set. + * + * @expectedException \LogicException + */ + public function testIfExceptionIsThrownWhenFieldAndRangeAreNotSet() + { + $agg = new Ipv4RangeAggregation('foo'); + $agg->toArray(); + } +} diff --git a/tests/Aggregation/MissingAggregationTest.php b/tests/Aggregation/MissingAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..300971ef16150a4cde2de255ed66b4900d06f951 --- /dev/null +++ b/tests/Aggregation/MissingAggregationTest.php @@ -0,0 +1,50 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\MissingAggregation; + +class MissingAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test if exception is thrown when field is not set. + * + * @expectedException \LogicException + * @expectedExceptionMessage Missing aggregation must have a field set. + */ + public function testIfExceptionIsThrown() + { + $agg = new MissingAggregation('test_agg'); + $agg->getArray(); + } + + /** + * Test getArray method. + */ + public function testMissingAggregationGetArray() + { + $aggregation = new MissingAggregation('foo'); + $aggregation->setField('bar'); + $result = $aggregation->getArray(); + $this->assertEquals('bar', $result['field']); + } + + /** + * Test getType method. + */ + public function testMissingAggregationGetType() + { + $aggregation = new MissingAggregation('bar'); + $result = $aggregation->getType(); + $this->assertEquals('missing', $result); + } +} diff --git a/tests/Aggregation/NestedAggregationTest.php b/tests/Aggregation/NestedAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..500ccb54259ac659e7c9386465ba124db08ec2ec --- /dev/null +++ b/tests/Aggregation/NestedAggregationTest.php @@ -0,0 +1,67 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\NestedAggregation; + +class NestedAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test for nested aggregation toArray() method exception. + * + * @expectedException \LogicException + */ + public function testToArrayException() + { + $aggregation = new NestedAggregation('test_agg'); + $aggregation->setPath('test_path'); + + $expectedResult = [ + 'agg_test_agg' => [ + 'nested' => ['path' => 'test_path'], + ], + ]; + + $this->assertEquals($expectedResult, $aggregation->toArray()); + } + + /** + * Test for nested aggregation toArray() method exception. + */ + public function testToArray() + { + $termMock = $this + ->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Aggregation\TermsAggregation') + ->disableOriginalConstructor() + ->getMock(); + + $termMock + ->expects($this->once()) + ->method('toArray') + ->will($this->returnValue(['terms' => []])); + + $aggregation = new NestedAggregation('test_nested_agg'); + $aggregation->setPath('test_path'); + $aggregation->addAggregation($termMock); + + $expectedResult = [ + 'agg_test_nested_agg' => [ + 'nested' => ['path' => 'test_path'], + 'aggregations' => [ + 'terms' => [], + ], + ], + ]; + + $this->assertEquals($expectedResult, $aggregation->toArray()); + } +} diff --git a/tests/Aggregation/PercentileRanksAggregationTest.php b/tests/Aggregation/PercentileRanksAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..a063b361372eb09a011d0654f1f0f8fdc183be59 --- /dev/null +++ b/tests/Aggregation/PercentileRanksAggregationTest.php @@ -0,0 +1,65 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\PercentileRanksAggregation; + +/** + * Percentile ranks aggregation unit tests. + */ +class PercentileRanksAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var PercentileRanksAggregation + */ + public $agg; + + /** + * Phpunit setup. + */ + public function setUp() + { + $this->agg = new PercentileRanksAggregation('foo'); + } + + /** + * Tests if exception is thrown when required parameters not set. + * + * @expectedException \LogicException + */ + public function testIfPercentileRanksAggregationThrowsAnException() + { + $this->agg->toArray(); + } + + /** + * Tests exception when only field is set. + * + * @expectedException \LogicException + */ + public function testIfExceptionIsThrownWhenFieldSetAndValueNotSet() + { + $this->agg->setField('bar'); + $this->agg->toArray(); + } + + /** + * Tests exception when only value is set. + * + * @expectedException \LogicException + */ + public function testIfExceptionIsThrownWhenScriptSetAndValueNotSet() + { + $this->agg->setScript('bar'); + $this->agg->toArray(); + } +} diff --git a/tests/Aggregation/PercentilesAggregationTest.php b/tests/Aggregation/PercentilesAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..f7d1fd7ea26eaa24818eaa5cad98a406908a8681 --- /dev/null +++ b/tests/Aggregation/PercentilesAggregationTest.php @@ -0,0 +1,29 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\PercentilesAggregation; + +class PercentilesAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests if PercentilesAggregation#getArray throws exception when expected. + * + * @expectedException \LogicException + * @expectedExceptionMessage Percentiles aggregation must have field or script set. + */ + public function testPercentilesAggregationGetArrayException() + { + $aggregation = new PercentilesAggregation('bar'); + $aggregation->getArray(); + } +} diff --git a/tests/Aggregation/RangeAggregationTest.php b/tests/Aggregation/RangeAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..fe8c317c7ecf48201d3329f3be51c25aeb4e5f61 --- /dev/null +++ b/tests/Aggregation/RangeAggregationTest.php @@ -0,0 +1,202 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\RangeAggregation; + +class RangeAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test addRange method. + */ + public function testRangeAggregationAddRange() + { + $aggregation = new RangeAggregation('test_agg'); + $aggregation->setField('test_field'); + $aggregation->addRange('10', 20); + + $result = [ + 'agg_test_agg' => [ + 'range' => [ + 'field' => 'test_field', + 'ranges' => [ + [ + 'from' => '10', + 'to' => 20, + ], + ], + 'keyed' => false, + ], + ], + ]; + + $this->assertEquals($result, $aggregation->toArray()); + } + + /** + * Test addRange method with multiple values. + */ + public function testRangeAggregationAddRangeMultiple() + { + $aggregation = new RangeAggregation('test_agg'); + $aggregation->setField('test_field'); + $aggregation->setKeyed(true); + $aggregation->addRange('10', null, 'range_1'); + $aggregation->addRange(null, '20', 'range_2'); + + $result = [ + 'agg_test_agg' => [ + 'range' => [ + 'field' => 'test_field', + 'ranges' => [ + [ + 'from' => '10', + 'key' => 'range_1', + ], + [ + 'to' => '20', + 'key' => 'range_2', + ], + ], + 'keyed' => true, + ], + ], + ]; + + $this->assertEquals($result, $aggregation->toArray()); + } + + /** + * Test addRange method with nested values. + */ + public function testRangeAggregationAddRangeNested() + { + $aggregation = new RangeAggregation('test_agg'); + $aggregation->setField('test_field'); + $aggregation->addRange('10', '10'); + + $aggregation2 = new RangeAggregation('test_agg_2'); + $aggregation2->addRange('20', '20'); + + $aggregation->addAggregation($aggregation2); + + $result = [ + 'agg_test_agg' => [ + 'range' => [ + 'field' => 'test_field', + 'ranges' => [ + [ + 'from' => '10', + 'to' => '10', + ], + ], + 'keyed' => false, + ], + 'aggregations' => [ + 'agg_test_agg_2' => [ + 'range' => [ + 'ranges' => [ + [ + 'from' => '20', + 'to' => '20', + ], + ], + 'keyed' => false, + ], + ], + ], + ], + ]; + + $this->assertEquals($result, $aggregation->toArray()); + } + + /** + * Tests getType method. + */ + public function testRangeAggregationGetType() + { + $agg = new RangeAggregation('foo'); + $result = $agg->getType(); + $this->assertEquals('range', $result); + } + + /** + * Tests removeRangeByKey method. + */ + public function testRangeAggregationRemoveRangeByKey() + { + $aggregation = new RangeAggregation('foo'); + $aggregation->setField('price'); + $aggregation->setKeyed(true); + $aggregation->addRange(100, 300, 'name'); + + $expected = [ + 'field' => 'price', + 'keyed' => true, + 'ranges' => [ + [ + 'from' => 100, + 'to' => 300, + 'key' => 'name', + ], + ], + ]; + + $result = $aggregation->getArray(); + $this->assertEquals($result, $expected, 'get array of ranges when keyed=true'); + + $result = $aggregation->removeRangeByKey('name'); + $this->assertTrue($result, 'returns true when removed valid range name'); + + $result = $aggregation->removeRangeByKey('not_existing_key'); + $this->assertFalse($result, 'should not allow remove not existing key if keyed=true'); + + $aggregation->setKeyed(false); + $result = $aggregation->removeRangeByKey('not_existing_key'); + $this->assertFalse($result, 'should not allow remove not existing key if keyed=false'); + + $aggregation->addRange(100, 300, 'name'); + $result = $aggregation->removeRangeByKey('name'); + $this->assertFalse($result, 'can not remove any existing range if keyed=false'); + } + + /** + * Tests removeRange method. + */ + public function testRangeAggregationRemoveRange() + { + $aggregation = new RangeAggregation('foo'); + $aggregation->setField('price'); + $aggregation->setKeyed(true); + $aggregation->addRange(100, 300, 'key'); + $aggregation->addRange(500, 700, 'range_2'); + + $expected = [ + 'field' => 'price', + 'keyed' => true, + 'ranges' => [ + [ + 'from' => 100, + 'to' => 300, + 'key' => 'key', + ], + ], + ]; + + $aggregation->removeRange(500, 700); + $result = $aggregation->getArray(); + $this->assertEquals($result, $expected, 'get expected array of ranges'); + $result = $aggregation->removeRange(500, 700); + $this->assertFalse($result, 'returns false after removing not-existing range'); + } +} diff --git a/tests/Aggregation/ReverseNestedAggregationTest.php b/tests/Aggregation/ReverseNestedAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..e4e8eeef0bee080d9466b7739f15ceeeabcc18b5 --- /dev/null +++ b/tests/Aggregation/ReverseNestedAggregationTest.php @@ -0,0 +1,97 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\ReverseNestedAggregation; + +class ReverseNestedAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test for reverse_nested aggregation toArray() method exception. + * + * @expectedException \LogicException + */ + public function testToArrayException() + { + $aggregation = new ReverseNestedAggregation('test_agg'); + $aggregation->setPath('test_path'); + + $expectedResult = [ + 'agg_test_agg' => [ + 'reverse_nested' => ['path' => 'test_path'], + ], + ]; + + $this->assertEquals($expectedResult, $aggregation->toArray()); + } + + /** + * Test for reverse_nested aggregation toArray() method exception. + */ + public function testToArray() + { + $termMock = $this + ->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Aggregation\TermsAggregation') + ->disableOriginalConstructor() + ->getMock(); + + $termMock + ->expects($this->once()) + ->method('toArray') + ->will($this->returnValue(['terms' => []])); + + $aggregation = new ReverseNestedAggregation('test_nested_agg'); + $aggregation->setPath('test_path'); + $aggregation->addAggregation($termMock); + + $expectedResult = [ + 'agg_test_nested_agg' => [ + 'reverse_nested' => ['path' => 'test_path'], + 'aggregations' => [ + 'terms' => [], + ], + ], + ]; + + $this->assertEquals($expectedResult, $aggregation->toArray()); + } + + /** + * Test for reverse_nested aggregation toArray() without path. + */ + public function testToArrayNoPath() + { + $termMock = $this + ->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Aggregation\TermsAggregation') + ->disableOriginalConstructor() + ->getMock(); + + $termMock + ->expects($this->once()) + ->method('toArray') + ->will($this->returnValue(['terms' => []])); + + $aggregation = new ReverseNestedAggregation('test_nested_agg'); + $aggregation->addAggregation($termMock); + + $expectedResult = [ + 'agg_test_nested_agg' => [ + 'reverse_nested' => new \stdClass(), + 'aggregations' => [ + 'terms' => [], + ], + ], + ]; + + $this->assertEquals($expectedResult, $aggregation->toArray()); + } +} diff --git a/tests/Aggregation/StatsAggregationTest.php b/tests/Aggregation/StatsAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..bbf0e562059ed7047bdc030cfb218c8a855e1960 --- /dev/null +++ b/tests/Aggregation/StatsAggregationTest.php @@ -0,0 +1,34 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\StatsAggregation; + +class StatsAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test for stats aggregation toArray() method. + */ + public function testToArray() + { + $aggregation = new StatsAggregation('test_agg'); + $aggregation->setField('test_field'); + + $expectedResult = [ + 'agg_test_agg' => [ + 'stats' => ['field' => 'test_field'], + ], + ]; + + $this->assertEquals($expectedResult, $aggregation->toArray()); + } +} diff --git a/tests/Aggregation/TermsAggregationTest.php b/tests/Aggregation/TermsAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..14565d5a814bfcc3b14f37b5f7baaae869b17866 --- /dev/null +++ b/tests/Aggregation/TermsAggregationTest.php @@ -0,0 +1,217 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\TermsAggregation; + +class TermsAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests setField method. + */ + public function testTermsAggregationSetField() + { + // Case #0 terms aggregation. + $aggregation = new TermsAggregation('test_agg'); + $aggregation->setField('test_field'); + + $result = [ + 'agg_test_agg' => [ + 'terms' => ['field' => 'test_field'], + ], + ]; + + $this->assertEquals($aggregation->toArray(), $result); + } + + /** + * Tests setSize method. + */ + public function testTermsAggregationSetSize() + { + // Case #1 terms aggregation with size. + $aggregation = new TermsAggregation('test_agg'); + $aggregation->setField('test_field'); + $aggregation->addParameter('size', 1); + + $result = [ + 'agg_test_agg' => [ + 'terms' => [ + 'field' => 'test_field', + 'size' => 1, + ], + ], + ]; + + $this->assertEquals($aggregation->toArray(), $result); + + // Case #2 terms aggregation with zero size. + $aggregation = new TermsAggregation('test_agg'); + $aggregation->setField('test_field'); + $aggregation->setSize(0); + + $result = [ + 'agg_test_agg' => [ + 'terms' => [ + 'field' => 'test_field', + 'size' => 0, + ], + ], + ]; + + $this->assertEquals($aggregation->toArray(), $result); + } + + /** + * Tests minDocumentCount method. + */ + public function testTermsAggregationMinDocumentCount() + { + // Case #3 terms aggregation with size and min document count. + $aggregation = new TermsAggregation('test_agg'); + $aggregation->setField('test_field'); + $aggregation->addParameter('size', 1); + $aggregation->addParameter('min_doc_count', 10); + + $result = [ + 'agg_test_agg' => [ + 'terms' => [ + 'field' => 'test_field', + 'size' => 1, + 'min_doc_count' => 10, + ], + ], + ]; + + $this->assertEquals($aggregation->toArray(), $result); + } + + /** + * Tests include, exclude method. + */ + public function testTermsAggregationSimpleIncludeExclude() + { + // Case #4 terms aggregation with simple include, exclude. + $aggregation = new TermsAggregation('test_agg'); + $aggregation->setField('test_field'); + $aggregation->addParameter('include', 'test_.*'); + $aggregation->addParameter('exclude', 'pizza_.*'); + + $result = [ + 'agg_test_agg' => [ + 'terms' => [ + 'field' => 'test_field', + 'include' => 'test_.*', + 'exclude' => 'pizza_.*', + ], + ], + ]; + + $this->assertEquals($aggregation->toArray(), $result); + } + + /** + * Tests include, exclude with flags method. + */ + public function testTermsAggregationIncludeExcludeFlags() + { + // Case #5 terms aggregation with include, exclude and flags. + $aggregation = new TermsAggregation('test_agg'); + $aggregation->setField('test_field'); + $aggregation->addParameter( + 'include', + [ + 'pattern' => 'test_.*', + 'flags' => 'CANON_EQ|CASE_INSENSITIVE', + ] + ); + $aggregation->addParameter( + 'exclude', + [ + 'pattern' => 'pizza_.*', + 'flags' => 'CASE_INSENSITIVE', + ] + ); + + $result = [ + 'agg_test_agg' => [ + 'terms' => [ + 'field' => 'test_field', + 'include' => [ + 'pattern' => 'test_.*', + 'flags' => 'CANON_EQ|CASE_INSENSITIVE', + ], + 'exclude' => [ + 'pattern' => 'pizza_.*', + 'flags' => 'CASE_INSENSITIVE', + ], + ], + ], + ]; + + $this->assertEquals($aggregation->toArray(), $result); + } + + /** + * Tests setOrder method. + */ + public function testTermsAggregationSetOrder() + { + // Case #6 terms aggregation with order default direction. + $aggregation = new TermsAggregation('test_agg'); + $aggregation->setField('test_field'); + $aggregation->addParameter('order', ['_count' => 'asc']); + + $result = [ + 'agg_test_agg' => [ + 'terms' => [ + 'field' => 'test_field', + 'order' => ['_count' => 'asc'], + ], + ], + ]; + + $this->assertEquals($aggregation->toArray(), $result); + } + + /** + * Tests setOrder DESC method. + */ + public function testTermsAggregationSetOrderDESC() + { + // Case #7 terms aggregation with order term mode, desc direction. + $aggregation = new TermsAggregation('test_agg'); + $aggregation->setField('test_field'); + $aggregation->addParameter('order', ['_term' => 'desc']); + + $result = [ + 'agg_test_agg' => [ + 'terms' => [ + 'field' => 'test_field', + 'order' => ['_term' => 'desc'], + ], + ], + ]; + + $this->assertEquals($aggregation->toArray(), $result); + } + + /** + * Tests getType method. + */ + public function testTermsAggregationGetType() + { + $aggregation = new TermsAggregation('foo'); + $result = $aggregation->getType(); + $this->assertEquals('terms', $result); + } +} diff --git a/tests/Aggregation/TopHitsAggregationTest.php b/tests/Aggregation/TopHitsAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..743818dcd082dee5ac088e95522591092038c958 --- /dev/null +++ b/tests/Aggregation/TopHitsAggregationTest.php @@ -0,0 +1,42 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\TopHitsAggregation; +use ONGR\ElasticsearchBundle\DSL\Sort\Sorts; + +/** + * Unit tests for top hits aggregation. + */ +class TopHitsAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Check if aggregation returns the expected array. + */ + public function testToArray() + { + $sorts = new Sorts(); + $aggregation = new TopHitsAggregation('test', 0, 1); + + $expectedAgg = new \stdClass(); + $expectedAgg->size = 0; + $expectedAgg->from = 1; + $expectedAgg->sort = $sorts->toArray(); + $expected = [ + 'agg_test' => [ + 'top_hits' => $expectedAgg, + ], + ]; + + $this->assertEquals($expected, $aggregation->toArray()); + } +} diff --git a/tests/Bool/BoolTest.php b/tests/Bool/BoolTest.php new file mode 100644 index 0000000000000000000000000000000000000000..492dc38fbe4acf6b07c14620fb00696eaa4d44a7 --- /dev/null +++ b/tests/Bool/BoolTest.php @@ -0,0 +1,90 @@ +<?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\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Bool\Bool; +use ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter; +use ONGR\ElasticsearchBundle\DSL\Filter\TermFilter; + +/** + * Unit test for Bool. + */ +class BoolTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests isRelevant method. + */ + public function testBoolIsRelevant() + { + $bool = new Bool(); + $this->assertFalse($bool->isRelevant()); + $bool->addToBool(new MissingFilter('test')); + $this->assertTrue($bool->isRelevant()); + } + + /** + * Test for addToBool() without setting a correct bool operator. + * + * @expectedException \UnexpectedValueException + * @expectedExceptionMessage The bool operator Should is not supported + */ + public function testBoolAddToBoolException() + { + $bool = new Bool(); + $bool->addToBool(new MissingFilter('test'), 'Should'); + } + + /** + * Tests toArray() method. + */ + public function testBoolToArray() + { + $bool = new Bool(); + $bool->addToBool(new TermFilter('key1', 'value1'), 'should'); + $bool->addToBool(new TermFilter('key2', 'value2'), 'must'); + $bool->addToBool(new TermFilter('key3', 'value3'), 'must_not'); + $expected = [ + 'should' => [ + [ + 'term' => [ + 'key1' => 'value1', + ], + ], + ], + 'must' => [ + [ + 'term' => [ + 'key2' => 'value2', + ], + ], + ], + 'must_not' => [ + [ + 'term' => [ + 'key3' => 'value3', + ], + ], + ], + ]; + $this->assertEquals($expected, $bool->toArray()); + } + + /** + * Test getType method. + */ + public function testBoolGetType() + { + $bool = new Bool(); + $result = $bool->getType(); + $this->assertEquals('bool', $result); + } +} diff --git a/tests/DslTypeAwareTraitTest.php b/tests/DslTypeAwareTraitTest.php new file mode 100644 index 0000000000000000000000000000000000000000..921620ea1716ab2ba98c4906c070277ed9713781 --- /dev/null +++ b/tests/DslTypeAwareTraitTest.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\ElasticsearchBundle\Tests\Unit\DSL; + +use ONGR\ElasticsearchBundle\DSL\DslTypeAwareTrait; + +/** + * Test for DslTypeAwareTrait. + */ +class DslTypeAwareTraitTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var DslTypeAwareTrait + */ + private $mock; + + /** + * {@inheritdoc} + */ + public function setUp() + { + $this->mock = $this->getMockForTrait('ONGR\ElasticsearchBundle\DSL\DslTypeAwareTrait'); + } + + /** + * Tests if setDslType throws exception. + * + * @expectedException \InvalidArgumentException + */ + public function testIfSetDslTypeExceptionThrowsException() + { + $this->mock->setDslType('foo'); + } + + /** + * Tests if setDslType sets filter. + */ + public function testIfSetDslTypeSetsFilter() + { + $this->mock->setDslType('filter'); + $this->assertEquals('filter', $this->mock->getDslType()); + } + + /** + * Tests if setDslType sets query. + */ + public function testIfSetDslTypeSetsQuery() + { + $this->mock->setDslType('query'); + $this->assertEquals('query', $this->mock->getDslType()); + } +} diff --git a/tests/Filter/AndFilterTest.php b/tests/Filter/AndFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..a7fe5153ef5bf8a659380bde47a57edd744e0938 --- /dev/null +++ b/tests/Filter/AndFilterTest.php @@ -0,0 +1,131 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\AndFilter; + +class AndFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new AndFilter([], []); + $result = $filter->getType(); + $this->assertEquals('and', $result); + } + + /** + * Data provider for testToArray function. + * + * @return array + */ + public function getArrayDataProvider() + { + $mockBuildeFfirstFilter = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->getMock(); + $mockBuildeFfirstFilter->expects($this->any()) + ->method('getType') + ->willReturn('term'); + $mockBuildeFfirstFilter->expects($this->any()) + ->method('toArray') + ->willReturn(['test_field' => ['test_value' => 'test']]); + + $mockBuilderSecondFilter = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->getMock(); + $mockBuilderSecondFilter->expects($this->any()) + ->method('getType') + ->willReturn('prefix'); + $mockBuilderSecondFilter->expects($this->any()) + ->method('toArray') + ->willReturn(['test_field' => ['test_value' => 'test']]); + + return [ + // Case #1. + [ + [$mockBuildeFfirstFilter], + [], + [ + [ + 'term' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + ], + ], + // Case #2. + [ + [$mockBuildeFfirstFilter, $mockBuilderSecondFilter], + [], + [ + [ + 'term' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + [ + 'prefix' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + ], + ], + // Case #3. + [ + [$mockBuildeFfirstFilter, $mockBuilderSecondFilter], + ['type' => 'acme'], + [ + 'filters' => [ + 0 => [ + 'term' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + 1 => [ + 'prefix' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + ], + 'type' => 'acme', + ], + ], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param BuilderInterface[] $filters Array. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($filters, $parameters, $expected) + { + $filter = new AndFilter($filters, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/ExistsFilterTest.php b/tests/Filter/ExistsFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..69f85ba43808794381ad09ce0d1e22f1d2837ec6 --- /dev/null +++ b/tests/Filter/ExistsFilterTest.php @@ -0,0 +1,38 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\ExistsFilter; + +/** + * Unit test for ExistsFilter. + */ +class ExistsFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests ExistsFilter#getType() method. + */ + public function testGetType() + { + $filter = new ExistsFilter('bar'); + $this->assertEquals('exists', $filter->getType()); + } + + /** + * Tests ExistsFilter#toArray() method. + */ + public function testToArray() + { + $filter = new ExistsFilter('bar'); + $this->assertEquals(['field' => 'bar'], $filter->toArray()); + } +} diff --git a/tests/Filter/GeoBoundingBoxFilterTest.php b/tests/Filter/GeoBoundingBoxFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..e5164c2b791a3177bf32316fdf9b9bda276cd08c --- /dev/null +++ b/tests/Filter/GeoBoundingBoxFilterTest.php @@ -0,0 +1,97 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\GeoBoundingBoxFilter; + +class GeoBoundingBoxFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests getType method. + */ + public function testGetType() + { + $filter = new GeoBoundingBoxFilter('location', []); + $result = $filter->getType(); + $this->assertEquals('geo_bounding_box', $result); + } + + /** + * Test if exception is thrown when geo points are not set. + * + * @expectedException \LogicException + */ + public function testGeoBoundBoxFilterException() + { + $filter = new GeoBoundingBoxFilter('location', []); + $filter->toArray(); + } + + /** + * Data provider to testToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1 (2 values). + [ + 'location', + [ + ['lat' => 40.73, 'lon' => -74.1], + ['lat' => 40.01, 'lon' => -71.12], + ], + ['parameter' => 'value'], + [ + 'location' => [ + 'top_left' => ['lat' => 40.73, 'lon' => -74.1], + 'bottom_right' => ['lat' => 40.01, 'lon' => -71.12], + ], + 'parameter' => 'value', + ], + ], + // Case #2 (4 values). + [ + 'location', + [40.73, -74.1, 40.01, -71.12], + ['parameter' => 'value'], + [ + 'location' => [ + 'top' => 40.73, + 'left' => -74.1, + 'bottom' => 40.01, + 'right' => -71.12, + ], + 'parameter' => 'value', + ], + ], + ]; + } + + /** + * Tests toArray method. + * + * @param string $field Field name. + * @param array $values Bounding box values. + * @param array $parameters Optional parameters. + * @param array $expected Expected result. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($field, $values, $parameters, $expected) + { + $filter = new GeoBoundingBoxFilter($field, $values, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/GeoDistanceFilterTest.php b/tests/Filter/GeoDistanceFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..f506159c1294fb0dd0a6b1d46dbe838d87e972e4 --- /dev/null +++ b/tests/Filter/GeoDistanceFilterTest.php @@ -0,0 +1,72 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\GeoDistanceFilter; + +class GeoDistanceFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new GeoDistanceFilter('test_field', 'test_distance', 'test_location'); + $result = $filter->getType(); + $this->assertEquals('geo_distance', $result); + } + + /** + * Data provider to testToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + [ + 'location', + '200km', + ['lat' => 40, 'lon' => -70], + [], + ['distance' => '200km', 'location' => ['lat' => 40, 'lon' => -70]], + ], + // Case #2. + [ + 'location', + '20km', + ['lat' => 0, 'lon' => 0], + ['parameter' => 'value'], + ['distance' => '20km', 'location' => ['lat' => 0, 'lon' => 0], 'parameter' => 'value'], + ], + ]; + } + + /** + * Tests toArray method. + * + * @param string $field Field name. + * @param string $distance Distance. + * @param array $location Location. + * @param array $parameters Optional parameters. + * @param array $expected Expected result. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($field, $distance, $location, $parameters, $expected) + { + $filter = new GeoDistanceFilter($field, $distance, $location, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/GeoDistanceRangeFilterTest.php b/tests/Filter/GeoDistanceRangeFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..a90c7c703a5cf75ee6e3bcf964cb899458e50109 --- /dev/null +++ b/tests/Filter/GeoDistanceRangeFilterTest.php @@ -0,0 +1,72 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\GeoDistanceRangeFilter; + +class GeoDistanceRangeFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new GeoDistanceRangeFilter('test_field', 'test_distance', 'test_location'); + $result = $filter->getType(); + $this->assertEquals('geo_distance_range', $result); + } + + /** + * Data provider to testToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + [ + 'location', + ['from' => '200km', 'to' => '400km'], + ['lat' => 40, 'lon' => -70], + [], + ['from' => '200km', 'to' => '400km', 'location' => ['lat' => 40, 'lon' => -70]], + ], + // Case #2. + [ + 'location', + ['from' => '150km', 'to' => '180km'], + ['lat' => 0, 'lon' => 0], + ['parameter' => 'value'], + ['from' => '150km', 'to' => '180km', 'location' => ['lat' => 0, 'lon' => 0], 'parameter' => 'value'], + ], + ]; + } + + /** + * Tests toArray method. + * + * @param string $field Field name. + * @param array $range Distance range. + * @param array $location Location. + * @param array $parameters Optional parameters. + * @param array $expected Expected result. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($field, $range, $location, $parameters, $expected) + { + $filter = new GeoDistanceRangeFilter($field, $range, $location, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/GeoPolygonFilterTest.php b/tests/Filter/GeoPolygonFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..f24e1c1c058aea632e384dca8ee4342416c07833 --- /dev/null +++ b/tests/Filter/GeoPolygonFilterTest.php @@ -0,0 +1,98 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\GeoPolygonFilter; + +class GeoPolygonFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new GeoPolygonFilter('test_field'); + $result = $filter->getType(); + $this->assertEquals('geo_polygon', $result); + } + + /** + * Data provider to testToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + [ + 'location', + [ + ['lat' => 20, 'lon' => -80], + ['lat' => 30, 'lon' => -40], + ['lat' => 70, 'lon' => -90], + ], + [], + [ + 'location' => [ + 'points' => [ + ['lat' => 20, 'lon' => -80], + ['lat' => 30, 'lon' => -40], + ['lat' => 70, 'lon' => -90], + ], + ], + ], + ], + // Case #2. + [ + 'location', + [], + ['parameter' => 'value'], + [ + 'location' => ['points' => []], + 'parameter' => 'value', + ], + ], + // Case #3. + [ + 'location', + [ + ['lat' => 20, 'lon' => -80], + ], + ['parameter' => 'value'], + [ + 'location' => [ + 'points' => [['lat' => 20, 'lon' => -80]], + ], + 'parameter' => 'value', + ], + ], + ]; + } + + /** + * Tests toArray method. + * + * @param string $field Field name. + * @param array $points Polygon's points. + * @param array $parameters Optional parameters. + * @param array $expected Expected result. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($field, $points, $parameters, $expected) + { + $filter = new GeoPolygonFilter($field, $points, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/HasChildFilterTest.php b/tests/Filter/HasChildFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..a4001c37da117141b35d578317189f8fe602eb55 --- /dev/null +++ b/tests/Filter/HasChildFilterTest.php @@ -0,0 +1,85 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\HasChildFilter; + +class HasChildFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $mock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface')->getMock(); + $filter = new HasChildFilter('test_field', $mock); + $result = $filter->getType(); + $this->assertEquals('has_child', $result); + } + + /** + * Data provider to testToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + [ + 'comment', + 'term', + ['name' => 'foo'], + [], + 'filter', + ['type' => 'comment', 'filter' => ['term' => ['name' => 'foo']]], + ], + // Case #2. + [ + 'comment', + 'term', + ['name' => 'foo'], + ['parameter' => 'value'], + 'query', + ['type' => 'comment', 'query' => ['term' => ['name' => 'foo']], 'parameter' => 'value'], + ], + ]; + } + + /** + * Tests toArray method. + * + * @param string $type Child type. + * @param string $queryType Type of query for mock query class. + * @param array $queryToArray Return value for mock query class toArray method. + * @param array $parameters Optional parameters. + * @param string $dslType Filter or query. + * @param array $expected Expected result. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($type, $queryType, $queryToArray, $parameters, $dslType, $expected) + { + $mockQuery = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface')->getMock(); + $mockQuery->expects($this->once()) + ->method('getType') + ->will($this->returnValue($queryType)); + $mockQuery->expects($this->once()) + ->method('toArray') + ->will($this->returnValue($queryToArray)); + + $filter = new HasChildFilter($type, $mockQuery, $parameters); + $filter->setDslType($dslType); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/HasParentFilterTest.php b/tests/Filter/HasParentFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..6f6c9031594bfd1f4d0df423620ffff00ac4350c --- /dev/null +++ b/tests/Filter/HasParentFilterTest.php @@ -0,0 +1,85 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\HasParentFilter; + +class HasParentFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $mock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface')->getMock(); + $filter = new HasParentFilter('test_field', $mock); + $result = $filter->getType(); + $this->assertEquals('has_parent', $result); + } + + /** + * Data provider to testToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + [ + 'content', + 'term', + ['title' => 'nested'], + [], + 'filter', + ['parent_type' => 'content', 'filter' => ['term' => ['title' => 'nested']]], + ], + // Case #2. + [ + 'content', + 'term', + ['title' => 'nested'], + ['parameter' => 'value'], + 'query', + ['parent_type' => 'content', 'query' => ['term' => ['title' => 'nested']], 'parameter' => 'value'], + ], + ]; + } + + /** + * Tests toArray method. + * + * @param string $parentType Parent type. + * @param string $queryType Type of query for mock query class. + * @param array $queryToArray Return value for mock query class toArray method. + * @param array $parameters Optional parameters. + * @param string $dslType Filter or query. + * @param array $expected Expected result. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($parentType, $queryType, $queryToArray, $parameters, $dslType, $expected) + { + $mockQuery = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface')->getMock(); + $mockQuery->expects($this->once()) + ->method('getType') + ->will($this->returnValue($queryType)); + $mockQuery->expects($this->once()) + ->method('toArray') + ->will($this->returnValue($queryToArray)); + + $filter = new HasParentFilter($parentType, $mockQuery, $parameters); + $filter->setDslType($dslType); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/IdsFilterTest.php b/tests/Filter/IdsFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..e30a272f2eff9051480f4a5bd867e7e8c14a9ae6 --- /dev/null +++ b/tests/Filter/IdsFilterTest.php @@ -0,0 +1,60 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\IdsFilter; + +class IdsFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new IdsFilter([], []); + $result = $filter->getType(); + $this->assertEquals('ids', $result); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + [['acme', 'bar'], ['type' => 'acme'], ['values' => ['acme', 'bar'], 'type' => 'acme']], + // Case #2. + [[], [], ['values' => []]], + // Case #3. + [['acme'], [], ['values' => ['acme']]], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string[] $values Ids' values. + * @param array $parameters Optional parameters. + * @param array $expected Expected result. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($values, $parameters, $expected) + { + $filter = new IdsFilter($values, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/IndicesFilterTest.php b/tests/Filter/IndicesFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..503496345864244681f2d66966fefb4bd82afb35 --- /dev/null +++ b/tests/Filter/IndicesFilterTest.php @@ -0,0 +1,108 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\IndicesFilter; + +class IndicesFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new IndicesFilter([], '', null); + $this->assertEquals('indices', $filter->getType()); + } + + /** + * Tests if Indices qty is greater than one. + */ + public function testToArrayIfIndicesQtyIsGreaterThanOne() + { + $mockBuilder = $this->indicesQtyMockBuilder(['test_field' => ['test_value' => 'test']]); + + $filter = new IndicesFilter(['foo', 'bar'], $mockBuilder, null); + $expectedResult = [ + 'indices' => [0 => 'foo', 1 => 'bar'], + 'filter' => ['term' => ['test_field' => ['test_value' => 'test']]], + ]; + $result = $filter->toArray(); + $this->assertEquals($expectedResult, $result); + } + + /** + * Test if Indices qty is less than one. + */ + public function testToArrayIfIndicesQtyIsLessThanOne() + { + $mockBuilder = $this->indicesQtyMockBuilder(['test_field' => ['test_value' => 'test']]); + $filter = new IndicesFilter(['foo'], $mockBuilder, null); + $expectedResult = ['index' => 'foo', 'filter' => ['term' => ['test_field' => ['test_value' => 'test']]]]; + $result = $filter->toArray(); + $this->assertEquals($expectedResult, $result); + } + + /** + * Test. + */ + public function testWhenNoMatchFilterIsNotNull() + { + $mockBuilder = $this->indicesQtyMockBuilder(['tag' => 'wow']); + $noMatchFilterMockBuilder = $this->indicesQtyMockBuilder(['tag' => 'kow']); + $filter = new IndicesFilter(['foo'], $mockBuilder, $noMatchFilterMockBuilder); + $expectedResult = [ + 'index' => 'foo', + 'filter' => ['term' => ['tag' => 'wow']], + 'no_match_filter' => ['term' => ['tag' => 'kow']], + ]; + $result = $filter->toArray(); + $this->assertEquals($expectedResult, $result); + } + + /** + * Test. + */ + public function testWhenNoMatchFilterIsEmpty() + { + $mockBuilder = $this->indicesQtyMockBuilder(['tag' => 'wow']); + $filter = new IndicesFilter(['foo'], $mockBuilder, ''); + $expectedResult = [ + 'index' => 'foo', + 'filter' => ['term' => ['tag' => 'wow']], + 'no_match_filter' => '', + ]; + $result = $filter->toArray(); + $this->assertEquals($expectedResult, $result); + } + + /** + * Mock Builder. + * + * @param array $param Expected values. + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + private function indicesQtyMockBuilder(array $param = []) + { + $mockBuilder = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->getMock(); + $mockBuilder->expects($this->any()) + ->method('getType') + ->willReturn('term'); + $mockBuilder->expects($this->any()) + ->method('toArray') + ->willReturn($param); + + return $mockBuilder; + } +} diff --git a/tests/Filter/LimitFilterTest.php b/tests/Filter/LimitFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..9d33e70eb8dad205ef20a4ba0cb8baaf4cdbd50f --- /dev/null +++ b/tests/Filter/LimitFilterTest.php @@ -0,0 +1,36 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\LimitFilter; + +class LimitFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new LimitFilter(0); + $this->assertEquals('limit', $filter->getType()); + } + + /** + * Test for filter toArray() method. + */ + public function testToArray() + { + $filter = new LimitFilter(0); + $expectedResult = ['value' => 0]; + $this->assertEquals($expectedResult, $filter->toArray()); + } +} diff --git a/tests/Filter/MatchAllFilterTest.php b/tests/Filter/MatchAllFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..5e75aae76853b2a8414215479089d33e96fe559f --- /dev/null +++ b/tests/Filter/MatchAllFilterTest.php @@ -0,0 +1,38 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\MatchAllFilter; + +class MatchAllFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method, it should return 'match_all'. + */ + public function testGetType() + { + $filter = new MatchAllFilter(); + $result = $filter->getType(); + $this->assertEquals('match_all', $result); + } + + /** + * Test toArray method. + */ + public function testToArrayItShouldReturnStdClass() + { + $filter = new MatchAllFilter(); + $result = $filter->toArray(); + $expectedResult = new \stdClass(); + $this->assertEquals($expectedResult, $result); + } +} diff --git a/tests/Filter/MissingFilterTest.php b/tests/Filter/MissingFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..c11fced3b4c464f1e2362fb223f6fe4d63b3e8c6 --- /dev/null +++ b/tests/Filter/MissingFilterTest.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\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter; + +class MissingFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new MissingFilter('', []); + $this->assertEquals('missing', $filter->getType()); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case 1. + ['', [], ['field' => '']], + // Case 2. + ['user', ['bar' => 'foo'], ['field' => 'user', 'bar' => 'foo']], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string $field + * @param array $parameters + * @param array $expected + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($field, $parameters, $expected) + { + $filter = new MissingFilter($field, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/NestedFilterTest.php b/tests/Filter/NestedFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..1e27ad8d36aab01acc13e1a3f84b2b4580f0bcd8 --- /dev/null +++ b/tests/Filter/NestedFilterTest.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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\NestedFilter; +use ONGR\ElasticsearchBundle\DSL\Filter\TermFilter; +use ONGR\ElasticsearchBundle\DSL\Filter\TermsFilter; + +class NestedFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new NestedFilter('', new TermFilter('foo', 'bar')); + $this->assertEquals('nested', $filter->getType()); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + $filter = [ + 'terms' => [ + 'foo' => 'bar', + ], + ]; + + return [ + // Case #0 Basic filter. + [ + 'product.sub_item', + [], + ['path' => 'product.sub_item', 'filter' => $filter], + ], + // Case #1 with parameters. + [ + 'product.sub_item', + ['_cache' => true, '_name' => 'named_result'], + [ + 'path' => 'product.sub_item', + 'filter' => $filter, + '_cache' => true, + '_name' => 'named_result', + ], + ], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string $path + * @param array $parameters + * @param array $expected + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($path, $parameters, $expected) + { + $query = new TermsFilter('foo', 'bar'); + $filter = new NestedFilter($path, $query, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/NotFilterTest.php b/tests/Filter/NotFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..211bba186681125151e5d67095a963f46ad34fed --- /dev/null +++ b/tests/Filter/NotFilterTest.php @@ -0,0 +1,95 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\NotFilter; + +class NotFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new NotFilter(); + $this->assertEquals('not', $filter->getType()); + } + + /** + * Data provider for testToArray function. + * + * @return array + */ + public function getArrayDataProvider() + { + $mockBuilder = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->getMock(); + $mockBuilder->expects($this->any()) + ->method('getType') + ->willReturn('range'); + $mockBuilder->expects($this->any()) + ->method('toArray') + ->willReturn(['postDate' => ['from' => '2010-03-01', 'to' => '2010-04-01']]); + + return [ + // Case #1. + [ + $mockBuilder, + [], + [ + 'filter' => [ + 'range' => [ + 'postDate' => [ + 'from' => '2010-03-01', + 'to' => '2010-04-01', + ], + ], + ], + ], + ], + // Case #2. + [ + $mockBuilder, + [ + 'type' => 'acme', + ], + [ + 'filter' => [ + 'range' => [ + 'postDate' => [ + 'from' => '2010-03-01', + 'to' => '2010-04-01', + ], + ], + ], + 'type' => 'acme', + ], + ], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param BuilderInterface $filter Filter. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayDataProvider + */ + public function testToArrayMethod($filter, $parameters, $expected) + { + $filter = new NotFilter($filter, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $filter->toArray()); + } +} diff --git a/tests/Filter/OrFilterTest.php b/tests/Filter/OrFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..4e6e5fc9b64015c4af5c3b8152105676923c7e55 --- /dev/null +++ b/tests/Filter/OrFilterTest.php @@ -0,0 +1,131 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\OrFilter; + +class OrFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new OrFilter(); + $result = $filter->getType(); + $this->assertEquals('or', $result); + } + + /** + * Data provider for testToArray function. + * + * @return array + */ + public function getArrayDataProvider() + { + $mockBuilderFirstFilter = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->getMock(); + $mockBuilderFirstFilter->expects($this->any()) + ->method('getType') + ->willReturn('term'); + $mockBuilderFirstFilter->expects($this->any()) + ->method('toArray') + ->willReturn(['test_field' => ['test_value' => 'test']]); + + $mockBuilderSecondFilter = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->getMock(); + $mockBuilderSecondFilter->expects($this->any()) + ->method('getType') + ->willReturn('prefix'); + $mockBuilderSecondFilter->expects($this->any()) + ->method('toArray') + ->willReturn(['test_field' => ['test_value' => 'test']]); + + return [ + // Case #1. + [ + [$mockBuilderFirstFilter], + [], + [ + [ + 'term' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + ], + ], + // Case #2. + [ + [$mockBuilderFirstFilter, $mockBuilderSecondFilter], + [], + [ + [ + 'term' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + [ + 'prefix' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + ], + ], + // Case #3. + [ + [$mockBuilderFirstFilter, $mockBuilderSecondFilter], + ['type' => 'acme'], + [ + 'filters' => [ + 0 => [ + 'term' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + 1 => [ + 'prefix' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + ], + 'type' => 'acme', + ], + ], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param BuilderInterface[] $filters Array. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($filters, $parameters, $expected) + { + $filter = new OrFilter($filters, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/PostFilterTest.php b/tests/Filter/PostFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..9b60ff1e15c3fa6622589057b00f81f3a473bf3d --- /dev/null +++ b/tests/Filter/PostFilterTest.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\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\PostFilter; + +class PostFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testIfGetType() + { + $postFilter = new PostFilter(); + $this->assertEquals('post_filter', $postFilter->getType()); + } + + /** + * Test if function is returning False. + */ + public function testIfIsRelevantFunctionIsReturningFalse() + { + $postFilter = new PostFilter(); + $this->assertFalse($postFilter->isRelevant()); + } + + /** + * Test addFilter method. + */ + public function testAddFilter() + { + $missingFilterMock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter') + ->disableOriginalConstructor() + ->getMock(); + $missingFilterMock + ->expects($this->once()) + ->method('toArray') + ->willReturn([]); + $missingFilterMock + ->expects($this->once()) + ->method('getType') + ->willReturn('test_type'); + + $postFilter = new PostFilter(); + $postFilter->setFilter($missingFilterMock); + $this->assertEquals(['test_type' => []], $postFilter->toArray()); + } +} diff --git a/tests/Filter/PrefixFilterTest.php b/tests/Filter/PrefixFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..afd46515cce974eba0c4add07bf309d42b8fde51 --- /dev/null +++ b/tests/Filter/PrefixFilterTest.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\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\PrefixFilter; + +class PrefixFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new PrefixFilter('', '', []); + $result = $filter->getType(); + $this->assertEquals('prefix', $result); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + ['', '', [], ['' => '']], + // Case #2. + ['prefix', 'foo', [], ['prefix' => 'foo']], + // Case #3. + ['prefix', 'foo', ['type' => 'acme'], ['prefix' => 'foo', 'type' => 'acme']], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string $field Field name. + * @param string $value Field value. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($field, $value, $parameters, $expected) + { + $filter = new PrefixFilter($field, $value, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/QueryFilterTest.php b/tests/Filter/QueryFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..0a5909af6ab51f60ef137be0b281118e3130f2dc --- /dev/null +++ b/tests/Filter/QueryFilterTest.php @@ -0,0 +1,67 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\QueryFilter; + +class QueryFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test query with '_cache' parameter. + */ + public function testToArrayWithGetTypeFqueryWithCache() + { + $mockBuilder = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->getMock(); + $mockBuilder->expects($this->any()) + ->method('getType') + ->willReturn('fquery'); + $filter = new QueryFilter($mockBuilder, ['_cache' => true]); + $result = $filter->toArray(); + $expectedResult = ['query' => ['fquery' => null], '_cache' => true]; + $this->assertEquals($expectedResult, $result); + } + + /** + * Test query without '_cache' parameter. + */ + public function testToArrayWithGetTypeQueryWithoutCache() + { + $mockBuilder = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface') + ->getMock(); + $mockBuilder->expects($this->any()) + ->method('getType') + ->willReturn('query'); + $filter = new QueryFilter($mockBuilder, []); + $result = $filter->toArray(); + $expectedResult = ['query' => null]; + $this->assertEquals($expectedResult, $result); + } + + /** + * Test GetType function, returns 'fquery'. + */ + public function testGetTypeWhenReturnsStringFquery() + { + $filter = new QueryFilter('', ['_cache' => true]); + $this->assertEquals('fquery', $filter->getType()); + } + + /** + * Test GetType function, returns 'query'. + */ + public function testgetTypeWhenReturnsStringQuery() + { + $filter = new QueryFilter('', []); + $this->assertEquals('query', $filter->getType()); + } +} diff --git a/tests/Filter/RangeFilterTest.php b/tests/Filter/RangeFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..4214082170b9ce6a093d4063aa85f93dd5aa7373 --- /dev/null +++ b/tests/Filter/RangeFilterTest.php @@ -0,0 +1,60 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\RangeFilter; + +class RangeFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new RangeFilter('', [], []); + $this->assertEquals('range', $filter->getType()); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + ['', [], [], ['' => []]], + // Case #2. + ['foo', [1, 5], [], ['foo' => [0 => 1, 1 => 5]]], + // Case #3. + ['test', ['foo', 'bar'], ['type' => 'acme'], ['test' => [0 => 'foo', 1 => 'bar'], 'type' => 'acme']], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string $field Field name. + * @param array $range Range values. + * @param array $parameters Optional parameters. + * @param array $expected Expected result. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($field, $range, $parameters, $expected) + { + $filter = new RangeFilter($field, $range, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/RegexpFilterTest.php b/tests/Filter/RegexpFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..b54eede9cce7ba61fa733ce2a130603f3ae0479d --- /dev/null +++ b/tests/Filter/RegexpFilterTest.php @@ -0,0 +1,58 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\RegexpFilter; + +class RegexpFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new RegexpFilter('', '\w', []); + $this->assertEquals('regexp', $filter->getType()); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + ['', '\w', [], ['' => ['value' => '\w']]], + // Case #2. + ['regexp', '\w', ['flags' => 'foo'], ['regexp' => ['value' => '\w', 'flags' => 'foo']]], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string $field Field name. + * @param string $regexp Regular expression. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($field, $regexp, $parameters, $expected) + { + $filter = new RegexpFilter($field, $regexp, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/ScriptFilterTest.php b/tests/Filter/ScriptFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..bcbdf68a711b11a2dc00b8f7dd4b467d947d6635 --- /dev/null +++ b/tests/Filter/ScriptFilterTest.php @@ -0,0 +1,59 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\ScriptFilter; + +class ScriptFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new ScriptFilter(''); + $this->assertEquals('script', $filter->getType()); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + ['', [], ['script' => '']], + // Case #2. + ['foo', [], ['script' => 'foo']], + // Case #3. + ['foo', ['type' => 'acme'], ['script' => 'foo', 'type' => 'acme']], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string $script Script. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($script, $parameters, $expected) + { + $filter = new ScriptFilter($script, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/TermFilterTest.php b/tests/Filter/TermFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..11af22b3cebd9da5c2cdba3140d3edb79863f831 --- /dev/null +++ b/tests/Filter/TermFilterTest.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\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\TermFilter; + +class TermFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new TermFilter('', '', []); + $result = $filter->getType(); + $this->assertEquals('term', $result); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + ['', '', [], ['' => '']], + // Case #2. + ['term', 'foo', [], ['term' => 'foo']], + // Case #3. + ['term', 'foo', ['type' => 'acme'], ['term' => 'foo', 'type' => 'acme']], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string $field Field name. + * @param string $term Field value. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($field, $term, $parameters, $expected) + { + $filter = new TermFilter($field, $term, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/TermsFilterTest.php b/tests/Filter/TermsFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..e9f7cdf0ff6d4fe7b345e141746ebe79414abd0f --- /dev/null +++ b/tests/Filter/TermsFilterTest.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\ElasticsearchBundle\Tests\Unit\DSL\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\TermsFilter; + +class TermsFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new TermsFilter('', [], []); + $result = $filter->getType(); + $this->assertEquals('terms', $result); + } + + /** + * Data provider to testGetToArray. + * + * @return array + */ + public function getArrayDataProvider() + { + return [ + // Case #1. + ['', [], [], ['' => []]], + // Case #2. + ['tags', ['foo', 'bar'], [], ['tags' => [0 => 'foo', 1 => 'bar']]], + // Case #3. + ['tags', ['foo', 'bar'], ['type' => 'acme'], ['tags' => [0 => 'foo', 1 => 'bar'], 'type' => 'acme']], + ]; + } + + /** + * Test for filter toArray() method. + * + * @param string $field Field name. + * @param array $terms An array of terms. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayDataProvider + */ + public function testToArray($field, $terms, $parameters, $expected) + { + $filter = new TermsFilter($field, $terms, $parameters); + $result = $filter->toArray(); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Filter/TypeFilterTest.php b/tests/Filter/TypeFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..99bbf0aefad40d3bd53a67b852294a28c697cb29 --- /dev/null +++ b/tests/Filter/TypeFilterTest.php @@ -0,0 +1,36 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\TypeFilter; + +class TypeFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new TypeFilter(''); + $this->assertEquals('type', $filter->getType()); + } + + /** + * Test for filter toArray() method. + */ + public function testToArray() + { + $filter = new TypeFilter('foo'); + $expectedResult = ['value' => 'foo']; + $this->assertEquals($expectedResult, $filter->toArray()); + } +} diff --git a/tests/Highlight/FieldTest.php b/tests/Highlight/FieldTest.php new file mode 100644 index 0000000000000000000000000000000000000000..93c3cf961141d86b1cd7bb4cd35b86f74c3359c9 --- /dev/null +++ b/tests/Highlight/FieldTest.php @@ -0,0 +1,80 @@ +<?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\Highlight; + +use ONGR\ElasticsearchBundle\DSL\Filter\TermFilter; +use ONGR\ElasticsearchBundle\DSL\Highlight\Field; + +/** + * Unit test for Field. + */ +class FieldTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests getType method. + */ + public function testGetType() + { + $field = new Field('test'); + + $field->setHighlighterType(Field::TYPE_FVH); + $this->assertEquals(Field::TYPE_FVH, $field->getType()); + + $field->setHighlighterType(Field::TYPE_PLAIN); + $this->assertEquals(Field::TYPE_PLAIN, $field->getType()); + + $field->setHighlighterType(Field::TYPE_POSTINGS); + $this->assertEquals(Field::TYPE_POSTINGS, $field->getType()); + + $initValue = $field->getType(); + + $field->setHighlighterType('wrongValue'); + $this->assertEquals($initValue, $field->getType()); + } + + /** + * Tests toArray method. + */ + public function testFieldToArray() + { + $field = new Field('test'); + $field->setFragmentSize(5); + $field->setNumberOfFragments(5); + $field->setHighlightQuery(new TermFilter('key1', 'value1')); + $field->setNoMatchSize(3); + $field->setForceSource(true); + + $result = [ + 'fragment_size' => 5, + 'number_of_fragments' => 5, + 'matched_fields' => ['test'], + 'highlight_query' => [ + 'term' => [ + 'key1' => 'value1', + ], + ], + 'no_match_size' => 3, + 'force_source' => true, + ]; + $this->assertEquals($result, $field->toArray()); + } + + /** + * Tests getName method. + */ + public function testFieldGetName() + { + $field = new Field('test'); + $result = $field->getName(); + $this->assertEquals('test', $result); + } +} diff --git a/tests/Highlight/HighlightTest.php b/tests/Highlight/HighlightTest.php new file mode 100644 index 0000000000000000000000000000000000000000..959f970fdc2be518c63c5b69c7511c5b9a5978f4 --- /dev/null +++ b/tests/Highlight/HighlightTest.php @@ -0,0 +1,52 @@ +<?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\Highlight; + +use ONGR\ElasticsearchBundle\DSL\Highlight\Field; +use ONGR\ElasticsearchBundle\DSL\Highlight\Highlight; + +/** + * Unit test for Highlight. + */ +class HighlightTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests toArray method. + */ + public function testHighlightToArray() + { + $highlight = new Highlight([new Field('name')]); + $highlight->setOrder('test'); + $highlight->setHighlighterType('postings'); + $highlight->setFragmentSize(5); + $highlight->setNumberOfFragments(5); + $highlight->setTagsSchema('styled'); + $highlight->setTag('tag', 'class'); + $highlight->setTag('only_tag'); + + $result = [ + 'order' => 'test', + 'type' => 'postings', + 'fragment_size' => 5, + 'number_of_fragments' => 5, + 'tags_schema' => 'styled', + 'post_tags' => ['</tag>', '</only_tag>'], + 'pre_tags' => ['<tag class="class">', '<only_tag>'], + 'fields' => [ + 'name' => [ + 'matched_fields' => ['name'], + ], + ], + ]; + $this->assertEquals($result, $highlight->toArray()); + } +} diff --git a/tests/NamedBuilderBagTest.php b/tests/NamedBuilderBagTest.php new file mode 100644 index 0000000000000000000000000000000000000000..9a0e98a737b2df611601b9dca423f31543d9c836 --- /dev/null +++ b/tests/NamedBuilderBagTest.php @@ -0,0 +1,104 @@ +<?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; + +use ONGR\ElasticsearchBundle\DSL\NamedBuilderBag; +use ONGR\ElasticsearchBundle\DSL\NamedBuilderInterface; + +class NamedBuilderBagTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests if bag knows if he has a builder. + */ + public function testHas() + { + $bag = new NamedBuilderBag( + [ + $this->getBuilder('foo'), + ] + ); + $this->assertTrue($bag->has('foo')); + } + + /** + * Tests if bag can remove a builder. + */ + public function testRemove() + { + $bag = new NamedBuilderBag( + [ + $this->getBuilder('foo'), + $this->getBuilder('baz'), + ] + ); + + $bag->remove('foo'); + + $this->assertFalse($bag->has('foo'), 'Foo builder should not exist anymore.'); + $this->assertTrue($bag->has('baz'), 'Baz builder should exist.'); + } + + /** + * Tests if bag can clear it's builders. + */ + public function testClear() + { + $bag = new NamedBuilderBag( + [ + $this->getBuilder('foo'), + $this->getBuilder('baz'), + ] + ); + + $bag->clear(); + + $this->assertEmpty($bag->all()); + } + + /** + * Tests if bag can get a builder. + */ + public function testGet() + { + $bag = new NamedBuilderBag( + [ + $this->getBuilder('baz'), + ] + ); + + $this->assertNotEmpty($bag->get('baz')); + } + + /** + * Returns builder. + * + * @param string $name + * + * @return \PHPUnit_Framework_MockObject_MockObject|NamedBuilderInterface + */ + private function getBuilder($name) + { + $friendlyBuilderMock = $this->getMock('ONGR\ElasticsearchBundle\DSL\NamedBuilderInterface'); + + $friendlyBuilderMock + ->expects($this->once()) + ->method('getName') + ->will($this->returnValue($name)); + + $friendlyBuilderMock + ->expects($this->any()) + ->method('toArray') + ->will($this->returnValue([])); + + return $friendlyBuilderMock; + } +} diff --git a/tests/ParametersTraitTest.php b/tests/ParametersTraitTest.php new file mode 100644 index 0000000000000000000000000000000000000000..3b9507b92a2f623aac6dbadf3f6a138b66e2a9fb --- /dev/null +++ b/tests/ParametersTraitTest.php @@ -0,0 +1,44 @@ +<?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; + +use ONGR\ElasticsearchBundle\DSL\ParametersTrait; + +/** + * Test for ParametersTrait. + */ +class ParametersTraitTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var ParametersTrait + */ + private $parametersTraitMock; + + /** + * {@inheritdoc} + */ + public function setUp() + { + $this->parametersTraitMock = $this->getMockForTrait('ONGR\ElasticsearchBundle\DSL\ParametersTrait'); + } + + /** + * Tests addParameter method. + */ + public function testGetAndAddParameter() + { + $this->parametersTraitMock->addParameter('acme', 123); + $this->assertEquals(123, $this->parametersTraitMock->getParameter('acme')); + $this->parametersTraitMock->addParameter('bar', 321); + $this->assertEquals(321, $this->parametersTraitMock->getParameter('bar')); + } +} diff --git a/tests/Query/HasChildQueryTest.php b/tests/Query/HasChildQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..4fe6d8293e056581e46f6550a17bc9ccee8bccc3 --- /dev/null +++ b/tests/Query/HasChildQueryTest.php @@ -0,0 +1,29 @@ +<?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; + +use ONGR\ElasticsearchBundle\DSL\Query\HasChildQuery; + +class HasChildQueryTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests whether __constructor calls setParameters method. + */ + public function testConstructor() + { + $missingFilterMock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter') + ->setConstructorArgs(['test_field']) + ->getMock(); + $query = new HasChildQuery('test_type', $missingFilterMock, ['test_parameter1']); + $this->assertEquals(['test_parameter1'], $query->getParameters()); + } +} diff --git a/tests/Query/HasParentQueryTest.php b/tests/Query/HasParentQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..57a8b77837150dca23efe0bdbec668d136f3c0ad --- /dev/null +++ b/tests/Query/HasParentQueryTest.php @@ -0,0 +1,29 @@ +<?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; + +use ONGR\ElasticsearchBundle\DSL\Query\HasParentQuery; + +class HasParentQueryTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests whether __constructor calls setParameters method. + */ + public function testConstructor() + { + $missingFilter = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter') + ->setConstructorArgs(['test_field']) + ->getMock(); + $query = new HasParentQuery('test_type', $missingFilter, ['test_parameter1']); + $this->assertEquals(['test_parameter1'], $query->getParameters()); + } +} diff --git a/tests/Query/IndicesQueryTest.php b/tests/Query/IndicesQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..13366530dc815857ff75b2bfa177e54a8f14a2c6 --- /dev/null +++ b/tests/Query/IndicesQueryTest.php @@ -0,0 +1,72 @@ +<?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; + +use ONGR\ElasticsearchBundle\DSL\Query\FilteredQuery; +use ONGR\ElasticsearchBundle\DSL\Query\IndicesQuery; +use ONGR\ElasticsearchBundle\Test\EncapsulationTestAwareTrait; + +class IndicesQueryTest extends \PHPUnit_Framework_TestCase +{ + /** + * Data provider for testToArrayManyIndices function. + * + * @return array + */ + public function getArrayWithManyIndicesDataProvider() + { + $queryMock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Query\Query') + ->getMock(); + $queryMock->expects($this->any()) + ->method('toArray') + ->willReturn(['testKey' => 'testValue']); + $queryMock->expects($this->any()) + ->method('getType') + ->willReturn('testType'); + + return [ + [ + $queryMock, + [ + 'test_indice1', + 'test_indice2', + ], + [ + 'indices' => [ + 'test_indice1', + 'test_indice2', + ], + 'query' => [ + 'testType' => [ + 'testKey' => 'testValue', + ], + ], + ], + ], + ]; + } + + /** + * Test toArray() method when the number of indices > 1. + * + * @param Query $query Query for testing. + * @param array $parameters Optional parameters. + * @param array $expected Expected values. + * + * @dataProvider getArrayWithManyIndicesDataProvider + */ + public function testToArrayWithManyIndices($query, $parameters, $expected) + { + $query = new IndicesQuery($parameters, $query); + $this->assertEquals($expected, $query->toArray()); + } +} diff --git a/tests/Query/NestedQueryTest.php b/tests/Query/NestedQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..545df36f1628deaff62455dbbc096092f40d57dd --- /dev/null +++ b/tests/Query/NestedQueryTest.php @@ -0,0 +1,43 @@ +<?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; + +use ONGR\ElasticsearchBundle\DSL\Query\NestedQuery; + +class NestedQueryTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests toArray method. + */ + public function testToArray() + { + $missingFilterMock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter') + ->setConstructorArgs(['test_field']) + ->getMock(); + $missingFilterMock->expects($this->any()) + ->method('getType') + ->willReturn('test_type'); + $missingFilterMock->expects($this->any()) + ->method('toArray') + ->willReturn(['testKey' => 'testValue']); + + $result = [ + 'path' => 'test_path', + 'query' => [ + 'test_type' => ['testKey' => 'testValue'], + ], + ]; + + $query = new NestedQuery('test_path', $missingFilterMock); + $this->assertEquals($result, $query->toArray()); + } +} diff --git a/tests/Query/QueryTest.php b/tests/Query/QueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..e2ffd57fd2108cc3428ec895b59419941c58d09f --- /dev/null +++ b/tests/Query/QueryTest.php @@ -0,0 +1,86 @@ +<?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; + +use ONGR\ElasticsearchBundle\DSL\Query\Query; + +class QueryTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests setBoolParameters method. + */ + public function testSetBoolParameters() + { + $missingFilterMock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter') + ->setConstructorArgs(['test_field']) + ->getMock(); + $missingFilterMock->expects($this->once()) + ->method('setParameters'); + + $query = new Query(); + $query->setQuery($missingFilterMock); + $query->setBoolParameters([false]); + } + + /** + * Tests addQuery method. + */ + public function testAddQuery() + { + $missingFilterMock = $this + ->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter') + ->disableOriginalConstructor() + ->setMethods(['add']) + ->getMock(); + $missingFilterMock + ->expects($this->once()) + ->method('add') + ->withAnyParameters(); + $postFilterMock = $this + ->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Filter\PostFilter') + ->disableOriginalConstructor() + ->getMock(); + + $query = new Query(); + $query->setQuery($missingFilterMock); + $query->addQuery($postFilterMock); + } + + /** + * Tests getType method. + */ + public function testGetType() + { + $query = new Query(); + $this->assertEquals('query', $query->getType()); + } + + /** + * Tests toArray method. + */ + public function testToArray() + { + $missingFilterMock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter') + ->disableOriginalConstructor() + ->getMock(); + $missingFilterMock->expects($this->once()) + ->method('getType') + ->willReturn('test_type'); + $missingFilterMock->expects($this->once()) + ->method('toArray') + ->willReturn('test_array'); + + $query = new Query(); + $query->setQuery($missingFilterMock); + $this->assertEquals(['test_type' => 'test_array'], $query->toArray()); + } +} diff --git a/tests/Query/Span/SpanFirstQueryTest.php b/tests/Query/Span/SpanFirstQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..9b1e4d1ef867e68d0ea3ab62835d837f5ed3d472 --- /dev/null +++ b/tests/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() + { + unset($this->mock); + } + + /** + * 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/tests/Query/Span/SpanMultiTermQueryTest.php b/tests/Query/Span/SpanMultiTermQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..dbbfe4edad1b212e6a434d090e0d49f24ea5f693 --- /dev/null +++ b/tests/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() + { + unset($this->mock); + } + + /** + * 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/tests/Query/Span/SpanNearQueryTest.php b/tests/Query/Span/SpanNearQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..ebdcfbc6d1ce43248aa03e59e050e9a09357cd0b --- /dev/null +++ b/tests/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() + { + unset($this->mock); + } + + /** + * 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/tests/Query/Span/SpanNotQueryTest.php b/tests/Query/Span/SpanNotQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..df6856a8bb455c8e8674559917b9c0f8f4caca19 --- /dev/null +++ b/tests/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() + { + unset($this->mock); + } + + /** + * 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/tests/Query/Span/SpanOrQueryTest.php b/tests/Query/Span/SpanOrQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..5e4b1fe92593385447a1f4e5b1799b15bbcc5994 --- /dev/null +++ b/tests/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() + { + unset($this->mock); + } + + /** + * 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/tests/Query/Span/SpanTermQueryTest.php b/tests/Query/Span/SpanTermQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..1af7971f32461096cc088393ff849edf6ecca69f --- /dev/null +++ b/tests/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); + } +} diff --git a/tests/SearchEndpoint/SearchEndpointFactoryTest.php b/tests/SearchEndpoint/SearchEndpointFactoryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..eeaee162242e13cb9884b856f85be768c53f7baf --- /dev/null +++ b/tests/SearchEndpoint/SearchEndpointFactoryTest.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\SearchEndpoint; + +use ONGR\ElasticsearchBundle\DSL\SearchEndpoint\SearchEndpointFactory; + +/** + * Unit test class for search endpoint factory. + */ +class SearchEndpointFactoryTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests get method exception. + * + * @expectedException \RuntimeException + */ + public function testGet() + { + SearchEndpointFactory::get('foo'); + } +} diff --git a/tests/Suggester/CompletionTest.php b/tests/Suggester/CompletionTest.php new file mode 100644 index 0000000000000000000000000000000000000000..973d8e0b468b91e94873b450d480580fcc607b9c --- /dev/null +++ b/tests/Suggester/CompletionTest.php @@ -0,0 +1,153 @@ +<?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\Suggester; + +use ONGR\ElasticsearchBundle\DSL\Suggester\Completion; +use ONGR\ElasticsearchBundle\Test\EncapsulationTestAwareTrait; + +class CompletionTest extends \PHPUnit_Framework_TestCase +{ + use EncapsulationTestAwareTrait; + + /** + * @return array + */ + public function getTestToArrayData() + { + $out = []; + + // Case #0: simple. + $completion0 = new Completion('my-field', 'lorem ipsum'); + $expected0 = [ + 'my-field-completion' => [ + 'text' => 'lorem ipsum', + 'completion' => ['field' => 'my-field'], + ], + ]; + + $out[] = [ + $expected0, + $completion0, + ]; + + // Case #1: using fuzzy. + $completion1 = new Completion('my-other-field', 'super awesome cat', 'my-completion1'); + $completion1->useFuzzy(true); + $expected1 = [ + 'my-completion1' => [ + 'text' => 'super awesome cat', + 'completion' => [ + 'field' => 'my-other-field', + 'fuzzy' => true, + ], + ], + ]; + + $out[] = [ + $expected1, + $completion1, + ]; + + // Case #2: providing all data. + $completion2 = new Completion('body', 'even more super awesome cat', 'my-completion2'); + $completion2 + ->setFuzziness(2) + ->setMinLength(3) + ->setPrefixLength(1) + ->setTranspositions(true) + ->setUnicodeAware(''); + + $expected2 = [ + 'my-completion2' => [ + 'text' => 'even more super awesome cat', + 'completion' => [ + 'field' => 'body', + 'fuzzy' => [ + 'fuzziness' => 2, + 'transpositions' => true, + 'min_length' => 3, + 'prefix_length' => 1, + ], + ], + ], + ]; + + $out[] = [ + $expected2, + $completion2, + ]; + + return $out; + } + + /** + * Tests toArray method. + * + * @param array $expected + * @param Completion $completion + * + * @dataProvider getTestToArrayData + */ + public function testToArray($expected, $completion) + { + $this->assertEquals($expected, $completion->toArray()); + } + + /** + * Tests if toArray method throws Logic Exception. + * + * @expectedException \LogicException + */ + public function testToArrayException() + { + $completion = new Completion('foo', 'bar'); + $completion->setField(null); + $completion->setText(null); + $completion->toArray(); + } + + /** + * Tests useFuzzy property. + */ + public function testUseFuzzy() + { + $completion = new Completion('foo', 'bar'); + $completion->useFuzzy(true); + $this->assertEquals(true, $completion->isFuzzy()); + } + + /** + * @return string + */ + public function getClassName() + { + $this->setStub(new Completion('foo', 'bar')); + $this->addIgnoredField('useFuzzy'); + + return 'ONGR\ElasticsearchBundle\DSL\Suggester\Completion'; + } + + /** + * @return array + */ + public function getFieldsData() + { + return [ + ['name'], + ['fuzziness'], + ['transpositions', 'boolean'], + ['minLength'], + ['prefixLength'], + ['unicodeAware'], + ]; + } +} diff --git a/tests/Suggester/Context/CategoryContextTest.php b/tests/Suggester/Context/CategoryContextTest.php new file mode 100644 index 0000000000000000000000000000000000000000..e5567cd2ed04c45fa63efc80cce8090fa88cfd9b --- /dev/null +++ b/tests/Suggester/Context/CategoryContextTest.php @@ -0,0 +1,41 @@ +<?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\Suggester\Context; + +use ONGR\ElasticsearchBundle\DSL\Suggester\Context\CategoryContext; +use ONGR\ElasticsearchBundle\Test\EncapsulationTestAwareTrait; + +class CategoryContextTest extends \PHPUnit_Framework_TestCase +{ + use EncapsulationTestAwareTrait; + + /** + * @return string + */ + public function getClassName() + { + $this->setStub(new CategoryContext('foo', 'bar')); + + return 'ONGR\ElasticsearchBundle\DSL\Suggester\Context\CategoryContext'; + } + + /** + * @return array + */ + public function getFieldsData() + { + return [ + ['name'], + ['value'], + ]; + } +} diff --git a/tests/Suggester/Context/GeoContextTest.php b/tests/Suggester/Context/GeoContextTest.php new file mode 100644 index 0000000000000000000000000000000000000000..8a8a918319237cdf5de24ee9b7021999da9699af --- /dev/null +++ b/tests/Suggester/Context/GeoContextTest.php @@ -0,0 +1,43 @@ +<?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\Suggester\Context; + +use ONGR\ElasticsearchBundle\DSL\Suggester\Context\CategoryContext; +use ONGR\ElasticsearchBundle\DSL\Suggester\Context\GeoContext; +use ONGR\ElasticsearchBundle\Test\EncapsulationTestAwareTrait; + +class GeoContextTest extends \PHPUnit_Framework_TestCase +{ + use EncapsulationTestAwareTrait; + + /** + * @return string + */ + public function getClassName() + { + $this->setStub(new GeoContext('foo', 'bar')); + + return 'ONGR\ElasticsearchBundle\DSL\Suggester\Context\GeoContext'; + } + + /** + * @return array + */ + public function getFieldsData() + { + return [ + ['precision'], + ['name'], + ['value'], + ]; + } +} diff --git a/tests/Suggester/ContextTest.php b/tests/Suggester/ContextTest.php new file mode 100644 index 0000000000000000000000000000000000000000..363a368317069dbdc96c3783b09e0ba9e71af79e --- /dev/null +++ b/tests/Suggester/ContextTest.php @@ -0,0 +1,69 @@ +<?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\Suggester; + +use ONGR\ElasticsearchBundle\DSL\Suggester\Completion; +use ONGR\ElasticsearchBundle\DSL\Suggester\Context; +use ONGR\ElasticsearchBundle\DSL\Suggester\Phrase; +use ONGR\ElasticsearchBundle\Test\EncapsulationTestAwareTrait; + +class ContextTest extends \PHPUnit_Framework_TestCase +{ + use EncapsulationTestAwareTrait; + + /** + * Tests toArray method when $this->getSize() !== null. + */ + public function testToArrayNotNull() + { + $name = 'testName'; + + $context = new Context('', '', $name); + $context->setSize(123); + $context->setContext(new Phrase('', '')); + + $result = $context->toArray(); + $this->assertArrayHasKey($name, $result); + + $data = $result[$name]; + $this->assertArrayHasKey('completion', $data); + + $completion = $data['completion']; + $this->assertArrayHasKey('size', $completion); + $this->assertEquals($completion['size'], 123); + } + + /** + * Returns list of fields to test. Works as data provider. + * + * @return array + */ + public function getFieldsData() + { + return [ + ['context'], + ['size'], + ]; + } + + /** + * Returns entity class name. + * + * @return string + */ + public function getClassName() + { + $this->setStub(new Context('foo', 'bar')); + + return 'ONGR\ElasticsearchBundle\DSL\Suggester\Context'; + } +} diff --git a/tests/Suggester/PhraseTest.php b/tests/Suggester/PhraseTest.php new file mode 100644 index 0000000000000000000000000000000000000000..946bcee0b880448a6e330b26ed3b396220685668 --- /dev/null +++ b/tests/Suggester/PhraseTest.php @@ -0,0 +1,130 @@ +<?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\Suggester; + +use ONGR\ElasticsearchBundle\DSL\Suggester\Phrase; +use ONGR\ElasticsearchBundle\Test\EncapsulationTestAwareTrait; + +class PhraseTest extends \PHPUnit_Framework_TestCase +{ + use EncapsulationTestAwareTrait; + + /** + * @return array + */ + public function getTestToArrayData() + { + $out = []; + + // Case #0: simple. + $phrase0 = new Phrase('body', 'lorem ipsum'); + $expected0 = [ + 'body-phrase' => [ + 'text' => 'lorem ipsum', + 'phrase' => ['field' => 'body'], + ], + ]; + + $out[] = [ + $expected0, + $phrase0, + ]; + + // Case #1: using all fields. + $phrase1 = new Phrase('description', 'awesome cat'); + $phrase1->setMaxErrors(2); + $phrase1->setGramSize(1); + $phrase1->setRealWordErrorLikelihood(0.95); + $phrase1->setHighlight(['pre_tag' => '<span class="info">', 'post_tag' => '</span>']); + $phrase1->setAnalyzer('simple'); + $phrase1->setConfidence(1); + $phrase1->setSize(6); + + $highlightObject = new \stdClass(); + $highlightObject->post_tag = '</span>'; + $highlightObject->pre_tag = '<span class="info">'; + + $expected1 = [ + 'description-phrase' => [ + 'text' => 'awesome cat', + 'phrase' => [ + 'analyzer' => 'simple', + 'field' => 'description', + 'size' => 6, + 'real_word_error_likelihood' => 0.95, + 'max_errors' => 2.0, + 'gram_size' => 1, + 'highlight' => $highlightObject, + ], + ], + ]; + + $out[] = [ + $expected1, + $phrase1, + ]; + + return $out; + } + + /** + * Tests toArray method. + * + * @param array $expected + * @param Phrase $phrase + * + * @dataProvider getTestToArrayData + */ + public function testToArray($expected, $phrase) + { + $this->assertEquals($expected, $phrase->toArray()); + } + + /** + * Tests toArray method exception. + * + * @expectedException \LogicException + */ + public function testToArrayException() + { + $phrase = new Phrase('', ''); + $phrase->toArray(); + } + + /** + * @return string + */ + public function getClassName() + { + $this->setStub(new Phrase('foo', 'bar')); + + return 'ONGR\ElasticsearchBundle\DSL\Suggester\Phrase'; + } + + /** + * Returns list of fields to test. Works as data provider. + * + * @return array + */ + public function getFieldsData() + { + return [ + ['analyzer'], + ['gramSize'], + ['realWordErrorLikelihood'], + ['confidence'], + ['maxErrors'], + ['highlight'], + ['size'], + ]; + } +} diff --git a/tests/Suggester/TermTest.php b/tests/Suggester/TermTest.php new file mode 100644 index 0000000000000000000000000000000000000000..c0ec281975d0ddaf6d1b73ad6b55ac4c1086c3de --- /dev/null +++ b/tests/Suggester/TermTest.php @@ -0,0 +1,156 @@ +<?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\Suggester; + +use ONGR\ElasticsearchBundle\DSL\Suggester\Term; +use ONGR\ElasticsearchBundle\Test\EncapsulationTestAwareTrait; + +class TermTest extends \PHPUnit_Framework_TestCase +{ + use EncapsulationTestAwareTrait; + + /** + * @return array + */ + public function getTestToArrayData() + { + $out = []; + + // Case #0: simple. + $term0 = new Term('body', 'lorem ipsum'); + $expected0 = [ + 'body-term' => [ + 'text' => 'lorem ipsum', + 'term' => ['field' => 'body'], + ], + ]; + $out[] = [ + $expected0, + $term0, + ]; + + // Case #1: full suggester. + $term1 = new Term('body', 'lorem ipsum'); + $term1 + ->setSize(2) + ->setAnalyzer('simple') + ->setSuggestMode(Term::SUGGEST_MODE_ALWAYS) + ->setSort(Term::SORT_BY_SCORE); + + $expected1 = [ + 'body-term' => [ + 'text' => 'lorem ipsum', + 'term' => [ + 'field' => 'body', + 'analyzer' => 'simple', + 'sort' => 'score', + 'suggest_mode' => 'always', + ], + 'size' => 2, + ], + ]; + + $out[] = [ + $expected1, + $term1, + ]; + + return $out; + } + + /** + * Tests toArray method. + * + * @param array $expected + * @param Term $suggester + * + * @dataProvider getTestToArrayData + */ + public function testToArray($expected, $suggester) + { + $this->assertEquals($expected, $suggester->toArray()); + } + + /** + * Tests toArray method exception. + * + * @expectedException \LogicException + */ + public function testToArrayException() + { + $term = new Term('', ''); + $term->toArray(); + } + + /** + * Tests setSort method. + */ + public function testSetSort() + { + $term = new Term('foo', 'bar'); + + $term->setSort(Term::SORT_BY_FREQ); + $this->assertEquals(Term::SORT_BY_FREQ, $term->getSort()); + + $term->setSort(Term::SORT_BY_SCORE); + $this->assertEquals(Term::SORT_BY_SCORE, $term->getSort()); + + $initValue = $term->getSort(); + $term->setSort('wrongSort'); + $this->assertEquals($initValue, $term->getSort()); + } + + /** + * Tests setSuggestMode method. + */ + public function testSetSuggestMode() + { + $term = new Term('foo', 'bar'); + + $term->setSuggestMode(Term::SUGGEST_MODE_ALWAYS); + $this->assertEquals(Term::SUGGEST_MODE_ALWAYS, $term->getSuggestMode()); + + $term->setSuggestMode(Term::SUGGEST_MODE_MISSING); + $this->assertEquals(Term::SUGGEST_MODE_MISSING, $term->getSuggestMode()); + + $initValue = $term->getSuggestMode(); + $term->setSuggestMode('wrongMode'); + $this->assertEquals($initValue, $term->getSuggestMode()); + } + + /** + * Returns list of fields to test. Works as data provider. + * + * @return array + */ + public function getFieldsData() + { + return [ + ['analyzer'], + ['size'], + ]; + } + + /** + * Returns entity class name. + * + * @return string + */ + public function getClassName() + { + $this->setStub(new Term('foo', 'bar')); + $this->addIgnoredField('sort'); + $this->addIgnoredField('suggestMode'); + + return 'ONGR\ElasticsearchBundle\DSL\Suggester\Term'; + } +}