a5e5c256f34ad2b13855bc4cb7fc26166d7ed274
[yaffs-website] / vendor / symfony / serializer / Tests / Encoder / CsvEncoderTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Serializer\Tests\Encoder;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Serializer\Encoder\CsvEncoder;
16
17 /**
18  * @author Kévin Dunglas <dunglas@gmail.com>
19  */
20 class CsvEncoderTest extends TestCase
21 {
22     /**
23      * @var CsvEncoder
24      */
25     private $encoder;
26
27     protected function setUp()
28     {
29         $this->encoder = new CsvEncoder();
30     }
31
32     public function testSupportEncoding()
33     {
34         $this->assertTrue($this->encoder->supportsEncoding('csv'));
35         $this->assertFalse($this->encoder->supportsEncoding('foo'));
36     }
37
38     public function testEncode()
39     {
40         $value = array('foo' => 'hello', 'bar' => 'hey ho');
41
42         $this->assertEquals(<<<'CSV'
43 foo,bar
44 hello,"hey ho"
45
46 CSV
47     , $this->encoder->encode($value, 'csv'));
48     }
49
50     public function testEncodeCollection()
51     {
52         $value = array(
53             array('foo' => 'hello', 'bar' => 'hey ho'),
54             array('foo' => 'hi', 'bar' => 'let\'s go'),
55         );
56
57         $this->assertEquals(<<<'CSV'
58 foo,bar
59 hello,"hey ho"
60 hi,"let's go"
61
62 CSV
63     , $this->encoder->encode($value, 'csv'));
64     }
65
66     public function testEncodePlainIndexedArray()
67     {
68         $this->assertEquals(<<<'CSV'
69 0,1,2
70 a,b,c
71
72 CSV
73             , $this->encoder->encode(array('a', 'b', 'c'), 'csv'));
74     }
75
76     public function testEncodeNonArray()
77     {
78         $this->assertEquals(<<<'CSV'
79 0
80 foo
81
82 CSV
83             , $this->encoder->encode('foo', 'csv'));
84     }
85
86     public function testEncodeNestedArrays()
87     {
88         $value = array('foo' => 'hello', 'bar' => array(
89             array('id' => 'yo', 1 => 'wesh'),
90             array('baz' => 'Halo', 'foo' => 'olá'),
91         ));
92
93         $this->assertEquals(<<<'CSV'
94 foo,bar.0.id,bar.0.1,bar.1.baz,bar.1.foo
95 hello,yo,wesh,Halo,olá
96
97 CSV
98     , $this->encoder->encode($value, 'csv'));
99     }
100
101     public function testEncodeCustomSettings()
102     {
103         $this->encoder = new CsvEncoder(';', "'", '|', '-');
104
105         $value = array('a' => 'he\'llo', 'c' => array('d' => 'foo'));
106
107         $this->assertEquals(<<<'CSV'
108 a;c-d
109 'he''llo';foo
110
111 CSV
112     , $this->encoder->encode($value, 'csv'));
113     }
114
115     public function testEncodeCustomSettingsPassedInContext()
116     {
117         $value = array('a' => 'he\'llo', 'c' => array('d' => 'foo'));
118
119         $this->assertSame(<<<'CSV'
120 a;c-d
121 'he''llo';foo
122
123 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 => '-',
129         )));
130     }
131
132     public function testEncodeEmptyArray()
133     {
134         $this->assertEquals("\n\n", $this->encoder->encode(array(), 'csv'));
135         $this->assertEquals("\n\n", $this->encoder->encode(array(array()), 'csv'));
136     }
137
138     public function testEncodeVariableStructure()
139     {
140         $value = array(
141             array('a' => array('foo', 'bar')),
142             array('a' => array(), 'b' => 'baz'),
143             array('a' => array('bar', 'foo'), 'c' => 'pong'),
144         );
145         $csv = <<<CSV
146 a.0,a.1,c,b
147 foo,bar,,
148 ,,,baz
149 bar,foo,pong,
150
151 CSV;
152
153         $this->assertEquals($csv, $this->encoder->encode($value, 'csv'));
154     }
155
156     public function testEncodeCustomHeaders()
157     {
158         $context = array(
159             CsvEncoder::HEADERS_KEY => array(
160                 'b',
161                 'c',
162             ),
163         );
164         $value = array(
165             array('a' => 'foo', 'b' => 'bar'),
166         );
167         $csv = <<<CSV
168 b,c,a
169 bar,,foo
170
171 CSV;
172
173         $this->assertEquals($csv, $this->encoder->encode($value, 'csv', $context));
174     }
175
176     public function testSupportsDecoding()
177     {
178         $this->assertTrue($this->encoder->supportsDecoding('csv'));
179         $this->assertFalse($this->encoder->supportsDecoding('foo'));
180     }
181
182     public function testDecode()
183     {
184         $expected = array('foo' => 'a', 'bar' => 'b');
185
186         $this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
187 foo,bar
188 a,b
189 CSV
190         , 'csv'));
191     }
192
193     public function testDecodeCollection()
194     {
195         $expected = array(
196             array('foo' => 'a', 'bar' => 'b'),
197             array('foo' => 'c', 'bar' => 'd'),
198             array('foo' => 'f'),
199         );
200
201         $this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
202 foo,bar
203 a,b
204 c,d
205 f
206
207 CSV
208         , 'csv'));
209     }
210
211     public function testDecodeToManyRelation()
212     {
213         $expected = array(
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'))),
218         );
219
220         $this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
221 foo,relations.0.a,relations.1.a
222 bar,b,b
223 bat,b,
224 bat,b
225 baz,c,c
226 CSV
227             , 'csv'));
228     }
229
230     public function testDecodeNestedArrays()
231     {
232         $expected = array(
233             array('foo' => 'a', 'bar' => array('baz' => array('bat' => 'b'))),
234             array('foo' => 'c', 'bar' => array('baz' => array('bat' => 'd'))),
235         );
236
237         $this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
238 foo,bar.baz.bat
239 a,b
240 c,d
241 CSV
242         , 'csv'));
243     }
244
245     public function testDecodeCustomSettings()
246     {
247         $this->encoder = new CsvEncoder(';', "'", '|', '-');
248
249         $expected = array('a' => 'hell\'o', 'bar' => array('baz' => 'b'));
250         $this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
251 a;bar-baz
252 'hell''o';b;c
253 CSV
254         , 'csv'));
255     }
256
257     public function testDecodeCustomSettingsPassedInContext()
258     {
259         $expected = array('a' => 'hell\'o', 'bar' => array('baz' => 'b'));
260         $this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
261 a;bar-baz
262 'hell''o';b;c
263 CSV
264         , 'csv', array(
265             CsvEncoder::DELIMITER_KEY => ';',
266             CsvEncoder::ENCLOSURE_KEY => "'",
267             CsvEncoder::ESCAPE_CHAR_KEY => '|',
268             CsvEncoder::KEY_SEPARATOR_KEY => '-',
269         )));
270     }
271
272     public function testDecodeMalformedCollection()
273     {
274         $expected = array(
275             array('foo' => 'a', 'bar' => 'b'),
276             array('foo' => 'c', 'bar' => 'd'),
277             array('foo' => 'f'),
278         );
279
280         $this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
281 foo,bar
282 a,b,e
283 c,d,g,h
284 f
285
286 CSV
287             , 'csv'));
288     }
289
290     public function testDecodeEmptyArray()
291     {
292         $this->assertEquals(array(), $this->encoder->decode('', 'csv'));
293     }
294 }