DumperTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. 'next-line' => ["\xC2\x85", '"\\N"'],
  254. 'non-breaking-space' => ["\xc2\xa0", '"\\_"'],
  255. 'line-separator' => ["\xE2\x80\xA8", '"\\L"'],
  256. 'paragraph-separator' => ["\xE2\x80\xA9", '"\\P"'],
  257. 'colon' => [':', "':'"],
  258. ];
  259. }
  260. public function testBinaryDataIsDumpedBase64Encoded()
  261. {
  262. $binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif');
  263. $expected = '{ data: !!binary '.base64_encode($binaryData).' }';
  264. $this->assertSame($expected, $this->dumper->dump(['data' => $binaryData]));
  265. }
  266. public function testNonUtf8DataIsDumpedBase64Encoded()
  267. {
  268. // "für" (ISO-8859-1 encoded)
  269. $this->assertSame('!!binary ZsM/cg==', $this->dumper->dump("f\xc3\x3fr"));
  270. }
  271. /**
  272. * @dataProvider objectAsMapProvider
  273. */
  274. public function testDumpObjectAsMap($object, $expected)
  275. {
  276. $yaml = $this->dumper->dump($object, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
  277. $this->assertEquals($expected, Yaml::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
  278. }
  279. public function objectAsMapProvider()
  280. {
  281. $tests = [];
  282. $bar = new \stdClass();
  283. $bar->class = 'classBar';
  284. $bar->args = ['bar'];
  285. $zar = new \stdClass();
  286. $foo = new \stdClass();
  287. $foo->bar = $bar;
  288. $foo->zar = $zar;
  289. $object = new \stdClass();
  290. $object->foo = $foo;
  291. $tests['stdClass'] = [$object, $object];
  292. $arrayObject = new \ArrayObject();
  293. $arrayObject['foo'] = 'bar';
  294. $arrayObject['baz'] = 'foobar';
  295. $parsedArrayObject = new \stdClass();
  296. $parsedArrayObject->foo = 'bar';
  297. $parsedArrayObject->baz = 'foobar';
  298. $tests['ArrayObject'] = [$arrayObject, $parsedArrayObject];
  299. $a = new A();
  300. $tests['arbitrary-object'] = [$a, null];
  301. return $tests;
  302. }
  303. public function testDumpingArrayObjectInstancesRespectsInlineLevel()
  304. {
  305. $deep = new \ArrayObject(['deep1' => 'd', 'deep2' => 'e']);
  306. $inner = new \ArrayObject(['inner1' => 'b', 'inner2' => 'c', 'inner3' => $deep]);
  307. $outer = new \ArrayObject(['outer1' => 'a', 'outer2' => $inner]);
  308. $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
  309. $expected = <<<YAML
  310. outer1: a
  311. outer2:
  312. inner1: b
  313. inner2: c
  314. inner3: { deep1: d, deep2: e }
  315. YAML;
  316. $this->assertSame($expected, $yaml);
  317. }
  318. public function testDumpingArrayObjectInstancesWithNumericKeysInlined()
  319. {
  320. $deep = new \ArrayObject(['d', 'e']);
  321. $inner = new \ArrayObject(['b', 'c', $deep]);
  322. $outer = new \ArrayObject(['a', $inner]);
  323. $yaml = $this->dumper->dump($outer, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
  324. $expected = <<<YAML
  325. { 0: a, 1: { 0: b, 1: c, 2: { 0: d, 1: e } } }
  326. YAML;
  327. $this->assertSame($expected, $yaml);
  328. }
  329. public function testDumpingArrayObjectInstancesWithNumericKeysRespectsInlineLevel()
  330. {
  331. $deep = new \ArrayObject(['d', 'e']);
  332. $inner = new \ArrayObject(['b', 'c', $deep]);
  333. $outer = new \ArrayObject(['a', $inner]);
  334. $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
  335. $expected = <<<YAML
  336. 0: a
  337. 1:
  338. 0: b
  339. 1: c
  340. 2: { 0: d, 1: e }
  341. YAML;
  342. $this->assertEquals($expected, $yaml);
  343. }
  344. public function testDumpEmptyArrayObjectInstanceAsMap()
  345. {
  346. $this->assertSame('{ }', $this->dumper->dump(new \ArrayObject(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
  347. }
  348. public function testDumpEmptyStdClassInstanceAsMap()
  349. {
  350. $this->assertSame('{ }', $this->dumper->dump(new \stdClass(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
  351. }
  352. public function testDumpingStdClassInstancesRespectsInlineLevel()
  353. {
  354. $deep = new \stdClass();
  355. $deep->deep1 = 'd';
  356. $deep->deep2 = 'e';
  357. $inner = new \stdClass();
  358. $inner->inner1 = 'b';
  359. $inner->inner2 = 'c';
  360. $inner->inner3 = $deep;
  361. $outer = new \stdClass();
  362. $outer->outer1 = 'a';
  363. $outer->outer2 = $inner;
  364. $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
  365. $expected = <<<YAML
  366. outer1: a
  367. outer2:
  368. inner1: b
  369. inner2: c
  370. inner3: { deep1: d, deep2: e }
  371. YAML;
  372. $this->assertSame($expected, $yaml);
  373. }
  374. public function testDumpingTaggedValueSequenceRespectsInlineLevel()
  375. {
  376. $data = [
  377. new TaggedValue('user', [
  378. 'username' => 'jane',
  379. ]),
  380. new TaggedValue('user', [
  381. 'username' => 'john',
  382. ]),
  383. ];
  384. $yaml = $this->dumper->dump($data, 2);
  385. $expected = <<<YAML
  386. - !user
  387. username: jane
  388. - !user
  389. username: john
  390. YAML;
  391. $this->assertSame($expected, $yaml);
  392. }
  393. public function testDumpingTaggedValueSequenceWithInlinedTagValues()
  394. {
  395. $data = [
  396. new TaggedValue('user', [
  397. 'username' => 'jane',
  398. ]),
  399. new TaggedValue('user', [
  400. 'username' => 'john',
  401. ]),
  402. ];
  403. $yaml = $this->dumper->dump($data, 1);
  404. $expected = <<<YAML
  405. - !user { username: jane }
  406. - !user { username: john }
  407. YAML;
  408. $this->assertSame($expected, $yaml);
  409. }
  410. public function testDumpingTaggedValueMapRespectsInlineLevel()
  411. {
  412. $data = [
  413. 'user1' => new TaggedValue('user', [
  414. 'username' => 'jane',
  415. ]),
  416. 'user2' => new TaggedValue('user', [
  417. 'username' => 'john',
  418. ]),
  419. ];
  420. $yaml = $this->dumper->dump($data, 2);
  421. $expected = <<<YAML
  422. user1: !user
  423. username: jane
  424. user2: !user
  425. username: john
  426. YAML;
  427. $this->assertSame($expected, $yaml);
  428. }
  429. public function testDumpingTaggedValueMapWithInlinedTagValues()
  430. {
  431. $data = [
  432. 'user1' => new TaggedValue('user', [
  433. 'username' => 'jane',
  434. ]),
  435. 'user2' => new TaggedValue('user', [
  436. 'username' => 'john',
  437. ]),
  438. ];
  439. $yaml = $this->dumper->dump($data, 1);
  440. $expected = <<<YAML
  441. user1: !user { username: jane }
  442. user2: !user { username: john }
  443. YAML;
  444. $this->assertSame($expected, $yaml);
  445. }
  446. public function testDumpingNotInlinedScalarTaggedValue()
  447. {
  448. $data = [
  449. 'user1' => new TaggedValue('user', 'jane'),
  450. 'user2' => new TaggedValue('user', 'john'),
  451. ];
  452. $expected = <<<YAML
  453. user1: !user jane
  454. user2: !user john
  455. YAML;
  456. $this->assertSame($expected, $this->dumper->dump($data, 2));
  457. }
  458. public function testDumpingNotInlinedNullTaggedValue()
  459. {
  460. $data = [
  461. 'foo' => new TaggedValue('bar', null),
  462. ];
  463. $expected = <<<YAML
  464. foo: !bar null
  465. YAML;
  466. $this->assertSame($expected, $this->dumper->dump($data, 2));
  467. }
  468. public function testDumpMultiLineStringAsScalarBlock()
  469. {
  470. $data = [
  471. 'data' => [
  472. 'single_line' => 'foo bar baz',
  473. 'multi_line' => "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz",
  474. 'multi_line_with_carriage_return' => "foo\nbar\r\nbaz",
  475. 'nested_inlined_multi_line_string' => [
  476. 'inlined_multi_line' => "foo\nbar\r\nempty line:\n\nbaz",
  477. ],
  478. ],
  479. ];
  480. $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));
  481. }
  482. public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace()
  483. {
  484. $data = [
  485. 'data' => [
  486. 'multi_line' => " the first line has leading spaces\nThe second line does not.",
  487. ],
  488. ];
  489. $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));
  490. }
  491. public function testCarriageReturnIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
  492. {
  493. $this->assertSame("- \"a\\r\\nb\\nc\"\n", $this->dumper->dump(["a\r\nb\nc"], 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
  494. }
  495. public function testZeroIndentationThrowsException()
  496. {
  497. $this->expectException('InvalidArgumentException');
  498. $this->expectExceptionMessage('The indentation must be greater than zero');
  499. new Dumper(0);
  500. }
  501. public function testNegativeIndentationThrowsException()
  502. {
  503. $this->expectException('InvalidArgumentException');
  504. $this->expectExceptionMessage('The indentation must be greater than zero');
  505. new Dumper(-4);
  506. }
  507. }
  508. class A
  509. {
  510. public $a = 'foo';
  511. }