DumperTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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)), $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. /**
  218. * @dataProvider getEscapeSequences
  219. */
  220. public function testEscapedEscapeSequencesInQuotedScalar($input, $expected)
  221. {
  222. $this->assertEquals($expected, $this->dumper->dump($input));
  223. }
  224. public function getEscapeSequences()
  225. {
  226. return array(
  227. 'empty string' => array('', "''"),
  228. 'null' => array("\x0", '"\\0"'),
  229. 'bell' => array("\x7", '"\\a"'),
  230. 'backspace' => array("\x8", '"\\b"'),
  231. 'horizontal-tab' => array("\t", '"\\t"'),
  232. 'line-feed' => array("\n", '"\\n"'),
  233. 'vertical-tab' => array("\v", '"\\v"'),
  234. 'form-feed' => array("\xC", '"\\f"'),
  235. 'carriage-return' => array("\r", '"\\r"'),
  236. 'escape' => array("\x1B", '"\\e"'),
  237. 'space' => array(' ', "' '"),
  238. 'double-quote' => array('"', "'\"'"),
  239. 'slash' => array('/', '/'),
  240. 'backslash' => array('\\', '\\'),
  241. 'next-line' => array("\xC2\x85", '"\\N"'),
  242. 'non-breaking-space' => array("\xc2\xa0", '"\\_"'),
  243. 'line-separator' => array("\xE2\x80\xA8", '"\\L"'),
  244. 'paragraph-separator' => array("\xE2\x80\xA9", '"\\P"'),
  245. 'colon' => array(':', "':'"),
  246. );
  247. }
  248. public function testBinaryDataIsDumpedBase64Encoded()
  249. {
  250. $binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif');
  251. $expected = '{ data: !!binary '.base64_encode($binaryData).' }';
  252. $this->assertSame($expected, $this->dumper->dump(array('data' => $binaryData)));
  253. }
  254. public function testNonUtf8DataIsDumpedBase64Encoded()
  255. {
  256. // "für" (ISO-8859-1 encoded)
  257. $this->assertSame('!!binary ZsM/cg==', $this->dumper->dump("f\xc3\x3fr"));
  258. }
  259. /**
  260. * @dataProvider objectAsMapProvider
  261. */
  262. public function testDumpObjectAsMap($object, $expected)
  263. {
  264. $yaml = $this->dumper->dump($object, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
  265. $this->assertEquals($expected, Yaml::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
  266. }
  267. public function objectAsMapProvider()
  268. {
  269. $tests = array();
  270. $bar = new \stdClass();
  271. $bar->class = 'classBar';
  272. $bar->args = array('bar');
  273. $zar = new \stdClass();
  274. $foo = new \stdClass();
  275. $foo->bar = $bar;
  276. $foo->zar = $zar;
  277. $object = new \stdClass();
  278. $object->foo = $foo;
  279. $tests['stdClass'] = array($object, $object);
  280. $arrayObject = new \ArrayObject();
  281. $arrayObject['foo'] = 'bar';
  282. $arrayObject['baz'] = 'foobar';
  283. $parsedArrayObject = new \stdClass();
  284. $parsedArrayObject->foo = 'bar';
  285. $parsedArrayObject->baz = 'foobar';
  286. $tests['ArrayObject'] = array($arrayObject, $parsedArrayObject);
  287. $a = new A();
  288. $tests['arbitrary-object'] = array($a, null);
  289. return $tests;
  290. }
  291. public function testDumpMultiLineStringAsScalarBlock()
  292. {
  293. $data = array(
  294. 'data' => array(
  295. 'single_line' => 'foo bar baz',
  296. 'multi_line' => "foo\nline with trailing spaces:\n \nbar\r\ninteger like line:\n123456789\nempty line:\n\nbaz",
  297. 'nested_inlined_multi_line_string' => array(
  298. 'inlined_multi_line' => "foo\nbar\r\nempty line:\n\nbaz",
  299. ),
  300. ),
  301. );
  302. $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));
  303. }
  304. /**
  305. * @expectedException \InvalidArgumentException
  306. * @expectedExceptionMessage The indentation must be greater than zero
  307. */
  308. public function testZeroIndentationThrowsException()
  309. {
  310. new Dumper(0);
  311. }
  312. /**
  313. * @expectedException \InvalidArgumentException
  314. * @expectedExceptionMessage The indentation must be greater than zero
  315. */
  316. public function testNegativeIndentationThrowsException()
  317. {
  318. new Dumper(-4);
  319. }
  320. }
  321. class A
  322. {
  323. public $a = 'foo';
  324. }