From a8149f271781f79220e3994b773ea415223cb4cc Mon Sep 17 00:00:00 2001 From: Mantas Simkus <mantas.simkus@ongr.io> Date: Fri, 20 Mar 2015 12:00:08 +0200 Subject: [PATCH] updated RangeAggregation unit test --- Aggregation/RangeAggregationTest.php | 119 +++++++++++++++++++++------ 1 file changed, 95 insertions(+), 24 deletions(-) diff --git a/Aggregation/RangeAggregationTest.php b/Aggregation/RangeAggregationTest.php index 6306bba..413bdf8 100644 --- a/Aggregation/RangeAggregationTest.php +++ b/Aggregation/RangeAggregationTest.php @@ -16,14 +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'); @@ -44,11 +40,14 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase ], ]; - $out[] = [ - $aggregation, - $result, - ]; + $this->assertEquals($result, $aggregation->toArray()); + } + /** + * Test addRange method with multiple values. + */ + public function testRangeAggregationAddRangeMultiple() + { // Case #1 multiple keyed ranges. $aggregation = new RangeAggregation('test_agg'); $aggregation->setField('test_field'); @@ -75,11 +74,14 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase ], ]; - $out[] = [ - $aggregation, - $result, - ]; + $this->assertEquals($result, $aggregation->toArray()); + } + /** + * Test addRange method with nested values. + */ + public function testRangeAggregationAddRangeNested() + { // Case #2 nested aggregation. $aggregation = new RangeAggregation('test_agg'); $aggregation->setField('test_field'); @@ -118,24 +120,93 @@ 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); + } + + /** + * Data provider for testRangeAggregationRemoveRangeByKey(), testRangeAggregationRemoveRange(). + * + * @return array + */ + public function testRangeAggregationDataProvider() + { + $expectedResults = [ + 'field' => 'price', + 'keyed' => true, + 'ranges' => [ + [ + 'from' => 100, + 'to' => 300, + 'key' => 'key', + ], + ], ]; - return $out; + return [[$expectedResults]]; + } + + /** + * Tests removeRangeByKey method. + * + * @param array $expected + * + * @dataProvider testRangeAggregationDataProvider + */ + public function testRangeAggregationRemoveRangeByKey($expected) + { + $aggregation = new RangeAggregation('foo'); + $aggregation->setField('price'); + $aggregation->setKeyed(true); + $aggregation->addRange(100, 300, 'key'); + + $result = $aggregation->getArray(); + $this->assertEquals($result, $expected); + + $result = $aggregation->removeRangeByKey('key'); + $this->assertTrue($result); + + $result = $aggregation->removeRangeByKey('not_existing_key'); + $this->assertFalse($result); + // Test with keyed=false. + $aggregation->setKeyed(false); + $result = $aggregation->removeRangeByKey('not_existing_key'); + $this->assertFalse($result); + + $aggregation->addRange(100, 300, 'key'); + $result = $aggregation->removeRangeByKey('key'); + $this->assertFalse($result); } /** - * Test for range aggregation toArray() method. + * Tests removeRange method. * - * @param RangeAggregation $aggregation - * @param array $expectedResult + * @param array $expected * - * @dataProvider getToArrayData + * @dataProvider testRangeAggregationDataProvider */ - public function testToArray($aggregation, $expectedResult) + public function testRangeAggregationRemoveRange($expected) { - $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'); + + $aggregation->removeRange(500, 700); + $result = $aggregation->getArray(); + $this->assertEquals($result, $expected); + // Test fake ranges. + $result = $aggregation->removeRange(500, 700); + $this->assertFalse($result); } } -- GitLab