Skip to content
Snippets Groups Projects
Commit 8f02938d authored by ONGR Team's avatar ONGR Team
Browse files

Initial code dump

parents
No related branches found
No related tags found
No related merge requests found
<?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\ElasticsearchBundle\Tests\Unit\DSL\Aggregation;
use ONGR\ElasticsearchBundle\DSL\Aggregation\FilterAggregation;
class FilterAggregationTest extends \PHPUnit_Framework_TestCase
{
/**
* Data provider for testToArray.
*
* @return array
*/
public function getToArrayData()
{
$out = [];
// Case #0 filter aggregation.
$aggregation = new FilterAggregation('test_agg');
$filter = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface')
->setMethods(['toArray', 'getType'])
->getMockForAbstractClass();
$filter->expects($this->any())
->method('getType')
->willReturn('test_filter');
$filter->expects($this->any())
->method('toArray')
->willReturn(['test_field' => ['test_value' => 'test']]);
$aggregation->setFilter($filter);
$result = [
'agg_test_agg' => [
'filter' => [
'test_filter' => [
'test_field' => [
'test_value' => 'test',
],
],
],
],
];
$out[] = [$aggregation, $result];
// Case #1 nested filter aggregation.
$aggregation = new FilterAggregation('test_agg');
$aggregation->setFilter($filter);
$aggregation2 = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Aggregation\AbstractAggregation')
->disableOriginalConstructor()
->setMethods(['toArray', 'getName'])
->getMockForAbstractClass();
$aggregation2->expects($this->any())
->method('toArray')
->willReturn(['agg_test_agg2' => ['avg' => []]]);
$aggregation2->expects($this->any())
->method('getName')
->willReturn('agg_test_agg2');
$aggregation->aggregations->addAggregation($aggregation2);
$result = [
'agg_test_agg' => [
'filter' => [
'test_filter' => [
'test_field' => [
'test_value' => 'test',
],
],
],
'aggregations' => [
'agg_test_agg2' => [
'avg' => [],
],
],
],
];
$out[] = [$aggregation, $result];
return $out;
}
/**
* Test for filter aggregation toArray() method.
*
* @param FilterAggregation $aggregation
* @param array $expectedResult
*
* @dataProvider getToArrayData
*/
public function testToArray($aggregation, $expectedResult)
{
$this->assertEquals($expectedResult, $aggregation->toArray());
}
/**
* Test for setField().
*
* @expectedException \LogicException
* @expectedExceptionMessage doesn't support `field` parameter
*/
public function testSetField()
{
$aggregation = new FilterAggregation('test_agg');
$aggregation->setField('test_field');
}
/**
* Test for toArray() without setting a filter.
*
* @expectedException \LogicException
* @expectedExceptionMessage has no filter added
*/
public function testToArrayNoFilter()
{
$aggregation = new FilterAggregation('test_agg');
$aggregation->toArray();
}
}
<?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\ElasticsearchBundle\Tests\Unit\DSL\Aggregation;
use ONGR\ElasticsearchBundle\DSL\Aggregation\GlobalAggregation;
class GlobalAggregationTest extends \PHPUnit_Framework_TestCase
{
/**
* Data provider for testToArray().
*
* @return array
*/
public function getToArrayData()
{
$out = [];
// Case #0 global aggregation.
$aggregation = new GlobalAggregation('test_agg');
$result = [
'agg_test_agg' => [
'global' => new \stdClass(),
],
];
$out[] = [$aggregation, $result];
// Case #1 nested global aggregation.
$aggregation = new GlobalAggregation('test_agg');
$aggregation2 = new GlobalAggregation('test_agg_2');
$aggregation->aggregations->addAggregation($aggregation2);
$result = [
'agg_test_agg' => [
'global' => new \stdClass(),
'aggregations' => [
'agg_test_agg_2' => [
'global' => new \stdClass(),
],
],
],
];
$out[] = [$aggregation, $result];
return $out;
}
/**
* Test for global aggregation toArray() method.
*
* @param GlobalAggregation $aggregation
* @param array $expectedResult
*
* @dataProvider getToArrayData
*/
public function testToArray($aggregation, $expectedResult)
{
$this->assertEquals($expectedResult, $aggregation->toArray());
}
/**
* Test for setField method on global aggregation.
*
* @expectedException \LogicException
* @expectedException doesn't support `field` parameter
*/
public function testSetField()
{
$aggregation = new GlobalAggregation('test_agg');
$aggregation->setField('test_field');
}
}
<?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\ElasticsearchBundle\Tests\Unit\DSL\Aggregation;
use ONGR\ElasticsearchBundle\DSL\Aggregation\RangeAggregation;
class RangeAggregationTest extends \PHPUnit_Framework_TestCase
{
/**
* Data provider for testToArray().
*
* @return array
*/
public function getToArrayData()
{
$out = [];
// Case #0 single range.
$aggregation = new RangeAggregation('test_agg');
$aggregation->setField('test_field');
$aggregation->addRange('10', 20);
$result = [
'agg_test_agg' => [
'range' => [
'field' => 'test_field',
'ranges' => [
['from' => '10', 'to' => 20],
],
'keyed' => false,
],
],
];
$out[] = [$aggregation, $result];
// Case #1 multiple keyed ranges.
$aggregation = new RangeAggregation('test_agg');
$aggregation->setField('test_field');
$aggregation->setKeyed(true);
$aggregation->addRange('10', null, 'range_1');
$aggregation->addRange(null, '20', 'range_2');
$result = [
'agg_test_agg' => [
'range' => [
'field' => 'test_field',
'ranges' => [
['from' => '10', 'key' => 'range_1'],
['to' => '20', 'key' => 'range_2'],
],
'keyed' => true,
],
],
];
$out[] = [$aggregation, $result];
// Case #2 nested aggregation.
$aggregation = new RangeAggregation('test_agg');
$aggregation->setField('test_field');
$aggregation->addRange('10', '10');
$aggregation2 = new RangeAggregation('test_agg_2');
$aggregation2->addRange('20', '20');
$aggregation->aggregations->addAggregation($aggregation2);
$result = [
'agg_test_agg' => [
'range' => [
'field' => 'test_field',
'ranges' => [
['from' => '10', 'to' => '10'],
],
'keyed' => false,
],
'aggregations' => [
'agg_test_agg_2' => [
'range' => [
'ranges' => [
['from' => '20', 'to' => '20'],
],
'keyed' => false,
],
],
],
],
];
$out[] = [$aggregation, $result];
return $out;
}
/**
* Test for range aggregation toArray() method.
*
* @param RangeAggregation $aggregation
* @param array $expectedResult
*
* @dataProvider getToArrayData
*/
public function testToArray($aggregation, $expectedResult)
{
$this->assertEquals($expectedResult, $aggregation->toArray());
}
}
<?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\ElasticsearchBundle\Tests\Unit\DSL\Aggregation;
use ONGR\ElasticsearchBundle\DSL\Aggregation\StatsAggregation;
class StatsAggregationTest extends \PHPUnit_Framework_TestCase
{
/**
* Test for stats aggregation toArray() method.
*/
public function testToArray()
{
$aggregation = new StatsAggregation('test_agg');
$aggregation->setField('test_field');
$expectedResult = [
'agg_test_agg' => [
'stats' => [
'field' => 'test_field',
],
],
];
$this->assertEquals($expectedResult, $aggregation->toArray());
}
}
<?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\ElasticsearchBundle\Tests\Unit\DSL\Aggregation;
use ONGR\ElasticsearchBundle\DSL\Aggregation\TermsAggregation;
class TermsAggregationTest extends \PHPUnit_Framework_TestCase
{
/**
* Data provider for testToArray().
*
* @return array
*/
public function getToArrayData()
{
$out = [];
// Case #0 terms aggregation.
$aggregation = new TermsAggregation('test_agg');
$aggregation->setField('test_field');
$result = [
'agg_test_agg' => [
'terms' => [
'field' => 'test_field',
],
],
];
$out[] = [$aggregation, $result];
// Case #1 terms aggregation with size.
$aggregation = new TermsAggregation('test_agg');
$aggregation->setField('test_field');
$aggregation->setSize(1);
$result = [
'agg_test_agg' => [
'terms' => [
'field' => 'test_field',
'size' => 1,
],
],
];
$out[] = [$aggregation, $result];
// Case #2 terms aggregation with size and min document count.
$aggregation = new TermsAggregation('test_agg');
$aggregation->setField('test_field');
$aggregation->setSize(1);
$aggregation->setMinDocumentCount(10);
$result = [
'agg_test_agg' => [
'terms' => [
'field' => 'test_field',
'size' => 1,
'min_doc_count' => 10,
],
],
];
$out[] = [$aggregation, $result];
// Case #3 terms aggregation with simple include, exclude.
$aggregation = new TermsAggregation('test_agg');
$aggregation->setField('test_field');
$aggregation->setInclude('test_.*');
$aggregation->setExclude('pizza_.*');
$result = [
'agg_test_agg' => [
'terms' => [
'field' => 'test_field',
'include' => 'test_.*',
'exclude' => 'pizza_.*',
],
],
];
$out[] = [$aggregation, $result];
// Case #4 terms aggregation with include, exclude and flags.
$aggregation = new TermsAggregation('test_agg');
$aggregation->setField('test_field');
$aggregation->setInclude('test_.*', 'CANON_EQ|CASE_INSENSITIVE');
$aggregation->setExclude('pizza_.*', 'CASE_INSENSITIVE');
$result = [
'agg_test_agg' => [
'terms' => [
'field' => 'test_field',
'include' => [
'pattern' => 'test_.*',
'flags' => 'CANON_EQ|CASE_INSENSITIVE',
],
'exclude' => [
'pattern' => 'pizza_.*',
'flags' => 'CASE_INSENSITIVE',
],
],
],
];
$out[] = [$aggregation, $result];
// Case #5 terms aggregation with order default direction.
$aggregation = new TermsAggregation('test_agg');
$aggregation->setField('test_field');
$aggregation->setOrder(TermsAggregation::MODE_COUNT);
$result = [
'agg_test_agg' => [
'terms' => [
'field' => 'test_field',
'order' => [
'_count' => 'asc',
],
],
],
];
$out[] = [$aggregation, $result];
// Case #6 terms aggregation with order term mode, desc direction.
$aggregation = new TermsAggregation('test_agg');
$aggregation->setField('test_field');
$aggregation->setOrder(TermsAggregation::MODE_TERM, TermsAggregation::DIRECTION_DESC);
$result = [
'agg_test_agg' => [
'terms' => [
'field' => 'test_field',
'order' => [
'_term' => 'desc',
],
],
],
];
$out[] = [$aggregation, $result];
return $out;
}
/**
* Test for toArray().
*
* @param TermsAggregation $aggregation
* @param array $expectedResults
*
* @dataProvider getToArrayData
*/
public function testToArray($aggregation, $expectedResults)
{
$this->assertEquals($expectedResults, $aggregation->toArray());
}
}
<?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\ElasticsearchBundle\Tests\Unit\DSL\Aggregation;
use ONGR\ElasticsearchBundle\DSL\Aggregation\TopHitsAggregation;
use ONGR\ElasticsearchBundle\DSL\Sort\Sorts;
/**
* Unit tests for top hits aggregation
*/
class TopHitsAggregationTest extends \PHPUnit_Framework_TestCase
{
/**
* Check if aggregation returns the expected array.
*/
public function testToArray()
{
$sorts = new Sorts();
$aggregation = new TopHitsAggregation('test', 1, 1, $sorts);
$expectedAgg = new \stdClass();
$expectedAgg->size = 1;
$expectedAgg->from = 1;
$expectedAgg->sort = $sorts->toArray();
$expected = [
'agg_test' => [
'top_hits' => $expectedAgg,
]
];
$this->assertEquals($expected, $aggregation->toArray());
}
}
<?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\ElasticsearchBundle\Tests\Unit\DSL\Suggester;
use ONGR\ElasticsearchBundle\DSL\Suggester\Completion;
class CompletionTest extends \PHPUnit_Framework_TestCase
{
/**
* @return array
*/
public function getTestToArrayData()
{
$out = [];
// Case #0: simple.
$completion0 = new Completion('my-field', 'lorem ipsum');
$expected0 = [
'my-field-completion' => [
'text' => 'lorem ipsum',
'completion' => [
'field' => 'my-field',
],
],
];
$out[] = [$expected0, $completion0];
// Case #1: using fuzzy.
$completion1 = new Completion('my-other-field', 'super awesome cat', 'my-completion1');
$completion1->useFuzzy(true);
$expected1 = [
'my-completion1' => [
'text' => 'super awesome cat',
'completion' => [
'field' => 'my-other-field',
'fuzzy' => true,
],
],
];
$out[] = [$expected1, $completion1];
// Case #2: providing all data.
$completion2 = new Completion('body', 'even more super awesome cat', 'my-completion2');
$completion2
->setFuzziness(2)
->setMinLength(3)
->setPrefixLength(1)
->setTranspositions(true)
->setUnicodeAware('');
$expected2 = [
'my-completion2' => [
'text' => 'even more super awesome cat',
'completion' => [
'field' => 'body',
'fuzzy' => [
'fuzziness' => 2,
'transpositions' => true,
'min_length' => 3,
'prefix_length' => 1,
],
],
],
];
$out[] = [$expected2, $completion2];
return $out;
}
/**
* Tests toArray method.
*
* @param array $expected
* @param Completion $phrase
*
* @dataProvider getTestToArrayData
*/
public function testToArray($expected, $phrase)
{
$this->assertEquals($expected, $phrase->toArray());
}
}
<?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\ElasticsearchBundle\Tests\Unit\DSL\Suggester;
use ONGR\ElasticsearchBundle\DSL\Suggester\Phrase;
class PhraseTest extends \PHPUnit_Framework_TestCase
{
/**
* @return array
*/
public function getTestToArrayData()
{
$out = [];
// Case #0: simple.
$phrase0 = new Phrase('body', 'lorem ipsum');
$expected0 = [
'body-phrase' => [
'text' => 'lorem ipsum',
'phrase' => [
'field' => 'body',
],
],
];
$out[] = [$expected0, $phrase0];
// Case #1: using all fields.
$phrase1 = new Phrase('description', 'awesome cat');
$phrase1->setMaxErrors(2);
$phrase1->setGramSize(1);
$phrase1->setRealWordErrorLikelihood(0.95);
$phrase1->setHighlight(['pre_tag' => '<span class="info">', 'post_tag' => '</span>']);
$phrase1->setAnalyzer('simple');
$phrase1->setConfidence(1);
$phrase1->setSize(6);
$highlightObject = new \stdClass();
$highlightObject->post_tag = '</span>';
$highlightObject->pre_tag = '<span class="info">';
$expected1 = [
'description-phrase' => [
'text' => 'awesome cat',
'phrase' => [
'analyzer' => 'simple',
'field' => 'description',
'size' => 6,
'real_word_error_likelihood' => 0.95,
'max_errors' => 2.0,
'gram_size' => 1,
'highlight' => $highlightObject,
],
],
];
$out[] = [$expected1, $phrase1];
return $out;
}
/**
* Tests toArray method.
*
* @param array $expected
* @param Phrase $phrase
*
* @dataProvider getTestToArrayData
*/
public function testToArray($expected, $phrase)
{
$this->assertEquals($expected, $phrase->toArray());
}
}
<?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\ElasticsearchBundle\Tests\Unit\DSL\Suggester;
use ONGR\ElasticsearchBundle\DSL\Suggester\Term;
class TermTest extends \PHPUnit_Framework_TestCase
{
/**
* @return array
*/
public function getTestToArrayData()
{
$out = [];
// Case #0: simple.
$term0 = new Term('body', 'lorem ipsum');
$expected0 = [
'body-term' => [
'text' => 'lorem ipsum',
'term' => [
'field' => 'body',
],
],
];
$out[] = [$expected0, $term0];
// Case #1: full suggester.
$term1 = new Term('body', 'lorem ipsum');
$term1
->setSize(2)
->setAnalyzer('simple')
->setSuggestMode(Term::SUGGEST_MODE_ALWAYS)
->setSort(Term::SORT_BY_SCORE);
$expected1 = [
'body-term' => [
'text' => 'lorem ipsum',
'term' => [
'field' => 'body',
'analyzer' => 'simple',
'sort' => 'score',
'suggest_mode' => 'always',
],
'size' => 2,
],
];
$out[] = [$expected1, $term1];
return $out;
}
/**
* Tests toArray method.
*
* @param array $expected
* @param Term $suggester
*
* @dataProvider getTestToArrayData
*/
public function testToArray($expected, $suggester)
{
$this->assertEquals($expected, $suggester->toArray());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment