|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File IterableTraitTest.php |
|
4
|
|
|
* |
|
5
|
|
|
* @author Edward Pfremmer <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Epfremme\Tests\Collection\Traits; |
|
8
|
|
|
|
|
9
|
|
|
use Iterator; |
|
10
|
|
|
use Epfremme\Collection\BaseCollection; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class IterableTraitTest |
|
14
|
|
|
* |
|
15
|
|
|
* @package Epfremme\Tests\Collection\Traits |
|
16
|
|
|
*/ |
|
17
|
|
|
class IterableTraitTest extends \PHPUnit_Framework_TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var BaseCollection |
|
21
|
|
|
*/ |
|
22
|
|
|
private $collection; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
public function setUp() |
|
28
|
|
|
{ |
|
29
|
|
|
parent::setUp(); |
|
30
|
|
|
|
|
31
|
|
|
$this->collection = new BaseCollection([1, 2, 3, 'a' => 4, 'b' => 5]); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Verify collection implements ArrayAccess interface |
|
36
|
|
|
* |
|
37
|
|
|
* @return void |
|
38
|
|
|
*/ |
|
39
|
|
|
public function testImplementsInterface() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->assertInstanceOf(Iterator::class, $this->collection); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Test collection key position |
|
46
|
|
|
* |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function testKey() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->collection->seek(2); |
|
52
|
|
|
$this->assertEquals(2, $this->collection->key()); |
|
53
|
|
|
|
|
54
|
|
|
$this->collection->seek('a'); |
|
55
|
|
|
$this->assertEquals('a', $this->collection->key()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Test collection current position |
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
public function testCurrent() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->collection->seek(2); |
|
66
|
|
|
$this->assertEquals(3, $this->collection->current()); |
|
67
|
|
|
|
|
68
|
|
|
$this->collection->seek('a'); |
|
69
|
|
|
$this->assertEquals(4, $this->collection->current()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Test collection first position |
|
74
|
|
|
* |
|
75
|
|
|
* @depends testKey |
|
76
|
|
|
* @depends testCurrent |
|
77
|
|
|
* @return void |
|
78
|
|
|
*/ |
|
79
|
|
|
public function testFirst() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->collection->seek(2); |
|
82
|
|
|
$this->collection->first(); |
|
83
|
|
|
$this->assertEquals(0, $this->collection->key()); |
|
84
|
|
|
$this->assertEquals(1, $this->collection->current()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Test collection last position |
|
89
|
|
|
* |
|
90
|
|
|
* @depends testKey |
|
91
|
|
|
* @depends testCurrent |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
public function testLast() |
|
95
|
|
|
{ |
|
96
|
|
|
$this->collection->last(); |
|
97
|
|
|
$this->assertEquals('b', $this->collection->key()); |
|
98
|
|
|
$this->assertEquals(5, $this->collection->current()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Test collection next position |
|
103
|
|
|
* |
|
104
|
|
|
* @depends testKey |
|
105
|
|
|
* @depends testCurrent |
|
106
|
|
|
* @return void |
|
107
|
|
|
*/ |
|
108
|
|
|
public function testNext() |
|
109
|
|
|
{ |
|
110
|
|
|
$this->collection->next(); |
|
111
|
|
|
$this->assertEquals(1, $this->collection->key()); |
|
112
|
|
|
$this->assertEquals(2, $this->collection->current()); |
|
113
|
|
|
|
|
114
|
|
|
$this->collection->last(); |
|
115
|
|
|
$this->collection->next(); |
|
116
|
|
|
$this->assertNull($this->collection->key()); |
|
117
|
|
|
$this->assertFalse($this->collection->current()); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Test collection previous position |
|
122
|
|
|
* |
|
123
|
|
|
* @depends testKey |
|
124
|
|
|
* @depends testCurrent |
|
125
|
|
|
* @depends testNext |
|
126
|
|
|
* @return void |
|
127
|
|
|
*/ |
|
128
|
|
|
public function testPrev() |
|
129
|
|
|
{ |
|
130
|
|
|
$this->collection->next(); |
|
131
|
|
|
$this->collection->prev(); |
|
132
|
|
|
$this->assertEquals(0, $this->collection->key()); |
|
133
|
|
|
$this->assertEquals(1, $this->collection->current()); |
|
134
|
|
|
|
|
135
|
|
|
$this->collection->prev(); |
|
136
|
|
|
$this->assertNull($this->collection->key()); |
|
137
|
|
|
$this->assertFalse($this->collection->current()); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Test collection valid |
|
142
|
|
|
* |
|
143
|
|
|
* @depends testNext |
|
144
|
|
|
* @depends testLast |
|
145
|
|
|
* @return void |
|
146
|
|
|
*/ |
|
147
|
|
|
public function testValid() |
|
148
|
|
|
{ |
|
149
|
|
|
$this->assertTrue($this->collection->valid()); |
|
150
|
|
|
|
|
151
|
|
|
$this->collection->last(); |
|
152
|
|
|
$this->assertTrue($this->collection->valid()); |
|
153
|
|
|
|
|
154
|
|
|
$this->collection->next(); |
|
155
|
|
|
$this->assertFalse($this->collection->valid()); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Test collection rewind |
|
160
|
|
|
* |
|
161
|
|
|
* @depends testKey |
|
162
|
|
|
* @depends testCurrent |
|
163
|
|
|
* @return void |
|
164
|
|
|
*/ |
|
165
|
|
|
public function testRewind() |
|
166
|
|
|
{ |
|
167
|
|
|
$this->collection->last(); |
|
168
|
|
|
$this->assertEquals('b', $this->collection->key()); |
|
169
|
|
|
$this->assertEquals(5, $this->collection->current()); |
|
170
|
|
|
|
|
171
|
|
|
$this->collection->rewind(); |
|
172
|
|
|
$this->assertEquals(0, $this->collection->key()); |
|
173
|
|
|
$this->assertEquals(1, $this->collection->current()); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|