4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Tests\Encoder;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Serializer\Encoder\CsvEncoder;
18 * @author Kévin Dunglas <dunglas@gmail.com>
20 class CsvEncoderTest extends TestCase
27 protected function setUp()
29 $this->encoder = new CsvEncoder();
32 public function testSupportEncoding()
34 $this->assertTrue($this->encoder->supportsEncoding('csv'));
35 $this->assertFalse($this->encoder->supportsEncoding('foo'));
38 public function testEncode()
40 $value = array('foo' => 'hello', 'bar' => 'hey ho');
42 $this->assertEquals(<<<'CSV'
47 , $this->encoder->encode($value, 'csv'));
50 public function testEncodeCollection()
53 array('foo' => 'hello', 'bar' => 'hey ho'),
54 array('foo' => 'hi', 'bar' => 'let\'s go'),
57 $this->assertEquals(<<<'CSV'
63 , $this->encoder->encode($value, 'csv'));
66 public function testEncodePlainIndexedArray()
68 $this->assertEquals(<<<'CSV'
73 , $this->encoder->encode(array('a', 'b', 'c'), 'csv'));
76 public function testEncodeNonArray()
78 $this->assertEquals(<<<'CSV'
83 , $this->encoder->encode('foo', 'csv'));
86 public function testEncodeNestedArrays()
88 $value = array('foo' => 'hello', 'bar' => array(
89 array('id' => 'yo', 1 => 'wesh'),
90 array('baz' => 'Halo', 'foo' => 'olá'),
93 $this->assertEquals(<<<'CSV'
94 foo,bar.0.id,bar.0.1,bar.1.baz,bar.1.foo
95 hello,yo,wesh,Halo,olá
98 , $this->encoder->encode($value, 'csv'));
101 public function testEncodeCustomSettings()
103 $this->encoder = new CsvEncoder(';', "'", '|', '-');
105 $value = array('a' => 'he\'llo', 'c' => array('d' => 'foo'));
107 $this->assertEquals(<<<'CSV'
112 , $this->encoder->encode($value, 'csv'));
115 public function testEncodeCustomSettingsPassedInContext()
117 $value = array('a' => 'he\'llo', 'c' => array('d' => 'foo'));
119 $this->assertSame(<<<'CSV'
124 , $this->encoder->encode($value, 'csv', array(
125 CsvEncoder::DELIMITER_KEY => ';',
126 CsvEncoder::ENCLOSURE_KEY => "'",
127 CsvEncoder::ESCAPE_CHAR_KEY => '|',
128 CsvEncoder::KEY_SEPARATOR_KEY => '-',
132 public function testEncodeEmptyArray()
134 $this->assertEquals("\n\n", $this->encoder->encode(array(), 'csv'));
135 $this->assertEquals("\n\n", $this->encoder->encode(array(array()), 'csv'));
138 public function testEncodeVariableStructure()
141 array('a' => array('foo', 'bar')),
142 array('a' => array(), 'b' => 'baz'),
143 array('a' => array('bar', 'foo'), 'c' => 'pong'),
153 $this->assertEquals($csv, $this->encoder->encode($value, 'csv'));
156 public function testEncodeCustomHeaders()
159 CsvEncoder::HEADERS_KEY => array(
165 array('a' => 'foo', 'b' => 'bar'),
173 $this->assertEquals($csv, $this->encoder->encode($value, 'csv', $context));
176 public function testSupportsDecoding()
178 $this->assertTrue($this->encoder->supportsDecoding('csv'));
179 $this->assertFalse($this->encoder->supportsDecoding('foo'));
182 public function testDecode()
184 $expected = array('foo' => 'a', 'bar' => 'b');
186 $this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
193 public function testDecodeCollection()
196 array('foo' => 'a', 'bar' => 'b'),
197 array('foo' => 'c', 'bar' => 'd'),
201 $this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
211 public function testDecodeToManyRelation()
214 array('foo' => 'bar', 'relations' => array(array('a' => 'b'), array('a' => 'b'))),
215 array('foo' => 'bat', 'relations' => array(array('a' => 'b'), array('a' => ''))),
216 array('foo' => 'bat', 'relations' => array(array('a' => 'b'))),
217 array('foo' => 'baz', 'relations' => array(array('a' => 'c'), array('a' => 'c'))),
220 $this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
221 foo,relations.0.a,relations.1.a
230 public function testDecodeNestedArrays()
233 array('foo' => 'a', 'bar' => array('baz' => array('bat' => 'b'))),
234 array('foo' => 'c', 'bar' => array('baz' => array('bat' => 'd'))),
237 $this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
245 public function testDecodeCustomSettings()
247 $this->encoder = new CsvEncoder(';', "'", '|', '-');
249 $expected = array('a' => 'hell\'o', 'bar' => array('baz' => 'b'));
250 $this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
257 public function testDecodeCustomSettingsPassedInContext()
259 $expected = array('a' => 'hell\'o', 'bar' => array('baz' => 'b'));
260 $this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
265 CsvEncoder::DELIMITER_KEY => ';',
266 CsvEncoder::ENCLOSURE_KEY => "'",
267 CsvEncoder::ESCAPE_CHAR_KEY => '|',
268 CsvEncoder::KEY_SEPARATOR_KEY => '-',
272 public function testDecodeMalformedCollection()
275 array('foo' => 'a', 'bar' => 'b'),
276 array('foo' => 'c', 'bar' => 'd'),
280 $this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
290 public function testDecodeEmptyArray()
292 $this->assertEquals(array(), $this->encoder->decode('', 'csv'));