22
33namespace Tests \Unit ;
44
5- use EnvEditor \EnvFile \EOLType ;
65use EnvEditor \Parser ;
76use Tests \TestCase ;
87
98class ParserTest extends TestCase
109{
11-
12- public function testFileEOLType (): void
13- {
14-
15- $ parser = new Parser ();
16-
17- $ win = "#test \r\n#test " ;
18- $ unix = "#test \n#test " ;
1910
20- $ this ->assertEquals (EOLType::WINDOWS , $ parser ->parse ($ win )->EOL );
21- $ this ->assertEquals (EOLType::UNIX , $ parser ->parse ($ unix )->EOL );
11+ private Parser $ parser ;
2212
23- $ parser -> EOL = EOLType:: UNIX ;
13+ private string $ newLine = " [TEST_EOL] " ;
2414
25- $ this ->assertEquals (EOLType::UNIX , $ parser ->parse ($ win )->EOL );
15+ protected function setUp (): void
16+ {
17+ parent ::setUp ();
2618
19+ $ detector = $ this ->createMock (Parser \EOLTypeDetector::class);
20+ $ detector ->method ('detect ' )->willReturn ($ this ->newLine );
21+ $ this ->parser = new Parser ($ detector );
2722 }
2823
29- /**
30- * @depends testFileEOLType
31- */
3224 public function testSimpleBlockCount (): void
3325 {
34- $ parser = new Parser () ;
26+ $ content = " #test { $ this -> newLine } #test " ;
3527
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 );
4129 }
4230
4331 /**
4432 * @depends testSimpleBlockCount
4533 */
4634 public function testComplexBlockCount (): void
4735 {
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 );
5238
53- $ invalidValueString = "#test \n X =VALUE\n foo 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 );
5541 }
5642
5743 /**
5844 * @depends testSimpleBlockCount
5945 */
6046 public function testCommentBlockText (): void
6147 {
62- $ parser = new Parser ();
63-
64- $ blocks = $ parser ->parse ("#test1 \n#test2 " )->blocks ;
48+ $ blocks = $ this ->parser ->parse ("#test1 {$ this ->newLine }#test2 " )->blocks ;
6549
6650 $ this ->assertEquals ("test1 " , $ blocks [0 ]->text );
6751 $ this ->assertEquals ("test2 " , $ blocks [1 ]->text );
@@ -73,10 +57,8 @@ public function testCommentBlockText(): void
7357 */
7458 public function testVariableBlockContent (): void
7559 {
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 ;
8062
8163 $ this ->assertEquals ("X " , $ blocks [0 ]->key );
8264 $ this ->assertEquals ("VALUE " , $ blocks [0 ]->value );
@@ -86,7 +68,7 @@ public function testVariableBlockContent(): void
8668 $ this ->assertEquals ("' " , $ blocks [1 ]->value ->quote );
8769
8870 $ this ->assertEquals ("Z " , $ blocks [2 ]->key );
89- $ this ->assertEquals ("foo \n bar " , $ blocks [2 ]->value );
71+ $ this ->assertEquals ("foo { $ this -> newLine } bar " , $ blocks [2 ]->value );
9072 $ this ->assertEquals ('" ' , $ blocks [2 ]->value ->quote );
9173
9274 }
@@ -96,9 +78,7 @@ public function testVariableBlockContent(): void
9678 */
9779 public function testVariableWhiteSpaces (): void
9880 {
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 ;
10282
10383 $ this ->assertEquals ("X " , $ blocks [0 ]->key );
10484 $ this ->assertEquals ("foo " , $ blocks [0 ]->value );
@@ -122,8 +102,7 @@ public function testVariableWhiteSpaces(): void
122102 */
123103 public function testUnknown (): void
124104 {
125- $ parser = new Parser ();
126- $ blocks = $ parser ->parse ("??? " )->blocks ;
105+ $ blocks = $ this ->parser ->parse ("??? " )->blocks ;
127106 $ this ->assertEquals ("??? " , $ blocks [0 ]->content );
128107 }
129108
@@ -132,8 +111,7 @@ public function testUnknown(): void
132111 */
133112 public function testEmpty (): void
134113 {
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 ;
137115 $ this ->assertEquals ("" , $ blocks [1 ]->content );
138116 }
139117
@@ -142,16 +120,13 @@ public function testEmpty(): void
142120 */
143121 public function testEmptyLast (): void
144122 {
145- $ parser = new Parser ();
146- $ blocks = $ parser ->parse ("#test \n" )->blocks ;
123+ $ blocks = $ this ->parser ->parse ("#test {$ this ->newLine }" )->blocks ;
147124 $ this ->assertEquals ("" , $ blocks [1 ]->content );
148125 }
149126
150127 public function testEmptyFile (): void
151128 {
152- $ parser = new Parser ();
153-
154- $ blocks = $ parser ->parse ("" )->blocks ;
129+ $ blocks = $ this ->parser ->parse ("" )->blocks ;
155130
156131 $ this ->assertCount (1 , $ blocks );
157132 $ this ->assertEquals ("" , $ blocks [0 ]->content );
0 commit comments