2
2
3
3
namespace Tests\Unit;
4
4
5
- use EnvEditor\EnvFile\EOLType;
6
5
use EnvEditor\Parser;
7
6
use Tests\TestCase;
8
7
9
8
class ParserTest extends TestCase
10
9
{
11
-
12
- public function testFileEOLType(): void
13
- {
14
-
15
- $parser = new Parser();
16
-
17
- $win = "#test\r\n#test";
18
- $unix = "#test\n#test";
19
10
20
- $this->assertEquals(EOLType::WINDOWS, $parser->parse($win)->EOL);
21
- $this->assertEquals(EOLType::UNIX, $parser->parse($unix)->EOL);
11
+ private Parser $parser;
22
12
23
- $parser->EOL = EOLType::UNIX ;
13
+ private string $newLine = "[TEST_EOL]" ;
24
14
25
- $this->assertEquals(EOLType::UNIX, $parser->parse($win)->EOL);
15
+ protected function setUp(): void
16
+ {
17
+ parent::setUp();
26
18
19
+ $detector = $this->createMock(Parser\EOLTypeDetector::class);
20
+ $detector->method('detect')->willReturn($this->newLine);
21
+ $this->parser = new Parser($detector);
27
22
}
28
23
29
- /**
30
- * @depends testFileEOLType
31
- */
32
24
public function testSimpleBlockCount(): void
33
25
{
34
- $parser = new Parser() ;
26
+ $content = "#test{$this->newLine}#test" ;
35
27
36
- $win = "#test\r\n#test";
37
- $unix = "#test\n#test";
38
-
39
- $this->assertCount(2, $parser->parse($win)->blocks);
40
- $this->assertCount(2, $parser->parse($unix)->blocks);
28
+ $this->assertCount(2, $this->parser->parse($content)->blocks);
41
29
}
42
30
43
31
/**
44
32
* @depends testSimpleBlockCount
45
33
*/
46
34
public function testComplexBlockCount(): void
47
35
{
48
- $parser = new Parser();
49
-
50
- $multilineString = "#test\nX=VALUE\nY='foo bar'\nZ=\"foo\nbar\"";
51
- $this->assertCount(4, $parser->parse($multilineString)->blocks);
36
+ $multilineString = "#test{$this->newLine}X=VALUE{$this->newLine}Y='foo bar'{$this->newLine}Z=\"foo{$this->newLine}bar\"";
37
+ $this->assertCount(4, $this->parser->parse($multilineString)->blocks);
52
38
53
- $invalidValueString = "#test\nX =VALUE\nfoo bar";
54
- $this->assertCount(3, $parser->parse($invalidValueString)->blocks);
39
+ $invalidValueString = "#test{$this->newLine}X =VALUE{$this->newLine}foo bar";
40
+ $this->assertCount(3, $this-> parser->parse($invalidValueString)->blocks);
55
41
}
56
42
57
43
/**
58
44
* @depends testSimpleBlockCount
59
45
*/
60
46
public function testCommentBlockText(): void
61
47
{
62
- $parser = new Parser();
63
-
64
- $blocks = $parser->parse("#test1\n#test2")->blocks;
48
+ $blocks = $this->parser->parse("#test1{$this->newLine}#test2")->blocks;
65
49
66
50
$this->assertEquals("test1", $blocks[0]->text);
67
51
$this->assertEquals("test2", $blocks[1]->text);
@@ -73,10 +57,8 @@ public function testCommentBlockText(): void
73
57
*/
74
58
public function testVariableBlockContent(): void
75
59
{
76
- $parser = new Parser();
77
-
78
- $multilineString = "X=VALUE\nY='foo bar'\nZ=\"foo\nbar\"";
79
- $blocks = $parser->parse($multilineString)->blocks;
60
+ $multilineString = "X=VALUE{$this->newLine}Y='foo bar'{$this->newLine}Z=\"foo{$this->newLine}bar\"";
61
+ $blocks = $this->parser->parse($multilineString)->blocks;
80
62
81
63
$this->assertEquals("X", $blocks[0]->key);
82
64
$this->assertEquals("VALUE", $blocks[0]->value);
@@ -86,7 +68,7 @@ public function testVariableBlockContent(): void
86
68
$this->assertEquals("'", $blocks[1]->value->quote);
87
69
88
70
$this->assertEquals("Z", $blocks[2]->key);
89
- $this->assertEquals("foo\nbar ", $blocks[2]->value);
71
+ $this->assertEquals("foo{$this->newLine}bar ", $blocks[2]->value);
90
72
$this->assertEquals('"', $blocks[2]->value->quote);
91
73
92
74
}
@@ -96,9 +78,7 @@ public function testVariableBlockContent(): void
96
78
*/
97
79
public function testVariableWhiteSpaces(): void
98
80
{
99
- $parser = new Parser();
100
-
101
- $blocks = $parser->parse("X =foo\n\tY = 'bar' ")->blocks;
81
+ $blocks = $this->parser->parse("X =foo{$this->newLine}\tY = 'bar' ")->blocks;
102
82
103
83
$this->assertEquals("X", $blocks[0]->key);
104
84
$this->assertEquals("foo", $blocks[0]->value);
@@ -122,8 +102,7 @@ public function testVariableWhiteSpaces(): void
122
102
*/
123
103
public function testUnknown(): void
124
104
{
125
- $parser = new Parser();
126
- $blocks = $parser->parse("???")->blocks;
105
+ $blocks = $this->parser->parse("???")->blocks;
127
106
$this->assertEquals("???", $blocks[0]->content);
128
107
}
129
108
@@ -132,8 +111,7 @@ public function testUnknown(): void
132
111
*/
133
112
public function testEmpty(): void
134
113
{
135
- $parser = new Parser();
136
- $blocks = $parser->parse("#test\n\n#test")->blocks;
114
+ $blocks = $this->parser->parse("#test{$this->newLine}{$this->newLine}#test")->blocks;
137
115
$this->assertEquals("", $blocks[1]->content);
138
116
}
139
117
@@ -142,16 +120,13 @@ public function testEmpty(): void
142
120
*/
143
121
public function testEmptyLast(): void
144
122
{
145
- $parser = new Parser();
146
- $blocks = $parser->parse("#test\n")->blocks;
123
+ $blocks = $this->parser->parse("#test{$this->newLine}")->blocks;
147
124
$this->assertEquals("", $blocks[1]->content);
148
125
}
149
126
150
127
public function testEmptyFile(): void
151
128
{
152
- $parser = new Parser();
153
-
154
- $blocks = $parser->parse("")->blocks;
129
+ $blocks = $this->parser->parse("")->blocks;
155
130
156
131
$this->assertCount(1, $blocks);
157
132
$this->assertEquals("", $blocks[0]->content);
0 commit comments