1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the LGPL. For more information please see |
17
|
|
|
* <http://phing.info>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Testcase for the Phing task/condition. |
22
|
|
|
* |
23
|
|
|
* @author Siad Ardroumli <[email protected]> |
24
|
|
|
* @package phing.tasks.system |
25
|
|
|
*/ |
26
|
|
|
class PhingTaskTest extends BuildFileTest |
27
|
|
|
{ |
28
|
|
|
public function setUp(): void |
29
|
|
|
{ |
30
|
|
|
$this->configureProject(PHING_TEST_BASE . '/etc/tasks/system/phing.xml'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function tearDown(): void |
34
|
|
|
{ |
35
|
|
|
$this->getProject()->executeTarget('cleanup'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function test1(): void |
39
|
|
|
{ |
40
|
|
|
$this->expectBuildException(__FUNCTION__, 'phing task self referencing.'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function test2(): void |
44
|
|
|
{ |
45
|
|
|
$this->expectBuildException(__FUNCTION__, 'phingcall without arguments.'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function test3(): void |
49
|
|
|
{ |
50
|
|
|
$this->expectBuildException(__FUNCTION__, 'No BuildException thrown.'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function test4(): void |
54
|
|
|
{ |
55
|
|
|
$this->expectBuildException(__FUNCTION__, 'phingcall with empty target.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function test4b(): void |
59
|
|
|
{ |
60
|
|
|
$this->expectBuildException(__FUNCTION__, 'phingcall with not existing target.'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function test5(): void |
64
|
|
|
{ |
65
|
|
|
$this->getProject()->executeTarget(__FUNCTION__); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function test6(): void |
69
|
|
|
{ |
70
|
|
|
$this->getProject()->executeTarget(__FUNCTION__); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testExplicitBasedir1(): void |
74
|
|
|
{ |
75
|
|
|
$dir1 = $this->getProject()->getBaseDir(); |
76
|
|
|
$dir2 = $this->getProject()->resolveFile(".."); |
77
|
|
|
$this->baseDirs('explicitBasedir1', [$dir1->getAbsolutePath(), $dir2->getAbsolutePath()]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function baseDirs(string $target, array $dirs): void |
81
|
|
|
{ |
82
|
|
|
$bc = new class ($dirs) implements BuildListener { |
83
|
|
|
private $expectedBasedirs; |
84
|
|
|
private $calls = 0; |
85
|
|
|
private $error; |
86
|
|
|
|
87
|
|
|
public function __construct(array $dirs) |
88
|
|
|
{ |
89
|
|
|
$this->expectedBasedirs = $dirs; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function buildStarted(BuildEvent $event) |
93
|
|
|
{ |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function buildFinished(BuildEvent $event) |
97
|
|
|
{ |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function targetFinished(BuildEvent $event) |
101
|
|
|
{ |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function taskStarted(BuildEvent $event) |
105
|
|
|
{ |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function taskFinished(BuildEvent $event) |
109
|
|
|
{ |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function messageLogged(BuildEvent $event) |
113
|
|
|
{ |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function targetStarted(BuildEvent $event) |
117
|
|
|
{ |
118
|
|
|
if ($event->getTarget()->getName() === '') { |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
if ($this->error === null) { |
122
|
|
|
try { |
123
|
|
|
BuildFileTest::assertEquals( |
124
|
|
|
$this->expectedBasedirs[$this->calls++], |
125
|
|
|
$event->getProject()->getBaseDir()->getAbsolutePath() |
126
|
|
|
); |
127
|
|
|
} catch (AssertionError $e) { |
128
|
|
|
$this->error = $e; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getError() |
134
|
|
|
{ |
135
|
|
|
return $this->error; |
136
|
|
|
} |
137
|
|
|
}; |
138
|
|
|
$this->getProject()->addBuildListener($bc); |
139
|
|
|
$this->executeTarget($target); |
140
|
|
|
$ae = $bc->getError(); |
141
|
|
|
if ($ae !== null) { |
142
|
|
|
throw $ae; |
143
|
|
|
} |
144
|
|
|
$this->getProject()->removeBuildListener($bc); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testExplicitBasedir2(): void |
148
|
|
|
{ |
149
|
|
|
$dir1 = $this->getProject()->getBaseDir(); |
150
|
|
|
$dir2 = $this->getProject()->resolveFile(".."); |
151
|
|
|
$this->baseDirs('explicitBasedir2', [$dir1->getAbsolutePath(), $dir2->getAbsolutePath()]); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testInheritBasedir(): void |
155
|
|
|
{ |
156
|
|
|
$basedir = $this->getProject()->getBaseDir()->getAbsolutePath(); |
157
|
|
|
$this->baseDirs('inheritBasedir', [$basedir, $basedir]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function testDoNotInheritBasedir(): void |
161
|
|
|
{ |
162
|
|
|
$dir1 = $this->getProject()->getBaseDir(); |
163
|
|
|
$dir2 = $this->getProject()->resolveFile('phing'); |
164
|
|
|
$this->baseDirs('doNotInheritBasedir', [$dir1->getAbsolutePath(), $dir2->getAbsolutePath()]); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testBasedirTripleCall(): void |
168
|
|
|
{ |
169
|
|
|
$dir1 = $this->getProject()->getBaseDir(); |
170
|
|
|
$dir2 = $this->getProject()->resolveFile("phing"); |
171
|
|
|
$this->baseDirs('tripleCall', [$dir1->getAbsolutePath(), $dir2->getAbsolutePath(), $dir1->getAbsolutePath()]); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testReferenceInheritance(): void |
175
|
|
|
{ |
176
|
|
|
$p = new Path($this->getProject(), 'test-path'); |
177
|
|
|
$this->getProject()->addReference('path', $p); |
178
|
|
|
$this->getProject()->addReference('no-override', $p); |
179
|
|
|
$this->reference('testInherit', ['path', 'path'], [true, true], $p); |
180
|
|
|
$this->reference('testInherit', ['no-override', 'no-override'], [true, false], $p); |
181
|
|
|
$this->reference('testInherit', ['no-override', 'no-override'], [false, false], null); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
protected function reference(string $target, array $keys, array $expect, $value): void |
185
|
|
|
{ |
186
|
|
|
$rc = new class ($keys, $expect, $value) implements BuildListener { |
187
|
|
|
private $keys; |
188
|
|
|
private $expectSame; |
189
|
|
|
private $value; |
190
|
|
|
private $calls = 0; |
191
|
|
|
private $error; |
192
|
|
|
|
193
|
|
|
public function __construct(array $keys, array $expectSame, $value) |
194
|
|
|
{ |
195
|
|
|
$this->keys = $keys; |
196
|
|
|
$this->expectSame = $expectSame; |
197
|
|
|
$this->value = $value; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function buildStarted(BuildEvent $event) |
201
|
|
|
{ |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function buildFinished(BuildEvent $event) |
205
|
|
|
{ |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function targetFinished(BuildEvent $event) |
209
|
|
|
{ |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function taskStarted(BuildEvent $event) |
213
|
|
|
{ |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function taskFinished(BuildEvent $event) |
217
|
|
|
{ |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function messageLogged(BuildEvent $event) |
221
|
|
|
{ |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function targetStarted(BuildEvent $event) |
225
|
|
|
{ |
226
|
|
|
if ($event->getTarget()->getName() === '') { |
227
|
|
|
return; |
228
|
|
|
} |
229
|
|
|
if ($this->error === null) { |
230
|
|
|
try { |
231
|
|
|
$msg = "Call " . $this->calls . " refid=\'" . $this->keys[$this->calls] . "\'"; |
232
|
|
|
if ($this->value === null) { |
233
|
|
|
$o = $event->getProject()->getReference($this->keys[$this->calls]); |
234
|
|
|
if ($this->expectSame[$this->calls++]) { |
235
|
|
|
PhingTaskTest::assertNull($o, $msg); |
236
|
|
|
} else { |
237
|
|
|
PhingTaskTest::assertNotNull($o, $msg); |
238
|
|
|
} |
239
|
|
|
} else { |
240
|
|
|
// a rather convoluted equals() test |
241
|
|
|
/** @var Path $expect */ |
242
|
|
|
$expect = $this->value; |
243
|
|
|
$received = $event->getProject()->getReference($this->keys[$this->calls]); |
244
|
|
|
$shouldBeEqual = $this->expectSame[$this->calls++]; |
245
|
|
|
if ($received === null) { |
246
|
|
|
PhingTaskTest::assertFalse($shouldBeEqual, $msg); |
247
|
|
|
} else { |
248
|
|
|
$l1 = $expect->listPaths(); |
249
|
|
|
$l2 = $received->listPaths(); |
250
|
|
|
if (count($l1) === count($l2)) { |
251
|
|
|
for ($i = 0, $iMax = count($l1); $i < $iMax; $i++) { |
252
|
|
|
if ($l1[$i] !== $l2[$i]) { |
253
|
|
|
PhingTaskTest::assertFalse($shouldBeEqual, $msg); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
PhingTaskTest::assertTrue($shouldBeEqual, $msg); |
257
|
|
|
} else { |
258
|
|
|
PhingTaskTest::assertFalse($shouldBeEqual, $msg); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
} catch (\Throwable $e) { |
263
|
|
|
$this->error = $e; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function getError() |
269
|
|
|
{ |
270
|
|
|
return $this->error; |
271
|
|
|
} |
272
|
|
|
}; |
273
|
|
|
$this->getProject()->addBuildListener($rc); |
274
|
|
|
$this->getProject()->executeTarget($target); |
275
|
|
|
$ae = $rc->getError(); |
276
|
|
|
if ($ae !== null) { |
277
|
|
|
throw $ae; |
278
|
|
|
} |
279
|
|
|
$this->getProject()->removeBuildListener($rc); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public function testReferenceNoInheritance(): void |
283
|
|
|
{ |
284
|
|
|
$p = new Path($this->getProject(), 'test-path'); |
285
|
|
|
$this->getProject()->addReference("path", $p); |
286
|
|
|
$this->getProject()->addReference("no-override", $p); |
287
|
|
|
$this->reference("testNoInherit", ["path", "path"], [true, false], $p); |
288
|
|
|
$this->reference("testNoInherit", ["path", "path"], [false, true], null); |
289
|
|
|
$this->reference("testInherit", ["no-override", "no-override"], [true, false], $p); |
290
|
|
|
$this->reference("testInherit", ["no-override", "no-override"], [false, false], null); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
public function testInheritPath(): void |
294
|
|
|
{ |
295
|
|
|
$this->getProject()->executeTarget('testInheritPath'); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
public function testLogfilePlacement(): void |
299
|
|
|
{ |
300
|
|
|
/** @var PhingFile[] $logFiles */ |
301
|
|
|
$logFiles = [ |
302
|
|
|
$this->getProject()->resolveFile("test1.log"), |
303
|
|
|
$this->getProject()->resolveFile("test2.log"), |
304
|
|
|
$this->getProject()->resolveFile("phing/test3.log"), |
305
|
|
|
$this->getProject()->resolveFile("phing/test4.log") |
306
|
|
|
]; |
307
|
|
|
|
308
|
|
|
foreach ($logFiles as $file) { |
309
|
|
|
$this->assertFalse($file->exists(), $file->getName() . " doesn't exist"); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
$this->getProject()->executeTarget(__FUNCTION__); |
313
|
|
|
|
314
|
|
|
foreach ($logFiles as $file) { |
315
|
|
|
$this->assertTrue($file->exists(), $file->getName() . " exist"); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
public function testUserPropertyWinsInheritAll(): void |
320
|
|
|
{ |
321
|
|
|
$this->getProject()->setUserProperty("test", "7"); |
322
|
|
|
$this->getProject()->executeTarget("test-property-override-inheritall-start"); |
323
|
|
|
$this->assertInLogs('The value of test is 7'); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
public function testUserPropertyWinsNoInheritAll() |
327
|
|
|
{ |
328
|
|
|
$this->getProject()->setUserProperty("test", "7"); |
329
|
|
|
$this->getProject()->executeTarget("test-property-override-no-inheritall-start"); |
330
|
|
|
$this->assertInLogs('The value of test is 7'); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function testOverrideWinsInheritAll() |
334
|
|
|
{ |
335
|
|
|
$this->expectLogContaining('test-property-override-inheritall-start', 'The value of test is 4'); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
public function testOverrideWinsNoInheritAll() |
339
|
|
|
{ |
340
|
|
|
$this->expectLogContaining('test-property-override-no-inheritall-start', 'The value of test is 4'); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Fail due to infinite recursion loop |
345
|
|
|
*/ |
346
|
|
|
public function testInfiniteLoopViaDepends(): void |
347
|
|
|
{ |
348
|
|
|
$this->expectBuildException('infinite-loop-via-depends', 'infinite loop'); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
public function testMultiSameProperty(): void |
352
|
|
|
{ |
353
|
|
|
$this->expectLogContaining('multi-same-property', 'prop is two'); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
public function testTopLevelTarget(): void |
357
|
|
|
{ |
358
|
|
|
$this->expectLogContaining('topleveltarget', 'Hello world'); |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|