From 54d51b6bec60ced4024c56831fe328d0294b4ed6 Mon Sep 17 00:00:00 2001 From: Aivaras Gotovskis <aivaras.gotovskis@ongr.io> Date: Thu, 2 Jul 2015 11:33:23 +0300 Subject: [PATCH] Fix FuzzyLikeThisQuery to return correct format. --- src/Query/FuzzyLikeThisQuery.php | 42 +++++++++++++++++++- tests/Query/FuzzyLikeThisQueryTest.php | 55 ++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tests/Query/FuzzyLikeThisQueryTest.php diff --git a/src/Query/FuzzyLikeThisQuery.php b/src/Query/FuzzyLikeThisQuery.php index cabbaf5..c19c36b 100644 --- a/src/Query/FuzzyLikeThisQuery.php +++ b/src/Query/FuzzyLikeThisQuery.php @@ -11,11 +11,38 @@ namespace ONGR\ElasticsearchDSL\Query; +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + /** * Elasticsearch fuzzy_like_this query class. */ -class FuzzyLikeThisQuery extends FuzzyLikeThisFieldQuery +class FuzzyLikeThisQuery implements BuilderInterface { + use ParametersTrait; + + /** + * @var string[] + */ + private $fields; + + /** + * @var string + */ + private $likeText; + + /** + * @param string[] $fields + * @param string $likeText + * @param array $parameters + */ + public function __construct(array $fields, $likeText, array $parameters = []) + { + $this->fields = $fields; + $this->likeText = $likeText; + $this->setParameters($parameters); + } + /** * {@inheritdoc} */ @@ -23,4 +50,17 @@ class FuzzyLikeThisQuery extends FuzzyLikeThisFieldQuery { return 'fuzzy_like_this'; } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $output = [ + 'fields' => $this->fields, + 'like_text' => $this->likeText, + ]; + + return $this->processArray($output); + } } diff --git a/tests/Query/FuzzyLikeThisQueryTest.php b/tests/Query/FuzzyLikeThisQueryTest.php new file mode 100644 index 0000000..32e193a --- /dev/null +++ b/tests/Query/FuzzyLikeThisQueryTest.php @@ -0,0 +1,55 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL\Query; + +use ONGR\ElasticsearchDSL\Query\FuzzyLikeThisQuery; + +/** + * Class FuzzyLikeThisQueryTest. + */ +class FuzzyLikeThisQueryTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests if toArray returns data in correct format with right data from constructor. + */ + public function testQuery() + { + $fuzzyLikeThisQuery = new FuzzyLikeThisQuery( + ['name.first', 'name.last'], + 'text like this one', + [ 'max_query_terms' => 12 ] + ); + + $this->assertSame( + [ + 'fields' => ['name.first', 'name.last'], + 'like_text' => 'text like this one', + 'max_query_terms' => 12, + ], + $fuzzyLikeThisQuery->toArray() + ); + } + + /** + * Tests if correct type is returned. + */ + public function testGetType() + { + /** @var FuzzyLikeThisQuery $fuzzyLikeThisQuery */ + $fuzzyLikeThisQuery = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\FuzzyLikeThisQuery') + ->disableOriginalConstructor() + ->setMethods(null) + ->getMock(); + + $this->assertEquals('fuzzy_like_this', $fuzzyLikeThisQuery->getType()); + } +} -- GitLab