ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
DumperTest.php
Go to the documentation of this file.
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
13
17
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
55 {
56 $dumper = new Dumper(7);
57 $expected = <<<'EOF'
58'': bar
59foo: '#bar'
60'foo''bar': { }
61bar:
62 - 1
63 - foo
64foobar:
65 foo: bar
66 bar:
67 - 1
68 - foo
69 foobar:
70 foo: bar
71 bar:
72 - 1
73 - foo
74
75EOF;
76 $this->assertEquals($expected, $dumper->dump($this->array, 4, 0));
77 }
78
82 public function testSetIndentation()
83 {
84 $this->dumper->setIndentation(7);
85
86 $expected = <<<'EOF'
87'': bar
88foo: '#bar'
89'foo''bar': { }
90bar:
91 - 1
92 - foo
93foobar:
94 foo: bar
95 bar:
96 - 1
97 - foo
98 foobar:
99 foo: bar
100 bar:
101 - 1
102 - foo
103
104EOF;
105 $this->assertEquals($expected, $this->dumper->dump($this->array, 4, 0));
106 }
107
108 public function testSpecifications()
109 {
110 $files = $this->parser->parse(file_get_contents($this->path.'/index.yml'));
111 foreach ($files as $file) {
112 $yamls = file_get_contents($this->path.'/'.$file.'.yml');
113
114 // split YAMLs documents
115 foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
116 if (!$yaml) {
117 continue;
118 }
119
120 $test = $this->parser->parse($yaml);
121 if (isset($test['dump_skip']) && $test['dump_skip']) {
122 continue;
123 } elseif (isset($test['todo']) && $test['todo']) {
124 // TODO
125 } else {
126 eval('$expected = '.trim($test['php']).';');
127 $this->assertSame($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']);
128 }
129 }
130 }
131 }
132
133 public function testInlineLevel()
134 {
135 $expected = <<<'EOF'
136{ '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
137EOF;
138 $this->assertEquals($expected, $this->dumper->dump($this->array, -10), '->dump() takes an inline level argument');
139 $this->assertEquals($expected, $this->dumper->dump($this->array, 0), '->dump() takes an inline level argument');
140
141 $expected = <<<'EOF'
142'': bar
143foo: '#bar'
144'foo''bar': { }
145bar: [1, foo]
146foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
147
148EOF;
149 $this->assertEquals($expected, $this->dumper->dump($this->array, 1), '->dump() takes an inline level argument');
150
151 $expected = <<<'EOF'
152'': bar
153foo: '#bar'
154'foo''bar': { }
155bar:
156 - 1
157 - foo
158foobar:
159 foo: bar
160 bar: [1, foo]
161 foobar: { foo: bar, bar: [1, foo] }
162
163EOF;
164 $this->assertEquals($expected, $this->dumper->dump($this->array, 2), '->dump() takes an inline level argument');
165
166 $expected = <<<'EOF'
167'': bar
168foo: '#bar'
169'foo''bar': { }
170bar:
171 - 1
172 - foo
173foobar:
174 foo: bar
175 bar:
176 - 1
177 - foo
178 foobar:
179 foo: bar
180 bar: [1, foo]
181
182EOF;
183 $this->assertEquals($expected, $this->dumper->dump($this->array, 3), '->dump() takes an inline level argument');
184
185 $expected = <<<'EOF'
186'': bar
187foo: '#bar'
188'foo''bar': { }
189bar:
190 - 1
191 - foo
192foobar:
193 foo: bar
194 bar:
195 - 1
196 - foo
197 foobar:
198 foo: bar
199 bar:
200 - 1
201 - foo
202
203EOF;
204 $this->assertEquals($expected, $this->dumper->dump($this->array, 4), '->dump() takes an inline level argument');
205 $this->assertEquals($expected, $this->dumper->dump($this->array, 10), '->dump() takes an inline level argument');
206 }
207
208 public function testObjectSupportEnabled()
209 {
210 $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_OBJECT);
211
212 $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');
213 }
214
219 {
220 $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
221
222 $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');
223 }
224
226 {
227 $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1));
228
229 $this->assertEquals('{ foo: null, bar: 1 }', $dump, '->dump() does not dump objects when disabled');
230 }
231
236 {
237 $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
238 }
239
245 {
246 $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
247 }
248
253 {
254 $this->assertEquals($expected, $this->dumper->dump($input));
255 }
256
257 public function getEscapeSequences()
258 {
259 return array(
260 'null' => array("\t\\0", '"\t\\\\0"'),
261 'bell' => array("\t\\a", '"\t\\\\a"'),
262 'backspace' => array("\t\\b", '"\t\\\\b"'),
263 'horizontal-tab' => array("\t\\t", '"\t\\\\t"'),
264 'line-feed' => array("\t\\n", '"\t\\\\n"'),
265 'vertical-tab' => array("\t\\v", '"\t\\\\v"'),
266 'form-feed' => array("\t\\f", '"\t\\\\f"'),
267 'carriage-return' => array("\t\\r", '"\t\\\\r"'),
268 'escape' => array("\t\\e", '"\t\\\\e"'),
269 'space' => array("\t\\ ", '"\t\\\\ "'),
270 'double-quote' => array("\t\\\"", '"\t\\\\\\""'),
271 'slash' => array("\t\\/", '"\t\\\\/"'),
272 'backslash' => array("\t\\\\", '"\t\\\\\\\\"'),
273 'next-line' => array("\t\\N", '"\t\\\\N"'),
274 'non-breaking-space' => array("\t\\�", '"\t\\\\�"'),
275 'line-separator' => array("\t\\L", '"\t\\\\L"'),
276 'paragraph-separator' => array("\t\\P", '"\t\\\\P"'),
277 );
278 }
279
281 {
282 $binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif');
283 $expected = '{ data: !!binary '.base64_encode($binaryData).' }';
284
285 $this->assertSame($expected, $this->dumper->dump(array('data' => $binaryData)));
286 }
287
289 {
290 // "für" (ISO-8859-1 encoded)
291 $this->assertSame('!!binary ZsM/cg==', $this->dumper->dump("f\xc3\x3fr"));
292 }
293
297 public function testDumpObjectAsMap($object, $expected)
298 {
299 $yaml = $this->dumper->dump($object, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
300
301 $this->assertEquals($expected, Yaml::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
302 }
303
304 public function objectAsMapProvider()
305 {
306 $tests = array();
307
308 $bar = new \stdClass();
309 $bar->class = 'classBar';
310 $bar->args = array('bar');
311 $zar = new \stdClass();
312 $foo = new \stdClass();
313 $foo->bar = $bar;
314 $foo->zar = $zar;
315 $object = new \stdClass();
316 $object->foo = $foo;
317 $tests['stdClass'] = array($object, $object);
318
319 $arrayObject = new \ArrayObject();
320 $arrayObject['foo'] = 'bar';
321 $arrayObject['baz'] = 'foobar';
322 $parsedArrayObject = new \stdClass();
323 $parsedArrayObject->foo = 'bar';
324 $parsedArrayObject->baz = 'foobar';
325 $tests['ArrayObject'] = array($arrayObject, $parsedArrayObject);
326
327 $a = new A();
328 $tests['arbitrary-object'] = array($a, null);
329
330 return $tests;
331 }
332
334 {
335 $data = array(
336 'data' => array(
337 'single_line' => 'foo bar baz',
338 'multi_line' => "foo\nline with trailing spaces:\n \nbar\r\ninteger like line:\n123456789\nempty line:\n\nbaz",
339 'nested_inlined_multi_line_string' => array(
340 'inlined_multi_line' => "foo\nbar\r\nempty line:\n\nbaz",
341 ),
342 ),
343 );
344
345 $this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block.yml'), $this->dumper->dump($data, 3, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
346 }
347
353 {
354 new Dumper(0);
355 }
356
362 {
363 new Dumper(-4);
364 }
365}
366
367class A
368{
369 public $a = 'foo';
370}
const EOF
How fgetc() reports an End Of File.
Definition: JSMin_lib.php:92
$test
Definition: Utf8Test.php:84
An exception for terminatinating execution or to throw for unit testing.
Dumper dumps PHP variables to YAML strings.
Definition: Dumper.php:20
Parser parses YAML strings to convert them to PHP arrays.
Definition: Parser.php:22
testZeroIndentationThrowsException()
@expectedException \InvalidArgumentException @expectedExceptionMessage The indentation must be greate...
Definition: DumperTest.php:352
testDumpObjectAsMap($object, $expected)
@dataProvider objectAsMapProvider
Definition: DumperTest.php:297
testObjectSupportDisabledWithExceptionsPassingTrue()
@group legacy @expectedException \Symfony\Component\Yaml\Exception\DumpException
Definition: DumperTest.php:244
testNegativeIndentationThrowsException()
@expectedException \InvalidArgumentException @expectedExceptionMessage The indentation must be greate...
Definition: DumperTest.php:361
testObjectSupportEnabledPassingTrue()
@group legacy
Definition: DumperTest.php:218
testObjectSupportDisabledWithExceptions()
@expectedException \Symfony\Component\Yaml\Exception\DumpException
Definition: DumperTest.php:235
testEscapedEscapeSequencesInQuotedScalar($input, $expected)
@dataProvider getEscapeSequences
Definition: DumperTest.php:252
Yaml offers convenience methods to load and dump YAML.
Definition: Yaml.php:22
const DUMP_MULTI_LINE_LITERAL_BLOCK
Definition: Yaml.php:30
static parse($input, $flags=0)
Parses YAML into a PHP value.
Definition: Yaml.php:48
const DUMP_EXCEPTION_ON_INVALID_TYPE
Definition: Yaml.php:27
$tests
Definition: bench.php:104
$files
Definition: metarefresh.php:49
$data
Definition: bench.php:6