Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Result/Aggregation/AggregationIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* This class hold aggregations from Elasticsearch result.
*/
class AggregationIterator implements \ArrayAccess, \Iterator
class AggregationIterator implements \ArrayAccess, \Iterator, \Countable
{
/**
* @var array
Expand Down Expand Up @@ -169,4 +169,12 @@ public function find($path)

return null;
}

/**
* {@inheritdoc}
*/
public function count()
{
return count($this->rawData);
}
}
105 changes: 105 additions & 0 deletions Tests/Unit/Result/Aggregation/AggregationIteratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchBundle\Tests\Unit\Result\Aggregation;

use ONGR\ElasticsearchBundle\Result\Aggregation\AggregationIterator;

/**
* Class AggregationIteratorTest.
*/
class AggregationIteratorTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests countable interface implementation.
*/
public function testCountable()
{
$this->assertInstanceOf('\\Countable', new AggregationIterator([]));
}

/**
* Data provider for testCount.
*/
public function countProvider()
{
$cases = [];

// Case #0. No data.
$cases[] = [
'data' => [],
'expectedCount' => 0,
];

// Case #2. With data.
$cases[] = [
'data' => [
[
'key' => 'weak',
'doc_count' => 2,
'agg_test_agg_2' => [
'buckets' => [
[
'key' => '*-20.0',
'to' => 20.0,
'to_as_string' => '20.0',
'doc_count' => 1,
],
[
'key' => '20.0-*',
'from' => 20.0,
'from_as_string' => '20.0',
'doc_count' => 1,
],
],
],
],
[
'key' => 'solid',
'doc_count' => 1,
'agg_test_agg_2' => [
'buckets' => [
[
'key' => '*-20.0',
'to' => 20.0,
'to_as_string' => '20.0',
'doc_count' => 1,
],
[
'key' => '20.0-*',
'from' => 20.0,
'from_as_string' => '20.0',
'doc_count' => 0,
],
],
],
],
],
'expectedCount' => 2,
];

return $cases;
}

/**
* Tests counting.
*
* @dataProvider countProvider
*
* @param array $data
* @param int $expectedCount
*/
public function testCount($data, $expectedCount)
{
$iterator = new AggregationIterator($data);
$this->assertCount($expectedCount, $iterator);
}
}