Passed
Push — master ( 972120...1c77fc )
by Michiel
08:19
created

PhingTaskTest::testReferenceInheritance()

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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