diff options
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.php | 100 |
1 files changed, 80 insertions, 20 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 index d5bd878c..ca115ccc 100644 --- a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateHistogramTest.php +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateHistogramTest.php @@ -1,8 +1,6 @@ <?php - namespace Elastica\Test\Aggregation; - use Elastica\Aggregation\DateHistogram; use Elastica\Document; use Elastica\Query; @@ -10,34 +8,96 @@ use Elastica\Type\Mapping; class DateHistogramTest extends BaseAggregationTest { - protected function setUp() + protected function _getIndexForTest() { - parent::setUp(); - $this->_index = $this->_createIndex("date_histogram"); - $mapping = new Mapping(); - $mapping->setProperties(array( - "created" => array("type" => "date") + $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')), )); - $type = $this->_index->getType("test"); - $type->setMapping($mapping); - $docs = array( - new Document("1", array("created" => 1390962135000)), - new Document("2", array("created" => 1390965735000)), - new Document("3", array("created" => 1390954935000)), - ); - $type->addDocuments($docs); - $this->_index->refresh(); + + $index->refresh(); + + return $index; } + /** + * @group functional + */ public function testDateHistogramAggregation() { - $agg = new DateHistogram("hist", "created", "1h"); + $agg = new DateHistogram('hist', 'created', '1h'); $query = new Query(); $query->addAggregation($agg); - $results = $this->_index->search($query)->getAggregation("hist"); + $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')); + } } -
\ No newline at end of file |