1 | <?php |
||
18 | class IteratorCollectionTest extends UnitTestCase |
||
19 | { |
||
20 | public function testCurrentReturnsCurrentValue() |
||
21 | { |
||
22 | $coll = Factory::create($this->fixtures['names']); |
||
23 | $this->assertEquals('Chelsea', $coll->current()); |
||
24 | } |
||
25 | |||
26 | public function testNextMovesCollectionInternalPointer() |
||
27 | { |
||
28 | $coll = Factory::create($this->fixtures['names']); |
||
29 | $this->assertEquals( |
||
30 | 'Chelsea', |
||
31 | $coll->current(), |
||
32 | 'Initial call to current() should return first item in collection.' |
||
33 | ); |
||
34 | $this->assertEquals( |
||
35 | 'Chelsea', |
||
36 | $coll->current(), |
||
37 | 'Subsequent calls to current() should continue to return the same value.' |
||
38 | ); |
||
39 | $coll->next(); |
||
40 | $this->assertEquals( |
||
41 | 'Adella', |
||
42 | $coll->current(), |
||
43 | 'After calling next(), current() should return the next item in the collection.' |
||
44 | ); |
||
45 | $coll->next(); |
||
46 | $this->assertEquals( |
||
47 | 'Monte', |
||
48 | $coll->current(), |
||
49 | 'Subsequent calls to next() and current() should return the next item in the collection.' |
||
50 | ); |
||
51 | $coll->next(); |
||
52 | $coll->next(); |
||
53 | $coll->next(); |
||
54 | $coll->next(); |
||
55 | $coll->next(); |
||
56 | $coll->next(); |
||
57 | $coll->next(); |
||
58 | $this->assertEquals('Nakia', $coll->current(), 'We should now be at the last item in the collection.'); |
||
59 | $coll->next(); |
||
60 | $this->assertFalse( |
||
61 | $coll->current(), |
||
62 | "Subsequent calls to current() should return false because we have next'd past the end of the collection." |
||
63 | ); |
||
64 | $coll->next(); |
||
65 | $this->assertFalse( |
||
66 | $coll->current(), |
||
67 | "Subsequent calls to current() should return false because we have next'd past the end of the collection." |
||
68 | ); |
||
69 | } |
||
70 | |||
71 | public function testKeyReturnsCurrentKeyInCollection() |
||
72 | { |
||
73 | $coll = Factory::create($this->fixtures['names']); |
||
74 | $this->assertSame(0, $coll->key(), 'Initial call to key() should return first item in collection.'); |
||
75 | $this->assertSame(0, $coll->key(), 'Subsequent calls to key() should continue to return the same value.'); |
||
76 | $coll->next(); |
||
77 | $this->assertSame( |
||
78 | 1, |
||
79 | $coll->key(), |
||
80 | 'After calling next(), key() should return the next item in the collection.' |
||
81 | ); |
||
82 | $coll->next(); |
||
83 | $this->assertSame( |
||
84 | 2, |
||
85 | $coll->key(), |
||
86 | 'Subsequent calls to next() and key() should return the next item in the collection.' |
||
87 | ); |
||
88 | $coll->next(); |
||
89 | $coll->next(); |
||
90 | $coll->next(); |
||
91 | $coll->next(); |
||
92 | $coll->next(); |
||
93 | $coll->next(); |
||
94 | $coll->next(); |
||
95 | $this->assertSame(9, $coll->key(), 'We should now be at the last item in the collection.'); |
||
96 | $coll->next(); |
||
97 | $this->assertNull( |
||
98 | $coll->key(), |
||
99 | 'Subsequent calls to key() should continue to return null because'. |
||
100 | " we have next'd past the end of the collection." |
||
101 | ); |
||
102 | $coll->next(); |
||
103 | $this->assertNull( |
||
104 | $coll->key(), |
||
105 | 'Subsequent calls to key() should continue to return null because'. |
||
106 | " we have next'd past the end of the collection." |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | public function testValidReturnsFalseWhenCollectionHasBeenIteratedBeyondItsLastItem() |
||
111 | { |
||
112 | $coll = Factory::create($this->fixtures['names']); |
||
113 | $this->assertTrue($coll->valid(), 'Initial call to valid() should always return true.'); |
||
114 | $coll->next(); |
||
115 | $this->assertTrue($coll->valid(), 'Subsequent calls to valid() should continue to return true.'); |
||
116 | $coll->next(); |
||
117 | $this->assertTrue($coll->valid(), 'Subsequent calls to valid() should continue to return true.'); |
||
118 | $coll->next(); |
||
119 | $coll->next(); |
||
120 | $coll->next(); |
||
121 | $coll->next(); |
||
122 | $coll->next(); |
||
123 | $coll->next(); |
||
124 | $coll->next(); |
||
125 | $this->assertTrue($coll->valid(), 'When at the last item in the collection, valid() should still return true.'); |
||
126 | $coll->next(); |
||
127 | $this->assertFalse( |
||
128 | $coll->valid(), |
||
129 | 'Finally, valid() should return false because we have iterated beyond the end of the collection.' |
||
130 | ); |
||
131 | } |
||
132 | |||
133 | public function testRewindWillReturnInternalPointerToItsInitialPosition() |
||
134 | { |
||
135 | $coll = Factory::create($this->fixtures['names']); |
||
136 | $this->assertEquals('Chelsea', $coll->first()); |
||
137 | $this->assertTrue($coll->valid(), 'Initial call to valid() should always return true.'); |
||
138 | $coll->next(); |
||
139 | $this->assertTrue($coll->valid(), 'Subsequent calls to valid() should continue to return true.'); |
||
140 | $coll->next(); |
||
141 | $this->assertTrue($coll->valid(), 'Subsequent calls to valid() should continue to return true.'); |
||
142 | $coll->next(); |
||
143 | $coll->next(); |
||
144 | $coll->next(); |
||
145 | $coll->next(); |
||
146 | $coll->next(); |
||
147 | $coll->next(); |
||
148 | $coll->next(); |
||
149 | $this->assertTrue($coll->valid(), 'When at the last item in the collection, valid() should still return true.'); |
||
150 | $this->assertEquals($coll->last(), $coll->current(), 'Current value should be the last value now.'); |
||
151 | $coll->next(); |
||
152 | $this->assertFalse( |
||
153 | $coll->valid(), |
||
154 | 'Finally, valid() should return false because we have iterated beyond the end of the collection.' |
||
155 | ); |
||
156 | $this->assertNull($coll->rewind(), 'Rewind MUST NOT have a return value.'); |
||
157 | $this->assertTrue( |
||
158 | $coll->valid(), |
||
159 | 'The rewind() method should have returned us to the beginning now so valid() should return true.' |
||
160 | ); |
||
161 | $this->assertEquals( |
||
162 | $coll->first(), |
||
163 | $coll->current(), |
||
164 | 'The rewind() method should have returned us to the beginning now so'. |
||
165 | ' current() should return the first item in the collection.' |
||
166 | ); |
||
167 | |||
168 | $coll->next(); |
||
169 | $coll2 = clone $coll; |
||
170 | $this->assertEquals($coll2->first(), $coll2->current(), 'A clone of a collection should be reset.'); |
||
171 | } |
||
172 | |||
173 | public function testInstantiationShouldRewindArrayArgsInternalPointer() |
||
174 | { |
||
175 | $arr = $this->fixtures['names']; |
||
176 | $this->assertEquals('Chelsea', current($arr)); |
||
177 | next($arr); |
||
178 | $this->assertEquals('Adella', current($arr)); |
||
179 | $coll = Factory::create($arr); |
||
180 | $this->assertEquals('Chelsea', $coll->current()); |
||
181 | } |
||
182 | } |
||
183 |