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

CollectionTest::testStateWithAddCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 15
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
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
    public function testDeferredInitialization()
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
}
202
203
class CountingTask extends BaseTask
204
{
205
    protected $count = 0;
206
207
    public function run()
208
    {
209
        $this->count++;
210
        return Result::success($this);
211
    }
212
213
    public function getCount()
214
    {
215
        return $this->count;
216
    }
217
}
218
219
class CollectionTestTask extends BaseTask
220
{
221
    protected $key;
222
    protected $value;
223
224
    public function __construct($key, $value)
225
    {
226
        $this->key = $key;
227
        $this->value = $value;
228
    }
229
230
    public function run()
231
    {
232
        return $this->getValue();
233
    }
234
235
    protected function getValue()
236
    {
237
        $result = Result::success($this);
238
        $result[$this->key] = $this->value;
239
240
        return $result;
241
    }
242
243
    // Note that by returning a value with the same
244
    // key as the result, we overwrite the value generated
245
    // by the primary task method ('run()').  If we returned
246
    // a result with a different key, then both values
247
    // would appear in the result.
248
    public function parenthesizer()
249
    {
250
        $this->value = "({$this->value})";
251
        return $this->getValue();
252
    }
253
254
    public function emphasizer()
255
    {
256
        $this->value = "*{$this->value}*";
257
        return $this->getValue();
258
    }
259
}
260
261
class PassthruTask extends BaseTask
262
{
263
    protected $data = [];
264
265
    public function run()
266
    {
267
        return Result::success($this, '', $this->data);
268
    }
269
270
    public function provideData($key, $value)
271
    {
272
        $this->data[$key] = $value;
273
    }
274
}
275