CollectionTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 297
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 6
dl 0
loc 297
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testStateWithAddCode() 0 25 1
A testStateWithTaskResult() 0 28 1
A testDeferredInitialization() 0 33 1
A testDeferredInitializationWithMessageStorage() 0 35 1
A testDeferredInitializationWithChainedInitialization() 0 45 1
A testAfterFilters() 0 45 1
A testBeforeFilters() 0 37 1
A testAddCodeRollbackAndCompletion() 0 35 1
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
        // Ensure that the results have the correct key values
50
        $this->assertEquals(
51
            'a-name,b-name,special-name,time',
52
            implode(',', array_keys($result->getData())));
53
54
        // Verify that all of the after tasks ran in
55
        // the correct order.
56
        $this->assertEquals(
57
            '*(value-a)*',
58
            $result['a-name']['a']);
59
        $this->assertEquals(
60
            '(*value-b*)',
61
            $result['b-name']['b']);
62
63
        // Note that the last after task is added with a special name;
64
        // its results therefore show up under the name given, rather
65
        // than being stored under the name of the task it was added after.
66
        $this->assertEquals(
67
            '((*value-b*))',
68
            $result['special-name']['b']);
69
    }
70
71
    public function testBeforeFilters()
72
    {
73
        $collection = new Collection();
74
        $collection->setLogger(Robo::logger());
75
76
        $taskA = new CollectionTestTask('a', 'value-a');
77
        $taskB = new CollectionTestTask('b', 'value-b');
78
79
        $collection
80
            ->add($taskA, 'a-name')
81
            ->add($taskB, 'b-name');
82
83
        // We add methods of our task instances as before and
84
        // after tasks. These methods have access to the task
85
        // class' fields, and may modify them as needed.
86
        $collection
87
            ->before('b-name', [$taskA, 'parenthesizer'])
88
            ->before('b-name', [$taskA, 'emphasizer'], 'special-before-name');
89
90
        $result = $collection->run();
91
92
        // Ensure that the results have the correct key values
93
        $this->assertEquals(
94
            'a-name,b-name,special-before-name,time',
95
            implode(',', array_keys($result->getData())));
96
97
        // The result from the 'before' task is attached
98
        // to 'b-name', since it was called as before('b-name', ...)
99
        $this->assertEquals(
100
            '(value-a)',
101
            $result['b-name']['a']);
102
        // When a 'before' task is given its own name, then
103
        // its results are attached under that name.
104
        $this->assertEquals(
105
            '*(value-a)*',
106
            $result['special-before-name']['a']);
107
    }
108
109
    public function testAddCodeRollbackAndCompletion()
110
    {
111
        $collection = new Collection();
112
        $collection->setLogger(Robo::logger());
113
114
        $rollback1 = new CountingTask();
115
        $rollback2 = new CountingTask();
116
        $completion1 = new CountingTask();
117
        $completion2 = new CountingTask();
118
119
        $collection
120
            ->progressMessage("start collection tasks")
121
            ->rollback($rollback1)
122
            ->completion($completion1)
123
            ->rollbackCode(function() use($rollback1) { $rollback1->run(); } )
124
            ->completionCode(function() use($completion1) { $completion1->run(); } )
125
            ->addCode(function () { return 42; })
126
            ->progressMessage("not reached")
127
            ->rollback($rollback2)
128
            ->completion($completion2)
129
            ->addCode(function () { return 13; });
130
131
        $collection->setLogger($this->guy->logger());
132
133
        $result = $collection->run();
134
        // Execution stops on the first error.
135
        // Confirm that status code is converted to a Result object.
136
        $this->assertEquals(42, $result->getExitCode());
137
        $this->assertEquals(2, $rollback1->getCount());
138
        $this->assertEquals(0, $rollback2->getCount());
139
        $this->assertEquals(2, $completion1->getCount());
140
        $this->assertEquals(0, $completion2->getCount());
141
        $this->guy->seeInOutput('start collection tasks');
142
        $this->guy->doNotSeeInOutput('not reached');
143
    }
144
145
    public function testStateWithAddCode()
146
    {
147
        $collection = new Collection();
148
        $collection->setLogger(Robo::logger());
149
150
        $result = $collection
0 ignored issues
show
Unused Code introduced by
$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 $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.

Loading history...
151
            ->addCode(
152
                function (Data $state) {
153
                    $state['one'] = 'first';
154
                })
155
            ->addCode(
156
                function (Data $state) {
157
                    $state['two'] = 'second';
158
                })
159
            ->addCode(
160
                function (Data $state) {
161
                    $state['three'] = "{$state['one']} and {$state['two']}";
162
                })
163
            ->run();
164
165
        $state = $collection->getState();
166
        $this->assertEquals(
167
            'first and second',
168
            $state['three']);
169
    }
