[Serializer] Add CsvEncoder tests for PHP 7.4
This commit is contained in:
parent
293a22a433
commit
760354d533
@ -39,8 +39,12 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
|
|||||||
* @param string $escapeChar
|
* @param string $escapeChar
|
||||||
* @param string $keySeparator
|
* @param string $keySeparator
|
||||||
*/
|
*/
|
||||||
public function __construct($delimiter = ',', $enclosure = '"', $escapeChar = '\\', $keySeparator = '.')
|
public function __construct($delimiter = ',', $enclosure = '"', $escapeChar = '', $keySeparator = '.')
|
||||||
{
|
{
|
||||||
|
if ('' === $escapeChar && \PHP_VERSION_ID < 70400) {
|
||||||
|
$escapeChar = '\\';
|
||||||
|
}
|
||||||
|
|
||||||
$this->delimiter = $delimiter;
|
$this->delimiter = $delimiter;
|
||||||
$this->enclosure = $enclosure;
|
$this->enclosure = $enclosure;
|
||||||
$this->escapeChar = $escapeChar;
|
$this->escapeChar = $escapeChar;
|
||||||
|
@ -36,15 +36,51 @@ class CsvEncoderTest extends TestCase
|
|||||||
'int' => 2,
|
'int' => 2,
|
||||||
'false' => false,
|
'false' => false,
|
||||||
'true' => true,
|
'true' => true,
|
||||||
|
'int_one' => 1,
|
||||||
|
'string_one' => '1',
|
||||||
];
|
];
|
||||||
|
|
||||||
// Check that true and false are appropriately handled
|
// Check that true and false are appropriately handled
|
||||||
$this->assertEquals(<<<'CSV'
|
$this->assertSame($csv = <<<'CSV'
|
||||||
string,int,false,true
|
string,int,false,true,int_one,string_one
|
||||||
foo,2,0,1
|
foo,2,0,1,1,1
|
||||||
|
|
||||||
CSV
|
CSV
|
||||||
, $this->encoder->encode($data, 'csv'));
|
, $this->encoder->encode($data, 'csv'));
|
||||||
|
|
||||||
|
$this->assertSame([
|
||||||
|
'string' => 'foo',
|
||||||
|
'int' => '2',
|
||||||
|
'false' => '0',
|
||||||
|
'true' => '1',
|
||||||
|
'int_one' => '1',
|
||||||
|
'string_one' => '1',
|
||||||
|
], $this->encoder->decode($csv, 'csv'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 7.4
|
||||||
|
*/
|
||||||
|
public function testDoubleQuotesAndSlashes()
|
||||||
|
{
|
||||||
|
$this->assertSame($csv = <<<'CSV'
|
||||||
|
0,1,2,3,4,5
|
||||||
|
,"""","foo""","\""",\,foo\
|
||||||
|
|
||||||
|
CSV
|
||||||
|
, $this->encoder->encode($data = ['', '"', 'foo"', '\\"', '\\', 'foo\\'], 'csv'));
|
||||||
|
|
||||||
|
$this->assertSame($data, $this->encoder->decode($csv, 'csv'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 7.4
|
||||||
|
*/
|
||||||
|
public function testSingleSlash()
|
||||||
|
{
|
||||||
|
$this->assertSame($csv = "0\n\\\n", $this->encoder->encode($data = ['\\'], 'csv'));
|
||||||
|
$this->assertSame($data, $this->encoder->decode($csv, 'csv'));
|
||||||
|
$this->assertSame($data, $this->encoder->decode(trim($csv), 'csv'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSupportEncoding()
|
public function testSupportEncoding()
|
||||||
|
Reference in New Issue
Block a user