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

CollectionTest::testStateWithTaskResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
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
use Robo\Task\ValueProviderTask;
14
use Robo\Task\CollectionTestTask;
15
use Robo\Task\CountingTask;
16
17
class CollectionTest extends \Codeception\TestCase\Test
18
{
19
    /**
20
     * @var \CodeGuy
21
     */
22
    protected $guy;
23
24
    public function testAfterFilters()
25
    {
26
        $collection = new Collection();
27
28
        $taskA = new CollectionTestTask('a', 'value-a');
29
        $taskB = new CollectionTestTask('b', 'value-b');
30
31
        $collection
32
            ->add($taskA, 'a-name')
33
            ->add($taskB, 'b-name');
34
35
        // We add methods of our task instances as before and
36
        // after tasks. These methods have access to the task
37
        // class' fields, and may modify them as needed.
38
        $collection
39
            ->after('a-name', [$taskA, 'parenthesizer'])
40
            ->after('a-name', [$taskA, 'emphasizer'])
41
            ->after('b-name', [$taskB, 'emphasizer'])
42
            ->after('b-name', [$taskB, 'parenthesizer'])
43
            ->after('b-name', [$taskB, 'parenthesizer'], 'special-name');
44
45
        $result = $collection->run();
46
47
        // verify(var_export($result->getData(), true))->equals('');
48
49
        // Ensure that the results have the correct key values
50
        verify(implode(',', array_keys($result->getData())))->equals('a-name,b-name,special-name,time');
51
52
        // Verify that all of the after tasks ran in
53
        // the correct order.
54
        verify($result['a-name']['a'])->equals('*(value-a)*');
55
        verify($result['b-name']['b'])->equals('(*value-b*)');
56
57
        // Note that the last after task is added with a special name;
58
        // its results therefore show up under the name given, rather
59
        // than being stored under the name of the task it was added after.
60
        verify($result['special-name']['b'])->equals('((*value-b*))');
61
    }
62
63
    public function testBeforeFilters()
64
    {
65
        $collection = new Collection();
66
67
        $taskA = new CollectionTestTask('a', 'value-a');
68
        $taskB = new CollectionTestTask('b', 'value-b');
69
70
        $collection
71
            ->add($taskA, 'a-name')
72
            ->add($taskB, 'b-name');
73
74
        // We add methods of our task instances as before and
75
        // after tasks. These methods have access to the task
76
        // class' fields, and may modify them as needed.
77
        $collection
78
            ->before('b-name', [$taskA, 'parenthesizer'])
79
            ->before('b-name', [$taskA, 'emphasizer'], 'special-before-name');
80
81
        $result = $collection->run();
82
83
        // Ensure that the results have the correct key values
84
        verify(implode(',', array_keys($result->getData())))->equals('a-name,b-name,special-before-name,time');
85
86
        // The result from the 'before' task is attached
87
        // to 'b-name', since it was called as before('b-name', ...)
88
        verify($result['b-name']['a'])->equals('(value-a)');
89
        // When a 'before' task is given its own name, then
90
        // its results are attached under that name.
91
        verify($result['special-before-name']['a'])->equals('*(value-a)*');
92
    }
93
94
    public function testAddCodeRollbackAndCompletion()
95
    {
96
        $collection = new Collection();
97
        $rollback1 = new CountingTask();
98
        $rollback2 = new CountingTask();
99
        $completion1 = new CountingTask();
100
        $completion2 = new CountingTask();
101
102
        $collection
103
            ->progressMessage("start collection tasks")
104
            ->rollback($rollback1)
105
            ->completion($completion1)
106
            ->rollbackCode(function() use($rollback1) { $rollback1->run(); } )
107
            ->completionCode(function() use($completion1) { $completion1->run(); } )
108
            ->addCode(function () { return 42; })
109
            ->progressMessage("not reached")
110
            ->rollback($rollback2)
111
            ->completion($completion2)
112
            ->addCode(function () { return 13; });
113
114
        $collection->setLogger($this->guy->logger());
115
116
        $result = $collection->run();
117
        // Execution stops on the first error.
118
        // Confirm that status code is converted to a Result object.
119
        verify($result->getExitCode())->equals(42);
120
        verify($rollback1->getCount())->equals(2);
121
        verify($rollback2->getCount())->equals(0);
122
        verify($completion1->getCount())->equals(2);
123
        verify($completion2->getCount())->equals(0);
124
        $this->guy->seeInOutput('start collection tasks');
125
        $this->guy->doNotSeeInOutput('not reached');
126
    }
127
128
    public function testStateWithAddCode()
129
    {
130
        $collection = new Collection();
131
132
        $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...
133
            ->addCode(
134
                function (ResultData $state) {
135
                    $state['one'] = 'first';
136
                })
137
            ->addCode(
138
                function (ResultData $state) {
139
                    $state['two'] = 'second';
140
                })
141
            ->addCode(
142
                function (ResultData $state) {
143
                    $state['three'] = "{$state['one']} and {$state['two']}";
144
                })
145
            ->run();
146
147
        $state = $collection->getState();
148
        verify($state['three'])->equals('first and second');
149
    }
150
151
    public function testStateWithTaskResult()
152
    {
153
        $collection = new Collection();
154
155
        $first = new ValueProviderTask();
156
        $first->provideData('one', 'First');
157
158
        $second = new ValueProviderTask();
159
        $second->provideData('two', 'Second');
160
161
        $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...
162
            ->add($first)
163
            ->add($second)
164
            ->addCode(
165
                function (ResultData $state) {
166
                    $state['three'] = "{$state['one']} and {$state['two']}";
167
                })
168
            ->run();
169
170
        $state = $collection->getState();
171
        verify($state['one'])->equals('First');
172
        verify($state['three'])->equals('First and Second');
173
    }
174
175
    public function testDeferredInitialization()
176
    {
177
        $collection = new Collection();
178
179
        $first = new ValueProviderTask();
180
        $first->provideData('one', 'First');
181
182
        $second = new ValueProviderTask();
183
        $second->provideData('two', 'Second');
184
185
        $third = new ValueProviderTask();
186
187
        $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...
188
            ->add($first)
189
            ->add($second)
190
            ->add($third)
191
                ->defer(
192
                    $third,
193
                    function ($task, $state) {
194
                        $task->provideData('three', "{$state['one']} and {$state['two']}");
195
                    }
196
                )
197
            ->run();
198
199
        $state = $collection->getState();
200
        verify($state['one'])->equals('First');
201
        verify($state['three'])->equals('First and Second');
202
    }
203
204 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...
205
    {
206
        $collection = new Collection();
207
208
        $first = new ValueProviderTask();
209
        $first->provideMessage('1st');
210
211
        $second = new ValueProviderTask();
212
        $second->provideData('other', '2nd');
213
214
        $third = new ValueProviderTask();
215
216
        $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...
217
            ->add($first)
218
                ->storeState($first, 'one')
219
            ->add($second)
220
                ->storeState($second, 'two', 'other')
221
            ->add($third)
222
                ->defer(
223
                    $third,
224
                    function ($task, $state) {
225
                        $task->provideData('three', "{$state['one']} and {$state['two']}");
226
                    }
227
                )
228
            ->run();
229
230
        $state = $collection->getState();
231
        verify($state['one'])->equals('1st');
232
        verify($state['three'])->equals('1st and 2nd');
233
    }
234 View Code Duplication
    public function testDeferredInitializationWithChainedInitialization()
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...
235
    {
236
        $collection = new Collection();
237
238
        $first = new ValueProviderTask();
239
        $first->provideMessage('1st');
240
241
        $second = new ValueProviderTask();
242
        $second->provideMessage('2nd');
243
244
        $third = new ValueProviderTask();
245
246
        $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...
247
            ->add($first)
248
                ->storeState($first, 'one')
249
            ->add($second)
250
                ->storeState($second, 'two')
251
            ->add($third)
252
                ->chainState($third, 'provideItem', 'one')
253
                ->chainState($third, 'provideMessage', 'two')
254
                ->storeState($third, 'final')
255
            ->run();
256
257
        $state = $collection->getState();
258
        verify($state['one'])->equals('1st');
259
        verify($state['item'])->equals('1st');
260
        verify($state['final'])->equals('2nd');
261
    }
262
}
263
264