170
171
    public function testStateWithTaskResult()
172
    {
173
        $collection = new Collection();
174
        $collection->setLogger(Robo::logger());
175
176
        $first = new ValueProviderTask();
177
        $first->provideData('one', 'First');
178
179
        $second = new ValueProviderTask();
180
        $second->provideData('two', 'Second');
181
182
        $result = $collection
0 ignored issues
show
Unused Code introduced by
$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 $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.

Loading history...
183
            ->add($first)
184
            ->add($second)
185
            ->addCode(
186
                function (Data $state) {
187
                    $state['three'] = "{$state['one']} and {$state['two']}";
188
                })
189
            ->run();
190
191
        $state = $collection->getState();
192
        $this->assertEquals(
193
            'First',
194
            $state['one']);
195
        $this->assertEquals(
196
            'First and Second',
197
            $state['three']);
198
    }
199
200
    public function testDeferredInitialization()
201
    {
202
        $collection = new Collection();
203
        $collection->setLogger(Robo::logger());
204
205
        $first = new ValueProviderTask();
206
        $first->provideData('one', 'First');
207
208
        $second = new ValueProviderTask();
209
        $second->provideData('two', 'Second');
210
211
        $third = new ValueProviderTask();
212
213
        $result = $collection
0 ignored issues
show
Unused Code introduced by
$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 $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.

Loading history...
214
            ->add($first)
215
            ->add($second)
216
            ->add($third)
217
                ->defer(
218
                    $third,
219
                    function ($task, $state) {
220
                        $task->provideData('three', "{$state['one']} and {$state['two']}");
221
                    }
222
                )
223
            ->run();
224
225
        $state = $collection->getState();
226
        $this->assertEquals(
227
            'First',
228
            $state['one']);
229
        $this->assertEquals(
230
            'First and Second',
231
            $state['three']);
232
    }
233
234
    public function testDeferredInitializationWithMessageStorage()
235
    {
236
        $collection = new Collection();
237
        $collection->setLogger(Robo::logger());
238
239
        $first = new ValueProviderTask();
240
        $first->provideMessage('1st');
241
242
        $second = new ValueProviderTask();
243
        $second->provideData('other', '2nd');
244
245
        $third = new ValueProviderTask();
246
247
        $result = $collection
0 ignored issues
show
Unused Code introduced by
$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 $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.

Loading history...
248
            ->add($first)
249
                ->storeState($first, 'one')
250
            ->add($second)
251
                ->storeState($second, 'two', 'other')
252
            ->add($third)
253
                ->defer(
254
                    $third,
255
                    function ($task, $state) {
256
                        $task->provideData('three', "{$state['one']} and {$state['two']}");
257
                    }
258
                )
259
            ->run();
260
261
        $state = $collection->getState();
262
        $this->assertEquals(
263
            '1st',
264
            $state['one']);
265
        $this->assertEquals(
266
            '1st and 2nd',
267
            $state['three']);
268
    }
269
    public function testDeferredInitializationWithChainedInitialization()
270
    {
271
        $collection = new Collection();
272
        $collection->setLogger(Robo::logger());
273
274
        // This task sets the Result message to '1st'
275
        $first = new ValueProviderTask();
276
        $first->provideMessage('1st');
277
278
        $second = new ValueProviderTask();
279
        $second->provideMessage('2nd');
280
281
        $third = new ValueProviderTask();
282
283
        $result = $collection
0 ignored issues
show
Unused Code introduced by
$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 $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.

Loading history...
284
            // $first will set its Result's message to '1st' at `run()` time
285
            ->add($first)
286
                // This will copy the message from $first's result to $state['one'] after $first runs.
287
                // Note that it does not matter what order the `storeState` messages are called in;
288
                // their first parameter determines when they run. This differs from CollectionBuilder,
289
                // which manages order.
290
                ->storeState($first, 'one')
291
            ->add($second)
292
                // This will copy the message from $second's result to $state['two']
293
                ->storeState($second, 'two')
294
            ->add($third)
295
                ->deferTaskConfiguration($third, 'provideItem', 'one')
296
                ->deferTaskConfiguration($third, 'provideMessage', 'two')
297
                ->storeState($third, 'final')
298
            ->progressMessage('The final result is {final}')
299
            ->run();
300
301
        $state = $collection->getState();
302
        $this->assertEquals(
303
            '1st',
304
            $state['one']);
305
        $this->assertEquals(
306
            '1st',
307
            $state['item']);
308
        $this->assertEquals(
309
            '2nd',
310
            $state['final']);
311
312
        $this->guy->seeInOutput("The final result is 2nd");
313
    }
314
}
315
316