8000 [Serializer] Add CsvEncoder tests for PHP 7.4 · symfony/symfony@760354d · GitHub
[go: up one dir, main page]

Skip to content

Commit 760354d

Browse files
ro0NLfabpot
authored andcommitted
[Serializer] Add CsvEncoder tests for PHP 7.4
1 parent 293a22a commit 760354d

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

src/Symfony/Component/Serializer/Encoder/CsvEncoder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
3939
* @param string $escapeChar
4040
* @param string $keySeparator
4141
*/
42-
public function __construct($delimiter = ',', $enclosure = '"', $escapeChar = '\\', $keySeparator = '.')
42+
public function __construct($delimiter = ',', $enclosure = '"', $escapeChar = '', $keySeparator = '.')
4343
{
44+
if ('' === $escapeChar && \PHP_VERSION_ID < 70400) {
45+
$escapeChar = '\\';
46+
}
47+
4448
$this->delimiter = $delimiter;
4549
$this->enclosure = $enclosure;
4650
$this->escapeChar = $escapeChar;

src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,51 @@ public function testTrueFalseValues()
3636
'int' => 2,
3737
'false' => false,
3838
'true' => true,
39+
'int_one' => 1,
40+
'string_one' => '1',
3941
];
4042

4143
// Check that true and false are appropriately handled
42-
$this->assertEquals(<<<'CSV'
43-
string,int,false,true
44-
foo,2,0,1
44+
$this->assertSame($csv = <<<'CSV'
45+
string,int,false,true,int_one,string_one
46+
foo,2,0,1,1,1
47+
48+
CSV
49+
, $this->encoder->encode($data, 'csv'));
50+
51+
$this->assertSame([
52+
'string' => 'foo',
53+
'int' => '2',
54+
'false' => '0',
55+
'true' => '1',
56+
'int_one' => '1',
57+
'string_one' => '1',
58+
], $this->encoder->decode($csv, 'csv'));
59+
}
60+
61+
/**
62+
* @requires PHP 7.4
63+
*/
64+
public function testDoubleQuotesAndSlashes()
65+
{
66+
$this->assertSame($csv = <<<'CSV'
67+
0,1,2,3,4,5
68+
,"""","foo""","\""",\,foo\
4569

4670
CSV
47-
, $this->encoder->encode($data, 'csv'));
71+
, $this->encoder->encode($data = ['', '"', 'foo"', '\\"', '\\', 'foo\\'], 'csv'));
72+
73+
$this->assertSame($data, $this->encoder->decode($csv, 'csv'));
74+
}
75+
76+
/**
77+
* @requires PHP 7.4
78+
*/
79+
public function testSingleSlash()
80+
{
81+
$this->assertSame($csv = "0\n\\\n", $this->encoder->encode($data = ['\\'], 'csv'));
82+
$this->assertSame($data, $this->encoder->decode($csv, 'csv'));
83+
$this->assertSame($data, $this->encoder->decode(trim($csv), 'csv'));
4884
}
4985

5086
public function testSupportEncoding()

0 commit comments

Comments
 (0)
0