/** * Tests the "Global" bucket aggregation */ public function testGlobalAggregation() { $this->specify('global aggregation was created', function () { $aggregation = $this->aggregationBuilder->createGlobalAggregation(); verify($aggregation)->isInstanceOf('\\Nord\\Lumen\\Elasticsearch\\Search\\Aggregation\\Bucket\\GlobalAggregation'); }); $this->specify('global aggregation format', function () { $aggregation = $this->aggregationBuilder->createGlobalAggregation(); $array = $aggregation->toArray(); verify($array)->equals(['global' => new stdClass(), 'aggs' => []]); }); $this->specify('global aggregation format with min/max aggregations', function () { $aggregation = $this->aggregationBuilder->createGlobalAggregation(); $aggregation->addAggregation($this->aggregationBuilder->createMinAggregation()->setField('field_name')->setName('min_name')); $aggregation->addAggregation($this->aggregationBuilder->createMaxAggregation()->setField('field_name')->setName('max_name')); $array = $aggregation->toArray(); verify($array)->equals(['global' => new stdClass(), 'aggs' => ['min_name' => ['min' => ['field' => 'field_name']], 'max_name' => ['max' => ['field' => 'field_name']]]]); }); }
/** * Tests collection. */ public function testCollection() { $this->specify('adding aggregations to collection', function () { $collection = new \Nord\Lumen\Elasticsearch\Search\Aggregation\AggregationCollection(); verify($collection->count())->equals(0); $collection->add($this->aggregationBuilder->createGlobalAggregation()->setName('global_name')); verify($collection->count())->equals(1); }); $this->specify('getting aggregations from collection', function () { $collection = new \Nord\Lumen\Elasticsearch\Search\Aggregation\AggregationCollection(); $collection->add($this->aggregationBuilder->createGlobalAggregation()->setName('global_name')); verify($collection->get(0)->getName())->equals('global_name'); }); $this->specify('iterating aggregation collection', function () { $collection = new \Nord\Lumen\Elasticsearch\Search\Aggregation\AggregationCollection(); $collection->add($this->aggregationBuilder->createGlobalAggregation()->setName('global_name')); foreach ($collection as $aggregation) { /** @var \Nord\Lumen\Elasticsearch\Search\Aggregation\Aggregation $aggregation */ verify($aggregation)->isInstanceOf('\\Nord\\Lumen\\Elasticsearch\\Search\\Aggregation\\Aggregation'); verify($aggregation->getName())->equals('global_name'); } }); $this->specify('removing aggregations from collection', function () { $collection = new \Nord\Lumen\Elasticsearch\Search\Aggregation\AggregationCollection(); verify($collection->count())->equals(0); $collection->add($this->aggregationBuilder->createGlobalAggregation()->setName('global_name')); verify($collection->count())->equals(1); $removed = $collection->remove(0); verify($removed)->notNull(); verify($collection->count())->equals(0); $removed = $collection->remove(0); verify($removed)->null(); }); $this->specify('init collection with aggregations', function () { $collection = new \Nord\Lumen\Elasticsearch\Search\Aggregation\AggregationCollection([$this->aggregationBuilder->createGlobalAggregation()->setName('global_name')]); verify($collection->count())->equals(1); }); }