summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateHistogramTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateHistogramTest.php')
-rw-r--r--vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateHistogramTest.php103
1 files changed, 103 insertions, 0 deletions
diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateHistogramTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateHistogramTest.php
new file mode 100644
index 00000000..ca115ccc
--- /dev/null
+++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateHistogramTest.php
@@ -0,0 +1,103 @@
+<?php
+namespace Elastica\Test\Aggregation;
+
+use Elastica\Aggregation\DateHistogram;
+use Elastica\Document;
+use Elastica\Query;
+use Elastica\Type\Mapping;
+
+class DateHistogramTest extends BaseAggregationTest
+{
+ protected function _getIndexForTest()
+ {
+ $index = $this->_createIndex();
+ $type = $index->getType('test');
+
+ $type->setMapping(new Mapping(null, array(
+ 'created' => array('type' => 'date'),
+ )));
+
+ $type->addDocuments(array(
+ new Document(1, array('created' => '2014-01-29T00:20:00')),
+ new Document(2, array('created' => '2014-01-29T02:20:00')),
+ new Document(3, array('created' => '2014-01-29T03:20:00')),
+ ));
+
+ $index->refresh();
+
+ return $index;
+ }
+
+ /**
+ * @group functional
+ */
+ public function testDateHistogramAggregation()
+ {
+ $agg = new DateHistogram('hist', 'created', '1h');
+
+ $query = new Query();
+ $query->addAggregation($agg);
+ $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
+
+ $this->assertEquals(3, sizeof($results['buckets']));
+ }
+
+ /**
+ * @group unit
+ */
+ public function testSetOffset()
+ {
+ $agg = new DateHistogram('hist', 'created', '1h');
+
+ $agg->setOffset('3m');
+
+ $expected = array(
+ 'date_histogram' => array(
+ 'field' => 'created',
+ 'interval' => '1h',
+ 'offset' => '3m',
+ ),
+ );
+
+ $this->assertEquals($expected, $agg->toArray());
+
+ $this->assertInstanceOf('Elastica\Aggregation\DateHistogram', $agg->setOffset('3m'));
+ }
+
+ /**
+ * @group functional
+ */
+ public function testSetOffsetWorks()
+ {
+ $agg = new DateHistogram('hist', 'created', '1m');
+ $agg->setOffset('+40s');
+
+ $query = new Query();
+ $query->addAggregation($agg);
+ $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
+
+ $this->assertEquals('2014-01-29T00:19:40.000Z', $results['buckets'][0]['key_as_string']);
+ }
+
+ /**
+ * @group unit
+ */
+ public function testSetTimezone()
+ {
+ $agg = new DateHistogram('hist', 'created', '1h');
+
+ $agg->setTimezone('-02:30');
+
+ $expected = array(
+ 'date_histogram' => array(
+ 'field' => 'created',
+ 'interval' => '1h',
+ 'time_zone' => '-02:30',
+ ),
+ );
+
+ $this->assertEquals($expected, $agg->toArray());
+
+ $this->assertInstanceOf('Elastica\Aggregation\DateHistogram', $agg->setTimezone('-02:30'));
+ }
+}