diff --git a/Aggregation/CardinalityAggregationTest.php b/Aggregation/CardinalityAggregationTest.php index f0f4af24e005d1ddfc7c1256ce66bbb0abd51e7c..3a03e480761f474471a0cbc6701ddca7fee3b832 100644 --- a/Aggregation/CardinalityAggregationTest.php +++ b/Aggregation/CardinalityAggregationTest.php @@ -18,6 +18,42 @@ use ONGR\ElasticsearchBundle\DSL\Aggregation\CardinalityAggregation; */ class CardinalityAggregationTest extends \PHPUnit_Framework_TestCase { + /** + * Tests getArray method. + */ + public function testGetArray() + { + $aggregation = new CardinalityAggregation('bar'); + + // When $script is set. + $aggregation->setScript('foo'); + $result = $aggregation->getArray(); + + $this->assertArrayHasKey('script', $result); + $this->assertEquals('foo', $result['script']); + + // When $field is set. + $aggregation->setField('foo'); + $result = $aggregation->getArray(); + + $this->assertArrayHasKey('field', $result); + $this->assertEquals('foo', $result['field']); + + // When $precisionThreshold is set. + $aggregation->setPrecisionThreshold(10); + $result = $aggregation->getArray(); + + $this->assertArrayHasKey('precision_threshold', $result); + $this->assertEquals(10, $result['precision_threshold']); + + // When $rehash is set. + $aggregation->setRehash(true); + $result = $aggregation->getArray(); + + $this->assertArrayHasKey('rehash', $result); + $this->assertEquals(true, $result['rehash']); + } + /** * Tests if CardinalityAggregation#getArray throws exception when expected. * diff --git a/Aggregation/ChildrenAggregationTest.php b/Aggregation/ChildrenAggregationTest.php new file mode 100644 index 0000000000000000000000000000000000000000..fc264d69bb26d8c823edcf51237064e0e8ee2bce --- /dev/null +++ b/Aggregation/ChildrenAggregationTest.php @@ -0,0 +1,31 @@ +<?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\ChildrenAggregation; + +/** + * Unit test for children aggregation. + */ +class ChildrenAggregationTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests if ChildrenAggregation#getArray throws exception when expected. + * + * @expectedException \LogicException + */ + public function testGetArrayException() + { + $aggregation = new ChildrenAggregation('foo'); + $aggregation->getArray(); + } +} diff --git a/Aggregation/FilterAggregationTest.php b/Aggregation/FilterAggregationTest.php index 0b45a48ae0951482ddb2e4317a22cb8d4e0040cf..520c18bb767def7cbbc7794d8678e3b65244f04d 100644 --- a/Aggregation/FilterAggregationTest.php +++ b/Aggregation/FilterAggregationTest.php @@ -12,6 +12,8 @@ namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Aggregation; use ONGR\ElasticsearchBundle\DSL\Aggregation\FilterAggregation; +use ONGR\ElasticsearchBundle\DSL\Filter\AndFilter; +use ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter; class FilterAggregationTest extends \PHPUnit_Framework_TestCase { @@ -130,4 +132,15 @@ class FilterAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new FilterAggregation('test_agg'); $aggregation->toArray(); } + + /** + * Test for toArray() with setting a filter. + */ + public function testToArrayWithFilter() + { + $aggregation = new FilterAggregation('test_agg'); + + $aggregation->setFilter(new MissingFilter('test')); + $aggregation->toArray(); + } } diff --git a/Bool/BoolTest.php b/Bool/BoolTest.php new file mode 100644 index 0000000000000000000000000000000000000000..097e352ea93c7c4e6bedf2a20030e2e4cbb5d1dc --- /dev/null +++ b/Bool/BoolTest.php @@ -0,0 +1,33 @@ +<?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\Bool\Bool; +use ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter; + +/** + * Unit test for Bool. + */ +class BoolTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests isRelevant method. + */ + public function testIsRelevant() + { + $bool = new Bool(); + $this->assertEquals(false, $bool->isRelevant()); + + $bool->addToBool(new MissingFilter('test')); + $this->assertEquals(true, $bool->isRelevant()); + } +} diff --git a/Filter/GeoDistanceFilterTest.php b/Filter/GeoDistanceFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..ef47d6741ef01e02baf9fbced57075c86241cdc2 --- /dev/null +++ b/Filter/GeoDistanceFilterTest.php @@ -0,0 +1,27 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\GeoDistanceFilter; + +class GeoDistanceFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new GeoDistanceFilter('test_field', 'test_distance', 'test_location'); + $result = $filter->getType(); + $this->assertEquals('geo_distance', $result); + } +} diff --git a/Filter/GeoDistanceRangeFilterTest.php b/Filter/GeoDistanceRangeFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..dc28570bd49aa3dae869b95b71a0816a156afebd --- /dev/null +++ b/Filter/GeoDistanceRangeFilterTest.php @@ -0,0 +1,27 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\GeoDistanceRangeFilter; + +class GeoDistanceRangeFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new GeoDistanceRangeFilter('test_field', 'test_distance', 'test_location'); + $result = $filter->getType(); + $this->assertEquals('geo_distance_range', $result); + } +} diff --git a/Filter/GeoPolygonFilterTest.php b/Filter/GeoPolygonFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..1eae928c245a787783b7137913755c826642e03d --- /dev/null +++ b/Filter/GeoPolygonFilterTest.php @@ -0,0 +1,27 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\GeoPolygonFilter; + +class GeoPolygonFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $filter = new GeoPolygonFilter('test_field'); + $result = $filter->getType(); + $this->assertEquals('geo_polygon', $result); + } +} diff --git a/Filter/HasChildFilterTest.php b/Filter/HasChildFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..b0312b7a8fcbef8c472f5066187a9d08b1e58dee --- /dev/null +++ b/Filter/HasChildFilterTest.php @@ -0,0 +1,28 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\HasChildFilter; + +class HasChildFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $mock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface')->getMock(); + $filter = new HasChildFilter('test_field', $mock); + $result = $filter->getType(); + $this->assertEquals('has_child', $result); + } +} diff --git a/Filter/HasParentFilterTest.php b/Filter/HasParentFilterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..b33f1cc7921027e984eac366061afa8d6e894a0a --- /dev/null +++ b/Filter/HasParentFilterTest.php @@ -0,0 +1,28 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Filter\HasParentFilter; + +class HasParentFilterTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests GetType method. + */ + public function testGetType() + { + $mock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface')->getMock(); + $filter = new HasParentFilter('test_field', $mock); + $result = $filter->getType(); + $this->assertEquals('has_parent', $result); + } +} diff --git a/Highlight/FieldTest.php b/Highlight/FieldTest.php new file mode 100644 index 0000000000000000000000000000000000000000..8961ca27e8200b329db915607860af722b7181e2 --- /dev/null +++ b/Highlight/FieldTest.php @@ -0,0 +1,42 @@ +<?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\Highlight\Field; + +/** + * Unit test for Field. + */ +class FieldTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests getType method. + */ + public function testGetType() + { + $field = new Field('test'); + + $field->setHighlighterType(Field::TYPE_FVH); + $this->assertEquals(Field::TYPE_FVH, $field->getType()); + + $field->setHighlighterType(Field::TYPE_PLAIN); + $this->assertEquals(Field::TYPE_PLAIN, $field->getType()); + + $field->setHighlighterType(Field::TYPE_POSTINGS); + $this->assertEquals(Field::TYPE_POSTINGS, $field->getType()); + + $initValue = $field->getType(); + + $field->setHighlighterType('wrongValue'); + $this->assertEquals($initValue, $field->getType()); + } +} diff --git a/Suggester/CompletionTest.php b/Suggester/CompletionTest.php index cbe6e4d66a44193a9627d09bbff1b3c35ed8f506..973d8e0b468b91e94873b450d480580fcc607b9c 100644 --- a/Suggester/CompletionTest.php +++ b/Suggester/CompletionTest.php @@ -12,9 +12,12 @@ namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Suggester; use ONGR\ElasticsearchBundle\DSL\Suggester\Completion; +use ONGR\ElasticsearchBundle\Test\EncapsulationTestAwareTrait; class CompletionTest extends \PHPUnit_Framework_TestCase { + use EncapsulationTestAwareTrait; + /** * @return array */ @@ -90,12 +93,61 @@ class CompletionTest extends \PHPUnit_Framework_TestCase * Tests toArray method. * * @param array $expected - * @param Completion $phrase + * @param Completion $completion * * @dataProvider getTestToArrayData */ - public function testToArray($expected, $phrase) + public function testToArray($expected, $completion) + { + $this->assertEquals($expected, $completion->toArray()); + } + + /** + * Tests if toArray method throws Logic Exception. + * + * @expectedException \LogicException + */ + public function testToArrayException() { - $this->assertEquals($expected, $phrase->toArray()); + $completion = new Completion('foo', 'bar'); + $completion->setField(null); + $completion->setText(null); + $completion->toArray(); + } + + /** + * Tests useFuzzy property. + */ + public function testUseFuzzy() + { + $completion = new Completion('foo', 'bar'); + $completion->useFuzzy(true); + $this->assertEquals(true, $completion->isFuzzy()); + } + + /** + * @return string + */ + public function getClassName() + { + $this->setStub(new Completion('foo', 'bar')); + $this->addIgnoredField('useFuzzy'); + + return 'ONGR\ElasticsearchBundle\DSL\Suggester\Completion'; + } + + /** + * @return array + */ + public function getFieldsData() + { + return [ + ['name'], + ['fuzziness'], + ['transpositions', 'boolean'], + ['minLength'], + ['prefixLength'], + ['unicodeAware'], + ]; } } diff --git a/Suggester/Context/CategoryContextTest.php b/Suggester/Context/CategoryContextTest.php new file mode 100644 index 0000000000000000000000000000000000000000..e5567cd2ed04c45fa63efc80cce8090fa88cfd9b --- /dev/null +++ b/Suggester/Context/CategoryContextTest.php @@ -0,0 +1,41 @@ +<?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\Context; + +use ONGR\ElasticsearchBundle\DSL\Suggester\Context\CategoryContext; +use ONGR\ElasticsearchBundle\Test\EncapsulationTestAwareTrait; + +class CategoryContextTest extends \PHPUnit_Framework_TestCase +{ + use EncapsulationTestAwareTrait; + + /** + * @return string + */ + public function getClassName() + { + $this->setStub(new CategoryContext('foo', 'bar')); + + return 'ONGR\ElasticsearchBundle\DSL\Suggester\Context\CategoryContext'; + } + + /** + * @return array + */ + public function getFieldsData() + { + return [ + ['name'], + ['value'], + ]; + } +} diff --git a/Suggester/Context/GeoContextTest.php b/Suggester/Context/GeoContextTest.php new file mode 100644 index 0000000000000000000000000000000000000000..8a8a918319237cdf5de24ee9b7021999da9699af --- /dev/null +++ b/Suggester/Context/GeoContextTest.php @@ -0,0 +1,43 @@ +<?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\Context; + +use ONGR\ElasticsearchBundle\DSL\Suggester\Context\CategoryContext; +use ONGR\ElasticsearchBundle\DSL\Suggester\Context\GeoContext; +use ONGR\ElasticsearchBundle\Test\EncapsulationTestAwareTrait; + +class GeoContextTest extends \PHPUnit_Framework_TestCase +{ + use EncapsulationTestAwareTrait; + + /** + * @return string + */ + public function getClassName() + { + $this->setStub(new GeoContext('foo', 'bar')); + + return 'ONGR\ElasticsearchBundle\DSL\Suggester\Context\GeoContext'; + } + + /** + * @return array + */ + public function getFieldsData() + { + return [ + ['precision'], + ['name'], + ['value'], + ]; + } +} diff --git a/Suggester/ContextTest.php b/Suggester/ContextTest.php new file mode 100644 index 0000000000000000000000000000000000000000..601ac2df1e9c3e43ea5592316606570510101dfc --- /dev/null +++ b/Suggester/ContextTest.php @@ -0,0 +1,71 @@ +<?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; +use ONGR\ElasticsearchBundle\DSL\Suggester\Context; +use ONGR\ElasticsearchBundle\DSL\Suggester\Phrase; +use ONGR\ElasticsearchBundle\Test\EncapsulationTestAwareTrait; + +class ContextTest extends \PHPUnit_Framework_TestCase +{ + use EncapsulationTestAwareTrait; + + /** + * Tests toArray method when $this->getSize() !== null. + */ + public function testToArrayNotNull() + { + $name = 'testName'; + + $context = new Context('', '', $name); + $context->setSize(123); + $context->setContext(new Phrase('', '')); + + $result = $context->toArray(); + + $this->assertArrayHasKey($name, $result); + + $data = $result[$name]; + $this->assertArrayHasKey('completion', $data); + + $completion = $data['completion']; + $this->assertArrayHasKey('size', $completion); + + $this->assertEquals($completion['size'], 123); + } + + /** + * Returns list of fields to test. Works as data provider. + * + * @return array + */ + public function getFieldsData() + { + return [ + ['context'], + ['size'], + ]; + } + + /** + * Returns entity class name. + * + * @return string + */ + public function getClassName() + { + $this->setStub(new Context('foo', 'bar')); + + return 'ONGR\ElasticsearchBundle\DSL\Suggester\Context'; + } +} diff --git a/Suggester/PhraseTest.php b/Suggester/PhraseTest.php index eac16ae48df93220779ece32562eb33ecc8b681b..946bcee0b880448a6e330b26ed3b396220685668 100644 --- a/Suggester/PhraseTest.php +++ b/Suggester/PhraseTest.php @@ -12,9 +12,12 @@ namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Suggester; use ONGR\ElasticsearchBundle\DSL\Suggester\Phrase; +use ONGR\ElasticsearchBundle\Test\EncapsulationTestAwareTrait; class PhraseTest extends \PHPUnit_Framework_TestCase { + use EncapsulationTestAwareTrait; + /** * @return array */ @@ -85,4 +88,43 @@ class PhraseTest extends \PHPUnit_Framework_TestCase { $this->assertEquals($expected, $phrase->toArray()); } + + /** + * Tests toArray method exception. + * + * @expectedException \LogicException + */ + public function testToArrayException() + { + $phrase = new Phrase('', ''); + $phrase->toArray(); + } + + /** + * @return string + */ + public function getClassName() + { + $this->setStub(new Phrase('foo', 'bar')); + + return 'ONGR\ElasticsearchBundle\DSL\Suggester\Phrase'; + } + + /** + * Returns list of fields to test. Works as data provider. + * + * @return array + */ + public function getFieldsData() + { + return [ + ['analyzer'], + ['gramSize'], + ['realWordErrorLikelihood'], + ['confidence'], + ['maxErrors'], + ['highlight'], + ['size'], + ]; + } } diff --git a/Suggester/TermTest.php b/Suggester/TermTest.php index 4851cde1b64a1652debce303d81701cf9c734806..c0ec281975d0ddaf6d1b73ad6b55ac4c1086c3de 100644 --- a/Suggester/TermTest.php +++ b/Suggester/TermTest.php @@ -12,9 +12,12 @@ namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Suggester; use ONGR\ElasticsearchBundle\DSL\Suggester\Term; +use ONGR\ElasticsearchBundle\Test\EncapsulationTestAwareTrait; class TermTest extends \PHPUnit_Framework_TestCase { + use EncapsulationTestAwareTrait; + /** * @return array */ @@ -76,4 +79,78 @@ class TermTest extends \PHPUnit_Framework_TestCase { $this->assertEquals($expected, $suggester->toArray()); } + + /** + * Tests toArray method exception. + * + * @expectedException \LogicException + */ + public function testToArrayException() + { + $term = new Term('', ''); + $term->toArray(); + } + + /** + * Tests setSort method. + */ + public function testSetSort() + { + $term = new Term('foo', 'bar'); + + $term->setSort(Term::SORT_BY_FREQ); + $this->assertEquals(Term::SORT_BY_FREQ, $term->getSort()); + + $term->setSort(Term::SORT_BY_SCORE); + $this->assertEquals(Term::SORT_BY_SCORE, $term->getSort()); + + $initValue = $term->getSort(); + $term->setSort('wrongSort'); + $this->assertEquals($initValue, $term->getSort()); + } + + /** + * Tests setSuggestMode method. + */ + public function testSetSuggestMode() + { + $term = new Term('foo', 'bar'); + + $term->setSuggestMode(Term::SUGGEST_MODE_ALWAYS); + $this->assertEquals(Term::SUGGEST_MODE_ALWAYS, $term->getSuggestMode()); + + $term->setSuggestMode(Term::SUGGEST_MODE_MISSING); + $this->assertEquals(Term::SUGGEST_MODE_MISSING, $term->getSuggestMode()); + + $initValue = $term->getSuggestMode(); + $term->setSuggestMode('wrongMode'); + $this->assertEquals($initValue, $term->getSuggestMode()); + } + + /** + * Returns list of fields to test. Works as data provider. + * + * @return array + */ + public function getFieldsData() + { + return [ + ['analyzer'], + ['size'], + ]; + } + + /** + * Returns entity class name. + * + * @return string + */ + public function getClassName() + { + $this->setStub(new Term('foo', 'bar')); + $this->addIgnoredField('sort'); + $this->addIgnoredField('suggestMode'); + + return 'ONGR\ElasticsearchBundle\DSL\Suggester\Term'; + } }