Completed
Push — master ( ac6391...9b00f4 )
by Michael
12:56
created

InlineTest::testParseWithMapObjects()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
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\Yaml\Tests;
13
14
use Symfony\Component\Yaml\Inline;
15
16
class InlineTest extends \PHPUnit_Framework_TestCase
17
{
18
    /**
19
     * @dataProvider getTestsForParse
20
     */
21
    public function testParse($yaml, $value)
22
    {
23
        $this->assertSame($value, Inline::parse($yaml), sprintf('::parse() converts an inline YAML to a PHP structure (%s)', $yaml));
24
    }
25
26
    /**
27
     * @dataProvider getTestsForParseWithMapObjects
28
     */
29
    public function testParseWithMapObjects($yaml, $value)
30
    {
31
        $actual = Inline::parse($yaml, false, false, true);
32
33
        $this->assertSame(serialize($value), serialize($actual));
34
    }
35
36
    /**
37
     * @dataProvider getTestsForDump
38
     */
39
    public function testDump($yaml, $value)
40
    {
41
        $this->assertEquals($yaml, Inline::dump($value), sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml));
42
43
        $this->assertSame($value, Inline::parse(Inline::dump($value)), 'check consistency');
44
    }
45
46
    public function testDumpNumericValueWithLocale()
47
    {
48
        $locale = setlocale(LC_NUMERIC, 0);
49
        if (false === $locale) {
50
            $this->markTestSkipped('Your platform does not support locales.');
51
        }
52
53
        try {
54
            $requiredLocales = array('fr_FR.UTF-8', 'fr_FR.UTF8', 'fr_FR.utf-8', 'fr_FR.utf8', 'French_France.1252');
55
            if (false === setlocale(LC_NUMERIC, $requiredLocales)) {
56
                $this->markTestSkipped('Could not set any of required locales: '.implode(', ', $requiredLocales));
57
            }
58
59
            $this->assertEquals('1.2', Inline::dump(1.2));
60
            $this->assertContains('fr', strtolower(setlocale(LC_NUMERIC, 0)));
61
            setlocale(LC_NUMERIC, $locale);
62
        } catch (\Exception $e) {
63
            setlocale(LC_NUMERIC, $locale);
64
            throw $e;
65
        }
66
    }
67
68
    public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF()
69
    {
70
        $value = '686e444';
71
72
        $this->assertSame($value, Inline::parse(Inline::dump($value)));
73
    }
74
75
    /**
76
     * @group legacy
77
     * throws \Symfony\Component\Yaml\Exception\ParseException in 3.0
78
     */
79
    public function testParseScalarWithNonEscapedBlackslashShouldThrowException()
80
    {
81
        $this->assertSame('Foo\Var', Inline::parse('"Foo\Var"'));
82
    }
83
84
    /**
85
     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
86
     */
87
    public function testParseScalarWithNonEscapedBlackslashAtTheEndShouldThrowException()
88
    {
89
        Inline::parse('"Foo\\"');
90
    }
91
92
    /**
93
     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
94
     */
95
    public function testParseScalarWithIncorrectlyQuotedStringShouldThrowException()
96
    {
97
        $value = "'don't do somthin' like that'";
98
        Inline::parse($value);
99
    }
100
101
    /**
102
     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
103
     */
104
    public function testParseScalarWithIncorrectlyDoubleQuotedStringShouldThrowException()
105
    {
106
        $value = '"don"t do somthin" like that"';
107
        Inline::parse($value);
108
    }
109
110
    /**
111
     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
112
     */
113
    public function testParseInvalidMappingKeyShouldThrowException()
114
    {
115
        $value = '{ "foo " bar": "bar" }';
116
        Inline::parse($value);
117
    }
118
119
    /**
120
     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
121
     */
122
    public function testParseInvalidMappingShouldThrowException()
123
    {
124
        Inline::parse('[foo] bar');
125
    }
126
127
    /**
128
     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
129
     */
130
    public function testParseInvalidSequenceShouldThrowException()
131
    {
132
        Inline::parse('{ foo: bar } bar');
133
    }
134
135
    public function testParseScalarWithCorrectlyQuotedStringShouldReturnString()
136
    {
137
        $value = "'don''t do somthin'' like that'";
138
        $expect = "don't do somthin' like that";
139
140
        $this->assertSame($expect, Inline::parseScalar($value));
141
    }
142
143
    /**
144
     * @dataProvider getDataForParseReferences
145
     */
146
    public function testParseReferences($yaml, $expected)
147
    {
148
        $this->assertSame($expected, Inline::parse($yaml, false, false, false, array('var' => 'var-value')));
149
    }
150
151
    public function getDataForParseReferences()
