DumperTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Yaml\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Yaml\Dumper;
  13. use Symfony\Component\Yaml\Parser;
  14. use Symfony\Component\Yaml\Tag\TaggedValue;
  15. use Symfony\Component\Yaml\Yaml;
  16. class DumperTest extends TestCase
  17. {
  18. protected $parser;
  19. protected $dumper;
  20. protected $path;
  21. protected $array = [
  22. '' => 'bar',
  23. 'foo' => '#bar',
  24. 'foo\'bar' => [],
  25. 'bar' => [1, 'foo'],
  26. 'foobar' => [
  27. 'foo' => 'bar',
  28. 'bar' => [1, 'foo'],
  29. 'foobar' => [
  30. 'foo' => 'bar',
  31. 'bar' => [1, 'foo'],
  32. ],
  33. ],
  34. ];
  35. protected function setUp()
  36. {
  37. $this->parser = new Parser();
  38. $this->dumper = new Dumper();
  39. $this->path = __DIR__.'/Fixtures';
  40. }
  41. protected function tearDown()
  42. {
  43. $this->parser = null;
  44. $this->dumper = null;
  45. $this->path = null;
  46. $this->array = null;
  47. }
  48. public function testIndentationInConstructor()
  49. {
  50. $dumper = new Dumper(7);
  51. $expected = <<<'EOF'
  52. '': bar
  53. foo: '#bar'
  54. 'foo''bar': { }
  55. bar:
  56. - 1
  57. - foo
  58. foobar:
  59. foo: bar
  60. bar:
  61. - 1
  62. - foo
  63. foobar:
  64. foo: bar
  65. bar:
  66. - 1
  67. - foo
  68. EOF;
  69. $this->assertEquals($expected, $dumper->dump($this->array, 4, 0));
  70. }
  71. /**
  72. * @group legacy
  73. */
  74. public function testSetIndentation()
  75. {
  76. $this->dumper->setIndentation(7);
  77. $expected = <<<'EOF'
  78. '': bar
  79. foo: '#bar'
  80. 'foo''bar': { }
  81. bar:
  82. - 1
  83. - foo
  84. foobar:
  85. foo: bar
  86. bar:
  87. - 1
  88. - foo
  89. foobar:
  90. foo: bar
  91. bar:
  92. - 1
  93. - foo
  94. EOF;
  95. $this->assertEquals($expected, $this->dumper->dump($this->array, 4, 0));
  96. }
  97. public function testSpecifications()
  98. {
  99. $files = $this->parser->parse(file_get_contents($this->path.'/index.yml'));
  100. foreach ($files as $file) {
  101. $yamls = file_get_contents($this->path.'/'.$file.'.yml');
  102. // split YAMLs documents
  103. foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
  104. if (!$yaml) {
  105. continue;
  106. }
  107. $test = $this->parser->parse($yaml);
  108. if (isset($test['dump_skip']) && $test['dump_skip']) {
  109. continue;
  110. } elseif (isset($test['todo']) && $test['todo']) {
  111. // TODO
  112. } else {
  113. eval('$expected = '.trim($test['php']).';');
  114. $this->assertSame($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']);
  115. }
  116. }
  117. }
  118. }
  119. public function testInlineLevel()
  120. {
  121. $expected = <<<'EOF'
  122. { '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
  123. EOF;
  124. $this->assertEquals($expected, $this->dumper->dump($this->array, -10), '->dump() takes an inline level argument');
  125. $this->assertEquals($expected, $this->dumper->dump($this->array, 0), '->dump() takes an inline level argument');
  126. $expected = <<<'EOF'
  127. '': bar
  128. foo: '#bar'
  129. 'foo''bar': { }
  130. bar: [1, foo]
  131. foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
  132. EOF;
  133. $this->assertEquals($expected, $this->dumper->dump($this->array, 1), '->dump() takes an inline level argument');
  134. $expected = <<<'EOF'
  135. '': bar
  136. foo: '#bar'
  137. 'foo''bar': { }
  138. bar:
  139. - 1
  140. - foo
  141. foobar:
  142. foo: bar
  143. bar: [1, foo]
  144. foobar: { foo: bar, bar: [1, foo] }
  145. EOF;
  146. $this->assertEquals($expected, $this->dumper->dump($this->array, 2), '->dump() takes an inline level argument');
  147. $expected = <<<'EOF'
  148. '': bar
  149. foo: '#bar'
  150. 'foo''bar': { }
  151. bar:
  152. - 1
  153. - foo
  154. foobar:
  155. foo: bar
  156. bar:
  157. - 1
  158. - foo
  159. foobar:
  160. foo: bar
  161. bar: [1, foo]
  162. EOF;
  163. $this->assertEquals($expected, $this->dumper->dump($this->array, 3), '->dump() takes an inline level argument');
  164. $expected = <<<'EOF'
  165. '': bar
  166. foo: '#bar'
  167. 'foo''bar': { }
  168. bar:
  169. - 1
  170. - foo
  171. foobar:
  172. foo: bar
  173. bar:
  174. - 1
  175. - foo
  176. foobar:
  177. foo: bar
  178. bar:
  179. - 1
  180. - foo
  181. EOF;
  182. $this->assertEquals($expected, $this->dumper->dump($this->array, 4), '->dump() takes an inline level argument');
  183. $this->assertEquals($expected, $this->dumper->dump($this->array, 10), '->dump() takes an inline level argument');
  184. }
  185. public function testObjectSupportEnabled()
  186. {
  187. $dump = $this->dumper->dump(['foo' => new A(), 'bar' => 1], 0, 0, Yaml::DUMP_OBJECT);
  188. $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');
  189. }
  190. /**
  191. * @group legacy
  192. */
  193. public function testObjectSupportEnabledPassingTrue()
  194. {
  195. $dump = $this->dumper->dump(['foo' => new A(), 'bar' => 1], 0, 0, false, true);
  196. $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');
  197. }
  198. public function testObjectSupportDisabledButNoExceptions()
  199. {
  200. $dump = $this->dumper->dump(['foo' => new A(), 'bar' => 1]);
  201. $this->assertEquals('{ foo: null, bar: 1 }', $dump, '->dump() does not dump objects when disabled');
  202. }
  203. public function testObjectSupportDisabledWithExceptions()
  204. {
  205. $this->expectException('Symfony\Component\Yaml\Exception\DumpException');
  206. $this->dumper->dump(['foo' => new A(), 'bar' => 1], 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
  207. }
  208. /**
  209. * @group legacy
  210. */
  211. public function testObjectSupportDisabledWithExceptionsPassingTrue()
  212. {
  213. $this->expectException('Symfony\Component\Yaml\Exception\DumpException');
  214. $this->dumper->dump(['foo' => new A(), 'bar' => 1], 0, 0, true);
  215. }
  216. public function testEmptyArray()
  217. {
  218. $dump = $this->dumper->dump([]);
  219. $this->assertEquals('{ }', $dump);
  220. $dump = $this->dumper->dump([], 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
  221. $this->assertEquals('[]', $dump);
  222. $dump = $this->dumper->dump([], 9, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
  223. $this->assertEquals('[]', $dump);
  224. $dump = $this->dumper->dump(new \ArrayObject(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP);
  225. $this->assertEquals('{ }', $dump);
  226. $dump = $this->dumper->dump(new \stdClass(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP);
  227. $this->assertEquals('{ }', $dump);
  228. }
  229. /**
  230. * @dataProvider getEscapeSequences
  231. */
  232. public function testEscapedEscapeSequencesInQuotedScalar($input, $expected)
  233. {
  234. $this->assertEquals($expected, $this->dumper->dump($input));
  235. }
  236. public function getEscapeSequences()
  237. {
  238. return [
  239. 'empty string' => ['', "''"],
  240. 'null' => ["\x0", '"\\0"'],
  241. 'bell' => ["\x7", '"\\a"'],
  242. 'backspace' => ["\x8", '"\\b"'],
  243. 'horizontal-tab' => ["\t", '"\\t"'],
  244. 'line-feed' => ["\n", '"\\n"'],
  245. 'vertical-tab' => ["\v", '"\\v"'],
  246. 'form-feed' => ["\xC", '"\\f"'],
  247. 'carriage-return' => ["\r", '"\\r"'],
  248. 'escape' => ["\x1B", '"\\e"'],
  249. 'space' => [' ', "' '"],
  250. 'double-quote' => ['"', "'\"'"],
  251. 'slash' => ['/', '/'],
  252. 'backslash' => ['\\', '\\'],
  253. 'del' => ["\x7f", '"\x7f"'],
  254. 'next-line' => ["\xC2\x85", '"\\N"'],
  255. 'non-breaking-space' => ["\xc2\xa0", '"\\_"'],
  256. 'line-separator' => ["\xE2\x80\xA8", '"\\L"'],
  257. 'paragraph-separator' => ["\xE2\x80\xA9", '"\\P"'],
  258. 'colon' => [':', "':'"],
  259. ];
  260. }
  261. public function testBinaryDataIsDumpedBase64Encoded()
  262. {
  263. $binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif');
  264. $expected = '{ data: !!binary '.base64_encode($binaryData).' }';
  265. $this->assertSame($expected, $this->dumper->dump(['data' => $binaryData]));
  266. }
  267. public function testNonUtf8DataIsDumpedBase64Encoded()
  268. {
  269. // "für" (ISO-8859-1 encoded)
  270. $this->assertSame('!!binary ZsM/cg==', $this->dumper->dump("f\xc3\x3fr"));
  271. }
  272. /**
  273. * @dataProvider objectAsMapProvider
  274. */
  275. public function testDumpObjectAsMap($object, $expected)
  276. {
  277. $yaml = $this->dumper->dump($object, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
  278. $this->assertEquals($expected, Yaml::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
  279. }
  280. public function objectAsMapProvider()
  281. {
  282. $tests = [];
  283. $bar = new \stdClass();
  284. $bar->class = 'classBar';
  285. $bar->args = ['bar'];
  286. $zar = new \stdClass();
  287. $foo = new \stdClass();
  288. $foo->bar = $bar;
  289. $foo->zar = $zar;
  290. $object = new \stdClass();
  291. $object->foo = $foo;
  292. $tests['stdClass'] = [$object, $object];
  293. $arrayObject = new \ArrayObject();
  294. $arrayObject['foo'] = 'bar';
  295. $arrayObject['baz'] = 'foobar';
  296. $parsedArrayObject = new \stdClass();
  297. $parsedArrayObject->foo = 'bar';
  298. $parsedArrayObject->baz = 'foobar';
  299. $tests['ArrayObject'] = [$arrayObject, $parsedArrayObject];
  300. $a = new A();
  301. $tests['arbitrary-object'] = [$a, null];
  302. return $tests;
  303. }
  304. public function testDumpingArrayObjectInstancesRespectsInlineLevel()
  305. {
  306. $deep = new \ArrayObject(['deep1' => 'd', 'deep2' => 'e']);
  307. $inner = new \ArrayObject(['inner1' => 'b', 'inner2' => 'c', 'inner3' => $deep]);
  308. $outer = new \ArrayObject(['outer1' => 'a', 'outer2' => $inner]);
  309. $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
  310. $expected = <<<YAML
  311. outer1: a
  312. outer2:
  313. inner1: b
  314. inner2: c
  315. inner3: { deep1: d, deep2: e }
  316. YAML;
  317. $this->assertSame($expected, $yaml);
  318. }
  319. public function testDumpingArrayObjectInstancesWithNumericKeysInlined()
  320. {
  321. $deep = new \ArrayObject(['d', 'e']);
  322. $inner = new \ArrayObject(['b', 'c', $deep]);
  323. $outer = new \ArrayObject(['a', $inner]);
  324. $yaml = $this->dumper->dump($outer, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
  325. $expected = <<<YAML
  326. { 0: a, 1: { 0: b, 1: c, 2: { 0: d, 1: e } } }
  327. YAML;
  328. $this->assertSame($expected, $yaml);
  329. }
  330. public function testDumpingArrayObjectInstancesWithNumericKeysRespectsInlineLevel()
  331. {
  332. $deep = new \ArrayObject(['d', 'e']);
  333. $inner = new \ArrayObject(['b', 'c', $deep]);
  334. $outer = new \ArrayObject(['a', $inner]);
  335. $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
  336. $expected = <<<YAML
  337. 0: a
  338. 1:
  339. 0: b
  340. 1: c
  341. 2: { 0: d, 1: e }
  342. YAML;
  343. $this->assertEquals($expected, $yaml);
  344. }
  345. public function testDumpEmptyArrayObjectInstanceAsMap()
  346. {
  347. $this->assertSame('{ }', $this->dumper->dump(new \ArrayObject(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
  348. }
  349. public function testDumpEmptyStdClassInstanceAsMap()
  350. {
  351. $this->assertSame('{ }', $this->dumper->dump(new \stdClass(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
  352. }
  353. public function testDumpingStdClassInstancesRespectsInlineLevel()
  354. {
  355. $deep = new \stdClass();
  356. $deep->deep1 = 'd';
  357. $deep->deep2 = 'e';
  358. $inner = new \stdClass();
  359. $inner->inner1 = 'b';
  360. $inner->inner2 = 'c';
  361. $inner->inner3 = $deep;
  362. $outer = new \stdClass();
  363. $outer->outer1 = 'a';
  364. $outer->outer2 = $inner;
  365. $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
  366. $expected = <<<YAML
  367. outer1: a
  368. outer2:
  369. inner1: b
  370. inner2: c
  371. inner3: { deep1: d, deep2: e }
  372. YAML;
  373. $this->assertSame($expected, $yaml);
  374. }
  375. public function testDumpingTaggedValueSequenceRespectsInlineLevel()
  376. {
  377. $data = [
  378. new TaggedValue('user', [
  379. 'username' => 'jane',
  380. ]),
  381. new TaggedValue('user', [
  382. 'username' => 'john',
  383. ]),
  384. ];
  385. $yaml = $this->dumper->dump($data, 2);
  386. $expected = <<<YAML
  387. - !user
  388. username: jane
  389. - !user
  390. username: john
  391. YAML;
  392. $this->assertSame($expected, $yaml);
  393. }
  394. public function testDumpingTaggedValueSequenceWithInlinedTagValues()
  395. {
  396. $data = [
  397. new TaggedValue('user', [
  398. 'username' => 'jane',
  399. ]),
  400. new TaggedValue('user', [
  401. 'username' => 'john',
  402. ]),
  403. ];
  404. $yaml = $this->dumper->dump($data, 1);
  405. $expected = <<<YAML
  406. - !user { username: jane }
  407. - !user { username: john }
  408. YAML;
  409. $this->assertSame($expected, $yaml);
  410. }
  411. public function testDumpingTaggedValueMapRespectsInlineLevel()
  412. {
  413. $data = [
  414. 'user1' => new TaggedValue('user', [
  415. 'username' => 'jane',
  416. ]),
  417. 'user2' => new TaggedValue('user', [
  418. 'username' => 'john',
  419. ]),
  420. ];
  421. $yaml = $this->dumper->dump($data, 2);
  422. $expected = <<<YAML
  423. user1: !user
  424. username: jane
  425. user2: !user
  426. username: john
  427. YAML;
  428. $this->assertSame($expected, $yaml);
  429. }
  430. public function testDumpingTaggedValueMapWithInlinedTagValues()
  431. {
  432. $data = [
  433. 'user1' => new TaggedValue('user', [
  434. 'username' => 'jane',
  435. ]),
  436. 'user2' => new TaggedValue('user', [
  437. 'username' => 'john',
  438. ]),
  439. ];
  440. $yaml = $this->dumper->dump($data, 1);
  441. $expected = <<<YAML
  442. user1: !user { username: jane }
  443. user2: !user { username: john }
  444. YAML;
  445. $this->assertSame($expected, $yaml);
  446. }
  447. public function testDumpingNotInlinedScalarTaggedValue()
  448. {
  449. $data = [
  450. 'user1' => new TaggedValue('user', 'jane'),
  451. 'user2' => new TaggedValue('user', 'john'),
  452. ];
  453. $expected = <<<YAML
  454. user1: !user jane
  455. user2: !user john
  456. YAML;
  457. $this->assertSame($expected, $this->dumper->dump($data, 2));
  458. }
  459. public function testDumpingNotInlinedNullTaggedValue()
  460. {
  461. $data = [
  462. 'foo' => new TaggedValue('bar', null),
  463. ];
  464. $expected = <<<YAML
  465. foo: !bar null
  466. YAML;
  467. $this->assertSame($expected, $this->dumper->dump($data, 2));
  468. }
  469. public function testDumpingMultiLineStringAsScalarBlockTaggedValue()
  470. {
  471. $data = [
  472. 'foo' => new TaggedValue('bar', "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"),
  473. ];
  474. $expected = <<<YAML
  475. foo: !bar |
  476. foo
  477. line with trailing spaces:
  478. bar
  479. integer like line:
  480. 123456789
  481. empty line:
  482. baz
  483. YAML;
  484. $this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
  485. }
  486. public function testDumpingInlinedMultiLineIfRnBreakLineInTaggedValue()
  487. {
  488. $data = [
  489. 'data' => [
  490. 'foo' => new TaggedValue('bar', "foo\r\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"),
  491. ],
  492. ];
  493. $this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block_for_tagged_values.yml'), $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
  494. }
  495. public function testDumpMultiLineStringAsScalarBlock()
  496. {
  497. $data = [
  498. 'data' => [
  499. 'single_line' => 'foo bar baz',
  500. 'multi_line' => "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz",
  501. 'multi_line_with_carriage_return' => "foo\nbar\r\nbaz",
  502. 'nested_inlined_multi_line_string' => [
  503. 'inlined_multi_line' => "foo\nbar\r\nempty line:\n\nbaz",
  504. ],
  505. ],
  506. ];
  507. $this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block.yml'), $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
  508. }
  509. public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace()
  510. {
  511. $data = [
  512. 'data' => [
  513. 'multi_line' => " the first line has leading spaces\nThe second line does not.",
  514. ],
  515. ];
  516. $this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block_leading_space_in_first_line.yml'), $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
  517. }
  518. public function testCarriageReturnFollowedByNewlineIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
  519. {
  520. $this->assertSame("- \"a\\r\\nb\\nc\"\n", $this->dumper->dump(["a\r\nb\nc"], 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
  521. }
  522. public function testCarriageReturnNotFollowedByNewlineIsPreservedWhenDumpingAsMultiLineLiteralBlock()
  523. {
  524. $expected = <<<'YAML'
  525. parent:
  526. foo: "bar\n\rbaz: qux"
  527. YAML;
  528. $this->assertSame($expected, $this->dumper->dump([
  529. 'parent' => [
  530. 'foo' => "bar\n\rbaz: qux",
  531. ],
  532. ], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
  533. }
  534. public function testZeroIndentationThrowsException()
  535. {
  536. $this->expectException('InvalidArgumentException');
  537. $this->expectExceptionMessage('The indentation must be greater than zero');
  538. new Dumper(0);
  539. }
  540. public function testNegativeIndentationThrowsException()
  541. {
  542. $this->expectException('InvalidArgumentException');
  543. $this->expectExceptionMessage('The indentation must be greater than zero');
  544. new Dumper(-4);
  545. }
  546. }
  547. class A
  548. {
  549. public $a = 'foo';
  550. }