|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace itertools; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class ComposingIteratorTest extends PHPUnit_Framework_TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
/** @test */ |
|
11
|
|
|
public function testBasicFunctionality() |
|
12
|
|
|
{ |
|
13
|
|
|
$values = ComposingIterator::newInstance() |
|
|
|
|
|
|
14
|
|
|
->range(0, 10) |
|
15
|
|
|
->filter(function($v) { return 10 != $v; }) |
|
16
|
|
|
->groupBy(function($v) { return 5 < $v; }) |
|
17
|
|
|
->chain() |
|
18
|
|
|
->slice(1) |
|
19
|
|
|
->map(function($v) { return (int) ($v / 2); }) |
|
20
|
|
|
->unique() |
|
21
|
|
|
->takeWhile(function($v) { return 4 > $v; }) |
|
22
|
|
|
->toArray(ComposingIterator::DONT_USE_KEYS); |
|
23
|
|
|
$this->assertEquals(range(0, 3), $values); |
|
24
|
|
|
|
|
25
|
|
|
$values = ComposingIterator::newInstance() |
|
26
|
|
|
->source(new RepeatIterator(1)) |
|
27
|
|
|
->cacheCurrent() |
|
28
|
|
|
->chunk(20) |
|
29
|
|
|
->map(function($batch) { return array_slice($batch, 0, 10); }) |
|
30
|
|
|
->slice(0, 10) |
|
31
|
|
|
->chain() |
|
32
|
|
|
->zipWith(new RepeatIterator(2)) |
|
33
|
|
|
->zipWithAll(array(new RepeatIterator(3), new RepeatIterator(4))) |
|
34
|
|
|
->takeWhile(function($v) { return 4 == $v[2]; }) |
|
35
|
|
|
->count(); |
|
36
|
|
|
$this->assertEquals(100, $values); |
|
37
|
|
|
|
|
38
|
|
|
$count = ComposingIterator::newInstance() |
|
39
|
|
|
->source(array(1, 2, 3, 4)) |
|
40
|
|
|
->skipFirst() |
|
41
|
|
|
->count(); |
|
42
|
|
|
$this->assertEquals(3, $count); |
|
43
|
|
|
|
|
44
|
|
|
$values = ComposingIterator::newInstance() |
|
45
|
|
|
->source(array( |
|
46
|
|
|
'jos , 5', |
|
47
|
|
|
'piet , 7', |
|
48
|
|
|
)) |
|
49
|
|
|
->fixedLengthFormattedStringFromTemplate('name , age') |
|
50
|
|
|
->toArray(); |
|
51
|
|
|
$this->assertEquals('jos ', $values[0]['name']); |
|
52
|
|
|
$this->assertEquals('5', $values[0]['age']); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @test |
|
57
|
|
|
* @expectedException BadMethodCallException |
|
58
|
|
|
*/ |
|
59
|
|
|
public function testCallingUnknownMethods() |
|
60
|
|
|
{ |
|
61
|
|
|
ComposingIterator::newInstance()->bliablabloe(); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: