@@ -202,6 +202,24 @@ public function testEncodeCustomSettingsPassedInContext()
202
202
]));
203
203
}
204
204
205
+ public function testEncodeCustomSettingsPassedInConstructor ()
206
+ {
207
+ $ encoder = new CsvEncoder ([
208
+ CsvEncoder::DELIMITER_KEY => '; ' ,
209
+ CsvEncoder::ENCLOSURE_KEY => "' " ,
210
+ CsvEncoder::ESCAPE_CHAR_KEY => '| ' ,
211
+ CsvEncoder::KEY_SEPARATOR_KEY => '- ' ,
212
+ ]);
213
+ $ value = ['a ' => 'he \'llo ' , 'c ' => ['d ' => 'foo ' ]];
214
+
215
+ $ this ->assertSame (<<<'CSV'
216
+ a;c-d
217
+ 'he''llo';foo
218
+
219
+ CSV
220
+ , $ encoder ->encode ($ value , 'csv ' ));
221
+ }
222
+
205
223
public function testEncodeEmptyArray ()
206
224
{
207
225
$ this ->assertEquals ("\n\n" , $ this ->encoder ->encode ([], 'csv ' ));
@@ -373,6 +391,15 @@ public function testEncodeWithoutHeader()
373
391
, $ this ->encoder ->encode ([['a ' , 'b ' ], ['c ' , 'd ' ]], 'csv ' , [
374
392
CsvEncoder::NO_HEADERS_KEY => true ,
375
393
]));
394
+ $ encoder = new CsvEncoder ([CsvEncoder::NO_HEADERS_KEY => true ]);
395
+ $ this ->assertSame (<<<'CSV'
396
+ a,b
397
+ c,d
398
+
399
+ CSV
400
+ , $ encoder ->encode ([['a ' , 'b ' ], ['c ' , 'd ' ]], 'csv ' , [
401
+ CsvEncoder::NO_HEADERS_KEY => true ,
402
+ ]));
376
403
}
377
404
378
405
public function testSupportsDecoding ()
@@ -524,6 +551,23 @@ public function testDecodeCustomSettingsPassedInContext()
524
551
]));
525
552
}
526
553
554
+ public function testDecodeCustomSettingsPassedInConstructor ()
555
+ {
556
+ $ encoder = new CsvEncoder ([
557
+ CsvEncoder::DELIMITER_KEY => '; ' ,
558
+ CsvEncoder::ENCLOSURE_KEY => "' " ,
559
+ CsvEncoder::ESCAPE_CHAR_KEY => '| ' ,
560
+ CsvEncoder::KEY_SEPARATOR_KEY => '- ' ,
561
+ CsvEncoder::AS_COLLECTION_KEY => true , // Can be removed in 5.0
562
+ ]);
563
+ $ expected = [['a ' => 'hell \'o ' , 'bar ' => ['baz ' => 'b ' ]]];
564
+ $ this ->assertEquals ($ expected , $ encoder ->decode (<<<'CSV'
565
+ a;bar-baz
566
+ 'hell''o';b;c
567
+ CSV
568
+ , 'csv ' ));
569
+ }
570
+
527
571
public function testDecodeMalformedCollection ()
528
572
{
529
573
$ expected = [
@@ -553,6 +597,15 @@ public function testDecodeWithoutHeader()
553
597
a,b
554
598
c,d
555
599
600
+ CSV
601
+ , 'csv ' , [
602
+ CsvEncoder::NO_HEADERS_KEY => true ,
603
+ ]));
604
+ $ encoder = new CsvEncoder ([CsvEncoder::NO_HEADERS_KEY => true ]);
605
+ $ this ->assertEquals ([['a ' , 'b ' ], ['c ' , 'd ' ]], $ encoder ->decode (<<<'CSV'
606
+ a,b
607
+ c,d
608
+
556
609
CSV
557
610
, 'csv ' , [
558
611
CsvEncoder::NO_HEADERS_KEY => true ,
0 commit comments