Passed
Push — master ( 8f9505...f00c46 )
by Michiel
07:03
created

PhingTaskTest.php$0 ➔ baseDirs()   B

Complexity

Conditions 2

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 65
rs 8.7636
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A PhingTaskTest.php$0 ➔ targetStarted() 0 13 4
A PhingTaskTest.php$0 ➔ buildFinished() 0 2 1
A PhingTaskTest.php$0 ➔ taskFinished() 0 2 1
A PhingTaskTest.php$0 ➔ __construct() 0 3 1
A PhingTaskTest.php$0 ➔ taskStarted() 0 2 1
A PhingTaskTest.php$0 ➔ messageLogged() 0 2 1
A PhingTaskTest.php$0 ➔ targetFinished() 0 2 1
A PhingTaskTest.php$0 ➔ getError() 0 3 1
A PhingTaskTest.php$0 ➔ buildStarted() 0 2 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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