|
3 | 3 | namespace Keboola\Csv\Tests; |
4 | 4 |
|
5 | 5 | use Keboola\Csv\CsvFile; |
| 6 | +use Keboola\Csv\Exception; |
6 | 7 | use PHPUnit\Framework\TestCase; |
7 | 8 |
|
8 | 9 | class CsvFileTest extends TestCase |
@@ -228,6 +229,83 @@ public function testWrite() |
228 | 229 | foreach ($rows as $row) { |
229 | 230 | $csvFile->writeRow($row); |
230 | 231 | } |
| 232 | + $data = file_get_contents($fileName); |
| 233 | + self::assertEquals( |
| 234 | + implode( |
| 235 | + "\n", |
| 236 | + [ |
| 237 | + '"col1","col2"', |
| 238 | + '"line without enclosure","second column"', |
| 239 | + '"enclosure "" in column","hello \\"', |
| 240 | + '"line with enclosure","second column"', |
| 241 | + '"column with enclosure "", and comma inside text","second column enclosure in text """', |
| 242 | + "\"columns with\nnew line\",\"columns with\ttab\"", |
| 243 | + '"column with \\n \\t \\\\","second col"', |
| 244 | + '', |
| 245 | + ] |
| 246 | + ), |
| 247 | + $data |
| 248 | + ); |
| 249 | + @unlink($fileName); |
| 250 | + } |
| 251 | + |
| 252 | + public function testWriteInvalidObject() |
| 253 | + { |
| 254 | + $fileName = __DIR__ . '/data/_out.csv'; |
| 255 | + if (file_exists($fileName)) { |
| 256 | + unlink($fileName); |
| 257 | + } |
| 258 | + |
| 259 | + $csvFile = new CsvFile($fileName); |
| 260 | + |
| 261 | + $rows = [ |
| 262 | + [ |
| 263 | + 'col1', 'col2', |
| 264 | + ], |
| 265 | + [ |
| 266 | + '1', new \stdClass(), |
| 267 | + ], |
| 268 | + ]; |
| 269 | + |
| 270 | + $csvFile->writeRow($rows[0]); |
| 271 | + self::expectException(Exception::class); |
| 272 | + self::expectExceptionMessage("Cannot write object into a column"); |
| 273 | + $csvFile->writeRow($rows[1]); |
| 274 | + @unlink($fileName); |
| 275 | + } |
| 276 | + |
| 277 | + public function testWriteValidObject() |
| 278 | + { |
| 279 | + $fileName = __DIR__ . '/data/_out.csv'; |
| 280 | + if (file_exists($fileName)) { |
| 281 | + unlink($fileName); |
| 282 | + } |
| 283 | + |
| 284 | + $csvFile = new CsvFile($fileName); |
| 285 | + $rows = [ |
| 286 | + [ |
| 287 | + 'col1', 'col2', |
| 288 | + ], |
| 289 | + [ |
| 290 | + '1', new StringObject(), |
| 291 | + ], |
| 292 | + ]; |
| 293 | + |
| 294 | + $csvFile->writeRow($rows[0]); |
| 295 | + $csvFile->writeRow($rows[1]); |
| 296 | + $data = file_get_contents($fileName); |
| 297 | + self::assertEquals( |
| 298 | + implode( |
| 299 | + "\n", |
| 300 | + [ |
| 301 | + '"col1","col2"' , |
| 302 | + '"1","me string"', |
| 303 | + '', |
| 304 | + ] |
| 305 | + ), |
| 306 | + $data |
| 307 | + ); |
| 308 | + @unlink($fileName); |
231 | 309 | } |
232 | 310 |
|
233 | 311 | public function testIterator() |
|
0 commit comments