Completed
Push — master ( 14eeb9...998770 )
by Siad
12:45
created

PhingTaskTest.php$0 ➔ testDoNotInheritBasedir()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 5
rs 10
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
/**
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