1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Axstrad library. |
4
|
|
|
* |
5
|
|
|
* (c) Dan Kempster <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Axstrad\Component\Test; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Axstrad\Component\Test\MockedIteratorTest |
15
|
|
|
* |
16
|
|
|
* Tests for _mock_ Iterator |
17
|
|
|
* |
18
|
|
|
* Here we have added a helper method (which you could add to your own abstract |
19
|
|
|
* base test class) that will help us mock iterators quickly and easily. |
20
|
|
|
* |
21
|
|
|
* @author Dave Gardner <[email protected]> |
22
|
|
|
* @author Dan Kempster <[email protected]> |
23
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
24
|
|
|
* @package Axstrad/Test |
25
|
|
|
* @subpackage Iterator |
26
|
|
|
*/ |
27
|
|
|
class MockedIteratorTest extends \PHPUnit_Framework_TestCase |
28
|
|
|
{ |
29
|
|
|
protected function mockGenericIteratableList() |
30
|
|
|
{ |
31
|
|
|
return $this->getMock('Axstrad\Component\Test\Example\ExampleList'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Mock iterator |
36
|
|
|
* |
37
|
|
|
* This attaches all the required expectations in the right order so that |
38
|
|
|
* our iterator will act like an iterator! |
39
|
|
|
* |
40
|
|
|
* @param \Iterator $iterator The iterator object; this is what we attach |
41
|
|
|
* all the expectations to |
42
|
|
|
* @param array An array of items that we will mock up, we will use the |
43
|
|
|
* keys (if needed) and values of this array to return |
44
|
|
|
* @param boolean $includeCallsToKey Whether we want to mock up the calls |
45
|
|
|
* to "key"; only needed if you are doing foreach ($foo as $k => $v) |
46
|
|
|
* as opposed to foreach ($foo as $v) |
47
|
|
|
*/ |
48
|
|
|
protected function mockIterator( |
49
|
|
|
\Iterator $iterator, |
50
|
|
|
array $items, |
51
|
|
|
$includeCallsToKey = FALSE |
52
|
|
|
) { |
53
|
|
|
$iterator |
54
|
|
|
->expects( |
55
|
|
|
$this->at(0) |
56
|
|
|
) |
57
|
|
|
->method('rewind') |
58
|
|
|
; |
59
|
|
|
$counter = 1; |
60
|
|
|
|
61
|
|
|
foreach ($items as $k => $v) { |
62
|
|
|
$iterator |
63
|
|
|
->expects( |
64
|
|
|
$this->at($counter++) |
65
|
|
|
) |
66
|
|
|
->method('valid') |
67
|
|
|
->will( |
68
|
|
|
$this->returnValue(TRUE) |
69
|
|
|
) |
70
|
|
|
; |
71
|
|
|
$iterator |
72
|
|
|
->expects( |
73
|
|
|
$this->at($counter++) |
74
|
|
|
) |
75
|
|
|
->method('current') |
76
|
|
|
->will( |
77
|
|
|
$this->returnValue($v) |
78
|
|
|
) |
79
|
|
|
; |
80
|
|
|
if ($includeCallsToKey) { |
81
|
|
|
$iterator |
82
|
|
|
->expects( |
83
|
|
|
$this->at($counter++) |
84
|
|
|
) |
85
|
|
|
->method('key') |
86
|
|
|
->will( |
87
|
|
|
$this->returnValue($k) |
88
|
|
|
) |
89
|
|
|
; |
90
|
|
|
} |
91
|
|
|
$iterator |
92
|
|
|
->expects( |
93
|
|
|
$this->at($counter++) |
94
|
|
|
) |
95
|
|
|
->method('next') |
96
|
|
|
; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$iterator |
100
|
|
|
->expects( |
101
|
|
|
$this->at($counter) |
102
|
|
|
) |
103
|
|
|
->method('valid') |
104
|
|
|
->will( |
105
|
|
|
$this->returnValue(FALSE) |
106
|
|
|
) |
107
|
|
|
; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testConstructs() |
111
|
|
|
{ |
112
|
|
|
$list = $this->mockGenericIteratableList(); |
113
|
|
|
$this->assertInstanceOf('Axstrad\Component\Test\Example\ExampleList', $list); |
114
|
|
|
$this->assertInstanceOf('Iterator', $list); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testHasZeroItemsWhenWeHaventMockedMethods() |
118
|
|
|
{ |
119
|
|
|
$list = $this->mockGenericIteratableList(); |
120
|
|
|
$counter = 0; |
121
|
|
|
foreach ($list as $item) { |
122
|
|
|
// @codeCoverageIgnoreStart |
123
|
|
|
$counter++; |
124
|
|
|
// @codeCoverageIgnoreEnd |
125
|
|
|
} |
126
|
|
|
$this->assertEquals(0, $counter); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testWhenMockOneIterationWithNoKey() |
130
|
|
|
{ |
131
|
|
|
$list = $this->mockGenericIteratableList(); |
132
|
|
|
|
133
|
|
|
$this->mockIterator($list, array('This is the first item')); |
134
|
|
|
|
135
|
|
|
$counter = 0; |
136
|
|
|
foreach ($list as $value) { |
137
|
|
|
$counter++; |
138
|
|
|
} |
139
|
|
|
$this->assertEquals(1, $counter); |
140
|
|
|
$this->assertEquals('This is the first item', $value); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testWhenMockOneIterationWithKey() |
144
|
|
|
{ |
145
|
|
|
$list = $this->mockGenericIteratableList(); |
146
|
|
|
|
147
|
|
|
$this->mockIterator($list, array('key1' => 'This is the first item'), TRUE); |
148
|
|
|
|
149
|
|
|
$counter = 0; |
150
|
|
|
foreach ($list as $key => $value) { |
151
|
|
|
$counter++; |
152
|
|
|
} |
153
|
|
|
$this->assertEquals(1, $counter); |
154
|
|
|
$this->assertEquals('key1', $key); |
|
|
|
|
155
|
|
|
$this->assertEquals('This is the first item', $value); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testWhenMockThreeIterationWithNoKey() |
159
|
|
|
{ |
160
|
|
|
$list = $this->mockGenericIteratableList(); |
161
|
|
|
|
162
|
|
|
$expectedValues = array( |
163
|
|
|
'This is the first item', |
164
|
|
|
'This is the second item', |
165
|
|
|
'And the final item' |
166
|
|
|
); |
167
|
|
|
$this->mockIterator($list, $expectedValues); |
168
|
|
|
|
169
|
|
|
$counter = 0; |
170
|
|
|
$values = array(); |
171
|
|
|
foreach ($list as $value) { |
172
|
|
|
$values[] = $value; |
173
|
|
|
$counter++; |
174
|
|
|
} |
175
|
|
|
$this->assertEquals(3, $counter); |
176
|
|
|
|
177
|
|
|
$this->assertEquals($expectedValues, $values); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function testWhenMockThreeIterationWithKey() |
181
|
|
|
{ |
182
|
|
|
$list = $this->mockGenericIteratableList(); |
183
|
|
|
|
184
|
|
|
$expectedValues = array( |
185
|
|
|
'item1' => 'This is the first item', |
186
|
|
|
'foo' => 'This is the second item!!', |
187
|
|
|
'bar' => 'And the final item' |
188
|
|
|
); |
189
|
|
|
$this->mockIterator($list, $expectedValues, TRUE); |
190
|
|
|
|
191
|
|
|
$counter = 0; |
192
|
|
|
$values = array(); |
193
|
|
|
foreach ($list as $key => $value) { |
194
|
|
|
$values[$key] = $value; |
195
|
|
|
$counter++; |
196
|
|
|
} |
197
|
|
|
$this->assertEquals(3, $counter); |
198
|
|
|
|
199
|
|
|
$this->assertEquals($expectedValues, $values); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
It seems like you are relying on a variable being defined by an iteration: