Няма описание

DumperTest.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 Symfony\Component\Yaml\Parser;
  12. use Symfony\Component\Yaml\Dumper;
  13. use Symfony\Component\Yaml\Yaml;
  14. class DumperTest extends \PHPUnit_Framework_TestCase
  15. {
  16. protected $parser;
  17. protected $dumper;
  18. protected $path;
  19. protected $array = array(
  20. '' => 'bar',
  21. 'foo' => '#bar',
  22. 'foo\'bar' => array(),
  23. 'bar' => array(1, 'foo'),
  24. 'foobar' => array(
  25. 'foo' => 'bar',
  26. 'bar' => array(1, 'foo'),
  27. 'foobar' => array(
  28. 'foo' => 'bar',
  29. 'bar' => array(1, 'foo'),
  30. ),
  31. ),
  32. );
  33. protected function setUp()
  34. {
  35. $this->parser = new Parser();
  36. $this->dumper = new Dumper();
  37. $this->path = __DIR__.'/Fixtures';
  38. }
  39. protected function tearDown()
  40. {
  41. $this->parser = null;
  42. $this->dumper = null;
  43. $this->path = null;
  44. $this->array = null;
  45. }
  46. public function testIndentationInConstructor()
  47. {
  48. $dumper = new Dumper(7);
  49. $expected = <<<'EOF'
  50. '': bar
  51. foo: '#bar'
  52. 'foo''bar': { }
  53. bar:
  54. - 1
  55. - foo
  56. foobar:
  57. foo: bar
  58. bar:
  59. - 1
  60. - foo
  61. foobar:
  62. foo: bar
  63. bar:
  64. - 1
  65. - foo
  66. EOF;
  67. $this->assertEquals($expected, $dumper->dump($this->array, 4, 0));
  68. }
  69. /**
  70. * @group legacy
  71. */
  72. public function testSetIndentation()
  73. {
  74. $this->dumper->setIndentation(7);
  75. $expected = <<<'EOF'
  76. '': bar
  77. foo: '#bar'
  78. 'foo''bar': { }
  79. bar:
  80. - 1
  81. - foo
  82. foobar:
  83. foo: bar
  84. bar:
  85. - 1
  86. - foo
  87. foobar:
  88. foo: bar
  89. bar:
  90. - 1
  91. - foo
  92. EOF;
  93. $this->assertEquals($expected, $this->dumper->dump($this->array, 4, 0));
  94. }
  95. public function testSpecifications()
  96. {
  97. $files = $this->parser->parse(file_get_contents($this->path.'/index.yml'));
  98. foreach ($files as $file) {
  99. $yamls = file_get_contents($this->path.'/'.$file.'.yml');
  100. // split YAMLs documents
  101. foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
  102. if (!$yaml) {
  103. continue;
  104. }
  105. $test = $this->parser->parse($yaml);
  106. if (isset($test['dump_skip']) && $test['dump_skip']) {
  107. continue;
  108. } elseif (isset($test['todo']) && $test['todo']) {
  109. // TODO
  110. } else {
  111. eval('$expected = '.trim($test['php']).';');
  112. $this->assertSame($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']);
  113. }
  114. }
  115. }
  116. }
  117. public function testInlineLevel()
  118. {
  119. $expected = <<<'EOF'
  120. { '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
  121. EOF;
  122. $this->assertEquals($expected, $this->dumper->dump($this->array, -10), '->dump() takes an inline level argument');
  123. $this->assertEquals($expected, $this->dumper->dump($this->array, 0), '->dump() takes an inline level argument');
  124. $expected = <<<'EOF'
  125. '': bar
  126. foo: '#bar'
  127. 'foo''bar': { }
  128. bar: [1, foo]
  129. foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
  130. EOF;
  131. $this->assertEquals($expected, $this->dumper->dump($this->array, 1), '->dump() takes an inline level argument');
  132. $expected = <<<'EOF'
  133. '': bar
  134. foo: '#bar'
  135. 'foo''bar': { }
  136. bar:
  137. - 1
  138. - foo
  139. foobar:
  140. foo: bar
  141. bar: [1, foo]
  142. foobar: { foo: bar, bar: [1, foo] }
  143. EOF;
  144. $this->assertEquals($expected, $this->dumper->dump($this->array, 2), '->dump() takes an inline level argument');
  145. $expected = <<<'EOF'
  146. '': bar
  147. foo: '#bar'
  148. 'foo''bar': { }
  149. bar:
  150. - 1
  151. - foo
  152. foobar:
  153. foo: bar
  154. bar:
  155. - 1
  156. - foo
  157. foobar:
  158. foo: bar
  159. bar: [1, foo]
  160. EOF;
  161. $this->assertEquals($expected, $this->dumper->dump($this->array, 3), '->dump() takes an inline level argument');
  162. $expected = <<<'EOF'
  163. '': bar
  164. foo: '#bar'
  165. 'foo''bar': { }
  166. bar:
  167. - 1
  168. - foo
  169. foobar:
  170. foo: bar
  171. bar:
  172. - 1
  173. - foo
  174. foobar:
  175. foo: bar
  176. bar:
  177. - 1
  178. - foo
  179. EOF;
  180. $this->assertEquals($expected, $this->dumper->dump($this->array, 4), '->dump() takes an inline level argument');
  181. $this->assertEquals($expected, $this->dumper->dump($this->array, 10), '->dump() takes an inline level argument');
  182. }
  183. public function testObjectSupportEnabled()
  184. {
  185. $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_OBJECT);
  186. $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');
  187. }
  188. /**
  189. * @group legacy
  190. */
  191. public function testObjectSupportEnabledPassingTrue()
  192. {
  193. $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
  194. $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');
  195. }
  196. public function testObjectSupportDisabledButNoExceptions()
  197. {
  198. $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1));
  199. $this->assertEquals('{ foo: null, bar: 1 }', $dump, '->dump() does not dump objects when disabled');
  200. }
  201. /**
  202. * @expectedException \Symfony\Component\Yaml\Exception\DumpException
  203. */
  204. public function testObjectSupportDisabledWithExceptions()
  205. {
  206. $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
  207. }
  208. /**
  209. * @group legacy
  210. * @expectedException \Symfony\Component\Yaml\Exception\DumpException
  211. */
  212. public function testObjectSupportDisabledWithExceptionsPassingTrue()
  213. {
  214. $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
  215. }
  216. /**
  217. * @dataProvider getEscapeSequences
  218. */
  219. public function testEscapedEscapeSequencesInQuotedScalar($input, $expected)
  220. {
  221. $this->assertEquals($expected, $this->dumper->dump($input));
  222. }
  223. public function getEscapeSequences()
  224. {
  225. return array(
  226. 'null' => array("\t\\0", '"\t\\\\0"'),
  227. 'bell' => array("\t\\a", '"\t\\\\a"'),
  228. 'backspace' => array("\t\\b", '"\t\\\\b"'),
  229. 'horizontal-tab' => array("\t\\t", '"\t\\\\t"'),
  230. 'line-feed' => array("\t\\n", '"\t\\\\n"'),
  231. 'vertical-tab' => array("\t\\v", '"\t\\\\v"'),
  232. 'form-feed' => array("\t\\f", '"\t\\\\f"'),
  233. 'carriage-return' => array("\t\\r", '"\t\\\\r"'),
  234. 'escape' => array("\t\\e", '"\t\\\\e"'),
  235. 'space' => array("\t\\ ", '"\t\\\\ "'),
  236. 'double-quote' => array("\t\\\"", '"\t\\\\\\""'),
  237. 'slash' => array("\t\\/", '"\t\\\\/"'),
  238. 'backslash' => array("\t\\\\", '"\t\\\\\\\\"'),
  239. 'next-line' => array("\t\\N", '"\t\\\\N"'),
  240. 'non-breaking-space' => array("\t\\�", '"\t\\\\�"'),
  241. 'line-separator' => array("\t\\L", '"\t\\\\L"'),
  242. 'paragraph-separator' => array("\t\\P", '"\t\\\\P"'),
  243. );
  244. }
  245. public function testBinaryDataIsDumpedBase64Encoded()
  246. {
  247. $binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif');
  248. $expected = '{ data: !!binary '.base64_encode($binaryData).' }';
  249. $this->assertSame($expected, $this->dumper->dump(array('data' => $binaryData)));
  250. }
  251. public function testNonUtf8DataIsDumpedBase64Encoded()
  252. {
  253. // "für" (ISO-8859-1 encoded)
  254. $this->assertSame('!!binary ZsM/cg==', $this->dumper->dump("f\xc3\x3fr"));
  255. }
  256. /**
  257. * @dataProvider objectAsMapProvider
  258. */
  259. public function testDumpObjectAsMap($object, $expected)
  260. {
  261. $yaml = $this->dumper->dump($object, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
  262. $this->assertEquals($expected, Yaml::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
  263. }
  264. public function objectAsMapProvider()
  265. {
  266. $tests = array();
  267. $bar = new \stdClass();
  268. $bar->class = 'classBar';
  269. $bar->args = array('bar');
  270. $zar = new \stdClass();
  271. $foo = new \stdClass();
  272. $foo->bar = $bar;
  273. $foo->zar = $zar;
  274. $object = new \stdClass();
  275. $object->foo = $foo;
  276. $tests['stdClass'] = array($object, $object);
  277. $arrayObject = new \ArrayObject();
  278. $arrayObject['foo'] = 'bar';
  279. $arrayObject['baz'] = 'foobar';
  280. $parsedArrayObject = new \stdClass();
  281. $parsedArrayObject->foo = 'bar';
  282. $parsedArrayObject->baz = 'foobar';
  283. $tests['ArrayObject'] = array($arrayObject, $parsedArrayObject);
  284. $a = new A();
  285. $tests['arbitrary-object'] = array($a, null);
  286. return $tests;
  287. }
  288. public function testDumpMultiLineStringAsScalarBlock()
  289. {
  290. $data = array(
  291. 'data' => array(
  292. 'single_line' => 'foo bar baz',
  293. 'multi_line' => "foo\nline with trailing spaces:\n \nbar\r\ninteger like line:\n123456789\nempty line:\n\nbaz",
  294. 'nested_inlined_multi_line_string' => array(
  295. 'inlined_multi_line' => "foo\nbar\r\nempty line:\n\nbaz",
  296. ),
  297. ),
  298. );
  299. $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));
  300. }
  301. /**
  302. * @expectedException \InvalidArgumentException
  303. * @expectedExceptionMessage The indentation must be greater than zero
  304. */
  305. public function testZeroIndentationThrowsException()
  306. {
  307. new Dumper(0);
  308. }
  309. /**
  310. * @expectedException \InvalidArgumentException
  311. * @expectedExceptionMessage The indentation must be greater than zero
  312. */
  313. public function testNegativeIndentationThrowsException()
  314. {
  315. new Dumper(-4);
  316. }
  317. }
  318. class A
  319. {
  320. public $a = 'foo';
  321. }