DumperTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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(): void
  36. {
  37. $this->parser = new Parser();
  38. $this->dumper = new Dumper();
  39. $this->path = __DIR__.'/Fixtures';
  40. }
  41. protected function tearDown(): void
  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. public function testSpecifications()
  72. {
  73. $files = $this->parser->parse(file_get_contents($this->path.'/index.yml'));
  74. foreach ($files as $file) {
  75. $yamls = file_get_contents($this->path.'/'.$file.'.yml');
  76. // split YAMLs documents
  77. foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
  78. if (!$yaml) {
  79. continue;
  80. }
  81. $test = $this->parser->parse($yaml);
  82. if (isset($test['dump_skip']) && $test['dump_skip']) {
  83. continue;
  84. } elseif (isset($test['todo']) && $test['todo']) {
  85. // TODO
  86. } else {
  87. eval('$expected = '.trim($test['php']).';');
  88. $this->assertSame($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']);
  89. }
  90. }
  91. }
  92. }
  93. public function testInlineLevel()
  94. {
  95. $expected = <<<'EOF'
  96. { '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
  97. EOF;
  98. $this->assertEquals($expected, $this->dumper->dump($this->array, -10), '->dump() takes an inline level argument');
  99. $this->assertEquals($expected, $this->dumper->dump($this->array, 0), '->dump() takes an inline level argument');
  100. $expected = <<<'EOF'
  101. '': bar
  102. foo: '#bar'
  103. 'foo''bar': { }
  104. bar: [1, foo]
  105. foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
  106. EOF;
  107. $this->assertEquals($expected, $this->dumper->dump($this->array, 1), '->dump() takes an inline level argument');
  108. $expected = <<<'EOF'
  109. '': bar
  110. foo: '#bar'
  111. 'foo''bar': { }
  112. bar:
  113. - 1
  114. - foo
  115. foobar:
  116. foo: bar
  117. bar: [1, foo]
  118. foobar: { foo: bar, bar: [1, foo] }
  119. EOF;
  120. $this->assertEquals($expected, $this->dumper->dump($this->array, 2), '->dump() takes an inline level argument');
  121. $expected = <<<'EOF'
  122. '': bar
  123. foo: '#bar'
  124. 'foo''bar': { }
  125. bar:
  126. - 1
  127. - foo
  128. foobar:
  129. foo: bar
  130. bar:
  131. - 1
  132. - foo
  133. foobar:
  134. foo: bar
  135. bar: [1, foo]
  136. EOF;
  137. $this->assertEquals($expected, $this->dumper->dump($this->array, 3), '->dump() takes an inline level argument');
  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:
  153. - 1
  154. - foo
  155. EOF;
  156. $this->assertEquals($expected, $this->dumper->dump($this->array, 4), '->dump() takes an inline level argument');
  157. $this->assertEquals($expected, $this->dumper->dump($this->array, 10), '->dump() takes an inline level argument');
  158. }
  159. public function testObjectSupportEnabled()
  160. {
  161. $dump = $this->dumper->dump(['foo' => new A(), 'bar' => 1], 0, 0, Yaml::DUMP_OBJECT);
  162. $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');
  163. }
  164. public function testObjectSupportDisabledButNoExceptions()
  165. {
  166. $dump = $this->dumper->dump(['foo' => new A(), 'bar' => 1]);
  167. $this->assertEquals('{ foo: null, bar: 1 }', $dump, '->dump() does not dump objects when disabled');
  168. }
  169. public function testObjectSupportDisabledWithExceptions()
  170. {
  171. $this->expectException('Symfony\Component\Yaml\Exception\DumpException');
  172. $this->dumper->dump(['foo' => new A(), 'bar' => 1], 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
  173. }
  174. /**
  175. * @dataProvider getEscapeSequences
  176. */
  177. public function testEscapedEscapeSequencesInQuotedScalar($input, $expected)
  178. {
  179. $this->assertEquals($expected, $this->dumper->dump($input));
  180. }
  181. public function getEscapeSequences()
  182. {
  183. return [
  184. 'empty string' => ['', "''"],
  185. 'null' => ["\x0", '"\\0"'],
  186. 'bell' => ["\x7", '"\\a"'],
  187. 'backspace' => ["\x8", '"\\b"'],
  188. 'horizontal-tab' => ["\t", '"\\t"'],
  189. 'line-feed' => ["\n", '"\\n"'],
  190. 'vertical-tab' => ["\v", '"\\v"'],
  191. 'form-feed' => ["\xC", '"\\f"'],
  192. 'carriage-return' => ["\r", '"\\r"'],
  193. 'escape' => ["\x1B", '"\\e"'],
  194. 'space' => [' ', "' '"],
  195. 'double-quote' => ['"', "'\"'"],
  196. 'slash' => ['/', '/'],
  197. 'backslash' => ['\\', '\\'],
  198. 'next-line' => ["\xC2\x85", '"\\N"'],
  199. 'non-breaking-space' => ["\xc2\xa0", '"\\_"'],
  200. 'line-separator' => ["\xE2\x80\xA8", '"\\L"'],
  201. 'paragraph-separator' => ["\xE2\x80\xA9", '"\\P"'],
  202. 'colon' => [':', "':'"],
  203. ];
  204. }
  205. public function testBinaryDataIsDumpedBase64Encoded()
  206. {
  207. $binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif');
  208. $expected = '{ data: !!binary '.base64_encode($binaryData).' }';
  209. $this->assertSame($expected, $this->dumper->dump(['data' => $binaryData]));
  210. }
  211. public function testNonUtf8DataIsDumpedBase64Encoded()
  212. {
  213. // "für" (ISO-8859-1 encoded)
  214. $this->assertSame('!!binary ZsM/cg==', $this->dumper->dump("f\xc3\x3fr"));
  215. }
  216. /**
  217. * @dataProvider objectAsMapProvider
  218. */
  219. public function testDumpObjectAsMap($object, $expected)
  220. {
  221. $yaml = $this->dumper->dump($object, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
  222. $this->assertEquals($expected, Yaml::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
  223. }
  224. public function objectAsMapProvider()
  225. {
  226. $tests = [];
  227. $bar = new \stdClass();
  228. $bar->class = 'classBar';
  229. $bar->args = ['bar'];
  230. $zar = new \stdClass();
  231. $foo = new \stdClass();
  232. $foo->bar = $bar;
  233. $foo->zar = $zar;
  234. $object = new \stdClass();
  235. $object->foo = $foo;
  236. $tests['stdClass'] = [$object, $object];
  237. $arrayObject = new \ArrayObject();
  238. $arrayObject['foo'] = 'bar';
  239. $arrayObject['baz'] = 'foobar';
  240. $parsedArrayObject = new \stdClass();
  241. $parsedArrayObject->foo = 'bar';
  242. $parsedArrayObject->baz = 'foobar';
  243. $tests['ArrayObject'] = [$arrayObject, $parsedArrayObject];
  244. $a = new A();
  245. $tests['arbitrary-object'] = [$a, null];
  246. return $tests;
  247. }
  248. public function testDumpingArrayObjectInstancesRespectsInlineLevel()
  249. {
  250. $deep = new \ArrayObject(['deep1' => 'd', 'deep2' => 'e']);
  251. $inner = new \ArrayObject(['inner1' => 'b', 'inner2' => 'c', 'inner3' => $deep]);
  252. $outer = new \ArrayObject(['outer1' => 'a', 'outer2' => $inner]);
  253. $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
  254. $expected = <<<YAML
  255. outer1: a
  256. outer2:
  257. inner1: b
  258. inner2: c
  259. inner3: { deep1: d, deep2: e }
  260. YAML;
  261. $this->assertSame($expected, $yaml);
  262. }
  263. public function testDumpingArrayObjectInstancesWithNumericKeysInlined()
  264. {
  265. $deep = new \ArrayObject(['d', 'e']);
  266. $inner = new \ArrayObject(['b', 'c', $deep]);
  267. $outer = new \ArrayObject(['a', $inner]);
  268. $yaml = $this->dumper->dump($outer, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
  269. $expected = <<<YAML
  270. { 0: a, 1: { 0: b, 1: c, 2: { 0: d, 1: e } } }
  271. YAML;
  272. $this->assertSame($expected, $yaml);
  273. }
  274. public function testDumpingArrayObjectInstancesWithNumericKeysRespectsInlineLevel()
  275. {
  276. $deep = new \ArrayObject(['d', 'e']);
  277. $inner = new \ArrayObject(['b', 'c', $deep]);
  278. $outer = new \ArrayObject(['a', $inner]);
  279. $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
  280. $expected = <<<YAML
  281. 0: a
  282. 1:
  283. 0: b
  284. 1: c
  285. 2: { 0: d, 1: e }
  286. YAML;
  287. $this->assertEquals($expected, $yaml);
  288. }
  289. public function testDumpEmptyArrayObjectInstanceAsMap()
  290. {
  291. $this->assertSame('{ }', $this->dumper->dump(new \ArrayObject(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
  292. }
  293. public function testDumpEmptyStdClassInstanceAsMap()
  294. {
  295. $this->assertSame('{ }', $this->dumper->dump(new \stdClass(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
  296. }
  297. public function testDumpingStdClassInstancesRespectsInlineLevel()
  298. {
  299. $deep = new \stdClass();
  300. $deep->deep1 = 'd';
  301. $deep->deep2 = 'e';
  302. $inner = new \stdClass();
  303. $inner->inner1 = 'b';
  304. $inner->inner2 = 'c';
  305. $inner->inner3 = $deep;
  306. $outer = new \stdClass();
  307. $outer->outer1 = 'a';
  308. $outer->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 testDumpingTaggedValueSequenceRespectsInlineLevel()
  320. {
  321. $data = [
  322. new TaggedValue('user', [
  323. 'username' => 'jane',
  324. ]),
  325. new TaggedValue('user', [
  326. 'username' => 'john',
  327. ]),
  328. ];
  329. $yaml = $this->dumper->dump($data, 2);
  330. $expected = <<<YAML
  331. - !user
  332. username: jane
  333. - !user
  334. username: john
  335. YAML;
  336. $this->assertSame($expected, $yaml);
  337. }
  338. public function testDumpingTaggedValueSequenceWithInlinedTagValues()
  339. {
  340. $data = [
  341. new TaggedValue('user', [
  342. 'username' => 'jane',
  343. ]),
  344. new TaggedValue('user', [
  345. 'username' => 'john',
  346. ]),
  347. ];
  348. $yaml = $this->dumper->dump($data, 1);
  349. $expected = <<<YAML
  350. - !user { username: jane }
  351. - !user { username: john }
  352. YAML;
  353. $this->assertSame($expected, $yaml);
  354. }
  355. public function testDumpingTaggedValueMapRespectsInlineLevel()
  356. {
  357. $data = [
  358. 'user1' => new TaggedValue('user', [
  359. 'username' => 'jane',
  360. ]),
  361. 'user2' => new TaggedValue('user', [
  362. 'username' => 'john',
  363. ]),
  364. ];
  365. $yaml = $this->dumper->dump($data, 2);
  366. $expected = <<<YAML
  367. user1: !user
  368. username: jane
  369. user2: !user
  370. username: john
  371. YAML;
  372. $this->assertSame($expected, $yaml);
  373. }
  374. public function testDumpingTaggedValueMapWithInlinedTagValues()
  375. {
  376. $data = [
  377. 'user1' => new TaggedValue('user', [
  378. 'username' => 'jane',
  379. ]),
  380. 'user2' => new TaggedValue('user', [
  381. 'username' => 'john',
  382. ]),
  383. ];
  384. $yaml = $this->dumper->dump($data, 1);
  385. $expected = <<<YAML
  386. user1: !user { username: jane }
  387. user2: !user { username: john }
  388. YAML;
  389. $this->assertSame($expected, $yaml);
  390. }
  391. public function testDumpingNotInlinedScalarTaggedValue()
  392. {
  393. $data = [
  394. 'user1' => new TaggedValue('user', 'jane'),
  395. 'user2' => new TaggedValue('user', 'john'),
  396. ];
  397. $expected = <<<YAML
  398. user1: !user jane
  399. user2: !user john
  400. YAML;
  401. $this->assertSame($expected, $this->dumper->dump($data, 2));
  402. }
  403. public function testDumpingNotInlinedNullTaggedValue()
  404. {
  405. $data = [
  406. 'foo' => new TaggedValue('bar', null),
  407. ];
  408. $expected = <<<YAML
  409. foo: !bar null
  410. YAML;
  411. $this->assertSame($expected, $this->dumper->dump($data, 2));
  412. }
  413. public function testDumpMultiLineStringAsScalarBlock()
  414. {
  415. $data = [
  416. 'data' => [
  417. 'single_line' => 'foo bar baz',
  418. 'multi_line' => "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz",
  419. 'multi_line_with_carriage_return' => "foo\nbar\r\nbaz",
  420. 'nested_inlined_multi_line_string' => [
  421. 'inlined_multi_line' => "foo\nbar\r\nempty line:\n\nbaz",
  422. ],
  423. ],
  424. ];
  425. $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));
  426. }
  427. public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace()
  428. {
  429. $data = [
  430. 'data' => [
  431. 'multi_line' => " the first line has leading spaces\nThe second line does not.",
  432. ],
  433. ];
  434. $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));
  435. }
  436. public function testCarriageReturnIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
  437. {
  438. $this->assertSame("- \"a\\r\\nb\\nc\"\n", $this->dumper->dump(["a\r\nb\nc"], 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
  439. }
  440. public function testZeroIndentationThrowsException()
  441. {
  442. $this->expectException('InvalidArgumentException');
  443. $this->expectExceptionMessage('The indentation must be greater than zero');
  444. new Dumper(0);
  445. }
  446. public function testNegativeIndentationThrowsException()
  447. {
  448. $this->expectException('InvalidArgumentException');
  449. $this->expectExceptionMessage('The indentation must be greater than zero');
  450. new Dumper(-4);
  451. }
  452. }
  453. class A
  454. {
  455. public $a = 'foo';
  456. }