152
    {
153
        return array(
154
            'scalar' => array('*var', 'var-value'),
155
            'list' => array('[ *var ]', array('var-value')),
156
            'list-in-list' => array('[[ *var ]]', array(array('var-value'))),
157
            'map-in-list' => array('[ { key: *var } ]', array(array('key' => 'var-value'))),
158
            'embedded-mapping-in-list' => array('[ key: *var ]', array(array('key' => 'var-value'))),
159
            'map' => array('{ key: *var }', array('key' => 'var-value')),
160
            'list-in-map' => array('{ key: [*var] }', array('key' => array('var-value'))),
161
            'map-in-map' => array('{ foo: { bar: *var } }', array('foo' => array('bar' => 'var-value'))),
162
        );
163
    }
164
165
    public function testParseMapReferenceInSequence()
166
    {
167
        $foo = array(
168
            'a' => 'Steve',
169
            'b' => 'Clark',
170
            'c' => 'Brian',
171
        );
172
        $this->assertSame(array($foo), Inline::parse('[*foo]', false, false, false, array('foo' => $foo)));
173
    }
174
175
    /**
176
     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
177
     * @expectedExceptionMessage A reference must contain at least one character.
178
     */
179
    public function testParseUnquotedAsterisk()
180
    {
181
        Inline::parse('{ foo: * }');
182
    }
183
184
    /**
185
     * @expectedException \Symfony\Component\Yaml\Exception\ParseException
186
     * @expectedExceptionMessage A reference must contain at least one character.
187
     */
188
    public function testParseUnquotedAsteriskFollowedByAComment()
189
    {
190
        Inline::parse('{ foo: * #foo }');
191
    }
192
193
    /**
194
     * @group legacy
195
     * @dataProvider getReservedIndicators
196
     * throws \Symfony\Component\Yaml\Exception\ParseException in 3.0
197
     */
198
    public function testParseUnquotedScalarStartingWithReservedIndicator($indicator)
199
    {
200
        Inline::parse(sprintf('{ foo: %sfoo }', $indicator));
201
    }
202
203
    public function getReservedIndicators()
204
    {
205
        return array(array('@'), array('`'));
206
    }
207
208
    /**
209
     * @group legacy
210
     * @dataProvider getScalarIndicators
211
     * throws \Symfony\Component\Yaml\Exception\ParseException in 3.0
212
     */
213
    public function testParseUnquotedScalarStartingWithScalarIndicator($indicator)
214
    {
215
        Inline::parse(sprintf('{ foo: %sfoo }', $indicator));
216
    }
217
218
    public function getScalarIndicators()
219
    {
220
        return array(array('|'), array('>'));
221
    }
222
223
    /**
224
     * @dataProvider getDataForIsHash
225
     */
226
    public function testIsHash($array, $expected)
227
    {
228
        $this->assertSame($expected, Inline::isHash($array));
229
    }
230
231
    public function getDataForIsHash()
232
    {
233
        return array(
234
            array(array(), false),
235
            array(array(1, 2, 3), false),
236
            array(array(2 => 1, 1 => 2, 0 => 3), true),
237
            array(array('foo' => 1, 'bar' => 2), true),
238
        );
239
    }
240
241
    public function getTestsForParse()
