DumperTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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\Yaml;
  12. use Symfony\Component\Yaml\Parser;
  13. use Symfony\Component\Yaml\Dumper;
  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 testSetIndentation()
  47. {
  48. $this->dumper->setIndentation(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, $this->dumper->dump($this->array, 4, 0));
  68. }
  69. public function testSpecifications()
  70. {
  71. $files = $this->parser->parse(file_get_contents($this->path.'/index.yml'));
  72. foreach ($files as $file) {
  73. $yamls = file_get_contents($this->path.'/'.$file.'.yml');
  74. // split YAMLs documents
  75. foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
  76. if (!$yaml) {
  77. continue;
  78. }
  79. $test = $this->parser->parse($yaml);
  80. if (isset($test['dump_skip']) && $test['dump_skip']) {
  81. continue;
  82. } elseif (isset($test['todo']) && $test['todo']) {
  83. // TODO
  84. } else {
  85. $expected = eval('return '.trim($test['php']).';');
  86. $this->assertEquals($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']);
  87. }
  88. }
  89. }
  90. }
  91. public function testInlineLevel()
  92. {
  93. $expected = <<<EOF
  94. { '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
  95. EOF;
  96. $this->assertEquals($expected, $this->dumper->dump($this->array, -10), '->dump() takes an inline level argument');
  97. $this->assertEquals($expected, $this->dumper->dump($this->array, 0), '->dump() takes an inline level argument');
  98. $expected = <<<EOF
  99. '': bar
  100. foo: '#bar'
  101. 'foo''bar': { }
  102. bar: [1, foo]
  103. foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
  104. EOF;
  105. $this->assertEquals($expected, $this->dumper->dump($this->array, 1), '->dump() takes an inline level argument');
  106. $expected = <<<EOF
  107. '': bar
  108. foo: '#bar'
  109. 'foo''bar': { }
  110. bar:
  111. - 1
  112. - foo
  113. foobar:
  114. foo: bar
  115. bar: [1, foo]
  116. foobar: { foo: bar, bar: [1, foo] }
  117. EOF;
  118. $this->assertEquals($expected, $this->dumper->dump($this->array, 2), '->dump() takes an inline level argument');
  119. $expected = <<<EOF
  120. '': bar
  121. foo: '#bar'
  122. 'foo''bar': { }
  123. bar:
  124. - 1
  125. - foo
  126. foobar:
  127. foo: bar
  128. bar:
  129. - 1
  130. - foo
  131. foobar:
  132. foo: bar
  133. bar: [1, foo]
  134. EOF;
  135. $this->assertEquals($expected, $this->dumper->dump($this->array, 3), '->dump() takes an inline level argument');
  136. $expected = <<<EOF
  137. '': bar
  138. foo: '#bar'
  139. 'foo''bar': { }
  140. bar:
  141. - 1
  142. - foo
  143. foobar:
  144. foo: bar
  145. bar:
  146. - 1
  147. - foo
  148. foobar:
  149. foo: bar
  150. bar:
  151. - 1
  152. - foo
  153. EOF;
  154. $this->assertEquals($expected, $this->dumper->dump($this->array, 4), '->dump() takes an inline level argument');
  155. $this->assertEquals($expected, $this->dumper->dump($this->array, 10), '->dump() takes an inline level argument');
  156. }
  157. public function testObjectSupportEnabled()
  158. {
  159. $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
  160. $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');
  161. }
  162. public function testObjectSupportDisabledButNoExceptions()
  163. {
  164. $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1));
  165. $this->assertEquals('{ foo: null, bar: 1 }', $dump, '->dump() does not dump objects when disabled');
  166. }
  167. /**
  168. * @expectedException \Symfony\Component\Yaml\Exception\DumpException
  169. */
  170. public function testObjectSupportDisabledWithExceptions()
  171. {
  172. $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true, false);
  173. }
  174. }
  175. class A
  176. {
  177. public $a = 'foo';
  178. }