Completed
Pull Request — master (#580)
by Greg
03:21
created

PassthruTask   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 27
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 4 1
A provideData() 0 5 1
A provideItem() 0 4 1
A provideMessage() 0 5 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\Result;
10
use Robo\ResultData;
11
use Robo\Task\BaseTask;
12
use Robo\Collection\Collection;
13
14
class CollectionTest extends \Codeception\TestCase\Test
15
{
16
    /**
17
     * @var \CodeGuy
18
     */
19
    protected $guy;
20
21
    public function testAfterFilters()
22
    {
23
        $collection = new Collection();
24
25
        $taskA = new CollectionTestTask('a', 'value-a');
26
        $taskB = new CollectionTestTask('b', 'value-b');
27
28
        $collection
29
            ->add($taskA, 'a-name')
30
            ->add($taskB, 'b-name');
31
32
        // We add methods of our task instances as before and
33
        // after tasks. These methods have access to the task
34
        // class' fields, and may modify them as needed.
35
        $collection
36
            ->after('a-name', [$taskA, 'parenthesizer'])
37
            ->after('a-name', [$taskA, 'emphasizer'])
38
            ->after('b-name', [$taskB, 'emphasizer'])
39
            ->after('b-name', [$taskB, 'parenthesizer'])
40
            ->after('b-name', [$taskB, 'parenthesizer'], 'special-name');
41
42
        $result = $collection->run();
43
44
        // verify(var_export($result->getData(), true))->equals('');
45
46
        // Ensure that the results have the correct key values
47
        verify(implode(',', array_keys($result->getData())))->equals('a-name,b-name,special-name,time');
48
49
        // Verify that all of the after tasks ran in
50
        // the correct order.
51
        verify($result['a-name']['a'])->equals('*(value-a)*');
52
        verify($result['b-name']['b'])->equals('(*value-b*)');
53
54
        // Note that the last after task is added with a special name;
55
        // its results therefore show up under the name given, rather
56
        // than being stored under the name of the task it was added after.
57
        verify($result['special-name']['b'])->equals('((*value-b*))');
58
    }
59
60
    public function testBeforeFilters()
61
    {
62
        $collection = new Collection();
63
64
        $taskA = new CollectionTestTask('a', 'value-a');
65
        $taskB = new CollectionTestTask('b', 'value-b');
66
67
        $collection
68
            ->add($taskA, 'a-name')
69
            ->add($taskB, 'b-name');
70
71
        // We add methods of our task instances as before and
72
        // after tasks. These methods have access to the task
73
        // class' fields, and may modify them as needed.
74
        $collection
75
            ->before('b-name', [$taskA, 'parenthesizer'])
76
            ->before('b-name', [$taskA, 'emphasizer'], 'special-before-name');
77
78
        $result = $collection->run();
79
80
        // Ensure that the results have the correct key values
81
        verify(implode(',', array_keys($result->getData())))->equals('a-name,b-name,special-before-name,time');
82
83
        // The result from the 'before' task is attached
84
        // to 'b-name', since it was called as before('b-name', ...)
85
        verify($result['b-name']['a'])->equals('(value-a)');
86
        // When a 'before' task is given its own name, then
87
        // its results are attached under that name.
88
        verify($result['special-before-name']['a'])->equals('*(value-a)*');
89
    }
90
91
    public function testAddCodeRollbackAndCompletion()
92
    {
93
        $collection = new Collection();
94
        $rollback1 = new CountingTask();
95
        $rollback2 = new CountingTask();
96
        $completion1 = new CountingTask();
97
        $completion2 = new CountingTask();
98
99
        $collection
100
            ->progressMessage("start collection tasks")
101
            ->rollback($rollback1)
102
            ->completion($completion1)
103
            ->rollbackCode(function() use($rollback1) { $rollback1->run(); } )
104
            ->completionCode(function() use($completion1) { $completion1->run(); } )
105
            ->addCode(function () { return 42; })
106
            ->progressMessage("not reached")
107
            ->rollback($rollback2)
108
            ->completion($completion2)
109
            ->addCode(function () { return 13; });
110
111
        $collection->setLogger($this->guy->logger());
112
113
        $result = $collection->run();
114
        // Execution stops on the first error.
115
        // Confirm that status code is converted to a Result object.
116
        verify($result->getExitCode())->equals(42);
117
        verify($rollback1->getCount())->equals(2);
118
        verify($rollback2->getCount())->equals(0);
119
        verify($completion1->getCount())->equals(2);
120
        verify($completion2->getCount())->equals(0);
121
        $this->guy->seeInOutput('start collection tasks');
122
        $this->guy->doNotSeeInOutput('not reached');
123
    }
124
125
    public function testStateWithAddCode()
126
    {
127
        $collection = new Collection();
128
129
        $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...
130
            ->addCode(
131
                function (ResultData $state) {
132
                    $state['one'] = 'first';
133
                })
134
            ->addCode(
135
                function (ResultData $state) {
136
                    $state['two'] = 'second';
137
                })
138
            ->addCode(
139
                function (ResultData $state) {
140
                    $state['three'] = "{$state['one']} and {$state['two']}";
141
                })
142
            ->run();
143
144
        $state = $collection->getState();
145
        verify($state['three'])->equals('first and second');
146
    }
147
148
    public function testStateWithTaskResult()
149
    {
150
        $collection = new Collection();
151
152
        $first = new PassthruTask();
153
        $first->provideData('one', 'First');
154
155
        $second = new PassthruTask();
156
        $second->provideData('two', 'Second');
157
158
        $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...
159
            ->add($first)
160
            ->add($second)
161
            ->addCode(
162
                function (ResultData $state) {
163
                    $state['three'] = "{$state['one']} and {$state['two']}";
164
                })
165
            ->run();
166
167
        $state = $collection->getState();
168
        verify($state['one'])->equals('First');
169
        verify($state['three'])->equals('First and Second');
170
    }
171
172 View Code Duplication
    public function testDeferredInitialization()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
    {
174
        $collection = new Collection();
175
176
        $first = new PassthruTask();
177
        $first->provideData('one', 'First');
178
179
        $second = new PassthruTask();
180
        $second->provideData('two', 'Second');
181
182
        $third = new PassthruTask();
183
184
        $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...
185
            ->add($first)
186
            ->add($second)
187
            ->add($third)
188
                ->defer(
189
                    $third,
190
                    function ($task, $state) {
191
                        $task->provideData('three', "{$state['one']} and {$state['two']}");
192
                    }
193
                )
194
            ->run();
195
196
        $state = $collection->getState();
197
        verify($state['one'])->equals('First');
198
        verify($state['three'])->equals('First and Second');
199
    }
200
201 View Code Duplication
    public function testDeferredInitializationWithMessageStorage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
    {
203
        $collection = new Collection();
204
205
        $first = new PassthruTask();
206
        $first->provideMessage('1st');
207
208
        $second = new PassthruTask();
209
        $second->provideMessage('2nd');
210
211
        $third = new PassthruTask();
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
                ->storeMessage($first, 'one')
216
            ->add($second)
217
                ->storeMessage($second, 'two')
218
            ->add($third)
219
                ->defer(
220
                    $third,
221
                    function ($task, $state) {
222
                        $task->provideData('three', "{$state['one']} and {$state['two']}");
223
                    }
224
                )
225
            ->run();
226
227
        $state = $collection->getState();
228
        verify($state['one'])->equals('1st');
229
        verify($state['three'])->equals('1st and 2nd');
230
    }
231
    public function testDeferredInitializationWithChainedInitialization()
232
    {
233
        $collection = new Collection();
234
235
        $first = new PassthruTask();
236
        $first->provideMessage('1st');
237
238
        $second = new PassthruTask();
239
        $second->provideMessage('2nd');
240
241
        $third = new PassthruTask();
242
243
        $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...
244
            ->add($first)
245
                ->storeMessage($first, 'one')
246
            ->add($second)
247
                ->storeMessage($second, 'two')
248
            ->add($third)
249
                ->chainState($third, 'provideItem', 'one')
250
                ->chainState($third, 'provideMessage', 'two')
251
                ->storeMessage($third, 'final')
252
            ->run();
253
254
        $state = $collection->getState();
255
        verify($state['one'])->equals('1st');
256
        verify($state['item'])->equals('1st');
257
        verify($state['final'])->equals('2nd');
258
    }
259
}
260
261
class CountingTask extends BaseTask
262
{
263
    protected $count = 0;
264
265
    public function run()
266
    {
267
        $this->count++;
268
        return Result::success($this);
269
    }
270
271
    public function getCount()
272
    {
273
        return $this->count;
274
    }
275
}
276
277
class CollectionTestTask extends BaseTask
278
{
279
    protected $key;
280
    protected $value;
281
282
    public function __construct($key, $value)
283
    {
284
        $this->key = $key;
285
        $this->value = $value;
286
    }
287
288
    public function run()
289
    {
290
        return $this->getValue();
291
    }
292
293
    protected function getValue()
294
    {
295
        $result = Result::success($this);
296
        $result[$this->key] = $this->value;
297
298
        return $result;
299
    }
300
301
    // Note that by returning a value with the same
302
    // key as the result, we overwrite the value generated
303
    // by the primary task method ('run()').  If we returned
304
    // a result with a different key, then both values
305
    // would appear in the result.
306
    public function parenthesizer()
307
    {
308
        $this->value = "({$this->value})";
309
        return $this->getValue();
310
    }
311
312
    public function emphasizer()
313
    {
314
        $this->value = "*{$this->value}*";
315
        return $this->getValue();
316
    }
317
}
318
319
class PassthruTask extends BaseTask
320
{
321
    protected $message = '';
322
    protected $data = [];
323
324
    public function run()
325
    {
326
        return Result::success($this, $this->message, $this->data);
327
    }
328
329
    public function provideData($key, $value)
330
    {
331
        $this->data[$key] = $value;
332
        return $this;
333
    }
334
335
    public function provideItem($value)
336
    {
337
        return $this->provideData('item', $value);
338
    }
339
340
    public function provideMessage($message)
341
    {
342
        $this->message = $message;
343
        return $this;
344
    }
345
}
346