Skip to content
Snippets Groups Projects
Commit 62a08b7d authored by Simonas Šerlinskas's avatar Simonas Šerlinskas
Browse files

Merge pull request #280 from mansimas/patch-dsl-unit-tests

Added more unit tests for DSL
parents d1f252f2 6ce5ac43
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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');
}
}
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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()
{
......
......@@ -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);
}
}
......@@ -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');
}
}
......@@ -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);
}
}
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