Passed
Push — main ( 7d351e...d05000 )
by Michiel
07:03
created

IniFileParserTest::provideIniFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 67
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 67
rs 9.328
c 0
b 0
f 0
cc 1
nc 1
nop 0

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
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing\Test\Io;
22
23
use org\bovigo\vfs\vfsStream;
24
use Phing\Io\File;
25
use Phing\Io\IniFileParser;
26
use Phing\Io\IOException;
27
use PHPUnit\Framework\TestCase;
28
29
/**
30
 * @author Fabian Grutschus <[email protected]>
31
 *
32
 * @internal
33
 */
34
class IniFileParserTest extends TestCase
35
{
36
    private $parser;
37
    private $root;
38
39
    protected function setUp(): void
40
    {
41
        if (! class_exists(vfsStream::class)) {
42
            $this->markTestSkipped('This test depends on the mikey179/vfsstream package being installed.');
43
        }
44
        $this->parser = new IniFileParser();
45
        $this->root = vfsStream::setUp();
46
    }
47
48
    /**
49
     * @dataProvider provideIniFilesProvider
50
     * @covers       \IniFileParser::inVal
51
     * @covers       \IniFileParser::parseFile
52
     *
53
     * @param mixed $data
54
     * @param mixed $expected
55
     *
56
     * @throws IOException
57
     * @throws IOException
58
     */
59
    #[\PHPUnit\Framework\Attributes\DataProvider('provideIniFilesProvider')]
60
    public function testParseFile($data, $expected): void
61
    {
62
        $file = $this->root->url() . '/test';
63
        file_put_contents($file, $data);
64
65
        $phingFile = new File($file);
66
        $this->assertSame($expected, $this->parser->parseFile($phingFile));
67
    }
68
69
    /**
70
     * @covers \IniFileParser::parseFile
71
     */
72
    public function testParseFileCouldntOpenFile(): void
73
    {
74
        $phingFile = new File(uniqid('', true));
75
76
        $this->expectException(IOException::class);
77
78
        $this->parser->parseFile($phingFile);
79
    }
80
81
    public static function provideIniFilesProvider(): array
82
    {
83
        return [
84
            [
85
                'data' => "property = test\nproperty2 = test2\nproperty3 = test3\n",
86
                'expected' => [
87
                    'property' => 'test',
88
                    'property2' => 'test2',
89
                    'property3' => 'test3',
90
                ],
91
            ],
92
            [
93
                'data' => "property = test\r\nproperty2 = test2\r\nproperty3 = test3\r\n",
94
                'expected' => [
95
                    'property' => 'test',
96
                    'property2' => 'test2',
97
                    'property3' => 'test3',
98
                ],
99
            ],
100
            [
101
                'data' => "property = test,\\\ntest2,\\\ntest3\n",
102
                'expected' => [
103
                    'property' => 'test,test2,test3',
104
                ],
105
            ],
106
            [
107
                'data' => "property = test,\\\r\ntest2,\\\r\ntest3\r\n",
108
                'expected' => [
109
                    'property' => 'test,test2,test3',
110
                ],
111
            ],
112
            [
113
                'data' => '# property = test',
114
                'expected' => [],
115
            ],
116
            [
117
                'data' => '   # property = test',
118
                'expected' => [],
119
            ],
120
            [
121
                'data' => '; property = test',
122
                'expected' => [],
123
            ],
124
            [
125
                'data' => 'property=test',
126
                'expected' => [
127
                    'property' => 'test',
128
                ],
129
            ],
130
            [
131
                'data' => 'property = true',
132
                'expected' => [
133
                    'property' => 'true',
134
                ],
135
            ],
136
            [
137
                'data' => 'property = false',
138
                'expected' => [
139
                    'property' => 'false',
140
                ],
141
            ],
142
            [
143
                'data' => "[app]\napp.uno=foo\napp.dos=bar\napp.tres=baz\n",
144
                'expected' => [
145
                    'app.uno' => 'foo',
146
                    'app.dos' => 'bar',
147
                    'app.tres' => 'baz',
148
                ],
149
            ],
150
        ];
151
    }
152
}
153