|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace itertools; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayIterator; |
|
6
|
|
|
use PHPUnit_Framework_TestCase; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class GroupByIteratorTest extends PHPUnit_Framework_TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** @test */ |
|
12
|
|
|
public function testSimpleGroupByWithDefaultComparator() |
|
13
|
|
|
{ |
|
14
|
|
|
$input = new GroupByIterator(array(0, 0, 1, 1, 2, 3)); |
|
15
|
|
|
$expectedResult = array( |
|
16
|
|
|
array(0, 0), |
|
17
|
|
|
array(1, 1), |
|
18
|
|
|
array(2), |
|
19
|
|
|
array(3), |
|
20
|
|
|
); |
|
21
|
|
|
$this->assertEquals($expectedResult, IterUtil::recursive_iterator_to_array($input, false)); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @test |
|
26
|
|
|
* @expectedException InvalidArgumentException |
|
27
|
|
|
*/ |
|
28
|
|
|
public function testInvalidInput() |
|
29
|
|
|
{ |
|
30
|
|
|
new GroupByIterator(array(), 'not-a-callable'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** @test */ |
|
34
|
|
|
public function testCustomComparator() |
|
35
|
|
|
{ |
|
36
|
|
|
$input = new GroupByIterator(array( |
|
37
|
|
|
(object) array('name' => 'jos', 'age' => 3), |
|
38
|
|
|
(object) array('name' => 'mieke', 'age' => 3), |
|
39
|
|
|
(object) array('name' => 'tom', 'age' => 4), |
|
40
|
|
|
(object) array('name' => 'lotte', 'age' => 5), |
|
41
|
|
|
(object) array('name' => 'jaak', 'age' => 5), |
|
42
|
|
|
), function($v) { return $v->age; }); |
|
43
|
|
|
$inputArray = IterUtil::recursive_iterator_to_array($input, false); |
|
44
|
|
|
|
|
45
|
|
|
$this->assertEquals('jos', $inputArray[0][0]->name); |
|
46
|
|
|
$this->assertEquals('mieke', $inputArray[0][1]->name); |
|
47
|
|
|
$this->assertEquals('tom', $inputArray[1][0]->name); |
|
48
|
|
|
$this->assertEquals('lotte', $inputArray[2][0]->name); |
|
49
|
|
|
$this->assertEquals('jaak', $inputArray[2][1]->name); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** @test */ |
|
53
|
|
|
public function testSkippingGroups() |
|
54
|
|
|
{ |
|
55
|
|
|
$input = new GroupByIterator(array(0, 0, 1, 1, 2, 3)); |
|
56
|
|
|
foreach($input as $i => $group) { |
|
57
|
|
|
switch($i) { |
|
58
|
|
|
case 1: |
|
59
|
|
|
$this->assertEquals(array(1, 1), iterator_to_array($group, false)); |
|
60
|
|
|
break; |
|
61
|
|
|
|
|
62
|
|
|
case 3: |
|
63
|
|
|
$this->assertEquals(array(3), iterator_to_array($group, false)); |
|
64
|
|
|
break; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|