242
    {
243
        return array(
244
            array('', ''),
245
            array('null', null),
246
            array('false', false),
247
            array('true', true),
248
            array('12', 12),
249
            array('-12', -12),
250
            array('"quoted string"', 'quoted string'),
251
            array("'quoted string'", 'quoted string'),
252
            array('12.30e+02', 12.30e+02),
253
            array('0x4D2', 0x4D2),
254
            array('02333', 02333),
255
            array('.Inf', -log(0)),
256
            array('-.Inf', log(0)),
257
            array("'686e444'", '686e444'),
258
            array('686e444', 646e444),
259
            array('123456789123456789123456789123456789', '123456789123456789123456789123456789'),
260
            array('"foo\r\nbar"', "foo\r\nbar"),
261
            array("'foo#bar'", 'foo#bar'),
262
            array("'foo # bar'", 'foo # bar'),
263
            array("'#cfcfcf'", '#cfcfcf'),
264
            array('::form_base.html.twig', '::form_base.html.twig'),
265
266
            // Pre-YAML-1.2 booleans
267
            array("'y'", 'y'),
268
            array("'n'", 'n'),
269
            array("'yes'", 'yes'),
270
            array("'no'", 'no'),
271
            array("'on'", 'on'),
272
            array("'off'", 'off'),
273
274
            array('2007-10-30', gmmktime(0, 0, 0, 10, 30, 2007)),
275
            array('2007-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 2007)),
276
            array('2007-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 2007)),
277
            array('1960-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 1960)),
278
            array('1730-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 1730)),
279
280
            array('"a \\"string\\" with \'quoted strings inside\'"', 'a "string" with \'quoted strings inside\''),
281
            array("'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''),
282
283
            // sequences
284
            // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
285
            array('[foo, http://urls.are/no/mappings, false, null, 12]', array('foo', 'http://urls.are/no/mappings', false, null, 12)),
286
            array('[  foo  ,   bar , false  ,  null     ,  12  ]', array('foo', 'bar', false, null, 12)),
287
            array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
288
289
            // mappings
290
            array('{foo:bar,bar:foo,false:false,null:null,integer:12}', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
291
            array('{ foo  : bar, bar : foo,  false  :   false,  null  :   null,  integer :  12  }', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
292
            array('{foo: \'bar\', bar: \'foo: bar\'}', array('foo' => 'bar', 'bar' => 'foo: bar')),
293
            array('{\'foo\': \'bar\', "bar": \'foo: bar\'}', array('foo' => 'bar', 'bar' => 'foo: bar')),
294
            array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', array('foo\'' => 'bar', 'bar"' => 'foo: bar')),
295
            array('{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}', array('foo: ' => 'bar', 'bar: ' => 'foo: bar')),
296
297
            // nested sequences and mappings
298
            array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
299
            array('[foo, {bar: foo}]', array('foo', array('bar' => 'foo'))),
300
            array('{ foo: {bar: foo} }', array('foo' => array('bar' => 'foo'))),
301
            array('{ foo: [bar, foo] }', array('foo' => array('bar', 'foo'))),
302
303
            array('[  foo, [  bar, foo  ]  ]', array('foo', array('bar', 'foo'))),
304
305
            array('[{ foo: {bar: foo} }]', array(array('foo' => array('bar' => 'foo')))),
306
307
            array('[foo, [bar, [foo, [bar, foo]], foo]]', array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo'))),
308
309
            array('[foo, {bar: foo, foo: [foo, {bar: foo}]}, [foo, {bar: foo}]]', array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo')))),
310
311
            array('[foo, bar: { foo: bar }]', array('foo', '1' => array('bar' => array('foo' => 'bar')))),
312
            array('[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']', array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, '@service_container')),
313
        );
314
    }
315
316
    public function getTestsForParseWithMapObjects()
317
    {
318
        return array(
319
            array('', ''),
320
            array('null', null),
321
            array('false', false),
322
            array('true', true),
323
            array('12', 12),
324
            array('-12', -12),
325
            array('"quoted string"', 'quoted string'),
326
            array("'quoted string'", 'quoted string'),
327
            array('12.30e+02', 12.30e+02),
328
            array('0x4D2', 0x4D2),
329
            array('02333', 02333),
330
            array('.Inf', -log(0)),
331
            array('-.Inf', log(0)),
332
            array("'686e444'", '686e444'),
333
            array('686e444', 646e444),
334
            array('123456789123456789123456789123456789', '123456789123456789123456789123456789'),
335
            array('"foo\r\nbar"', "foo\r\nbar"),
336
            array("'foo#bar'", 'foo#bar'),
337
            array("'foo # bar'", 'foo # bar'),
338
            array("'#cfcfcf'", '#cfcfcf'),
339
            array('::form_base.html.twig', '::form_base.html.twig'),
340
341
            array('2007-10-30', gmmktime(0, 0, 0, 10, 30, 2007)),
342
            array('2007-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 2007)),
343
            array('2007-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 2007)),
344
            array('1960-10-30 02:59:43 Z', gmmktime(2, 59, 43, 10, 30, 1960)),
345
            array('1730-10-30T02:59:43Z', gmmktime(2, 59, 43, 10, 30, 1730)),
346
347
            array('"a \\"string\\" with \'quoted strings inside\'"', 'a "string" with \'quoted strings inside\''),
348
            array("'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''),
349
350
            // sequences
351
            // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon
352
            array('[foo, http://urls.are/no/mappings, false, null, 12]', array('foo', 'http://urls.are/no/mappings', false, null, 12)),
353
            array('[  foo  ,   bar , false  ,  null     ,  12  ]', array('foo', 'bar', false, null, 12)),
354
            array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
355
356
            // mappings
357
            array('{foo:bar,bar:foo,false:false,null:null,integer:12}', (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
358
            array('{ foo  : bar, bar : foo,  false  :   false,  null  :   null,  integer :  12  }', (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
359
            array('{foo: \'bar\', bar: \'foo: bar\'}', (object) array('foo' => 'bar', 'bar' => 'foo: bar')),
360
            array('{\'foo\': \'bar\', "bar": \'foo: bar\'}', (object) array('foo' => 'bar', 'bar' => 'foo: bar')),
361
            array('{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}', (object) array('foo\'' => 'bar', 'bar"' => 'foo: bar')),
362
            array('{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}', (object) array('foo: ' => 'bar', 'bar: ' => 'foo: bar')),
363
364
            // nested sequences and mappings
365
            array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
366
            array('[foo, {bar: foo}]', array('foo', (object) array('bar' => 'foo'))),
367
            array('{ foo: {bar: foo} }', (object) array('foo' => (object) array('bar' => 'foo'))),
368
            array('{ foo: [bar, foo] }', (object) array('foo' => array('bar', 'foo'))),
369
370
            array('[  foo, [  bar, foo  ]  ]', array('foo', array('bar', 'foo'))),
371
372
            array('[{ foo: {bar: foo} }]', array((object) array('foo' => (object) array('bar' => 'foo')))),
373
374
            array('[foo, [bar, [foo, [bar, foo]], foo]]', array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo'))),
375
376
            array('[foo, {bar: foo, foo: [foo, {bar: foo}]}, [foo, {bar: foo}]]', array('foo', (object) array('bar' => 'foo', 'foo' => array('foo', (object) array('bar' => 'foo'))), array('foo', (object) array('bar' => 'foo')))),
377
378
            array('[foo, bar: { foo: bar }]', array('foo', '1' => (object) array('bar' => (object) array('foo' => 'bar')))),
379
            array('[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']', array('foo', '@foo.baz', (object) array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, '@service_container')),
380
381
            array('{}', new \stdClass()),
382
            array('{ foo  : bar, bar : {}  }', (object) array('foo' => 'bar', 'bar' => new \stdClass())),
383
            array('{ foo  : [], bar : {}  }', (object) array('foo' => array(), 'bar' => new \stdClass())),
384
            array('{foo: \'bar\', bar: {} }', (object) array('foo' => 'bar', 'bar' => new \stdClass())),
385
            array('{\'foo\': \'bar\', "bar": {}}', (object) array('foo' => 'bar', 'bar' => new \stdClass())),
386
            array('{\'foo\': \'bar\', "bar": \'{}\'}', (object) array('foo' => 'bar', 'bar' => '{}')),
387
388
            array('[foo, [{}, {}]]', array('foo', array(new \stdClass(), new \stdClass()))),
389
            array('[foo, [[], {}]]', array('foo', array(array(), new \stdClass()))),
390
            array('[foo, [[{}, {}], {}]]', array('foo', array(array(new \stdClass(), new \stdClass()), new \stdClass()))),
391
            array('[foo, {bar: {}}]', array('foo', '1' => (object) array('bar' => new \stdClass()))),
392
        );
393
    }
394
395
    public function getTestsForDump()
396
    {
397
        return array(
398
            array('null', null),
399
            array('false', false),
400
            array('true', true),
401
            array('12', 12),
402
            array("'quoted string'", 'quoted string'),
403
            array('!!float 1230', 12.30e+02),
404
            array('1234', 0x4D2),
405
            array('1243', 02333),
406
            array('.Inf', -log(0)),
407
            array('-.Inf', log(0)),
408
            array("'686e444'", '686e444'),
409
            array('"foo\r\nbar"', "foo\r\nbar"),
410
            array("'foo#bar'", 'foo#bar'),
411
            array("'foo # bar'", 'foo # bar'),
412
            array("'#cfcfcf'", '#cfcfcf'),
413
414
            array("'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''),
415
416
            array("'-dash'", '-dash'),
417
            array("'-'", '-'),
418
419
            // Pre-YAML-1.2 booleans
420
            array("'y'", 'y'),
421
            array("'n'", 'n'),
422
            array("'yes'", 'yes'),
423
            array("'no'", 'no'),
424
            array("'on'", 'on'),
425
            array("'off'", 'off'),
426
427
            // sequences
428
            array('[foo, bar, false, null, 12]', array('foo', 'bar', false, null, 12)),
429
            array('[\'foo,bar\', \'foo bar\']', array('foo,bar', 'foo bar')),
430
431
            // mappings
432
            array('{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }', array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12)),
433
            array('{ foo: bar, bar: \'foo: bar\' }', array('foo' => 'bar', 'bar' => 'foo: bar')),
434
435
            // nested sequences and mappings
436
            array('[foo, [bar, foo]]', array('foo', array('bar', 'foo'))),
437
438
            array('[foo, [bar, [foo, [bar, foo]], foo]]', array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo'))),
439
440
            array('{ foo: { bar: foo } }', array('foo' => array('bar' => 'foo'))),
441
442
            array('[foo, { bar: foo }]', array('foo', array('bar' => 'foo'))),
443
444
            array('[foo, { bar: foo, foo: [foo, { bar: foo }] }, [foo, { bar: foo }]]', array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo')))),
445
446
            array('[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']', array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, '@service_container')),
447
448
            array('{ foo: { bar: { 1: 2, baz: 3 } } }', array('foo' => array('bar' => array(1 => 2, 'baz' => 3)))),
449
        );
450
    }
451
}
452