77namespace test \unit \Ingenerator \PHPUtils \CSV ;
88
99
10+ use ErrorException ;
1011use Ingenerator \PHPUtils \CSV \CSVWriter ;
1112use Ingenerator \PHPUtils \CSV \MismatchedSchemaException ;
13+ use LogicException ;
14+ use PHPUnit \Framework \Attributes \TestWith ;
1215use PHPUnit \Framework \TestCase ;
16+ use function fclose ;
17+ use function fgetcsv ;
18+ use function file_get_contents ;
19+ use function fopen ;
20+ use function fread ;
21+ use function is_resource ;
22+ use function rewind ;
23+ use function stream_get_contents ;
24+ use function strlen ;
25+ use function sys_get_temp_dir ;
26+ use function tempnam ;
27+ use function unlink ;
1328
1429class CSVWriterTest extends TestCase
1530{
@@ -22,14 +37,14 @@ public function test_it_is_initialisable()
2237 public function test_it_throws_if_file_cannot_be_opened ()
2338 {
2439 $ subject = $ this ->newSubject ();
25- $ this ->expectException (\ ErrorException::class);
40+ $ this ->expectException (ErrorException::class);
2641 $ subject ->open ('/invalid_csv_file ' );
2742 }
2843
2944 public function test_it_throws_if_writing_before_opening_file ()
3045 {
3146 $ subject = $ this ->newSubject ();
32- $ this ->expectException (\ LogicException::class);
47+ $ this ->expectException (LogicException::class);
3348 $ subject ->write (['any ' => 'junk ' ]);
3449 }
3550
@@ -38,56 +53,56 @@ public function test_it_throws_if_writing_after_closing_file()
3853 $ subj = $ this ->newSubject ();
3954 $ subj ->open ('php://temp ' );
4055 $ subj ->close ();
41- $ this ->expectException (\ LogicException::class);
56+ $ this ->expectException (LogicException::class);
4257 $ subj ->write (['any ' => 'content ' ]);
4358 }
4459
4560 public function test_open_accepts_and_allows_writing_to_existing_stream_resource ()
4661 {
47- $ file = \ fopen ('php://memory ' , 'w ' );
62+ $ file = fopen ('php://memory ' , 'w ' );
4863 try {
4964 $ subj = $ this ->newSubject ();
5065 $ subj ->open ($ file );
5166 $ subj ->write (['some ' => 'csv ' ]);
52- \ rewind ($ file );
53- $ this ->assertNotEmpty (\ stream_get_contents ($ file ));
67+ rewind ($ file );
68+ $ this ->assertNotEmpty (stream_get_contents ($ file ));
5469 } finally {
55- \ fclose ($ file );
70+ fclose ($ file );
5671 }
5772 }
5873
5974 public function test_it_throws_if_writing_to_externally_closed_resource ()
6075 {
61- $ file = \ fopen ('php://memory ' , 'w ' );
76+ $ file = fopen ('php://memory ' , 'w ' );
6277 try {
6378 $ subj = $ this ->newSubject ();
6479 $ subj ->open ($ file );
6580 } finally {
66- \ fclose ($ file );
81+ fclose ($ file );
6782 }
6883
69- $ this ->expectException (\ LogicException::class);
84+ $ this ->expectException (LogicException::class);
7085 $ subj ->write (['some ' => 'csv ' ]);
7186 }
7287
7388 public function test_it_can_open_and_write_to_filename ()
7489 {
75- $ name = \ tempnam (\ sys_get_temp_dir (), 'csv-test.csv ' );
90+ $ name = tempnam (sys_get_temp_dir (), 'csv-test.csv ' );
7691 try {
7792 $ subj = $ this ->newSubject ();
7893 $ subj ->open ($ name , []);
7994 $ subj ->write (['a ' => 'b ' , '1 ' => 2 ]);
8095 $ subj ->write (['a ' => 'c ' , '1 ' => 3 ]);
8196 $ subj ->close ();
82- $ this ->assertEquals ("a,1 \nb,2 \nc,3 \n" , \ file_get_contents ($ name ));
97+ $ this ->assertEquals ("a,1 \nb,2 \nc,3 \n" , file_get_contents ($ name ));
8398 } finally {
84- \ unlink ($ name );
99+ unlink ($ name );
85100 }
86101 }
87102
88103 public function test_it_writes_column_headers_before_first_row ()
89104 {
90- $ file = \ fopen ('php://memory ' , 'w ' );
105+ $ file = fopen ('php://memory ' , 'w ' );
91106 $ subj = $ this ->newSubject ();
92107 $ subj ->open ($ file );
93108 $ subj ->write (['our ' => 'data ' , 'is ' => 'here ' ]);
@@ -98,12 +113,12 @@ public function test_it_writes_column_headers_before_first_row()
98113 ],
99114 $ file
100115 );
101- \ fclose ($ file );
116+ fclose ($ file );
102117 }
103118
104119 public function test_it_does_not_write_column_headers_before_subsequent_rows ()
105120 {
106- $ file = \ fopen ('php://memory ' , 'w ' );
121+ $ file = fopen ('php://memory ' , 'w ' );
107122 $ subj = $ this ->newSubject ();
108123 $ subj ->open ($ file );
109124 $ subj ->write (['our ' => 'data ' , 'is ' => 'here ' ]);
@@ -116,62 +131,58 @@ public function test_it_does_not_write_column_headers_before_subsequent_rows()
116131 ],
117132 $ file
118133 );
119- \ fclose ($ file );
134+ fclose ($ file );
120135 }
121136
122- /**
123- * @testWith [true]
124- * [false]
125- */
137+ #[TestWith([true ])]
138+ #[TestWith([false ])]
126139 public function test_it_optionally_writes_byte_order_mark_at_start_of_file ($ write_bom )
127140 {
128- $ file = \ fopen ('php://memory ' , 'w ' );
141+ $ file = fopen ('php://memory ' , 'w ' );
129142 $ subj = $ this ->newSubject ();
130143 $ subj ->open ($ file , ['write_utf8_bom ' => $ write_bom ]);
131144 $ subj ->write (['first ' => 'row ' ]);
132- \ rewind ($ file );
145+ rewind ($ file );
133146 if ($ write_bom ) {
134- $ this ->assertSame (CSVWriter::UTF8_BOM , \ fread ($ file , \ strlen (CSVWriter::UTF8_BOM )));
147+ $ this ->assertSame (CSVWriter::UTF8_BOM , fread ($ file , strlen (CSVWriter::UTF8_BOM )));
135148 }
136- $ this ->assertSame (['first ' ], \ fgetcsv ($ file ));
149+ $ this ->assertSame (['first ' ], fgetcsv ($ file ));
137150 }
138151
139- /**
140- * @testWith [{"is": "jumbled", "our": "up"}]
141- * [{"our": "things", "went": "bad"}]
142- */
152+ #[TestWith([['is ' => 'jumbled ' , 'our ' => 'up ' ]])]
153+ #[TestWith([['our ' => 'things ' , 'went ' => 'bad ' ]])]
143154 public function test_it_throws_if_subsequent_row_headers_do_not_match ($ second_row )
144155 {
145- $ file = \ fopen ('php://memory ' , 'w ' );
156+ $ file = fopen ('php://memory ' , 'w ' );
146157 try {
147158 $ subj = $ this ->newSubject ();
148159 $ subj ->open ($ file );
149160 $ subj ->write (['our ' => 'data ' , 'is ' => 'here ' ]);
150161 $ this ->expectException (MismatchedSchemaException::class);
151162 $ subj ->write ($ second_row );
152163 } finally {
153- \ fclose ($ file );
164+ fclose ($ file );
154165 }
155166 }
156167
157168 public function test_it_does_not_close_externally_provided_stream ()
158169 {
159- $ file = \ fopen ('php://memory ' , 'w ' );
170+ $ file = fopen ('php://memory ' , 'w ' );
160171 try {
161172 $ subj = $ this ->newSubject ();
162173 $ subj ->open ($ file );
163174 $ subj ->close ();
164- $ this ->assertTrue (\ is_resource ($ file ));
175+ $ this ->assertTrue (is_resource ($ file ));
165176 } finally {
166- \ fclose ($ file );
177+ fclose ($ file );
167178 }
168179 }
169180
170181 protected function assertCSVContent (array $ expect , $ file )
171182 {
172- \ rewind ($ file );
183+ rewind ($ file );
173184 $ actual = [];
174- while ($ row = \ fgetcsv ($ file )) {
185+ while ($ row = fgetcsv ($ file )) {
175186 $ actual [] = $ row ;
176187 }
177188
0 commit comments