diff --git a/Aggregation/CardinalityAggregationTest.php b/Aggregation/CardinalityAggregationTest.php index 3a03e480761f474471a0cbc6701ddca7fee3b832..75b9d19b53f5a8c5887eb2ecfbb25d3dd607cf61 100644 --- a/Aggregation/CardinalityAggregationTest.php +++ b/Aggregation/CardinalityAggregationTest.php @@ -25,33 +25,29 @@ class CardinalityAggregationTest extends \PHPUnit_Framework_TestCase { $aggregation = new CardinalityAggregation('bar'); - // When $script is set. $aggregation->setScript('foo'); $result = $aggregation->getArray(); - $this->assertArrayHasKey('script', $result); - $this->assertEquals('foo', $result['script']); + $this->assertArrayHasKey('script', $result, 'key=script when script is set'); + $this->assertEquals('foo', $result['script'], 'script=foo when scripts name=foo'); - // When $field is set. $aggregation->setField('foo'); $result = $aggregation->getArray(); - $this->assertArrayHasKey('field', $result); - $this->assertEquals('foo', $result['field']); + $this->assertArrayHasKey('field', $result, 'key=field when field is set'); + $this->assertEquals('foo', $result['field'], 'field=foo when fields name=foo'); - // When $precisionThreshold is set. $aggregation->setPrecisionThreshold(10); $result = $aggregation->getArray(); - $this->assertArrayHasKey('precision_threshold', $result); - $this->assertEquals(10, $result['precision_threshold']); + $this->assertArrayHasKey('precision_threshold', $result, 'key=precision_threshold when is set'); + $this->assertEquals(10, $result['precision_threshold'], 'precision_threshold=10 when is set'); - // When $rehash is set. $aggregation->setRehash(true); $result = $aggregation->getArray(); - $this->assertArrayHasKey('rehash', $result); - $this->assertEquals(true, $result['rehash']); + $this->assertArrayHasKey('rehash', $result, 'key=rehash when rehash is set'); + $this->assertEquals(true, $result['rehash'], 'rehash=true when rehash is set to true'); } /** @@ -65,4 +61,14 @@ class CardinalityAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new CardinalityAggregation('bar'); $aggregation->getArray(); } + + /** + * Tests getType method. + */ + public function testCardinallyAggregationGetType() + { + $aggregation = new CardinalityAggregation('foo'); + $result = $aggregation->getType(); + $this->assertEquals('cardinality', $result); + } } diff --git a/Aggregation/ChildrenAggregationTest.php b/Aggregation/ChildrenAggregationTest.php index fc264d69bb26d8c823edcf51237064e0e8ee2bce..11e4446099eb0c48fb110edcc80205535cb1d9bc 100644 --- a/Aggregation/ChildrenAggregationTest.php +++ b/Aggregation/ChildrenAggregationTest.php @@ -28,4 +28,30 @@ class ChildrenAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new ChildrenAggregation('foo'); $aggregation->getArray(); } + + /** + * Tests getType method. + */ + public function testChildrenAggregationGetType() + { + $aggregation = new ChildrenAggregation('foo'); + $result = $aggregation->getType(); + $this->assertEquals('children', $result); + } + + /** + * Tests getArray method. + */ + public function testChildrenAggregationGetArray() + { + $mock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\Aggregation\AbstractAggregation') + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + $aggregation = new ChildrenAggregation('foo'); + $aggregation->addAggregation($mock); + $aggregation->setChildren('question'); + $result = $aggregation->getArray(); + $expected = ['type' => 'question']; + $this->assertEquals($expected, $result); + } } diff --git a/Aggregation/DateRangeAggregationTest.php b/Aggregation/DateRangeAggregationTest.php index 26081ef0cff2e1a6500e5d5ac54f11b2a69f39b5..f2643adaaf0d311824f8245c51fea4bc0aac5c98 100644 --- a/Aggregation/DateRangeAggregationTest.php +++ b/Aggregation/DateRangeAggregationTest.php @@ -38,4 +38,32 @@ class DateRangeAggregationTest extends \PHPUnit_Framework_TestCase $agg = new DateRangeAggregation('test_agg'); $agg->addRange(null, null); } + + /** + * Test getArray method. + */ + public function testDateRangeAggregationGetArray() + { + $agg = new DateRangeAggregation('foo'); + $agg->addRange(10, 20); + $agg->setFormat('bar'); + $agg->setField('baz'); + $result = $agg->getArray(); + $expected = [ + 'format' => 'bar', + 'field' => 'baz', + 'ranges' => [['from' => 10, 'to' => 20]], + ]; + $this->assertEquals($expected, $result); + } + + /** + * Tests getType method. + */ + public function testDateRangeAggregationGetType() + { + $aggregation = new DateRangeAggregation('foo'); + $result = $aggregation->getType(); + $this->assertEquals('date_range', $result); + } } diff --git a/Aggregation/FiltersAggregationTest.php b/Aggregation/FiltersAggregationTest.php index 4bdcc2a5c454e958aa4600252600438ebb21180d..38c9cae2ab577436b14891ea8b8bdbaa27862297 100644 --- a/Aggregation/FiltersAggregationTest.php +++ b/Aggregation/FiltersAggregationTest.php @@ -30,4 +30,71 @@ class FiltersAggregationTest extends \PHPUnit_Framework_TestCase $aggregation = new FiltersAggregation('test_agg'); $aggregation->addFilter($mock); } + + /** + * Test GetArray method. + */ + public function testFiltersAggregationGetArray() + { + $mock = $this->getMockBuilder('ONGR\ElasticsearchBundle\DSL\BuilderInterface')->getMock(); + $aggregation = new FiltersAggregation('test_agg'); + $aggregation->setAnonymous(true); + $aggregation->addFilter($mock, 'name'); + $result = $aggregation->getArray(); + $this->assertArrayHasKey('filters', $result); + } + + /** + * Tests getType method. + */ + public function testFiltersAggregationGetType() + { + $aggregation = new FiltersAggregation('foo'); + $result = $aggregation->getType(); + $this->assertEquals('filters', $result); + } + + /** + * Test for filter aggregation toArray() method. + */ + public function testToArray() + { + $aggregation = new FiltersAggregation('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->addFilter($filter, 'first'); + $aggregation->addFilter($filter, 'second'); + $results = $aggregation->toArray(); + $expected = [ + 'agg_test_agg' => [ + 'filters' => [ + 'filters' => [ + 'first' => [ + 'test_filter' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + 'second' => [ + 'test_filter' => [ + 'test_field' => [ + 'test_value' => 'test', + ], + ], + ], + ], + ], + ], + ]; + $this->assertEquals($expected, $results); + } } diff --git a/Aggregation/GeoBoundsAggregationTest.php b/Aggregation/GeoBoundsAggregationTest.php index d1ede4672dc7005adb5626e7d6314739ac2f5a81..829f371ef59cff8252d6d1f15b44de6d737ff42a 100644 --- a/Aggregation/GeoBoundsAggregationTest.php +++ b/Aggregation/GeoBoundsAggregationTest.php @@ -28,4 +28,44 @@ class GeoBoundsAggregationTest extends \PHPUnit_Framework_TestCase $agg = new GeoBoundsAggregation('test_agg'); $agg->getArray(); } + + /** + * Tests getType method. + */ + public function testGeoBoundsAggregationGetType() + { + $agg = new GeoBoundsAggregation('foo'); + $result = $agg->getType(); + $this->assertEquals('geo_bounds', $result); + } + + /** + * Tests getArray method. + */ + public function testGeoBoundsAggregationGetArray() + { + $agg = new GeoBoundsAggregation('foo'); + $agg->setField('bar'); + $agg->setWrapLongitude(true); + $result = [ + 'agg_foo' => [ + 'geo_bounds' => [ + 'field' => 'bar', + 'wrap_longitude' => true, + ], + ], + ]; + $this->assertEquals($result, $agg->toArray(), 'when wraplongitude is true'); + + $agg->setWrapLongitude(false); + $result = [ + 'agg_foo' => [ + 'geo_bounds' => [ + 'field' => 'bar', + 'wrap_longitude' => false, + ], + ], + ]; + $this->assertEquals($result, $agg->toArray(), 'when wraplongitude is false'); + } } diff --git a/Aggregation/GeoDistanceAggregationTest.php b/Aggregation/GeoDistanceAggregationTest.php index 73075a6b2c18a78e408b55795921847221ad4503..4dd3e0ece475d757f7a795f3df2257bd988adf95 100644 --- a/Aggregation/GeoDistanceAggregationTest.php +++ b/Aggregation/GeoDistanceAggregationTest.php @@ -52,4 +52,64 @@ class GeoDistanceAggregationTest extends \PHPUnit_Framework_TestCase $agg = new GeoDistanceAggregation('test_agg'); $agg->addRange(); } + + /** + * Data provider for testGeoDistanceAggregationGetArray(). + * + * @return array + */ + public function testGeoDistanceAggregationGetArrayDataProvider() + { + $out = []; + $filterData = [ + 'field' => 'location', + 'origin' => '52.3760, 4.894', + 'unit' => 'mi', + 'distance_type' => 'plane', + 'ranges' => [100, 300], + ]; + + $expectedResults = [ + 'field' => 'location', + 'origin' => '52.3760, 4.894', + 'unit' => 'mi', + 'distance_type' => 'plane', + 'ranges' => [['from' => 100, 'to' => 300]], + ]; + + $out[] = [$filterData, $expectedResults]; + + return $out; + } + + /** + * Tests getArray method. + * + * @param array $filterData + * @param array $expected + * + * @dataProvider testGeoDistanceAggregationGetArrayDataProvider + */ + public function testGeoDistanceAggregationGetArray($filterData, $expected) + { + $aggregation = new GeoDistanceAggregation('foo'); + $aggregation->setOrigin($filterData['origin']); + $aggregation->setField($filterData['field']); + $aggregation->setUnit($filterData['unit']); + $aggregation->setDistanceType($filterData['distance_type']); + $aggregation->addRange($filterData['ranges'][0], $filterData['ranges'][1]); + + $result = $aggregation->getArray(); + $this->assertEquals($result, $expected); + } + + /** + * Tests getType method. + */ + public function testGeoDistanceAggregationGetType() + { + $aggregation = new GeoDistanceAggregation('foo'); + $result = $aggregation->getType(); + $this->assertEquals('geo_distance', $result); + } } diff --git a/Aggregation/GeoHashGridAggregationTest.php b/Aggregation/GeoHashGridAggregationTest.php index 0a520e9fe28f99f9afcbce6e0d45359150da080f..aac572646b759121e1621439afaa6e8b4d749e4c 100644 --- a/Aggregation/GeoHashGridAggregationTest.php +++ b/Aggregation/GeoHashGridAggregationTest.php @@ -28,4 +28,62 @@ class GeoHashGridAggregationTest extends \PHPUnit_Framework_TestCase $agg = new GeoHashGridAggregation('test_agg'); $agg->getArray(); } + + /** + * Data provider for testGeoHashGridAggregationGetArray(). + * + * @return array + */ + public function getArrayDataProvider() + { + $out = []; + + $filterData = [ + 'field' => 'location', + 'precision' => 3, + 'size' => 10, + 'shard_size' => 10, + ]; + + $expectedResults = [ + 'field' => 'location', + 'precision' => 3, + 'size' => 10, + 'shard_size' => 10, + ]; + + $out[] = [$filterData, $expectedResults]; + + return $out; + } + + /** + * Tests getArray method. + * + * @param array $filterData + * @param array $expected + * + * @dataProvider getArrayDataProvider + */ + public function testGeoHashGridAggregationGetArray($filterData, $expected) + { + $aggregation = new GeoHashGridAggregation('foo'); + $aggregation->setPrecision($filterData['precision']); + $aggregation->setSize($filterData['size']); + $aggregation->setShardSize($filterData['shard_size']); + $aggregation->setField($filterData['field']); + + $result = $aggregation->getArray(); + $this->assertEquals($result, $expected); + } + + /** + * Tests getType method. + */ + public function testGeoHashGridAggregationGetType() + { + $aggregation = new GeoHashGridAggregation('foo'); + $result = $aggregation->getType(); + $this->assertEquals('geohash_grid', $result); + } } diff --git a/Aggregation/GlobalAggregationTest.php b/Aggregation/GlobalAggregationTest.php index 127e7f56e3a675fa16713095272e83b3c47d000f..351c27e2b932718dbad4965e40ebc8e509ba7c7d 100644 --- a/Aggregation/GlobalAggregationTest.php +++ b/Aggregation/GlobalAggregationTest.php @@ -79,7 +79,6 @@ class GlobalAggregationTest extends \PHPUnit_Framework_TestCase * Test for setField method on global aggregation. * * @expectedException \LogicException - * @expectedException doesn't support `field` parameter */ public function testSetField() { diff --git a/Aggregation/MissingAggregationTest.php b/Aggregation/MissingAggregationTest.php index df8e0b164e36068b38f2d49d4c3cc1b687e808e1..300971ef16150a4cde2de255ed66b4900d06f951 100644 --- a/Aggregation/MissingAggregationTest.php +++ b/Aggregation/MissingAggregationTest.php @@ -26,4 +26,25 @@ class MissingAggregationTest extends \PHPUnit_Framework_TestCase $agg = new MissingAggregation('test_agg'); $agg->getArray(); } + + /** + * Test getArray method. + */ + public function testMissingAggregationGetArray() + { + $aggregation = new MissingAggregation('foo'); + $aggregation->setField('bar'); + $result = $aggregation->getArray(); + $this->assertEquals('bar', $result['field']); + } + + /** + * Test getType method. + */ + public function testMissingAggregationGetType() + { + $aggregation = new MissingAggregation('bar'); + $result = $aggregation->getType(); + $this->assertEquals('missing', $result); + } } diff --git a/Aggregation/RangeAggregationTest.php b/Aggregation/RangeAggregationTest.php index 6306bba4d753a002ea87da8664440fe4b70d1f8c..fe8c317c7ecf48201d3329f3be51c25aeb4e5f61 100644 --- a/Aggregation/RangeAggregationTest.php +++ b/Aggregation/RangeAggregationTest.php @@ -16,15 +16,10 @@ use ONGR\ElasticsearchBundle\DSL\Aggregation\RangeAggregation; class RangeAggregationTest extends \PHPUnit_Framework_TestCase { /** - * Data provider for testToArray(). - * - * @return array + * Test addRange method. */ - public function getToArrayData() + public function testRangeAggregationAddRange() { - $out = []; - - // Case #0 single range. $aggregation = new RangeAggregation('test_agg'); $aggregation->setField('test_field'); $aggregation->addRange('10', 20); @@ -44,12 +39,14 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase ], ]; - $out[] = [ - $aggregation, - $result, - ]; + $this->assertEquals($result, $aggregation->toArray()); + } - // Case #1 multiple keyed ranges. + /** + * Test addRange method with multiple values. + */ + public function testRangeAggregationAddRangeMultiple() + { $aggregation = new RangeAggregation('test_agg'); $aggregation->setField('test_field'); $aggregation->setKeyed(true); @@ -75,12 +72,14 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase ], ]; - $out[] = [ - $aggregation, - $result, - ]; + $this->assertEquals($result, $aggregation->toArray()); + } - // Case #2 nested aggregation. + /** + * Test addRange method with nested values. + */ + public function testRangeAggregationAddRangeNested() + { $aggregation = new RangeAggregation('test_agg'); $aggregation->setField('test_field'); $aggregation->addRange('10', '10'); @@ -118,24 +117,86 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase ], ]; - $out[] = [ - $aggregation, - $result, + $this->assertEquals($result, $aggregation->toArray()); + } + + /** + * Tests getType method. + */ + public function testRangeAggregationGetType() + { + $agg = new RangeAggregation('foo'); + $result = $agg->getType(); + $this->assertEquals('range', $result); + } + + /** + * Tests removeRangeByKey method. + */ + public function testRangeAggregationRemoveRangeByKey() + { + $aggregation = new RangeAggregation('foo'); + $aggregation->setField('price'); + $aggregation->setKeyed(true); + $aggregation->addRange(100, 300, 'name'); + + $expected = [ + 'field' => 'price', + 'keyed' => true, + 'ranges' => [ + [ + 'from' => 100, + 'to' => 300, + 'key' => 'name', + ], + ], ]; - return $out; + $result = $aggregation->getArray(); + $this->assertEquals($result, $expected, 'get array of ranges when keyed=true'); + + $result = $aggregation->removeRangeByKey('name'); + $this->assertTrue($result, 'returns true when removed valid range name'); + + $result = $aggregation->removeRangeByKey('not_existing_key'); + $this->assertFalse($result, 'should not allow remove not existing key if keyed=true'); + + $aggregation->setKeyed(false); + $result = $aggregation->removeRangeByKey('not_existing_key'); + $this->assertFalse($result, 'should not allow remove not existing key if keyed=false'); + + $aggregation->addRange(100, 300, 'name'); + $result = $aggregation->removeRangeByKey('name'); + $this->assertFalse($result, 'can not remove any existing range if keyed=false'); } /** - * Test for range aggregation toArray() method. - * - * @param RangeAggregation $aggregation - * @param array $expectedResult - * - * @dataProvider getToArrayData + * Tests removeRange method. */ - public function testToArray($aggregation, $expectedResult) + public function testRangeAggregationRemoveRange() { - $this->assertEquals($expectedResult, $aggregation->toArray()); + $aggregation = new RangeAggregation('foo'); + $aggregation->setField('price'); + $aggregation->setKeyed(true); + $aggregation->addRange(100, 300, 'key'); + $aggregation->addRange(500, 700, 'range_2'); + + $expected = [ + 'field' => 'price', + 'keyed' => true, + 'ranges' => [ + [ + 'from' => 100, + 'to' => 300, + 'key' => 'key', + ], + ], + ]; + + $aggregation->removeRange(500, 700); + $result = $aggregation->getArray(); + $this->assertEquals($result, $expected, 'get expected array of ranges'); + $result = $aggregation->removeRange(500, 700); + $this->assertFalse($result, 'returns false after removing not-existing range'); } } diff --git a/Aggregation/TermsAggregationTest.php b/Aggregation/TermsAggregationTest.php index 4d3b3418479e58f4ea2958f147164c52b3e98ccd..25f551ba9211b225b810803aed9e6d3d53975d93 100644 --- a/Aggregation/TermsAggregationTest.php +++ b/Aggregation/TermsAggregationTest.php @@ -16,14 +16,10 @@ use ONGR\ElasticsearchBundle\DSL\Aggregation\TermsAggregation; class TermsAggregationTest extends \PHPUnit_Framework_TestCase { /** - * Data provider for testToArray(). - * - * @return array + * Tests setField method. */ - public function getToArrayData() + public function testTermsAggregationSetField() { - $out = []; - // Case #0 terms aggregation. $aggregation = new TermsAggregation('test_agg'); $aggregation->setField('test_field'); @@ -34,11 +30,14 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase ], ]; - $out[] = [ - $aggregation, - $result, - ]; + $this->assertEquals($aggregation->toArray(), $result); + } + /** + * Tests setSize method. + */ + public function testTermsAggregationSetSize() + { // Case #1 terms aggregation with size. $aggregation = new TermsAggregation('test_agg'); $aggregation->setField('test_field'); @@ -53,11 +52,14 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase ], ]; - $out[] = [ - $aggregation, - $result, - ]; + $this->assertEquals($aggregation->toArray(), $result); + } + /** + * Tests minDocumentCount method. + */ + public function testTermsAggregationMinDocumentCount() + { // Case #2 terms aggregation with size and min document count. $aggregation = new TermsAggregation('test_agg'); $aggregation->setField('test_field'); @@ -74,11 +76,14 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase ], ]; - $out[] = [ - $aggregation, - $result, - ]; + $this->assertEquals($aggregation->toArray(), $result); + } + /** + * Tests include, exclude method. + */ + public function testTermsAggregationSimpleIncludeExclude() + { // Case #3 terms aggregation with simple include, exclude. $aggregation = new TermsAggregation('test_agg'); $aggregation->setField('test_field'); @@ -95,11 +100,14 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase ], ]; - $out[] = [ - $aggregation, - $result, - ]; + $this->assertEquals($aggregation->toArray(), $result); + } + /** + * Tests include, exclude with flags method. + */ + public function testTermsAggregationIncludeExcludeFlags() + { // Case #4 terms aggregation with include, exclude and flags. $aggregation = new TermsAggregation('test_agg'); $aggregation->setField('test_field'); @@ -122,11 +130,14 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase ], ]; - $out[] = [ - $aggregation, - $result, - ]; + $this->assertEquals($aggregation->toArray(), $result); + } + /** + * Tests setOrder method. + */ + public function testTermsAggregationSetOrder() + { // Case #5 terms aggregation with order default direction. $aggregation = new TermsAggregation('test_agg'); $aggregation->setField('test_field'); @@ -141,11 +152,14 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase ], ]; - $out[] = [ - $aggregation, - $result, - ]; + $this->assertEquals($aggregation->toArray(), $result); + } + /** + * Tests setOrder DESC method. + */ + public function testTermsAggregationSetOrderDESC() + { // Case #6 terms aggregation with order term mode, desc direction. $aggregation = new TermsAggregation('test_agg'); $aggregation->setField('test_field'); @@ -160,24 +174,16 @@ class TermsAggregationTest extends \PHPUnit_Framework_TestCase ], ]; - $out[] = [ - $aggregation, - $result, - ]; - - return $out; + $this->assertEquals($aggregation->toArray(), $result); } /** - * Test for toArray(). - * - * @param TermsAggregation $aggregation - * @param array $expectedResults - * - * @dataProvider getToArrayData + * Tests getType method. */ - public function testToArray($aggregation, $expectedResults) + public function testTermsAggregationGetType() { - $this->assertEquals($expectedResults, $aggregation->toArray()); + $aggregation = new TermsAggregation('foo'); + $result = $aggregation->getType(); + $this->assertEquals('terms', $result); } }