Skip to content

Commit 4605249

Browse files
committed
Merge pull request #398 from GrandLTU/countable
Implement Countable interface in AggregationIterator.
2 parents 677999a + eb073b5 commit 4605249

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

Result/Aggregation/AggregationIterator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* This class hold aggregations from Elasticsearch result.
1818
*/
19-
class AggregationIterator implements \ArrayAccess, \Iterator
19+
class AggregationIterator implements \ArrayAccess, \Iterator, \Countable
2020
{
2121
/**
2222
* @var array
@@ -172,4 +172,12 @@ public function find($path)
172172

173173
return null;
174174
}
175+
176+
/**
177+
* {@inheritdoc}
178+
*/
179+
public function count()
180+
{
181+
return count($this->rawData);
182+
}
175183
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Tests\Unit\Result\Aggregation;
13+
14+
use ONGR\ElasticsearchBundle\Result\Aggregation\AggregationIterator;
15+
16+
/**
17+
* Class AggregationIteratorTest.
18+
*/
19+
class AggregationIteratorTest extends \PHPUnit_Framework_TestCase
20+
{
21+
/**
22+
* Tests countable interface implementation.
23+
*/
24+
public function testCountable()
25+
{
26+
$this->assertInstanceOf('\\Countable', new AggregationIterator([]));
27+
}
28+
29+
/**
30+
* Data provider for testCount.
31+
*/
32+
public function countProvider()
33+
{
34+
$cases = [];
35+
36+
// Case #0. No data.
37+
$cases[] = [
38+
'data' => [],
39+
'expectedCount' => 0,
40+
];
41+
42+
// Case #2. With data.
43+
$cases[] = [
44+
'data' => [
45+
[
46+
'key' => 'weak',
47+
'doc_count' => 2,
48+
'agg_test_agg_2' => [
49+
'buckets' => [
50+
[
51+
'key' => '*-20.0',
52+
'to' => 20.0,
53+
'to_as_string' => '20.0',
54+
'doc_count' => 1,
55+
],
56+
[
57+
'key' => '20.0-*',
58+
'from' => 20.0,
59+
'from_as_string' => '20.0',
60+
'doc_count' => 1,
61+
],
62+
],
63+
],
64+
],
65+
[
66+
'key' => 'solid',
67+
'doc_count' => 1,
68+
'agg_test_agg_2' => [
69+
'buckets' => [
70+
[
71+
'key' => '*-20.0',
72+
'to' => 20.0,
73+
'to_as_string' => '20.0',
74+
'doc_count' => 1,
75+
],
76+
[
77+
'key' => '20.0-*',
78+
'from' => 20.0,
79+
'from_as_string' => '20.0',
80+
'doc_count' => 0,
81+
],
82+
],
83+
],
84+
],
85+
],
86+
'expectedCount' => 2,
87+
];
88+
89+
return $cases;
90+
}
91+
92+
/**
93+
* Tests counting.
94+
*
95+
* @dataProvider countProvider
96+
*
97+
* @param array $data
98+
* @param int $expectedCount
99+
*/
100+
public function testCount($data, $expectedCount)
101+
{
102+
$iterator = new AggregationIterator($data);
103+
$this->assertCount($expectedCount, $iterator);
104+
}
105+
}

0 commit comments

Comments
 (0)