菜谱项目

DumperTest.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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\Parser;
  13. use Symfony\Component\Yaml\Dumper;
  14. use Symfony\Component\Yaml\Yaml;
  15. class DumperTest extends TestCase
  16. {
  17. protected $parser;
  18. protected $dumper;
  19. protected $path;
  20. protected $array = array(
  21. '' => 'bar',
  22. 'foo' => '#bar',
  23. 'foo\'bar' => array(),
  24. 'bar' => array(1, 'foo'),
  25. 'foobar' => array(
  26. 'foo' => 'bar',
  27. 'bar' => array(1, 'foo'),
  28. 'foobar' => array(
  29. 'foo' => 'bar',
  30. 'bar' => array(1, 'foo'),
  31. ),
  32. ),
  33. );
  34. protected function setUp()
  35. {
  36. $this->parser = new Parser();
  37. $this->dumper = new Dumper();
  38. $this->path = __DIR__.'/Fixtures';
  39. }
  40. protected function tearDown()
  41. {
  42. $this->parser = null;
  43. $this->dumper = null;
  44. $this->path = null;
  45. $this->array = null;
  46. }
  47. public function testIndentationInConstructor()
  48. {
  49. $dumper = new Dumper(7);
  50. $expected = <<<'EOF'
  51. '': bar
  52. foo: '#bar'
  53. 'foo''bar': { }
  54. bar:
  55. - 1
  56. - foo
  57. foobar:
  58. foo: bar
  59. bar:
  60. - 1
  61. - foo
  62. foobar:
  63. foo: bar
  64. bar:
  65. - 1
  66. - foo
  67. EOF;
  68. $this->assertEquals($expected, $dumper->dump($this->array, 4, 0));
  69. }
  70. /**
  71. * @group legacy
  72. */
  73. public function testSetIndentation()
  74. {
  75. $this->dumper->setIndentation(7);
  76. $expected = <<<'EOF'
  77. '': bar
  78. foo: '#bar'
  79. 'foo''bar': { }
  80. bar:
  81. - 1
  82. - foo
  83. foobar:
  84. foo: bar
  85. bar:
  86. - 1
  87. - foo
  88. foobar:
  89. foo: bar
  90. bar:
  91. - 1
  92. - foo
  93. EOF;
  94. $this->assertEquals($expected, $this->dumper->dump($this->array, 4, 0));
  95. }
  96. public function testSpecifications()
  97. {
  98. $files = $this->parser->parse(file_get_contents($this->path.'/index.yml'));
  99. foreach ($files as $file) {
  100. $yamls = file_get_contents($this->path.'/'.$file.'.yml');
  101. // split YAMLs documents
  102. foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
  103. if (!$yaml) {
  104. continue;
  105. }
  106. $test = $this->parser->parse($yaml);
  107. if (isset($test['dump_skip']) && $test['dump_skip']) {
  108. continue;
  109. } elseif (isset($test['todo']) && $test['todo']) {
  110. // TODO
  111. } else {
  112. eval('$expected = '.trim($test['php']).';');
  113. $this->assertSame($expected, $this->parser->parse($this->dumper->dump($expected, 10), Yaml::PARSE_KEYS_AS_STRINGS), $test['test']);
  114. }
  115. }
  116. }
  117. }
  118. public function testInlineLevel()
  119. {
  120. $expected = <<<'EOF'
  121. { '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
  122. EOF;
  123. $this->assertEquals($expected, $this->dumper->dump($this->array, -10), '->dump() takes an inline level argument');
  124. $this->assertEquals($expected, $this->dumper->dump($this->array, 0), '->dump() takes an inline level argument');
  125. $expected = <<<'EOF'
  126. '': bar
  127. foo: '#bar'
  128. 'foo''bar': { }
  129. bar: [1, foo]
  130. foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
  131. EOF;
  132. $this->assertEquals($expected, $this->dumper->dump($this->array, 1), '->dump() takes an inline level argument');
  133. $expected = <<<'EOF'
  134. '': bar
  135. foo: '#bar'
  136. 'foo''bar': { }
  137. bar:
  138. - 1
  139. - foo
  140. foobar:
  141. foo: bar
  142. bar: [1, foo]
  143. foobar: { foo: bar, bar: [1, foo] }
  144. EOF;
  145. $this->assertEquals($expected, $this->dumper->dump($this->array, 2), '->dump() takes an inline level argument');
  146. $expected = <<<'EOF'
  147. '': bar
  148. foo: '#bar'
  149. 'foo''bar': { }
  150. bar:
  151. - 1
  152. - foo
  153. foobar:
  154. foo: bar
  155. bar:
  156. - 1
  157. - foo
  158. foobar:
  159. foo: bar
  160. bar: [1, foo]
  161. EOF;
  162. $this->assertEquals($expected, $this->dumper->dump($this->array, 3), '->dump() takes an inline level argument');
  163. $expected = <<<'EOF'
  164. '': bar
  165. foo: '#bar'
  166. 'foo''bar': { }
  167. bar:
  168. - 1
  169. - foo
  170. foobar:
  171. foo: bar
  172. bar:
  173. - 1
  174. - foo
  175. foobar:
  176. foo: bar
  177. bar:
  178. - 1
  179. - foo
  180. EOF;
  181. $this->assertEquals($expected, $this->dumper->dump($this->array, 4), '->dump() takes an inline level argument');
  182. $this->assertEquals($expected, $this->dumper->dump($this->array, 10), '->dump() takes an inline level argument');
  183. }
  184. public function testObjectSupportEnabled()
  185. {
  186. $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_OBJECT);
  187. $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');
  188. }
  189. /**
  190. * @group legacy
  191. */
  192. public function testObjectSupportEnabledPassingTrue()
  193. {
  194. $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
  195. $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');
  196. }
  197. public function testObjectSupportDisabledButNoExceptions()
  198. {
  199. $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1));
  200. $this->assertEquals('{ foo: null, bar: 1 }', $dump, '->dump() does not dump objects when disabled');
  201. }
  202. /**
  203. * @expectedException \Symfony\Component\Yaml\Exception\DumpException
  204. */
  205. public function testObjectSupportDisabledWithExceptions()
  206. {
  207. $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
  208. }
  209. /**
  210. * @group legacy
  211. * @expectedException \Symfony\Component\Yaml\Exception\DumpException
  212. */
  213. public function testObjectSupportDisabledWithExceptionsPassingTrue()
  214. {
  215. $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
  216. }
  217. public function testEmptyArray()
  218. {
  219. $dump = $this->dumper->dump(array());
  220. $this->assertEquals('{ }', $dump);
  221. $dump = $this->dumper->dump(array(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
  222. $this->assertEquals('[]', $dump);
  223. $dump = $this->dumper->dump(array(), 9, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
  224. $this->assertEquals('[]', $dump);
  225. $dump = $this->dumper->dump(new \ArrayObject(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP);
  226. $this->assertEquals('{ }', $dump);
  227. $dump = $this->dumper->dump(new \stdClass(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP);
  228. $this->assertEquals('{ }', $dump);
  229. }
  230. /**
  231. * @dataProvider getEscapeSequences
  232. */
  233. public function testEscapedEscapeSequencesInQuotedScalar($input, $expected)
  234. {
  235. $this->assertEquals($expected, $this->dumper->dump($input));
  236. }
  237. public function getEscapeSequences()
  238. {
  239. return array(
  240. 'empty string' => array('', "''"),
  241. 'null' => array("\x0", '"\\0"'),
  242. 'bell' => array("\x7", '"\\a"'),
  243. 'backspace' => array("\x8", '"\\b"'),
  244. 'horizontal-tab' => array("\t", '"\\t"'),
  245. 'line-feed' => array("\n", '"\\n"'),
  246. 'vertical-tab' => array("\v", '"\\v"'),
  247. 'form-feed' => array("\xC", '"\\f"'),
  248. 'carriage-return' => array("\r", '"\\r"'),
  249. 'escape' => array("\x1B", '"\\e"'),
  250. 'space' => array(' ', "' '"),
  251. 'double-quote' => array('"', "'\"'"),
  252. 'slash' => array('/', '/'),
  253. 'backslash' => array('\\', '\\'),
  254. 'next-line' => array("\xC2\x85", '"\\N"'),
  255. 'non-breaking-space' => array("\xc2\xa0", '"\\_"'),
  256. 'line-separator' => array("\xE2\x80\xA8", '"\\L"'),
  257. 'paragraph-separator' => array("\xE2\x80\xA9", '"\\P"'),
  258. 'colon' => array(':', "':'"),
  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(array('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 = array();
  283. $bar = new \stdClass();
  284. $bar->class = 'classBar';
  285. $bar->args = array('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'] = array($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'] = array($arrayObject, $parsedArrayObject);
  300. $a = new A();
  301. $tests['arbitrary-object'] = array($a, null);
  302. return $tests;
  303. }
  304. public function testDumpingArrayObjectInstancesRespectsInlineLevel()
  305. {
  306. $deep = new \ArrayObject(array('deep1' => 'd', 'deep2' => 'e'));
  307. $inner = new \ArrayObject(array('inner1' => 'b', 'inner2' => 'c', 'inner3' => $deep));
  308. $outer = new \ArrayObject(array('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(array('d', 'e'));
  322. $inner = new \ArrayObject(array('b', 'c', $deep));
  323. $outer = new \ArrayObject(array('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(array('d', 'e'));
  333. $inner = new \ArrayObject(array('b', 'c', $deep));
  334. $outer = new \ArrayObject(array('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 testDumpMultiLineStringAsScalarBlock()
  376. {
  377. $data = array(
  378. 'data' => array(
  379. 'single_line' => 'foo bar baz',
  380. 'multi_line' => "foo\nline with trailing spaces:\n \nbar\r\ninteger like line:\n123456789\nempty line:\n\nbaz",
  381. 'nested_inlined_multi_line_string' => array(
  382. 'inlined_multi_line' => "foo\nbar\r\nempty line:\n\nbaz",
  383. ),
  384. ),
  385. );
  386. $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));
  387. }
  388. /**
  389. * @expectedException \InvalidArgumentException
  390. * @expectedExceptionMessage The indentation must be greater than zero
  391. */
  392. public function testZeroIndentationThrowsException()
  393. {
  394. new Dumper(0);
  395. }
  396. /**
  397. * @expectedException \InvalidArgumentException
  398. * @expectedExceptionMessage The indentation must be greater than zero
  399. */
  400. public function testNegativeIndentationThrowsException()
  401. {
  402. new Dumper(-4);
  403. }
  404. }
  405. class A
  406. {
  407. public $a = 'foo';
  408. }