|
11 | 11 | use Ingenerator\PHPUtils\CSV\CSVWriter; |
12 | 12 | use Ingenerator\PHPUtils\CSV\MismatchedSchemaException; |
13 | 13 | use LogicException; |
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
14 | 15 | use PHPUnit\Framework\Attributes\TestWith; |
15 | 16 | use PHPUnit\Framework\TestCase; |
| 17 | +use RuntimeException; |
16 | 18 | use function fclose; |
17 | 19 | use function fgetcsv; |
18 | 20 | use function file_get_contents; |
@@ -134,6 +136,64 @@ public function test_it_does_not_write_column_headers_before_subsequent_rows() |
134 | 136 | fclose($file); |
135 | 137 | } |
136 | 138 |
|
| 139 | + public static function providerQuotedHeaders(): array |
| 140 | + { |
| 141 | + $row = ['our data' => 'is at times', 'really big' => 'often']; |
| 142 | + |
| 143 | + return [ |
| 144 | + 'default (with quotes)' => [ |
| 145 | + $row, |
| 146 | + [], |
| 147 | + <<<CSV |
| 148 | + "our data","really big" |
| 149 | + "is at times",often |
| 150 | +
|
| 151 | + CSV, |
| 152 | + ], |
| 153 | + 'with quotes' => [ |
| 154 | + $row, |
| 155 | + ['quote_headers' => true], |
| 156 | + <<<CSV |
| 157 | + "our data","really big" |
| 158 | + "is at times",often |
| 159 | +
|
| 160 | + CSV, |
| 161 | + ], |
| 162 | + 'without quotes' => [ |
| 163 | + $row, |
| 164 | + ['quote_headers' => false], |
| 165 | + <<<CSV |
| 166 | + our data,really big |
| 167 | + "is at times",often |
| 168 | +
|
| 169 | + CSV, |
| 170 | + ], |
| 171 | + ]; |
| 172 | + } |
| 173 | + |
| 174 | + #[DataProvider('providerQuotedHeaders')] |
| 175 | + public function test_it_optionally_writes_column_headers_without_quotes(array $row, array $options, string $expect): void |
| 176 | + { |
| 177 | + $file = fopen('php://memory', 'w'); |
| 178 | + $subj = $this->newSubject(); |
| 179 | + $subj->open($file, $options); |
| 180 | + $subj->write($row); |
| 181 | + rewind($file); |
| 182 | + $this->assertSame($expect, stream_get_contents($file)); |
| 183 | + fclose($file); |
| 184 | + } |
| 185 | + |
| 186 | + public function test_it_throws_if_column_headers_contain_comma_when_unquoted(): void |
| 187 | + { |
| 188 | + $file = fopen('php://memory', 'w'); |
| 189 | + $subj = $this->newSubject(); |
| 190 | + $subj->open($file, ['quote_headers' => false]); |
| 191 | + $this->expectException(RuntimeException::class); |
| 192 | + $this->expectExceptionMessage('Column header `really, really big` cannot contain comma if headers are not quoted'); |
| 193 | + $subj->write(['our data' => 'is, at times', 'really, really big' => 'often']); |
| 194 | + fclose($file); |
| 195 | + } |
| 196 | + |
137 | 197 | #[TestWith([true])] |
138 | 198 | #[TestWith([false])] |
139 | 199 | public function test_it_optionally_writes_byte_order_mark_at_start_of_file($write_bom) |
@@ -189,8 +249,7 @@ protected function assertCSVContent(array $expect, $file) |
189 | 249 | $this->assertSame($expect, $actual, 'CSV content should match'); |
190 | 250 | } |
191 | 251 |
|
192 | | - |
193 | | - protected function newSubject() |
| 252 | + private function newSubject(): CSVWriter |
194 | 253 | { |
195 | 254 | return new CSVWriter(); |
196 | 255 | } |
|
0 commit comments