From 57987c613cf5975cd573f0eea82c3f97a7355406 Mon Sep 17 00:00:00 2001 From: David Vondrak <david.vondrak@ongr.io> Date: Thu, 19 Mar 2015 11:23:44 +0200 Subject: [PATCH] Added unit tests to DSL filters. --- Filter/GeoBoundingBoxFilterTest.php | 69 ++++++++++++++++++++++++++ Filter/GeoDistanceFilterTest.php | 45 +++++++++++++++++ Filter/GeoDistanceRangeFilterTest.php | 45 +++++++++++++++++ Filter/GeoPolygonFilterTest.php | 71 +++++++++++++++++++++++++++ Filter/HasChildFilterTest.php | 57 +++++++++++++++++++++ Filter/HasParentFilterTest.php | 57 +++++++++++++++++++++ 6 files changed, 344 insertions(+) diff --git a/Filter/GeoBoundingBoxFilterTest.php b/Filter/GeoBoundingBoxFilterTest.php index 034cc8c..e5164c2 100644 --- a/Filter/GeoBoundingBoxFilterTest.php +++ b/Filter/GeoBoundingBoxFilterTest.php @@ -15,6 +15,16 @@ 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. * @@ -25,4 +35,63 @@ class GeoBoundingBoxFilterTest extends \PHPUnit_Framework_TestCase $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/Filter/GeoDistanceFilterTest.php b/Filter/GeoDistanceFilterTest.php index ef47d67..f506159 100644 --- a/Filter/GeoDistanceFilterTest.php +++ b/Filter/GeoDistanceFilterTest.php @@ -24,4 +24,49 @@ class GeoDistanceFilterTest extends \PHPUnit_Framework_TestCase $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/Filter/GeoDistanceRangeFilterTest.php b/Filter/GeoDistanceRangeFilterTest.php index dc28570..a90c7c7 100644 --- a/Filter/GeoDistanceRangeFilterTest.php +++ b/Filter/GeoDistanceRangeFilterTest.php @@ -24,4 +24,49 @@ class GeoDistanceRangeFilterTest extends \PHPUnit_Framework_TestCase $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/Filter/GeoPolygonFilterTest.php b/Filter/GeoPolygonFilterTest.php index 1eae928..f24e1c1 100644 --- a/Filter/GeoPolygonFilterTest.php +++ b/Filter/GeoPolygonFilterTest.php @@ -24,4 +24,75 @@ class GeoPolygonFilterTest extends \PHPUnit_Framework_TestCase $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/Filter/HasChildFilterTest.php b/Filter/HasChildFilterTest.php index b0312b7..a4001c3 100644 --- a/Filter/HasChildFilterTest.php +++ b/Filter/HasChildFilterTest.php @@ -25,4 +25,61 @@ class HasChildFilterTest extends \PHPUnit_Framework_TestCase $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/Filter/HasParentFilterTest.php b/Filter/HasParentFilterTest.php index b33f1cc..6f6c903 100644 --- a/Filter/HasParentFilterTest.php +++ b/Filter/HasParentFilterTest.php @@ -25,4 +25,61 @@ class HasParentFilterTest extends \PHPUnit_Framework_TestCase $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); + } } -- GitLab