These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace unit; |
||
3 | |||
4 | // @codingStandardsIgnoreFile |
||
5 | // We do not want NitPick CI to report results about this file, |
||
6 | // as we have a couple of private test classes that appear in this file |
||
7 | // rather than in their own file. |
||
8 | |||
9 | use Robo\Robo; |
||
10 | use Robo\Result; |
||
11 | use Robo\State\Data; |
||
12 | use Robo\Task\BaseTask; |
||
13 | use Robo\Collection\Collection; |
||
14 | use Robo\Task\ValueProviderTask; |
||
15 | use Robo\Task\CollectionTestTask; |
||
16 | use Robo\Task\CountingTask; |
||
17 | |||
18 | class CollectionTest extends \Codeception\TestCase\Test |
||
19 | { |
||
20 | /** |
||
21 | * @var \CodeGuy |
||
22 | */ |
||
23 | protected $guy; |
||
24 | |||
25 | public function testAfterFilters() |
||
26 | { |
||
27 | $collection = new Collection(); |
||
28 | $collection->setLogger(Robo::logger()); |
||
29 | |||
30 | $taskA = new CollectionTestTask('a', 'value-a'); |
||
31 | $taskB = new CollectionTestTask('b', 'value-b'); |
||
32 | |||
33 | $collection |
||
34 | ->add($taskA, 'a-name') |
||
35 | ->add($taskB, 'b-name'); |
||
36 | |||
37 | // We add methods of our task instances as before and |
||
38 | // after tasks. These methods have access to the task |
||
39 | // class' fields, and may modify them as needed. |
||
40 | $collection |
||
41 | ->after('a-name', [$taskA, 'parenthesizer']) |
||
42 | ->after('a-name', [$taskA, 'emphasizer']) |
||
43 | ->after('b-name', [$taskB, 'emphasizer']) |
||
44 | ->after('b-name', [$taskB, 'parenthesizer']) |
||
45 | ->after('b-name', [$taskB, 'parenthesizer'], 'special-name'); |
||
46 | |||
47 | $result = $collection->run(); |
||
48 | |||
49 | // verify(var_export($result->getData(), true))->equals(''); |
||
50 | |||
51 | // Ensure that the results have the correct key values |
||
52 | verify(implode(',', array_keys($result->getData())))->equals('a-name,b-name,special-name,time'); |
||
53 | |||
54 | // Verify that all of the after tasks ran in |
||
55 | // the correct order. |
||
56 | verify($result['a-name']['a'])->equals('*(value-a)*'); |
||
57 | verify($result['b-name']['b'])->equals('(*value-b*)'); |
||
58 | |||
59 | // Note that the last after task is added with a special name; |
||
60 | // its results therefore show up under the name given, rather |
||
61 | // than being stored under the name of the task it was added after. |
||
62 | verify($result['special-name']['b'])->equals('((*value-b*))'); |
||
63 | } |
||
64 | |||
65 | public function testBeforeFilters() |
||
66 | { |
||
67 | $collection = new Collection(); |
||
68 | $collection->setLogger(Robo::logger()); |
||
69 | |||
70 | $taskA = new CollectionTestTask('a', 'value-a'); |
||
71 | $taskB = new CollectionTestTask('b', 'value-b'); |
||
72 | |||
73 | $collection |
||
74 | ->add($taskA, 'a-name') |
||
75 | ->add($taskB, 'b-name'); |
||
76 | |||
77 | // We add methods of our task instances as before and |
||
78 | // after tasks. These methods have access to the task |
||
79 | // class' fields, and may modify them as needed. |
||
80 | $collection |
||
81 | ->before('b-name', [$taskA, 'parenthesizer']) |
||
82 | ->before('b-name', [$taskA, 'emphasizer'], 'special-before-name'); |
||
83 | |||
84 | $result = $collection->run(); |
||
85 | |||
86 | // Ensure that the results have the correct key values |
||
87 | verify(implode(',', array_keys($result->getData())))->equals('a-name,b-name,special-before-name,time'); |
||
88 | |||
89 | // The result from the 'before' task is attached |
||
90 | // to 'b-name', since it was called as before('b-name', ...) |
||
91 | verify($result['b-name']['a'])->equals('(value-a)'); |
||
92 | // When a 'before' task is given its own name, then |
||
93 | // its results are attached under that name. |
||
94 | verify($result['special-before-name']['a'])->equals('*(value-a)*'); |
||
95 | } |
||
96 | |||
97 | public function testAddCodeRollbackAndCompletion() |
||
98 | { |
||
99 | $collection = new Collection(); |
||
100 | $collection->setLogger(Robo::logger()); |
||
101 | |||
102 | $rollback1 = new CountingTask(); |
||
103 | $rollback2 = new CountingTask(); |
||
104 | $completion1 = new CountingTask(); |
||
105 | $completion2 = new CountingTask(); |
||
106 | |||
107 | $collection |
||
108 | ->progressMessage("start collection tasks") |
||
109 | ->rollback($rollback1) |
||
110 | ->completion($completion1) |
||
111 | ->rollbackCode(function() use($rollback1) { $rollback1->run(); } ) |
||
112 | ->completionCode(function() use($completion1) { $completion1->run(); } ) |
||
113 | ->addCode(function () { return 42; }) |
||
114 | ->progressMessage("not reached") |
||
115 | ->rollback($rollback2) |
||
116 | ->completion($completion2) |
||
117 | ->addCode(function () { return 13; }); |
||
118 | |||
119 | $collection->setLogger($this->guy->logger()); |
||
120 | |||
121 | $result = $collection->run(); |
||
122 | // Execution stops on the first error. |
||
123 | // Confirm that status code is converted to a Result object. |
||
124 | verify($result->getExitCode())->equals(42); |
||
125 | verify($rollback1->getCount())->equals(2); |
||
126 | verify($rollback2->getCount())->equals(0); |
||
127 | verify($completion1->getCount())->equals(2); |
||
128 | verify($completion2->getCount())->equals(0); |
||
129 | $this->guy->seeInOutput('start collection tasks'); |
||
130 | $this->guy->doNotSeeInOutput('not reached'); |
||
131 | } |
||
132 | |||
133 | public function testStateWithAddCode() |
||
134 | { |
||
135 | $collection = new Collection(); |
||
136 | $collection->setLogger(Robo::logger()); |
||
137 | |||
138 | $result = $collection |
||
0 ignored issues
–
show
|
|||
139 | ->addCode( |
||
140 | function (Data $state) { |
||
141 | $state['one'] = 'first'; |
||
142 | }) |
||
143 | ->addCode( |
||
144 | function (Data $state) { |
||
145 | $state['two'] = 'second'; |
||
146 | }) |
||
147 | ->addCode( |
||
148 | function (Data $state) { |
||
149 | $state['three'] = "{$state['one']} and {$state['two']}"; |
||
150 | }) |
||
151 | ->run(); |
||
152 | |||
153 | $state = $collection->getState(); |
||
154 | verify($state['three'])->equals('first and second'); |
||
155 | } |
||
156 | |||
157 | public function testStateWithTaskResult() |
||
158 | { |
||
159 | $collection = new Collection(); |
||
160 | $collection->setLogger(Robo::logger()); |
||
161 | |||
162 | $first = new ValueProviderTask(); |
||
163 | $first->provideData('one', 'First'); |
||
164 | |||
165 | $second = new ValueProviderTask(); |
||
166 | $second->provideData('two', 'Second'); |
||
167 | |||
168 | $result = $collection |
||
0 ignored issues
–
show
$result is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
169 | ->add($first) |
||
170 | ->add($second) |
||
171 | ->addCode( |
||
172 | function (Data $state) { |
||
173 | $state['three'] = "{$state['one']} and {$state['two']}"; |
||
174 | }) |
||
175 | ->run(); |
||
176 | |||
177 | $state = $collection->getState(); |
||
178 | verify($state['one'])->equals('First'); |
||
179 | verify($state['three'])->equals('First and Second'); |
||
180 | } |
||
181 | |||
182 | public function testDeferredInitialization() |
||
183 | { |
||
184 | $collection = new Collection(); |
||
185 | $collection->setLogger(Robo::logger()); |
||
186 | |||
187 | $first = new ValueProviderTask(); |
||
188 | $first->provideData('one', 'First'); |
||
189 | |||
190 | $second = new ValueProviderTask(); |
||
191 | $second->provideData('two', 'Second'); |
||
192 | |||
193 | $third = new ValueProviderTask(); |
||
194 | |||
195 | $result = $collection |
||
0 ignored issues
–
show
$result is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
196 | ->add($first) |
||
197 | ->add($second) |
||
198 | ->add($third) |
||
199 | ->defer( |
||
200 | $third, |
||
201 | function ($task, $state) { |
||
202 | $task->provideData('three', "{$state['one']} and {$state['two']}"); |
||
203 | } |
||
204 | ) |
||
205 | ->run(); |
||
206 | |||
207 | $state = $collection->getState(); |
||
208 | verify($state['one'])->equals('First'); |
||
209 | verify($state['three'])->equals('First and Second'); |
||
210 | } |
||
211 | |||
212 | public function testDeferredInitializationWithMessageStorage() |
||
213 | { |
||
214 | $collection = new Collection(); |
||
215 | $collection->setLogger(Robo::logger()); |
||
216 | |||
217 | $first = new ValueProviderTask(); |
||
218 | $first->provideMessage('1st'); |
||
219 | |||
220 | $second = new ValueProviderTask(); |
||
221 | $second->provideData('other', '2nd'); |
||
222 | |||
223 | $third = new ValueProviderTask(); |
||
224 | |||
225 | $result = $collection |
||
0 ignored issues
–
show
$result is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
226 | ->add($first) |
||
227 | ->storeState($first, 'one') |
||
228 | ->add($second) |
||
229 | ->storeState($second, 'two', 'other') |
||
230 | ->add($third) |
||
231 | ->defer( |
||
232 | $third, |
||
233 | function ($task, $state) { |
||
234 | $task->provideData('three', "{$state['one']} and {$state['two']}"); |
||
235 | } |
||
236 | ) |
||
237 | ->run(); |
||
238 | |||
239 | $state = $collection->getState(); |
||
240 | verify($state['one'])->equals('1st'); |
||
241 | verify($state['three'])->equals('1st and 2nd'); |
||
242 | } |
||
243 | public function testDeferredInitializationWithChainedInitialization() |
||
244 | { |
||
245 | $collection = new Collection(); |
||
246 | $collection->setLogger(Robo::logger()); |
||
247 | |||
248 | // This task sets the Result message to '1st' |
||
249 | $first = new ValueProviderTask(); |
||
250 | $first->provideMessage('1st'); |
||
251 | |||
252 | $second = new ValueProviderTask(); |
||
253 | $second->provideMessage('2nd'); |
||
254 | |||
255 | $third = new ValueProviderTask(); |
||
256 | |||
257 | $result = $collection |
||
0 ignored issues
–
show
$result is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
258 | // $first will set its Result's message to '1st' at `run()` time |
||
259 | ->add($first) |
||
260 | // This will copy the message from $first's result to $state['one'] after $first runs. |
||
261 | // Note that it does not matter what order the `storeState` messages are called in; |
||
262 | // their first parameter determines when they run. This differs from CollectionBuilder, |
||
263 | // which manages order. |
||
264 | ->storeState($first, 'one') |
||
265 | ->add($second) |
||
266 | // This will copy the message from $second's result to $state['two'] |
||
267 | ->storeState($second, 'two') |
||
268 | ->add($third) |
||
269 | ->deferTaskConfiguration($third, 'provideItem', 'one') |
||
270 | ->deferTaskConfiguration($third, 'provideMessage', 'two') |
||
271 | ->storeState($third, 'final') |
||
272 | ->progressMessage('The final result is {final}') |
||
273 | ->run(); |
||
274 | |||
275 | $state = $collection->getState(); |
||
276 | verify($state['one'])->equals('1st'); |
||
277 | verify($state['item'])->equals('1st'); |
||
278 | verify($state['final'])->equals('2nd'); |
||
279 | |||
280 | $this->guy->seeInOutput("The final result is 2nd"); |
||
281 | } |
||
282 | } |
||
283 | |||
284 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.