1
|
|
|
<?php |
2
|
|
|
namespace Robo; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
use Robo\Collection\CollectionBuilder; |
6
|
|
|
use Robo\State\Data; |
7
|
|
|
use Robo\Traits\TestTasksTrait; |
8
|
|
|
|
9
|
|
|
class ForEachTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
use TestTasksTrait; |
12
|
|
|
use Collection\loadTasks; |
13
|
|
|
|
14
|
|
|
protected $fixtures; |
15
|
|
|
|
16
|
|
|
public function setUp() |
17
|
|
|
{ |
18
|
|
|
$this->fixtures = new Fixtures(); |
19
|
|
|
$this->initTestTasksTrait(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function tearDown() |
23
|
|
|
{ |
24
|
|
|
$this->fixtures->cleanup(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public function examples() |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
'without items' => [ |
34
|
|
|
'expected' => [], |
35
|
|
|
'items' => [], |
36
|
|
|
], |
37
|
|
|
'with items' => [ |
38
|
|
|
'expected' => [ |
39
|
|
|
'a = b', |
40
|
|
|
'c = d', |
41
|
|
|
'e = f', |
42
|
|
|
], |
43
|
|
|
'items' => [ |
44
|
|
|
'a' => 'b', |
45
|
|
|
'c' => 'd', |
46
|
|
|
'e' => 'f', |
47
|
|
|
], |
48
|
|
|
], |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @dataProvider examples |
54
|
|
|
*/ |
55
|
|
|
public function testSetIterableInConstructor($expected, $items) |
56
|
|
|
{ |
57
|
|
|
$actual = []; |
58
|
|
|
|
59
|
|
|
// set iterable in the __constructor |
60
|
|
|
$result = $this |
|
|
|
|
61
|
|
|
->taskForEach($items) |
62
|
|
View Code Duplication |
->withBuilder(function (CollectionBuilder $builder, $key, $value) use (&$actual) { |
|
|
|
|
63
|
|
|
$builder->addCode(function () use ($key, $value, &$actual) { |
64
|
|
|
$actual[] = "$key = $value"; |
65
|
|
|
|
66
|
|
|
return 0; |
67
|
|
|
}); |
68
|
|
|
}) |
69
|
|
|
->run(); |
70
|
|
|
$this->assertTrue($result->wasSuccessful(), $result->getMessage()); |
71
|
|
|
|
72
|
|
|
$this->assertEquals($expected, $actual); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @dataProvider examples |
77
|
|
|
*/ |
78
|
|
|
public function testSetIterableWithDeferTaskConfiguration($expected, $items) |
79
|
|
|
{ |
80
|
|
|
$actual = []; |
81
|
|
|
|
82
|
|
|
// set iterable with deferTaskConfiguration() |
83
|
|
|
$result = $this |
84
|
|
|
->collectionBuilder() |
85
|
|
|
->addCode(function (Data $data) use ($items) { |
86
|
|
|
$data['items'] = $items; |
87
|
|
|
|
88
|
|
|
return 0; |
89
|
|
|
}) |
90
|
|
|
->addTask( |
91
|
|
|
$this |
|
|
|
|
92
|
|
|
->taskForEach() |
93
|
|
|
->deferTaskConfiguration('setIterable', 'items') |
94
|
|
View Code Duplication |
->withBuilder(function (CollectionBuilder $builder, $key, $value) use (&$actual) { |
|
|
|
|
95
|
|
|
$builder->addCode(function () use ($key, $value, &$actual) { |
96
|
|
|
$actual[] = "$key = $value"; |
97
|
|
|
|
98
|
|
|
return 0; |
99
|
|
|
}); |
100
|
|
|
}) |
101
|
|
|
) |
102
|
|
|
->run(); |
103
|
|
|
$this->assertTrue($result->wasSuccessful(), $result->getMessage()); |
104
|
|
|
|
105
|
|
|
$this->assertEquals($expected, $actual); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testUninitializedIterable() |
109
|
|
|
{ |
110
|
|
|
$actual = 0; |
111
|
|
|
// call the __constructor() without argument |
112
|
|
|
$result = $this |
|
|
|
|
113
|
|
|
->taskForEach() |
114
|
|
|
->withBuilder(function (CollectionBuilder $builder, $key, $value) use (&$actual) { |
115
|
|
|
$builder->addCode(function () use ($key, $value, &$actual) { |
116
|
|
|
$actual++; |
117
|
|
|
|
118
|
|
|
return 0; |
119
|
|
|
}); |
120
|
|
|
}) |
121
|
|
|
->run(); |
122
|
|
|
$this->assertTrue($result->wasSuccessful(), $result->getMessage()); |
123
|
|
|
|
124
|
|
|
$this->assertEquals(0, $actual); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: