1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Enzyme\Loopy\Each; |
4
|
|
|
use Mockery as m; |
5
|
|
|
|
6
|
|
|
class EachTest extends PHPUnit_Framework_TestCase |
7
|
|
|
{ |
8
|
|
|
public function tearDown() |
9
|
|
|
{ |
10
|
|
|
m::close(); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
public function testStandardShallowBegin() |
14
|
|
|
{ |
15
|
|
|
$expected = 1; |
16
|
|
|
$array = [1, 2, 3]; |
17
|
|
|
|
18
|
|
|
Each::shallow()->begin($array, function($bag) use(&$expected) { |
19
|
|
|
$actual = $bag->value(); |
20
|
|
|
$this->assertEquals($expected, $actual); |
21
|
|
|
|
22
|
|
|
$expected++; |
23
|
|
|
}); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testFilterIsExecuted() |
27
|
|
|
{ |
28
|
|
|
$expected = 1; |
29
|
|
|
$array = [1, 2, 3]; |
30
|
|
|
$filter = m::mock('Enzyme\Loopy\Filters\FilterInterface', function($mock) { |
31
|
|
|
$mock->shouldReceive('passes')->times(3)->andReturn(true); |
32
|
|
|
}); |
33
|
|
|
|
34
|
|
|
Each::shallow($filter)->begin($array, function($bag) use(&$expected) { |
35
|
|
|
$actual = $bag->value(); |
36
|
|
|
$this->assertEquals($expected, $actual); |
37
|
|
|
|
38
|
|
|
$expected++; |
39
|
|
|
}); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @expectedException \Enzyme\Loopy\InvalidLoopException |
44
|
|
|
*/ |
45
|
|
|
public function testNonEnumerableArgument() |
46
|
|
|
{ |
47
|
|
|
$expected = 1; |
48
|
|
|
$array = 'foobar'; |
49
|
|
|
|
50
|
|
|
Each::shallow()->begin($array, function($bag) use(&$expected) { |
51
|
|
|
// Should throw an exception before getting here. |
52
|
|
|
}); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testStandardShallowBeginMultipleCycles() |
56
|
|
|
{ |
57
|
|
|
$expected = 1; |
58
|
|
|
$cycles = 2; |
59
|
|
|
$cur_cycle = 0; |
60
|
|
|
$array = [1, 2, 3]; |
61
|
|
|
|
62
|
|
View Code Duplication |
Each::shallow()->begin($array, function($bag) use(&$expected, &$cur_cycle, $array) { |
63
|
|
|
$actual = $bag->value(); |
64
|
|
|
$this->assertEquals($expected, $actual); |
65
|
|
|
|
66
|
|
|
$expected++; |
67
|
|
|
|
68
|
|
|
$actual = $bag->cycle(); |
69
|
|
|
$this->assertEquals($cur_cycle, $actual); |
70
|
|
|
|
71
|
|
|
if ($expected > $array[count($array) - 1]) { |
72
|
|
|
$expected = 1; |
73
|
|
|
$cur_cycle++; |
74
|
|
|
} |
75
|
|
|
}, $cycles); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testStandardDeepBegin() |
79
|
|
|
{ |
80
|
|
|
$expected = 1; |
81
|
|
|
$array = [1, 2, 3, 4 => [4, 5, 6]]; |
82
|
|
|
|
83
|
|
|
Each::deep()->begin($array, function($bag) use(&$expected) { |
84
|
|
|
$actual = $bag->value(); |
85
|
|
|
$this->assertEquals($expected, $actual); |
86
|
|
|
|
87
|
|
|
$expected++; |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testStandardDeepBeginMultipleCycles() |
92
|
|
|
{ |
93
|
|
|
$expected = 1; |
94
|
|
|
$cycles = 2; |
95
|
|
|
$cur_cycle = 0; |
96
|
|
|
$array = [1, 2, 3, 4 => [4, 5, 6]]; |
97
|
|
|
|
98
|
|
View Code Duplication |
Each::deep()->begin($array, function($bag) use(&$expected, &$cur_cycle, $array) { |
99
|
|
|
$actual = $bag->value(); |
100
|
|
|
$this->assertEquals($expected, $actual); |
101
|
|
|
|
102
|
|
|
$expected++; |
103
|
|
|
|
104
|
|
|
$actual = $bag->cycle(); |
105
|
|
|
$this->assertEquals($cur_cycle, $actual); |
106
|
|
|
|
107
|
|
|
// Here the number six refers to the last item in the |
108
|
|
|
// array [1, 2, 3, 4 => [4, 5, 6]] if it were flattened. |
|
|
|
|
109
|
|
|
if ($expected > 6) { |
110
|
|
|
$expected = 1; |
111
|
|
|
$cur_cycle++; |
112
|
|
|
} |
113
|
|
|
}, $cycles); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testExtraDeepBeginMultipleCyclesAndFollowIndex() |
117
|
|
|
{ |
118
|
|
|
$expected = 1; |
119
|
|
|
$cycles = 5; |
120
|
|
|
$cur_cycle = 0; |
121
|
|
|
$array = [1, 2, 3, 4 => [4, 5, 6 => [6, 7, 8, 9]]]; |
122
|
|
|
$index = 0; |
123
|
|
|
|
124
|
|
|
Each::deep()->begin($array, function($bag) use(&$expected, &$cur_cycle, &$index, $array) { |
125
|
|
|
$actual = $bag->value(); |
126
|
|
|
$this->assertEquals($expected, $actual); |
127
|
|
|
|
128
|
|
|
$expected++; |
129
|
|
|
|
130
|
|
|
$actual = $bag->cycle(); |
131
|
|
|
$this->assertEquals($cur_cycle, $actual); |
132
|
|
|
|
133
|
|
|
$actual = $bag->index(); |
134
|
|
|
$this->assertEquals($index, $actual); |
135
|
|
|
$index++; |
136
|
|
|
|
137
|
|
|
// Here the number six refers to the last item in the |
138
|
|
|
// array [1, 2, 3, 4 => [4, 5, 6 => [6, 7, 8, 9]]] if it were flattened. |
|
|
|
|
139
|
|
|
if ($expected > 9) { |
140
|
|
|
$expected = 1; |
141
|
|
|
$cur_cycle++; |
142
|
|
|
} |
143
|
|
|
}, $cycles); |
144
|
|
|
} |
145
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.