From b581a8d1e4cd629bb70cd5633d7708051a8ad1cf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simonas=20=C5=A0erlinskas?= <simonas.serlinskas@nfq.com>
Date: Wed, 12 Aug 2015 10:14:01 +0300
Subject: [PATCH] fixed issues with array conversion from empty array to {}

---
 src/Aggregation/ReverseNestedAggregation.php       |  2 +-
 src/Query/TermQuery.php                            | 12 ++++++++----
 tests/Aggregation/GlobalAggregationTest.php        |  9 ++++++---
 tests/Aggregation/ReverseNestedAggregationTest.php |  7 +++++--
 tests/Aggregation/TopHitsAggregationTest.php       |  4 ++--
 tests/Query/BoolQueryTest.php                      |  4 ++--
 6 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/src/Aggregation/ReverseNestedAggregation.php b/src/Aggregation/ReverseNestedAggregation.php
index 58febb5..55b58b2 100644
--- a/src/Aggregation/ReverseNestedAggregation.php
+++ b/src/Aggregation/ReverseNestedAggregation.php
@@ -77,7 +77,7 @@ class ReverseNestedAggregation extends AbstractAggregation
 
         $output = new \stdClass();
         if ($this->getPath()) {
-            $output['path'] = $this->getPath();
+            $output = ['path' => $this->getPath()];
         }
 
         return $output;
diff --git a/src/Query/TermQuery.php b/src/Query/TermQuery.php
index 3a0bf24..e561721 100644
--- a/src/Query/TermQuery.php
+++ b/src/Query/TermQuery.php
@@ -56,12 +56,16 @@ class TermQuery implements BuilderInterface
      */
     public function toArray()
     {
-        $query = [
-            'value' => $this->value,
-        ];
+        $query = $this->processArray();
+
+        if (empty($query)) {
+            $query = $this->value;
+        } else {
+            $query['value'] = $this->value;
+        }
 
         $output = [
-            $this->field => $this->processArray($query),
+            $this->field => $query,
         ];
 
         return $output;
diff --git a/tests/Aggregation/GlobalAggregationTest.php b/tests/Aggregation/GlobalAggregationTest.php
index 48fb372..357eb4c 100644
--- a/tests/Aggregation/GlobalAggregationTest.php
+++ b/tests/Aggregation/GlobalAggregationTest.php
@@ -28,7 +28,7 @@ class GlobalAggregationTest extends \PHPUnit_Framework_TestCase
         $aggregation = new GlobalAggregation('test_agg');
 
         $result = [
-            'global' => [],
+            'global' => new \stdClass(),
         ];
 
         $out[] = [
@@ -42,7 +42,7 @@ class GlobalAggregationTest extends \PHPUnit_Framework_TestCase
         $aggregation->addAggregation($aggregation2);
 
         $result = [
-            'global' => [],
+            'global' => new \stdClass(),
             'aggregations' => [
                 $aggregation2->getName() => $aggregation2->toArray(),
             ],
@@ -66,7 +66,10 @@ class GlobalAggregationTest extends \PHPUnit_Framework_TestCase
      */
     public function testToArray($aggregation, $expectedResult)
     {
-        $this->assertEquals($expectedResult, $aggregation->toArray());
+        $this->assertEquals(
+            json_encode($expectedResult),
+            json_encode($aggregation->toArray())
+        );
     }
 
     /**
diff --git a/tests/Aggregation/ReverseNestedAggregationTest.php b/tests/Aggregation/ReverseNestedAggregationTest.php
index 531c150..0c79712 100644
--- a/tests/Aggregation/ReverseNestedAggregationTest.php
+++ b/tests/Aggregation/ReverseNestedAggregationTest.php
@@ -66,12 +66,15 @@ class ReverseNestedAggregationTest extends \PHPUnit_Framework_TestCase
         $aggregation->addAggregation($termAggregation);
 
         $expectedResult = [
-            'reverse_nested' => [],
+            'reverse_nested' => new \stdClass(),
             'aggregations' => [
                 $termAggregation->getName() => $termAggregation->toArray(),
             ],
         ];
 
-        $this->assertEquals($expectedResult, $aggregation->toArray());
+        $this->assertEquals(
+            json_encode($expectedResult),
+            json_encode($aggregation->toArray())
+        );
     }
 }
diff --git a/tests/Aggregation/TopHitsAggregationTest.php b/tests/Aggregation/TopHitsAggregationTest.php
index 02abe2d..5b6dd99 100644
--- a/tests/Aggregation/TopHitsAggregationTest.php
+++ b/tests/Aggregation/TopHitsAggregationTest.php
@@ -25,13 +25,13 @@ class TopHitsAggregationTest extends \PHPUnit_Framework_TestCase
      */
     public function testToArray()
     {
-        $sort = new FieldSort('acme');
+        $sort = new FieldSort('acme', ['order' => 'asc']);
         $aggregation = new TopHitsAggregation('acme', 1, 1, $sort);
 
         $expected = [
             'top_hits' => [
                 'sort' => [
-                    'acme' => [],
+                    'acme' => ['order' => 'asc'],
                 ],
                 'size' => 1,
                 'from' => 1,
diff --git a/tests/Query/BoolQueryTest.php b/tests/Query/BoolQueryTest.php
index 1634e76..4505857 100644
--- a/tests/Query/BoolQueryTest.php
+++ b/tests/Query/BoolQueryTest.php
@@ -41,7 +41,7 @@ class BoolQueryTest extends \PHPUnit_Framework_TestCase
         $this->assertFalse($bool->isRelevant());
         $bool->add(new TermQuery('acme', 'foo'), BoolQuery::SHOULD);
 
-        $this->assertFalse($bool->isRelevant());
+        $this->assertTrue($bool->isRelevant());
     }
 
     /**
@@ -61,7 +61,7 @@ class BoolQueryTest extends \PHPUnit_Framework_TestCase
      * Test for addToBool() without setting a correct bool operator.
      *
      * @expectedException        \UnexpectedValueException
-     * @expectedExceptionMessage The provided bool operator is not supported
+     * @expectedExceptionMessage The bool operator acme is not supported
      */
     public function testBoolAddToBoolException()
     {
-- 
GitLab