Passed
Push — master ( 275e36...8c0fb0 )
by Siad
13:40
created

SonarConfigurationFileParserTest::testFileWithNL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 25
rs 9.7666
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
 *
22
 * @author Bernhard Mendl <[email protected]>
23
 * @package phing.tasks.ext.sonar
24
 */
25
class SonarConfigurationFileParserTest extends BuildFileTest
26
{
27
    protected function setUp(): void
28
    {
29
        $buildXmlFile = PHING_TEST_BASE . '/etc/tasks/ext/sonar/ConfigurationFileParserTest.xml';
30
        $this->configureProject($buildXmlFile);
31
    }
32
33
    private function initParser($fileName)
34
    {
35
        $fullFileName = PHING_TEST_BASE . '/etc/tasks/ext/sonar/properties/' . $fileName . '.properties';
36
        $parser = new SonarConfigurationFileParser($fullFileName, $this->getProject());
37
38
        return $parser;
39
    }
40
41
    public function testConstructFileIsNullThrowsException()
42
    {
43
        $file = null;
44
45
        $this->expectException(BuildException::class);
46
47
        new SonarConfigurationFileParser($file, $this->getProject());
48
    }
49
50
    public function testConstructFileIsEmptyFhrowsException()
51
    {
52
        $file = '';
53
54
        $this->expectException(BuildException::class);
55
56
        new SonarConfigurationFileParser($file, $this->getProject());
57
    }
58
59
    public function testConstructFileDoesNotExistThrowsException()
60
    {
61
        $file = 'ThisFileDoesNotExist';
62
        $parser = new SonarConfigurationFileParser($file, $this->getProject());
63
64
        $this->expectException(BuildException::class);
65
66
        $parser->parse();
67
    }
68
69
    public function testEmptyFile()
70
    {
71
        $parser = $this->initParser('test-empty-file');
72
73
        $properties = $parser->parse();
74
75
        if (method_exists($this, 'assertIsArray')) {
76
            $this->assertIsArray($properties);
77
        } else {
78
            $this->assertIsArray($properties);
79
        }
80
        $this->assertEmpty($properties);
81
    }
82
83
    public function testPropertyWithColonAndWithoutWhitespace()
84
    {
85
        $parser = $this->initParser('test-property-with-colon-and-without-whitespace');
86
87
        $properties = $parser->parse();
88
89
        $this->assertArrayHasKey('foo', $properties);
90
        $this->assertContains('bar', $properties);
91
    }
92
93
    public function testPropertyWithColonAndWithWhitespace()
94
    {
95
        $parser = $this->initParser('test-property-with-colon-and-with-whitespace');
96
97
        $properties = $parser->parse();
98
99
        $this->assertArrayHasKey('foo', $properties);
100
        $this->assertContains('bar', $properties);
101
    }
102
103
    public function testPropertyWithEqualsSignAndWithoutWhitespace()
104
    {
105
        $parser = $this->initParser('test-property-with-equals-sign-and-without-whitespace');
106
107
        $properties = $parser->parse();
108
109
        $this->assertArrayHasKey('foo', $properties);
110
        $this->assertContains('bar', $properties);
111
    }
112
113
    public function testPropertyWithEqualsSignAndWithWhitespace()
114
    {
115
        $parser = $this->initParser('test-property-with-equals-sign-and-with-whitespace');
116
117
        $properties = $parser->parse();
118
119
        $this->assertArrayHasKey('foo', $properties);
120
        $this->assertContains('bar', $properties);
121
    }
122
123
    public function testCommentAtBeginOfLine()
124
    {
125
        $parser = $this->initParser('test-property-with-comment-at-begin-of-line');
126
127
        $properties = $parser->parse();
128
129
        $this->assertArrayNotHasKey('comment', $properties);
130
    }
131
132
    public function testCommentInMiddleOfLine()
133
    {
134
        $parser = $this->initParser('test-property-with-comment-in-middle-of-line');
135
136
        $properties = $parser->parse();
137
138
        $this->assertArrayNotHasKey('comment', $properties);
139
    }
140
141
    public function testPropertyHasMultiLineValue()
142
    {
143
        $parser = $this->initParser('test-multiline-property');
144
145
        $properties = $parser->parse();
146
147
        $this->assertArrayHasKey('foo', $properties);
148
        $this->assertContains('This is a multi-line comment.', $properties);
149
    }
150
151
    public function testPropertyEndsWithABackSlash()
152
    {
153
        $parser = $this->initParser('test-property-with-trailing-backslash');
154
155
        $properties = $parser->parse();
156
157
        $this->assertArrayHasKey('foo', $properties);
158
        $this->assertArrayHasKey('bar', $properties);
159
        $this->assertContains('This is not a multi-line property, but ends with a backslash\\', $properties);
160
        $this->assertContains('baz', $properties);
161
    }
162
163
    public function testPropertyHasMultiLineValueIntermediateLineIsEmpty()
164
    {
165
        $parser = $this->initParser('test-multiline-property-with-empty-intermediate-line');
166
167
        $properties = $parser->parse();
168
169
        $this->assertArrayHasKey('foo', $properties);
170
        $this->assertContains('This is a multi-line comment.', $properties);
171
    }
172
173
    /*
174
     * Tests property file with Newline(LF) line termination
175
     *
176
     * @covers SonarConfigurationFileParser::parse
177
     */
178
    public function testFileWithNL()
179
    {
180
        $tmpFile = tempnam(sys_get_temp_dir(), 'cfp');
181
182
        $fh = fopen($tmpFile, 'w');
183
184
        if (false !== $fh) {
185
            register_shutdown_function(function () use ($tmpFile) {
186
                unlink($tmpFile);
187
            });
188
189
            fwrite($fh, "foo:bar\nbrown:cow\n");
190
            fclose($fh);
191
192
            $parser = new SonarConfigurationFileParser($tmpFile, $this->getProject());
193
194
            $properties = $parser->parse();
195
196
            $this->assertArrayHasKey('foo', $properties);
197
            $this->assertContains('bar', $properties);
198
199
            $this->assertArrayHasKey('brown', $properties);
200
            $this->assertContains('cow', $properties);
201
        } else {
202
            $this->fail('Failed to create temporary file');
203
        }
204
    }
205
206
    /*
207
     * Tests property file with CarriageReturn/LineFeed (CRLF) line termination
208
     *
209
     * @covers SonarConfigurationFileParser::parse
210
     */
211
    public function testFileWithCRLF()
212
    {
213
        $tmpFile = tempnam(sys_get_temp_dir(), 'cfp');
214
215
        $fh = fopen($tmpFile, 'w');
216
        if (false !== $fh) {
217
            register_shutdown_function(function () use ($tmpFile) {
218
                unlink($tmpFile);
219
            });
220
221
            fwrite($fh, "rag:doll\r\nhouse:cat\r\n");
222
            fclose($fh);
223
224
            $parser = new SonarConfigurationFileParser($tmpFile, $this->getProject());
225
226
            $properties = $parser->parse();
227
228
            $this->assertArrayHasKey('rag', $properties);
229
            $this->assertContains('doll', $properties);
230
231
            $this->assertArrayHasKey('house', $properties);
232
            $this->assertContains('cat', $properties);
233
        } else {
234
            $this->fail('Failed to create temporary file');
235
        }
236
    }
237
}
238