From d18b247abdda60dc387e6939b4c07c3225dcbfef Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mantas=20Jonu=C5=A1as?= <mantas.jonusas@nfq.lt>
Date: Wed, 4 Mar 2015 11:35:28 +0200
Subject: [PATCH] Added more tests, fixed query formating bug in date range
 aggregation

---
 Aggregation/DateRangeAggregation.php | 23 +++++++++++++----------
 Aggregation/MissingAggregation.php   |  3 +--
 2 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/Aggregation/DateRangeAggregation.php b/Aggregation/DateRangeAggregation.php
index 6af52d0..034b1fd 100644
--- a/Aggregation/DateRangeAggregation.php
+++ b/Aggregation/DateRangeAggregation.php
@@ -58,16 +58,19 @@ class DateRangeAggregation extends AbstractAggregation
      */
     public function addRange($from = null, $to = null)
     {
-        if ($from === null && $to === null) {
-            throw new \LogicException('Missing range');
-        } elseif ($from === null) {
-            $this->ranges = [['to' => $to]];
-        } elseif ($to === null) {
-            $this->ranges = [['from' => $from]];
-        } else {
-            $this->ranges = [['from' => $from], ['to' => $to]];
+        $range = array_filter(
+            [
+                'from' => $from,
+                'to' => $to,
+            ]
+        );
+
+        if (empty($range)) {
+            throw new \LogicException('Either from or to must be set. Both cannot be null.');
         }
 
+        $this->ranges[] = $range;
+
         return $this;
     }
 
@@ -80,12 +83,12 @@ class DateRangeAggregation extends AbstractAggregation
             $data = [
                 'format' => $this->getFormat(),
                 'field' => $this->getField(),
-                'ranges' => array_values($this->ranges),
+                'ranges' => $this->ranges,
             ];
 
             return $data;
         }
-        throw new \LogicException('Date range aggregation must have field and format set.');
+        throw new \LogicException('Date range aggregation must have field, format set and range added.');
     }
 
     /**
diff --git a/Aggregation/MissingAggregation.php b/Aggregation/MissingAggregation.php
index bdd9ed8..8431c5e 100644
--- a/Aggregation/MissingAggregation.php
+++ b/Aggregation/MissingAggregation.php
@@ -12,7 +12,6 @@
 namespace ONGR\ElasticsearchBundle\DSL\Aggregation;
 
 use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\BucketingTrait;
-use Symfony\Component\Process\Exception\LogicException;
 
 /**
  * Class representing missing aggregation.
@@ -29,7 +28,7 @@ class MissingAggregation extends AbstractAggregation
         if ($this->getField()) {
             return ['field' => $this->getField()];
         }
-        throw new LogicException('Missing aggregation must have a field set');
+        throw new \LogicException('Missing aggregation must have a field set.');
     }
 
     /**
-- 
GitLab