From e7021ffc1da465a691a28d28ca2a4983b1da673b Mon Sep 17 00:00:00 2001
From: Mantas Simkus <mantas.simkus@ongr.io>
Date: Wed, 18 Mar 2015 18:09:12 +0200
Subject: [PATCH] Added tests, improved coverage

---
 Aggregation/ChildrenAggregationTest.php    | 23 ++++++++
 Aggregation/DateRangeAggregationTest.php   | 28 +++++++++
 Aggregation/FiltersAggregationTest.php     | 67 ++++++++++++++++++++++
 Aggregation/GeoBoundsAggregationTest.php   | 28 +++++++++
 Aggregation/GeoDistanceAggregationTest.php | 60 +++++++++++++++++++
 Aggregation/GeoHashGridAggregationTest.php | 58 +++++++++++++++++++
 Aggregation/MissingAggregationTest.php     | 22 +++++++
 7 files changed, 286 insertions(+)

diff --git a/Aggregation/ChildrenAggregationTest.php b/Aggregation/ChildrenAggregationTest.php
index fc264d6..e050d87 100644
--- a/Aggregation/ChildrenAggregationTest.php
+++ b/Aggregation/ChildrenAggregationTest.php
@@ -28,4 +28,27 @@ class ChildrenAggregationTest extends \PHPUnit_Framework_TestCase
         $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->getArray();
+    }
 }
diff --git a/Aggregation/DateRangeAggregationTest.php b/Aggregation/DateRangeAggregationTest.php
index 26081ef..f2643ad 100644
--- a/Aggregation/DateRangeAggregationTest.php
+++ b/Aggregation/DateRangeAggregationTest.php
@@ -38,4 +38,32 @@ class DateRangeAggregationTest extends \PHPUnit_Framework_TestCase
         $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/Aggregation/FiltersAggregationTest.php b/Aggregation/FiltersAggregationTest.php
index 4bdcc2a..38c9cae 100644
--- a/Aggregation/FiltersAggregationTest.php
+++ b/Aggregation/FiltersAggregationTest.php
@@ -30,4 +30,71 @@ class FiltersAggregationTest extends \PHPUnit_Framework_TestCase
         $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/Aggregation/GeoBoundsAggregationTest.php b/Aggregation/GeoBoundsAggregationTest.php
index d1ede46..25eb2d7 100644
--- a/Aggregation/GeoBoundsAggregationTest.php
+++ b/Aggregation/GeoBoundsAggregationTest.php
@@ -28,4 +28,32 @@ class GeoBoundsAggregationTest extends \PHPUnit_Framework_TestCase
         $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(false);
+        $result = $agg->getArray();
+        $this->assertArrayHasKey('field', $result);
+        $this->assertArrayHasKey('wrap_longitude', $result);
+        $this->assertEquals('bar', $result['field']);
+        $this->assertFalse($result['wrap_longitude']);
+        $agg->setWrapLongitude(true);
+        $result = $agg->getArray();
+        $this->assertTrue($result['wrap_longitude']);
+    }
 }
diff --git a/Aggregation/GeoDistanceAggregationTest.php b/Aggregation/GeoDistanceAggregationTest.php
index 73075a6..4dd3e0e 100644
--- a/Aggregation/GeoDistanceAggregationTest.php
+++ b/Aggregation/GeoDistanceAggregationTest.php
@@ -52,4 +52,64 @@ class GeoDistanceAggregationTest extends \PHPUnit_Framework_TestCase
         $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/Aggregation/GeoHashGridAggregationTest.php b/Aggregation/GeoHashGridAggregationTest.php
index 0a520e9..aac5726 100644
--- a/Aggregation/GeoHashGridAggregationTest.php
+++ b/Aggregation/GeoHashGridAggregationTest.php
@@ -28,4 +28,62 @@ class GeoHashGridAggregationTest extends \PHPUnit_Framework_TestCase
         $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/Aggregation/MissingAggregationTest.php b/Aggregation/MissingAggregationTest.php
index df8e0b1..52b63fc 100644
--- a/Aggregation/MissingAggregationTest.php
+++ b/Aggregation/MissingAggregationTest.php
@@ -26,4 +26,26 @@ class MissingAggregationTest extends \PHPUnit_Framework_TestCase
         $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']);
+        $this->assertArrayHasKey('field', $result);
+    }
+
+    /**
+     * Test getType method.
+     */
+    public function testMissingAggregationGetType()
+    {
+        $aggregation = new MissingAggregation('bar');
+        $result = $aggregation->getType();
+        $this->assertEquals('missing', $result);
+    }
 }
-- 
GitLab