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
|
|
|
namespace Phing\Test\Task\System; |
21
|
|
|
|
22
|
|
|
use AssertionError; |
23
|
|
|
use Phing\Io\File; |
24
|
|
|
use Phing\Listener\BuildEvent; |
25
|
|
|
use Phing\Listener\BuildListener; |
26
|
|
|
use Phing\Test\Support\BuildFileTest; |
27
|
|
|
use Phing\Type\Path; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Testcase for the Phing task/condition. |
31
|
|
|
* |
32
|
|
|
* @author Siad Ardroumli <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class PhingTaskTest extends BuildFileTest |
35
|
|
|
{ |
36
|
|
|
public function setUp(): void |
37
|
|
|
{ |
38
|
|
|
$this->configureProject(PHING_TEST_BASE . '/etc/tasks/system/phing.xml'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function tearDown(): void |
42
|
|
|
{ |
43
|
|
|
$this->getProject()->executeTarget('cleanup'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function test1(): void |
47
|
|
|
{ |
48
|
|
|
$this->expectBuildException(__FUNCTION__, 'phing task self referencing.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function test2(): void |
52
|
|
|
{ |
53
|
|
|
$this->expectBuildException(__FUNCTION__, 'phingcall without arguments.'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function test3(): void |
57
|
|
|
{ |
58
|
|
|
$this->expectBuildException(__FUNCTION__, 'No BuildException thrown.'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function test4(): void |
62
|
|
|
{ |
63
|
|
|
$this->expectBuildException(__FUNCTION__, 'phingcall with empty target.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function test4b(): void |
67
|
|
|
{ |
68
|
|
|
$this->expectBuildException(__FUNCTION__, 'phingcall with not existing target.'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function test5(): void |
72
|
|
|
{ |
73
|
|
|
$this->expectNotToPerformAssertions(); |
74
|
|
|
$this->getProject()->executeTarget(__FUNCTION__); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function test6(): void |
78
|
|
|
{ |
79
|
|
|
$this->expectNotToPerformAssertions(); |
80
|
|
|
$this->getProject()->executeTarget(__FUNCTION__); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testExplicitBasedir1(): void |
84
|
|
|
{ |
85
|
|
|
$dir1 = $this->getProject()->getBaseDir(); |
86
|
|
|
$dir2 = $this->getProject()->resolveFile(".."); |
87
|
|
|
$this->baseDirs('explicitBasedir1', [$dir1->getAbsolutePath(), $dir2->getAbsolutePath()]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function baseDirs(string $target, array $dirs): void |
91
|
|
|
{ |
92
|
|
|
$bc = new class($dirs) implements BuildListener { |
93
|
|
|
private $expectedBasedirs; |
94
|
|
|
private $calls = 0; |
95
|
|
|
private $error; |
96
|
|
|
|
97
|
|
|
public function __construct(array $dirs) |
98
|
|
|
{ |
99
|
|
|
$this->expectedBasedirs = $dirs; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function buildStarted(BuildEvent $event) |
103
|
|
|
{ |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function buildFinished(BuildEvent $event) |
107
|
|
|
{ |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function targetFinished(BuildEvent $event) |
111
|
|
|
{ |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function taskStarted(BuildEvent $event) |
115
|
|
|
{ |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function taskFinished(BuildEvent $event) |
119
|
|
|
{ |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function messageLogged(BuildEvent $event) |
123
|
|
|
{ |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function targetStarted(BuildEvent $event) |
127
|
|
|
{ |
128
|
|
|
if ($event->getTarget()->getName() === '') { |
129
|
|
|
return; |
130
|
|
|
} |
131
|
|
|
if ($this->error === null) { |
132
|
|
|
try { |
133
|
|
|
BuildFileTest::assertEquals( |
134
|
|
|
$this->expectedBasedirs[$this->calls++], |
135
|
|
|
$event->getProject()->getBaseDir()->getAbsolutePath() |
136
|
|
|
); |
137
|
|
|
} catch (AssertionError $e) { |
138
|
|
|
$this->error = $e; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getError() |
144
|
|
|
{ |
145
|
|
|
return $this->error; |
146
|
|
|
} |
147
|
|
|
}; |
148
|
|
|
$this->getProject()->addBuildListener($bc); |
149
|
|
|
$this->executeTarget($target); |
150
|
|
|
$ae = $bc->getError(); |
151
|
|
|
if ($ae !== null) { |
152
|
|
|
throw $ae; |
153
|
|
|
} |
154
|
|
|
$this->getProject()->removeBuildListener($bc); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function testExplicitBasedir2(): void |
158
|
|
|
{ |
159
|
|
|
$dir1 = $this->getProject()->getBaseDir(); |
160
|
|
|
$dir2 = $this->getProject()->resolveFile(".."); |
161
|
|
|
$this->baseDirs('explicitBasedir2', [$dir1->getAbsolutePath(), $dir2->getAbsolutePath()]); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testInheritBasedir(): void |
165
|
|
|
{ |
166
|
|
|
$basedir = $this->getProject()->getBaseDir()->getAbsolutePath(); |
167
|
|
|
$this->baseDirs('inheritBasedir', [$basedir, $basedir]); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function testDoNotInheritBasedir(): void |
171
|
|
|
{ |
172
|
|
|
$dir1 = $this->getProject()->getBaseDir(); |
173
|
|
|
$dir2 = $this->getProject()->resolveFile('phing'); |
174
|
|
|
$this->baseDirs('doNotInheritBasedir', [$dir1->getAbsolutePath(), $dir2->getAbsolutePath()]); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function testBasedirTripleCall(): void |
178
|
|
|
{ |
179
|
|
|
$dir1 = $this->getProject()->getBaseDir(); |
180
|
|
|
$dir2 = $this->getProject()->resolveFile("phing"); |
181
|
|
|
$this->baseDirs('tripleCall', [$dir1->getAbsolutePath(), $dir2->getAbsolutePath(), $dir1->getAbsolutePath()]); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function testReferenceInheritance(): void |
185
|
|
|
{ |
186
|
|
|
$p = new Path($this->getProject(), 'test-path'); |
187
|
|
|
$this->getProject()->addReference('path', $p); |
188
|
|
|
$this->getProject()->addReference('no-override', $p); |
189
|
|
|
$this->reference('testInherit', ['path', 'path'], [true, true], $p); |
190
|
|
|
$this->reference('testInherit', ['no-override', 'no-override'], [true, false], $p); |
191
|
|
|
$this->reference('testInherit', ['no-override', 'no-override'], [false, false], null); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
protected function reference(string $target, array $keys, array $expect, $value): void |
195
|
|
|
{ |
196
|
|
|
$rc = new class($keys, $expect, $value) implements BuildListener { |
197
|
|
|
private $keys; |
198
|
|
|
private $expectSame; |
199
|
|
|
private $value; |
200
|
|
|
private $calls = 0; |
201
|
|
|
private $error; |
202
|
|
|
|
203
|
|
|
public function __construct(array $keys, array $expectSame, $value) |
204
|
|
|
{ |
205
|
|
|
$this->keys = $keys; |
206
|
|
|
$this->expectSame = $expectSame; |
207
|
|
|
$this->value = $value; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function buildStarted(BuildEvent $event) |
211
|
|
|
{ |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function buildFinished(BuildEvent $event) |
215
|
|
|
{ |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function targetFinished(BuildEvent $event) |
219
|
|
|
{ |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function taskStarted(BuildEvent $event) |
223
|
|
|
{ |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function taskFinished(BuildEvent $event) |
227
|
|
|
{ |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function messageLogged(BuildEvent $event) |
231
|
|
|
{ |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function targetStarted(BuildEvent $event) |
235
|
|
|
{ |
236
|
|
|
if ($event->getTarget()->getName() === '') { |
237
|
|
|
return; |
238
|
|
|
} |
239
|
|
|
if ($this->error === null) { |
240
|
|
|
try { |
241
|
|
|
$msg = "Call " . $this->calls . " refid=\'" . $this->keys[$this->calls] . "\'"; |
242
|
|
|
if ($this->value === null) { |
243
|
|
|
$o = $event->getProject()->getReference($this->keys[$this->calls]); |
244
|
|
|
if ($this->expectSame[$this->calls++]) { |
245
|
|
|
PhingTaskTest::assertNull($o, $msg); |
246
|
|
|
} else { |
247
|
|
|
PhingTaskTest::assertNotNull($o, $msg); |
248
|
|
|
} |
249
|
|
|
} else { |
250
|
|
|
// a rather convoluted equals() test |
251
|
|
|
/** @var Path $expect */ |
252
|
|
|
$expect = $this->value; |
253
|
|
|
$received = $event->getProject()->getReference($this->keys[$this->calls]); |
254
|
|
|
$shouldBeEqual = $this->expectSame[$this->calls++]; |
255
|
|
|
if ($received === null) { |
256
|
|
|
PhingTaskTest::assertFalse($shouldBeEqual, $msg); |
257
|
|
|
} else { |
258
|
|
|
$l1 = $expect->listPaths(); |
259
|
|
|
$l2 = $received->listPaths(); |
260
|
|
|
if (count($l1) === count($l2)) { |
261
|
|
|
for ($i = 0, $iMax = count($l1); $i < $iMax; $i++) { |
262
|
|
|
if ($l1[$i] !== $l2[$i]) { |
263
|
|
|
PhingTaskTest::assertFalse($shouldBeEqual, $msg); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
PhingTaskTest::assertTrue($shouldBeEqual, $msg); |
267
|
|
|
} else { |
268
|
|
|
PhingTaskTest::assertFalse($shouldBeEqual, $msg); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
} catch (\Throwable $e) { |
273
|
|
|
$this->error = $e; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function getError() |
279
|
|
|
{ |
280
|
|
|
return $this->error; |
281
|
|
|
} |
282
|
|
|
}; |
283
|
|
|
$this->getProject()->addBuildListener($rc); |
284
|
|
|
$this->getProject()->executeTarget($target); |
285
|
|
|
$ae = $rc->getError(); |
286
|
|
|
if ($ae !== null) { |
287
|
|
|
throw $ae; |
288
|
|
|
} |
289
|
|
|
$this->getProject()->removeBuildListener($rc); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function testReferenceNoInheritance(): void |
293
|
|
|
{ |
294
|
|
|
$p = new Path($this->getProject(), 'test-path'); |
295
|
|
|
$this->getProject()->addReference("path", $p); |
296
|
|
|
$this->getProject()->addReference("no-override", $p); |
297
|
|
|
$this->reference("testNoInherit", ["path", "path"], [true, false], $p); |
298
|
|
|
$this->reference("testNoInherit", ["path", "path"], [false, true], null); |
299
|
|
|
$this->reference("testInherit", ["no-override", "no-override"], [true, false], $p); |
300
|
|
|
$this->reference("testInherit", ["no-override", "no-override"], [false, false], null); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
public function testInheritPath(): void |
304
|
|
|
{ |
305
|
|
|
$this->expectNotToPerformAssertions(); |
306
|
|
|
$this->getProject()->executeTarget('testInheritPath'); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
public function testLogfilePlacement(): void |
310
|
|
|
{ |
311
|
|
|
/** @var File[] $logFiles */ |
312
|
|
|
$logFiles = [ |
313
|
|
|
$this->getProject()->resolveFile("test1.log"), |
314
|
|
|
$this->getProject()->resolveFile("test2.log"), |
315
|
|
|
$this->getProject()->resolveFile("phing/test3.log"), |
316
|
|
|
$this->getProject()->resolveFile("phing/test4.log"), |
317
|
|
|
]; |
318
|
|
|
|
319
|
|
|
foreach ($logFiles as $file) { |
320
|
|
|
$this->assertFalse($file->exists(), $file->getName() . " doesn't exist"); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
$this->getProject()->executeTarget(__FUNCTION__); |
324
|
|
|
|
325
|
|
|
foreach ($logFiles as $file) { |
326
|
|
|
$this->assertTrue($file->exists(), $file->getName() . " exist"); |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
public function testUserPropertyWinsInheritAll(): void |
331
|
|
|
{ |
332
|
|
|
$this->getProject()->setUserProperty("test", "7"); |
333
|
|
|
$this->getProject()->executeTarget("test-property-override-inheritall-start"); |
334
|
|
|
$this->assertInLogs('The value of test is 7'); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
public function testUserPropertyWinsNoInheritAll() |
338
|
|
|
{ |
339
|
|
|
$this->getProject()->setUserProperty("test", "7"); |
340
|
|
|
$this->getProject()->executeTarget("test-property-override-no-inheritall-start"); |
341
|
|
|
$this->assertInLogs('The value of test is 7'); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
public function testOverrideWinsInheritAll() |
345
|
|
|
{ |
346
|
|
|
$this->expectLogContaining('test-property-override-inheritall-start', 'The value of test is 4'); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
public function testOverrideWinsNoInheritAll() |
350
|
|
|
{ |
351
|
|
|
$this->expectLogContaining('test-property-override-no-inheritall-start', 'The value of test is 4'); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Fail due to infinite recursion loop |
356
|
|
|
*/ |
357
|
|
|
public function testInfiniteLoopViaDepends(): void |
358
|
|
|
{ |
359
|
|
|
$this->markTestSkipped('infinite loop could occure'); |
360
|
|
|
// $this->expectBuildException('infinite-loop-via-depends', 'infinite loop'); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
public function testMultiSameProperty(): void |
364
|
|
|
{ |
365
|
|
|
$this->expectLogContaining('multi-same-property', 'prop is two'); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
public function testTopLevelTarget(): void |
369
|
|
|
{ |
370
|
|
|
$this->expectLogContaining('topleveltarget', 'Hello world'); |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|