1: | <?php
|
2: |
|
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: |
|
11: |
|
12: | namespace Symfony\Component\Yaml\Tests;
|
13: |
|
14: | use PHPUnit\Framework\TestCase;
|
15: | use Symfony\Component\Yaml\Dumper;
|
16: | use Symfony\Component\Yaml\Parser;
|
17: |
|
18: | class DumperTest extends TestCase
|
19: | {
|
20: | protected $parser;
|
21: | protected $dumper;
|
22: | protected $path;
|
23: |
|
24: | protected $array = array(
|
25: | '' => 'bar',
|
26: | 'foo' => '#bar',
|
27: | 'foo\'bar' => array(),
|
28: | 'bar' => array(1, 'foo'),
|
29: | 'foobar' => array(
|
30: | 'foo' => 'bar',
|
31: | 'bar' => array(1, 'foo'),
|
32: | 'foobar' => array(
|
33: | 'foo' => 'bar',
|
34: | 'bar' => array(1, 'foo'),
|
35: | ),
|
36: | ),
|
37: | );
|
38: |
|
39: | protected function setUp()
|
40: | {
|
41: | $this->parser = new Parser();
|
42: | $this->dumper = new Dumper();
|
43: | $this->path = __DIR__.'/Fixtures';
|
44: | }
|
45: |
|
46: | protected function tearDown()
|
47: | {
|
48: | $this->parser = null;
|
49: | $this->dumper = null;
|
50: | $this->path = null;
|
51: | $this->array = null;
|
52: | }
|
53: |
|
54: | public function testSetIndentation()
|
55: | {
|
56: | $this->dumper->setIndentation(7);
|
57: |
|
58: | $expected = <<<'EOF'
|
59: | '': bar
|
60: | foo: '#bar'
|
61: | 'foo''bar': { }
|
62: | bar:
|
63: | - 1
|
64: | - foo
|
65: | foobar:
|
66: | foo: bar
|
67: | bar:
|
68: | - 1
|
69: | - foo
|
70: | foobar:
|
71: | foo: bar
|
72: | bar:
|
73: | - 1
|
74: | - foo
|
75: |
|
76: | EOF;
|
77: | $this->assertEquals($expected, $this->dumper->dump($this->array, 4, 0));
|
78: | }
|
79: |
|
80: | public function testSpecifications()
|
81: | {
|
82: | $files = $this->parser->parse(file_get_contents($this->path.'/index.yml'));
|
83: | foreach ($files as $file) {
|
84: | $yamls = file_get_contents($this->path.'/'.$file.'.yml');
|
85: |
|
86: |
|
87: | foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
|
88: | if (!$yaml) {
|
89: | continue;
|
90: | }
|
91: |
|
92: | $test = $this->parser->parse($yaml);
|
93: | if (isset($test['dump_skip']) && $test['dump_skip']) {
|
94: | continue;
|
95: | } elseif (isset($test['todo']) && $test['todo']) {
|
96: |
|
97: | } else {
|
98: | eval('$expected = '.trim($test['php']).';');
|
99: | $this->assertSame($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']);
|
100: | }
|
101: | }
|
102: | }
|
103: | }
|
104: |
|
105: | public function testInlineLevel()
|
106: | {
|
107: | $expected = <<<'EOF'
|
108: | { '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
|
109: | EOF;
|
110: | $this->assertEquals($expected, $this->dumper->dump($this->array, -10), '->dump() takes an inline level argument');
|
111: | $this->assertEquals($expected, $this->dumper->dump($this->array, 0), '->dump() takes an inline level argument');
|
112: |
|
113: | $expected = <<<'EOF'
|
114: | '': bar
|
115: | foo: '#bar'
|
116: | 'foo''bar': { }
|
117: | bar: [1, foo]
|
118: | foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
|
119: |
|
120: | EOF;
|
121: | $this->assertEquals($expected, $this->dumper->dump($this->array, 1), '->dump() takes an inline level argument');
|
122: |
|
123: | $expected = <<<'EOF'
|
124: | '': bar
|
125: | foo: '#bar'
|
126: | 'foo''bar': { }
|
127: | bar:
|
128: | - 1
|
129: | - foo
|
130: | foobar:
|
131: | foo: bar
|
132: | bar: [1, foo]
|
133: | foobar: { foo: bar, bar: [1, foo] }
|
134: |
|
135: | EOF;
|
136: | $this->assertEquals($expected, $this->dumper->dump($this->array, 2), '->dump() takes an inline level argument');
|
137: |
|
138: | $expected = <<<'EOF'
|
139: | '': bar
|
140: | foo: '#bar'
|
141: | 'foo''bar': { }
|
142: | bar:
|
143: | - 1
|
144: | - foo
|
145: | foobar:
|
146: | foo: bar
|
147: | bar:
|
148: | - 1
|
149: | - foo
|
150: | foobar:
|
151: | foo: bar
|
152: | bar: [1, foo]
|
153: |
|
154: | EOF;
|
155: | $this->assertEquals($expected, $this->dumper->dump($this->array, 3), '->dump() takes an inline level argument');
|
156: |
|
157: | $expected = <<<'EOF'
|
158: | '': bar
|
159: | foo: '#bar'
|
160: | 'foo''bar': { }
|
161: | bar:
|
162: | - 1
|
163: | - foo
|
164: | foobar:
|
165: | foo: bar
|
166: | bar:
|
167: | - 1
|
168: | - foo
|
169: | foobar:
|
170: | foo: bar
|
171: | bar:
|
172: | - 1
|
173: | - foo
|
174: |
|
175: | EOF;
|
176: | $this->assertEquals($expected, $this->dumper->dump($this->array, 4), '->dump() takes an inline level argument');
|
177: | $this->assertEquals($expected, $this->dumper->dump($this->array, 10), '->dump() takes an inline level argument');
|
178: | }
|
179: |
|
180: | public function testObjectSupportEnabled()
|
181: | {
|
182: | $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
|
183: |
|
184: | $this->assertEquals('{ foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\A":1:{s:1:"a";s:3:"foo";}, bar: 1 }', $dump, '->dump() is able to dump objects');
|
185: | }
|
186: |
|
187: | public function testObjectSupportDisabledButNoExceptions()
|
188: | {
|
189: | $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1));
|
190: |
|
191: | $this->assertEquals('{ foo: null, bar: 1 }', $dump, '->dump() does not dump objects when disabled');
|
192: | }
|
193: |
|
194: | |
195: | |
196: |
|
197: | public function testObjectSupportDisabledWithExceptions()
|
198: | {
|
199: | $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true, false);
|
200: | }
|
201: |
|
202: | |
203: | |
204: |
|
205: | public function testEscapedEscapeSequencesInQuotedScalar($input, $expected)
|
206: | {
|
207: | $this->assertEquals($expected, $this->dumper->dump($input));
|
208: | }
|
209: |
|
210: | public function getEscapeSequences()
|
211: | {
|
212: | return array(
|
213: | 'empty string' => array('', "''"),
|
214: | 'null' => array("\x0", '"\\0"'),
|
215: | 'bell' => array("\x7", '"\\a"'),
|
216: | 'backspace' => array("\x8", '"\\b"'),
|
217: | 'horizontal-tab' => array("\t", '"\\t"'),
|
218: | 'line-feed' => array("\n", '"\\n"'),
|
219: | 'vertical-tab' => array("\v", '"\\v"'),
|
220: | 'form-feed' => array("\xC", '"\\f"'),
|
221: | 'carriage-return' => array("\r", '"\\r"'),
|
222: | 'escape' => array("\x1B", '"\\e"'),
|
223: | 'space' => array(' ', "' '"),
|
224: | 'double-quote' => array('"', "'\"'"),
|
225: | 'slash' => array('/', '/'),
|
226: | 'backslash' => array('\\', '\\'),
|
227: | 'next-line' => array("\xC2\x85", '"\\N"'),
|
228: | 'non-breaking-space' => array("\xc2\xa0", '"\\_"'),
|
229: | 'line-separator' => array("\xE2\x80\xA8", '"\\L"'),
|
230: | 'paragraph-separator' => array("\xE2\x80\xA9", '"\\P"'),
|
231: | 'colon' => array(':', "':'"),
|
232: | );
|
233: | }
|
234: |
|
235: | |
236: | |
237: | |
238: |
|
239: | public function testZeroIndentationThrowsException()
|
240: | {
|
241: | $this->dumper->setIndentation(0);
|
242: | }
|
243: |
|
244: | |
245: | |
246: | |
247: |
|
248: | public function testNegativeIndentationThrowsException()
|
249: | {
|
250: | $this->dumper->setIndentation(-4);
|
251: | }
|
252: | }
|
253: |
|
254: | class A
|
255: | {
|
256: | public $a = 'foo';
|
257: | }
|
258: | |