DumperTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. 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. /**
  170. * @expectedException \Symfony\Component\Yaml\Exception\DumpException
  171. */
  172. public function testObjectSupportDisabledWithExceptions()
  173. {
  174. $this->dumper->dump(['foo' => new A(), 'bar' => 1], 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
  175. }
  176. /**
  177. * @dataProvider getEscapeSequences
  178. */
  179. public function testEscapedEscapeSequencesInQuotedScalar($input, $expected)
  180. {
  181. $this->assertEquals($expected, $this->dumper->dump($input));
  182. }
  183. public function getEscapeSequences()
  184. {
  185. return [
  186. 'empty string' => ['', "''"],
  187. 'null' => ["\x0", '"\\0"'],
  188. 'bell' => ["\x7", '"\\a"'],
  189. 'backspace' => ["\x8", '"\\b"'],
  190. 'horizontal-tab' => ["\t", '"\\t"'],
  191. 'line-feed' => ["\n", '"\\n"'],
  192. 'vertical-tab' => ["\v", '"\\v"'],
  193. 'form-feed' => ["\xC", '"\\f"'],
  194. 'carriage-return' => ["\r", '"\\r"'],
  195. 'escape' => ["\x1B", '"\\e"'],
  196. 'space' => [' ', "' '"],
  197. 'double-quote' => ['"', "'\"'"],
  198. 'slash' => ['/', '/'],
  199. 'backslash' => ['\\', '\\'],
  200. 'next-line' => ["\xC2\x85", '"\\N"'],
  201. 'non-breaking-space' => ["\xc2\xa0", '"\\_"'],
  202. 'line-separator' => ["\xE2\x80\xA8", '"\\L"'],
  203. 'paragraph-separator' => ["\xE2\x80\xA9", '"\\P"'],
  204. 'colon' => [':', "':'"],
  205. ];
  206. }
  207. public function testBinaryDataIsDumpedBase64Encoded()
  208. {
  209. $binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif');
  210. $expected = '{ data: !!binary '.base64_encode($binaryData).' }';
  211. $this->assertSame($expected, $this->dumper->dump(['data' => $binaryData]));
  212. }
  213. public function testNonUtf8DataIsDumpedBase64Encoded()
  214. {
  215. // "für" (ISO-8859-1 encoded)
  216. $this->assertSame('!!binary ZsM/cg==', $this->dumper->dump("f\xc3\x3fr"));
  217. }
  218. /**
  219. * @dataProvider objectAsMapProvider
  220. */
  221. public function testDumpObjectAsMap($object, $expected)
  222. {
  223. $yaml = $this->dumper->dump($object, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
  224. $this->assertEquals($expected, Yaml::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
  225. }
  226. public function objectAsMapProvider()
  227. {
  228. $tests = [];
  229. $bar = new \stdClass();
  230. $bar->class = 'classBar';
  231. $bar->args = ['bar'];
  232. $zar = new \stdClass();
  233. $foo = new \stdClass();
  234. $foo->bar = $bar;
  235. $foo->zar = $zar;
  236. $object = new \stdClass();
  237. $object->foo = $foo;
  238. $tests['stdClass'] = [$object, $object];
  239. $arrayObject = new \ArrayObject();
  240. $arrayObject['foo'] = 'bar';
  241. $arrayObject['baz'] = 'foobar';
  242. $parsedArrayObject = new \stdClass();
  243. $parsedArrayObject->foo = 'bar';
  244. $parsedArrayObject->baz = 'foobar';
  245. $tests['ArrayObject'] = [$arrayObject, $parsedArrayObject];
  246. $a = new A();
  247. $tests['arbitrary-object'] = [$a, null];
  248. return $tests;
  249. }
  250. public function testDumpingArrayObjectInstancesRespectsInlineLevel()
  251. {
  252. $deep = new \ArrayObject(['deep1' => 'd', 'deep2' => 'e']);
  253. $inner = new \ArrayObject(['inner1' => 'b', 'inner2' => 'c', 'inner3' => $deep]);
  254. $outer = new \ArrayObject(['outer1' => 'a', 'outer2' => $inner]);
  255. $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
  256. $expected = <<<YAML
  257. outer1: a
  258. outer2:
  259. inner1: b
  260. inner2: c
  261. inner3: { deep1: d, deep2: e }
  262. YAML;
  263. $this->assertSame($expected, $yaml);
  264. }
  265. public function testDumpingArrayObjectInstancesWithNumericKeysInlined()
  266. {
  267. $deep = new \ArrayObject(['d', 'e']);
  268. $inner = new \ArrayObject(['b', 'c', $deep]);
  269. $outer = new \ArrayObject(['a', $inner]);
  270. $yaml = $this->dumper->dump($outer, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
  271. $expected = <<<YAML
  272. { 0: a, 1: { 0: b, 1: c, 2: { 0: d, 1: e } } }
  273. YAML;
  274. $this->assertSame($expected, $yaml);
  275. }
  276. public function testDumpingArrayObjectInstancesWithNumericKeysRespectsInlineLevel()
  277. {
  278. $deep = new \ArrayObject(['d', 'e']);
  279. $inner = new \ArrayObject(['b', 'c', $deep]);
  280. $outer = new \ArrayObject(['a', $inner]);
  281. $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
  282. $expected = <<<YAML
  283. 0: a
  284. 1:
  285. 0: b
  286. 1: c
  287. 2: { 0: d, 1: e }
  288. YAML;
  289. $this->assertEquals($expected, $yaml);
  290. }
  291. public function testDumpEmptyArrayObjectInstanceAsMap()
  292. {
  293. $this->assertSame('{ }', $this->dumper->dump(new \ArrayObject(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
  294. }
  295. public function testDumpEmptyStdClassInstanceAsMap()
  296. {
  297. $this->assertSame('{ }', $this->dumper->dump(new \stdClass(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
  298. }
  299. public function testDumpingStdClassInstancesRespectsInlineLevel()
  300. {
  301. $deep = new \stdClass();
  302. $deep->deep1 = 'd';
  303. $deep->deep2 = 'e';
  304. $inner = new \stdClass();
  305. $inner->inner1 = 'b';
  306. $inner->inner2 = 'c';
  307. $inner->inner3 = $deep;
  308. $outer = new \stdClass();
  309. $outer->outer1 = 'a';
  310. $outer->outer2 = $inner;
  311. $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
  312. $expected = <<<YAML
  313. outer1: a
  314. outer2:
  315. inner1: b
  316. inner2: c
  317. inner3: { deep1: d, deep2: e }
  318. YAML;
  319. $this->assertSame($expected, $yaml);
  320. }
  321. public function testDumpingTaggedValueSequenceRespectsInlineLevel()
  322. {
  323. $data = [
  324. new TaggedValue('user', [
  325. 'username' => 'jane',
  326. ]),
  327. new TaggedValue('user', [
  328. 'username' => 'john',
  329. ]),
  330. ];
  331. $yaml = $this->dumper->dump($data, 2);
  332. $expected = <<<YAML
  333. - !user
  334. username: jane
  335. - !user
  336. username: john
  337. YAML;
  338. $this->assertSame($expected, $yaml);
  339. }
  340. public function testDumpingTaggedValueSequenceWithInlinedTagValues()
  341. {
  342. $data = [
  343. new TaggedValue('user', [
  344. 'username' => 'jane',
  345. ]),
  346. new TaggedValue('user', [
  347. 'username' => 'john',
  348. ]),
  349. ];
  350. $yaml = $this->dumper->dump($data, 1);
  351. $expected = <<<YAML
  352. - !user { username: jane }
  353. - !user { username: john }
  354. YAML;
  355. $this->assertSame($expected, $yaml);
  356. }
  357. public function testDumpingTaggedValueMapRespectsInlineLevel()
  358. {
  359. $data = [
  360. 'user1' => new TaggedValue('user', [
  361. 'username' => 'jane',
  362. ]),
  363. 'user2' => new TaggedValue('user', [
  364. 'username' => 'john',
  365. ]),
  366. ];
  367. $yaml = $this->dumper->dump($data, 2);
  368. $expected = <<<YAML
  369. user1: !user
  370. username: jane
  371. user2: !user
  372. username: john
  373. YAML;
  374. $this->assertSame($expected, $yaml);
  375. }
  376. public function testDumpingTaggedValueMapWithInlinedTagValues()
  377. {
  378. $data = [
  379. 'user1' => new TaggedValue('user', [
  380. 'username' => 'jane',
  381. ]),
  382. 'user2' => new TaggedValue('user', [
  383. 'username' => 'john',
  384. ]),
  385. ];
  386. $yaml = $this->dumper->dump($data, 1);
  387. $expected = <<<YAML
  388. user1: !user { username: jane }
  389. user2: !user { username: john }
  390. YAML;
  391. $this->assertSame($expected, $yaml);
  392. }
  393. public function testDumpMultiLineStringAsScalarBlock()
  394. {
  395. $data = [
  396. 'data' => [
  397. 'single_line' => 'foo bar baz',
  398. 'multi_line' => "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz",
  399. 'multi_line_with_carriage_return' => "foo\nbar\r\nbaz",
  400. 'nested_inlined_multi_line_string' => [
  401. 'inlined_multi_line' => "foo\nbar\r\nempty line:\n\nbaz",
  402. ],
  403. ],
  404. ];
  405. $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));
  406. }
  407. public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace()
  408. {
  409. $data = [
  410. 'data' => [
  411. 'multi_line' => " the first line has leading spaces\nThe second line does not.",
  412. ],
  413. ];
  414. $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));
  415. }
  416. public function testCarriageReturnIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
  417. {
  418. $this->assertSame("- \"a\\r\\nb\\nc\"\n", $this->dumper->dump(["a\r\nb\nc"], 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
  419. }
  420. /**
  421. * @expectedException \InvalidArgumentException
  422. * @expectedExceptionMessage The indentation must be greater than zero
  423. */
  424. public function testZeroIndentationThrowsException()
  425. {
  426. new Dumper(0);
  427. }
  428. /**
  429. * @expectedException \InvalidArgumentException
  430. * @expectedExceptionMessage The indentation must be greater than zero
  431. */
  432. public function testNegativeIndentationThrowsException()
  433. {
  434. new Dumper(-4);
  435. }
  436. }
  437. class A
  438. {
  439. public $a = 'foo';
  440. }