diff --git a/src/Query/FuzzyLikeThisQuery.php b/src/Query/FuzzyLikeThisQuery.php index cabbaf5799677bdbb315220de97820707d78d6d9..c19c36be0858aa789d646b45f72b5d70f14ecb55 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 0000000000000000000000000000000000000000..32e193ab968b441c2ab4c1fa55b89ee6fd0cbefb --- /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()); + } +}