diff --git a/Filter/GeoBoundingBoxFilterTest.php b/Filter/GeoBoundingBoxFilterTest.php index 034cc8c2aa7392a7ac9c37eac07677555fd8176f..e5164c2b791a3177bf32316fdf9b9bda276cd08c 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 ef47d6741ef01e02baf9fbced57075c86241cdc2..f506159c1294fb0dd0a6b1d46dbe838d87e972e4 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 dc28570bd49aa3dae869b95b71a0816a156afebd..a90c7c703a5cf75ee6e3bcf964cb899458e50109 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 1eae928c245a787783b7137913755c826642e03d..f24e1c1c058aea632e384dca8ee4342416c07833 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 b0312b7a8fcbef8c472f5066187a9d08b1e58dee..a4001c37da117141b35d578317189f8fe602eb55 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 b33f1cc7921027e984eac366061afa8d6e894a0a..6f6c9031594bfd1f4d0df423620ffff00ac4350c 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); + } }