From fd2b28f117101a94918ee1842cfa75be621e354c Mon Sep 17 00:00:00 2001
From: Aivaras Gotovskis <aivaras.gotovskis@ongr.io>
Date: Fri, 17 Jul 2015 10:58:27 +0300
Subject: [PATCH] Change FuzzyLikeThisTestQuery to accept single field as
 string.

---
 src/Query/FuzzyLikeThisQuery.php       | 12 ++++++++----
 tests/Query/FuzzyLikeThisQueryTest.php | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/src/Query/FuzzyLikeThisQuery.php b/src/Query/FuzzyLikeThisQuery.php
index c19c36b..af2ff21 100644
--- a/src/Query/FuzzyLikeThisQuery.php
+++ b/src/Query/FuzzyLikeThisQuery.php
@@ -32,12 +32,16 @@ class FuzzyLikeThisQuery implements BuilderInterface
     private $likeText;
 
     /**
-     * @param string[] $fields
-     * @param string   $likeText
-     * @param array    $parameters
+     * @param string|string[] $fields
+     * @param string          $likeText
+     * @param array           $parameters
      */
-    public function __construct(array $fields, $likeText, array $parameters = [])
+    public function __construct($fields, $likeText, array $parameters = [])
     {
+        if (!is_array($fields)) {
+            $fields = [$fields];
+        }
+
         $this->fields = $fields;
         $this->likeText = $likeText;
         $this->setParameters($parameters);
diff --git a/tests/Query/FuzzyLikeThisQueryTest.php b/tests/Query/FuzzyLikeThisQueryTest.php
index 32e193a..3563b64 100644
--- a/tests/Query/FuzzyLikeThisQueryTest.php
+++ b/tests/Query/FuzzyLikeThisQueryTest.php
@@ -52,4 +52,25 @@ class FuzzyLikeThisQueryTest extends \PHPUnit_Framework_TestCase
 
         $this->assertEquals('fuzzy_like_this', $fuzzyLikeThisQuery->getType());
     }
+
+    /**
+     * Tests if query accepts single field as string.
+     */
+    public function testSingleField()
+    {
+        $fuzzyLikeThisQuery = new FuzzyLikeThisQuery(
+            'name.first',
+            'text like this one',
+            [ 'max_query_terms' => 12 ]
+        );
+
+        $this->assertSame(
+            [
+                'fields' => ['name.first'],
+                'like_text' => 'text like this one',
+                'max_query_terms' => 12,
+            ],
+            $fuzzyLikeThisQuery->toArray()
+        );
+    }
 }
-- 
GitLab