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

PassthruTask::provideData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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
173
class CountingTask extends BaseTask
174
{
175
    protected $count = 0;
176
177
    public function run()
178
    {
179
        $this->count++;
180
        return Result::success($this);
181
    }
182
183
    public function getCount()
184
    {
185
        return $this->count;
186
    }
187
}
188
189
class CollectionTestTask extends BaseTask
190
{
191
    protected $key;
192
    protected $value;
193
194
    public function __construct($key, $value)
195
    {
196
        $this->key = $key;
197
        $this->value = $value;
198
    }
199
200
    public function run()
201
    {
202
        return $this->getValue();
203
    }
204
205
    protected function getValue()
206
    {
207
        $result = Result::success($this);
208
        $result[$this->key] = $this->value;
209
210
        return $result;
211
    }
212
213
    // Note that by returning a value with the same
214
    // key as the result, we overwrite the value generated
215
    // by the primary task method ('run()').  If we returned
216
    // a result with a different key, then both values
217
    // would appear in the result.
218
    public function parenthesizer()
219
    {
220
        $this->value = "({$this->value})";
221
        return $this->getValue();
222
    }
223
224
    public function emphasizer()
225
    {
226
        $this->value = "*{$this->value}*";
227
        return $this->getValue();
228
    }
229
}
230
231
class PassthruTask extends BaseTask
232
{
233
    protected $data = [];
234
235
    public function run()
236
    {
237
        return Result::success($this, '', $this->data);
238
    }
239
240
    public function provideData($key, $value)
241
    {
242
        $this->data[$key] = $value;
243
    }
244
}
245