IterUtilTest   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 4
dl 0
loc 226
rs 10
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A testInvoke() 0 5 1
A testSum() 0 4 1
A testAsIterator() 0 7 1
A testAsTraversable() 0 7 1
A testAsTraversableWithNonTraversableAsArgument() 0 4 1
A testIsCollection() 0 9 1
A testAssertIsCollectionForCollections() 0 7 1
A testAssertIsCollectionForNonCollections() 0 4 1
A testIteratorReduce() 0 15 1
A testGetCurrentAndAdvanceForArray() 0 9 1
A testGetCurrentAndAdvanceForArrayShouldThrowExceptionOnEndOfRange() 0 8 1
A testGetCurrentAndAdvanceForArrayShouldNotThrowErrorWHenProvidedWithDefault() 0 9 1
A testGetCurrentAndAdvanceForIterator() 0 10 1
A testGetCurrentAndAdvanceForIteratorShouldThrowExceptionOnEndOfRange() 0 9 1
A testGetCurrentAndAdvanceForIteratorShouldNotThrowErrorWHenProvidedWithDefault() 0 10 1
A testResursiveIteratorToArray() 0 6 1
A testAll() 0 17 1
A testAny() 0 17 1
A testAnyShouldThrowExceptionIfSuppliedWithInvalidCallable() 0 4 1
A testAllShouldThrowExceptionIfSuppliedWithInvalidCallable() 0 4 1
1
<?php
2
3
namespace itertools;
4
5
use ArrayIterator;
6
use IteratorIterator;
7
use PHPUnit_Framework_TestCase;
8
use SimpleXMLElement;
9
use SplObjectStorage;
10
use stdClass;
11
12
13
class IterUtilTest extends PHPUnit_Framework_TestCase
14
{
15
	/** @test */
16
	public function testInvoke()
17
	{
18
		$input = array(new SplObjectStorage(), new SplObjectStorage());
19
		$this->assertEquals(array(0, 0), iterator_to_array(IterUtil::invoke($input, 'count')));
20
	}
21
22
	/** @test */
23
	public function testSum()
24
	{
25
		$this->assertEquals(10, IterUtil::sum(array(1, 2, 3), 4));
26
	}
27
28
	/** @test */
29
	public function testAsIterator()
30
	{
31
		$this->assertTrue(IterUtil::asIterator(array()) instanceof ArrayIterator);
32
		$this->assertTrue(IterUtil::asIterator(new RangeIterator(1, 2)) instanceof RangeIterator);
33
		$this->assertTrue(IterUtil::asIterator(new Queue()) instanceof ArrayAccessIterator);
34
		$this->assertTrue(IterUtil::asIterator(new SimpleXMLElement('<root/>')) instanceof IteratorIterator);
35
	}
36
37
	/** @test */
38
	public function testAsTraversable()
39
	{
40
		$this->assertTrue(IterUtil::asTraversable(array()) instanceof ArrayIterator);
41
		$this->assertTrue(IterUtil::asTraversable(new RangeIterator(1, 2)) instanceof RangeIterator);
42
		$this->assertTrue(IterUtil::asTraversable(new Queue()) instanceof Queue);
43
		$this->assertTrue(IterUtil::asTraversable(new SimpleXMLElement('<root/>')) instanceof SimpleXMLElement);
44
	}
45
46
	/**
47
	 * @test
48
	 * @expectedException InvalidArgumentException
49
	 */
50
	public function testAsTraversableWithNonTraversableAsArgument()
51
	{
52
		IterUtil::asTraversable(1);
53
	}
54
55
	/** @test */
56
	public function testIsCollection()
57
	{
58
		$this->assertTrue(IterUtil::isCollection(array()));
59
		$this->assertTrue(IterUtil::isCollection(new RangeIterator(1, 2)));
60
		$this->assertTrue(IterUtil::isCollection(new Queue()));
61
		$this->assertTrue(IterUtil::isCollection(new SimpleXMLElement('<root/>')));
62
63
		$this->assertFalse(IterUtil::isCollection(1));
64
	}
65
66
	/** @test */
67
	public function testAssertIsCollectionForCollections()
68
	{
69
		IterUtil::assertIsCollection(array());
70
		IterUtil::assertIsCollection(new RangeIterator(1, 2));
71
		IterUtil::assertIsCollection(new Queue());
72
		IterUtil::assertIsCollection(new SimpleXMLElement('<root/>'));
73
	}
74
75
	/**
76
	 * @test
77
	 * @expectedException UnexpectedValueException
78
	 */
79
	public function testAssertIsCollectionForNonCollections()
80
	{
81
		IterUtil::assertIsCollection(1);
82
	}
83
84
	public function testIteratorReduce()
85
	{
86
		$rangeIterator = new RangeIterator(1, 2);
87
		$genericObject1 = new stdClass();
88
		$genericObject1->test = 5;
89
		$genericObject2 = new stdClass();
90
		$genericObject2->test = 10;
91
		$arrayIterator = new ArrayIterator(array($genericObject1, $genericObject2));
92
93
		$this->assertEquals(3, IterUtil::iterator_reduce($rangeIterator, function(&$carry, $element) { return $carry += $element;}));
94
		$this->assertEquals(5, IterUtil::iterator_reduce($rangeIterator, function(&$carry, $element) { return $carry += $element;}, 2));
95
96
		$this->assertEquals(15, IterUtil::iterator_reduce($arrayIterator, function(&$carry, $element) { return $carry += $element->test;}));
97
		$this->assertEquals(19, IterUtil::iterator_reduce($arrayIterator, function(&$carry, $element) { return $carry += $element->test;}, 4));
98
	}
99
100
	/** @test */
101
	public function testGetCurrentAndAdvanceForArray()
102
	{
103
		$a = range(0, 5);
104
		next($a);
105
		$this->assertEquals(1, current($a));
106
		$c = IterUtil::getCurrentAndAdvance($a);
107
		$this->assertEquals(1, $c);
108
		$this->assertEquals(2, current($a));
109
	}
110
111
	/**
112
	 * @test
113
	 * @expectedException UnexpectedValueException
114
	 * @expectedExceptionMessage Error while advancing iterable
115
	 **/
116
	public function testGetCurrentAndAdvanceForArrayShouldThrowExceptionOnEndOfRange()
117
	{
118
		$a = range(0, 1);
119
		next($a);
120
		$this->assertEquals(1, current($a));
121
		next($a);
122
		IterUtil::getCurrentAndAdvance($a);
123
	}
124
125
	/** @test */
126
	public function testGetCurrentAndAdvanceForArrayShouldNotThrowErrorWHenProvidedWithDefault()
127
	{
128
		$a = range(0, 1);
129
		next($a);
130
		$this->assertEquals(1, current($a));
131
		next($a);
132
		$c = IterUtil::getCurrentAndAdvance($a, array('default' => 'default-value'));
133
		$this->assertEquals($c, 'default-value');
134
	}
135
136
	/** @test */
137
	public function testGetCurrentAndAdvanceForIterator()
138
	{
139
		$a = new ArrayIterator(range(0, 5));
140
		$a->rewind();
141
		$a->next();
142
		$this->assertEquals(1, $a->current());
143
		$c = IterUtil::getCurrentAndAdvance($a);
144
		$this->assertEquals(1, $c);
145
		$this->assertEquals(2, $a->current());
146
	}
147
148
	/**
149
	 * @test
150
	 * @expectedException UnexpectedValueException
151
	 * @expectedExceptionMessage Error while advancing iterable
152
	 **/
153
	public function testGetCurrentAndAdvanceForIteratorShouldThrowExceptionOnEndOfRange()
154
	{
155
		$a = new ArrayIterator(range(0, 1));
156
		$a->rewind();
157
		$a->next();
158
		$this->assertEquals(1, $a->current());
159
		$a->next();
160
		IterUtil::getCurrentAndAdvance($a);
161
	}
162
163
	/** @test */
164
	public function testGetCurrentAndAdvanceForIteratorShouldNotThrowErrorWHenProvidedWithDefault()
165
	{
166
		$a = new ArrayIterator(range(0, 1));
167
		$a->rewind();
168
		$a->next();
169
		$this->assertEquals(1, $a->current());
170
		$a->next();
171
		$c = IterUtil::getCurrentAndAdvance($a, array('default' => 'default-value'));
172
		$this->assertEquals($c, 'default-value');
173
	}
174
175
	/** @test */
176
	public function testResursiveIteratorToArray()
177
	{
178
		$iterator = new ArrayIterator(array(new ArrayIterator(range(0, 2)), new ArrayIterator(range(0, 2))));
179
		$expectedResult = array(range(0, 2), range(0, 2));
180
		$this->assertEquals($expectedResult, IterUtil::recursive_iterator_to_array($iterator));
181
	}
182
183
	/** @test */
184
	public function testAll()
185
	{
186
		$testValue = IterUtil::all(array(true, true, true));
187
		$this->assertTrue($testValue);
188
189
		$testValue = IterUtil::all(array(true, true, false));
190
		$this->assertFalse($testValue);
191
192
		$testValue = IterUtil::all(new ArrayIterator(array(5, 5, 5)), function($v) { return $v == 5; });
193
		$this->assertTrue($testValue);
194
195
		$testValue = IterUtil::all(new ArrayIterator(array(5, 5, 6)), function($v) { return $v == 5; });
196
		$this->assertFalse($testValue);
197
198
		$testValue = IterUtil::all(array());
199
		$this->assertTrue($testValue);
200
	}
201
202
	/** @test */
203
	public function testAny()
204
	{
205
		$testValue = IterUtil::any(array(false, false, false));
206
		$this->assertFalse($testValue);
207
208
		$testValue = IterUtil::any(array(true, false, false));
209
		$this->assertTrue($testValue);
210
211
		$testValue = IterUtil::any(new ArrayIterator(array(6, 6, 6)), function($v) { return $v == 5; });
212
		$this->assertFalse($testValue);
213
214
		$testValue = IterUtil::any(new ArrayIterator(array(5, 6, 6)), function($v) { return $v == 5; });
215
		$this->assertTrue($testValue);
216
217
		$testValue = IterUtil::any(array());
218
		$this->assertFalse($testValue);
219
	}
220
221
	/**
222
	 * @test
223
	 * @expectedException InvalidArgumentException
224
	 **/
225
	public function testAnyShouldThrowExceptionIfSuppliedWithInvalidCallable()
226
	{
227
		IterUtil::any(array(), 66);
228
	}
229
230
	/**
231
	 * @test
232
	 * @expectedException InvalidArgumentException
233
	 **/
234
	public function testAllShouldThrowExceptionIfSuppliedWithInvalidCallable()
235
	{
236
		IterUtil::all(array(), 66);
237
	}
238
}
239
240