DumperTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. class DumperTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $parser;
  16. protected $dumper;
  17. protected $path;
  18. protected $array = array(
  19. '' => 'bar',
  20. 'foo' => '#bar',
  21. 'foo\'bar' => array(),
  22. 'bar' => array(1, 'foo'),
  23. 'foobar' => array(
  24. 'foo' => 'bar',
  25. 'bar' => array(1, 'foo'),
  26. 'foobar' => array(
  27. 'foo' => 'bar',
  28. 'bar' => array(1, 'foo'),
  29. ),
  30. ),
  31. );
  32. protected function setUp()
  33. {
  34. $this->parser = new Parser();
  35. $this->dumper = new Dumper();
  36. $this->path = __DIR__.'/Fixtures';
  37. }
  38. protected function tearDown()
  39. {
  40. $this->parser = null;
  41. $this->dumper = null;
  42. $this->path = null;
  43. $this->array = null;
  44. }
  45. public function testSetIndentation()
  46. {
  47. $this->dumper->setIndentation(7);
  48. $expected = <<<EOF
  49. '': bar
  50. foo: '#bar'
  51. 'foo''bar': { }
  52. bar:
  53. - 1
  54. - foo
  55. foobar:
  56. foo: bar
  57. bar:
  58. - 1
  59. - foo
  60. foobar:
  61. foo: bar
  62. bar:
  63. - 1
  64. - foo
  65. EOF;
  66. $this->assertEquals($expected, $this->dumper->dump($this->array, 4, 0));
  67. }
  68. public function testSpecifications()
  69. {
  70. $files = $this->parser->parse(file_get_contents($this->path.'/index.yml'));
  71. foreach ($files as $file) {
  72. $yamls = file_get_contents($this->path.'/'.$file.'.yml');
  73. // split YAMLs documents
  74. foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
  75. if (!$yaml) {
  76. continue;
  77. }
  78. $test = $this->parser->parse($yaml);
  79. if (isset($test['dump_skip']) && $test['dump_skip']) {
  80. continue;
  81. } elseif (isset($test['todo']) && $test['todo']) {
  82. // TODO
  83. } else {
  84. eval('$expected = '.trim($test['php']).';');
  85. $this->assertSame($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']);
  86. }
  87. }
  88. }
  89. }
  90. public function testInlineLevel()
  91. {
  92. $expected = <<<EOF
  93. { '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
  94. EOF;
  95. $this->assertEquals($expected, $this->dumper->dump($this->array, -10), '->dump() takes an inline level argument');
  96. $this->assertEquals($expected, $this->dumper->dump($this->array, 0), '->dump() takes an inline level argument');
  97. $expected = <<<EOF
  98. '': bar
  99. foo: '#bar'
  100. 'foo''bar': { }
  101. bar: [1, foo]
  102. foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
  103. EOF;
  104. $this->assertEquals($expected, $this->dumper->dump($this->array, 1), '->dump() takes an inline level argument');
  105. $expected = <<<EOF
  106. '': bar
  107. foo: '#bar'
  108. 'foo''bar': { }
  109. bar:
  110. - 1
  111. - foo
  112. foobar:
  113. foo: bar
  114. bar: [1, foo]
  115. foobar: { foo: bar, bar: [1, foo] }
  116. EOF;
  117. $this->assertEquals($expected, $this->dumper->dump($this->array, 2), '->dump() takes an inline level argument');
  118. $expected = <<<EOF
  119. '': bar
  120. foo: '#bar'
  121. 'foo''bar': { }
  122. bar:
  123. - 1
  124. - foo
  125. foobar:
  126. foo: bar
  127. bar:
  128. - 1
  129. - foo
  130. foobar:
  131. foo: bar
  132. bar: [1, foo]
  133. EOF;
  134. $this->assertEquals($expected, $this->dumper->dump($this->array, 3), '->dump() takes an inline level argument');
  135. $expected = <<<EOF
  136. '': bar
  137. foo: '#bar'
  138. 'foo''bar': { }
  139. bar:
  140. - 1
  141. - foo
  142. foobar:
  143. foo: bar
  144. bar:
  145. - 1
  146. - foo
  147. foobar:
  148. foo: bar
  149. bar:
  150. - 1
  151. - foo
  152. EOF;
  153. $this->assertEquals($expected, $this->dumper->dump($this->array, 4), '->dump() takes an inline level argument');
  154. $this->assertEquals($expected, $this->dumper->dump($this->array, 10), '->dump() takes an inline level argument');
  155. }
  156. public function testObjectSupportEnabled()
  157. {
  158. $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
  159. $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');
  160. }
  161. public function testObjectSupportDisabledButNoExceptions()
  162. {
  163. $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1));
  164. $this->assertEquals('{ foo: null, bar: 1 }', $dump, '->dump() does not dump objects when disabled');
  165. }
  166. /**
  167. * @expectedException \Symfony\Component\Yaml\Exception\DumpException
  168. */
  169. public function testObjectSupportDisabledWithExceptions()
  170. {
  171. $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true, false);
  172. }
  173. /**
  174. * @dataProvider getEscapeSequences
  175. */
  176. public function testEscapedEscapeSequencesInQuotedScalar($input, $expected)
  177. {
  178. $this->assertEquals($expected, $this->dumper->dump($input));
  179. }
  180. public function getEscapeSequences()
  181. {
  182. return array(
  183. 'null' => array("\t\\0", '"\t\\\\0"'),
  184. 'bell' => array("\t\\a", '"\t\\\\a"'),
  185. 'backspace' => array("\t\\b", '"\t\\\\b"'),
  186. 'horizontal-tab' => array("\t\\t", '"\t\\\\t"'),
  187. 'line-feed' => array("\t\\n", '"\t\\\\n"'),
  188. 'vertical-tab' => array("\t\\v", '"\t\\\\v"'),
  189. 'form-feed' => array("\t\\f", '"\t\\\\f"'),
  190. 'carriage-return' => array("\t\\r", '"\t\\\\r"'),
  191. 'escape' => array("\t\\e", '"\t\\\\e"'),
  192. 'space' => array("\t\\ ", '"\t\\\\ "'),
  193. 'double-quote' => array("\t\\\"", '"\t\\\\\\""'),
  194. 'slash' => array("\t\\/", '"\t\\\\/"'),
  195. 'backslash' => array("\t\\\\", '"\t\\\\\\\\"'),
  196. 'next-line' => array("\t\\N", '"\t\\\\N"'),
  197. 'non-breaking-space' => array("\t\\�", '"\t\\\\�"'),
  198. 'line-separator' => array("\t\\L", '"\t\\\\L"'),
  199. 'paragraph-separator' => array("\t\\P", '"\t\\\\P"'),
  200. );
  201. }
  202. }
  203. class A
  204. {
  205. public $a = 'foo';
  206. }