DumperTest.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\Tests\Component\Yaml;
  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 function setUp()
  20. {
  21. $this->parser = new Parser();
  22. $this->dumper = new Dumper();
  23. $this->path = __DIR__.'/Fixtures';
  24. }
  25. protected function tearDown()
  26. {
  27. $this->parser = null;
  28. $this->dumper = null;
  29. $this->path = null;
  30. }
  31. public function testSpecifications()
  32. {
  33. $files = $this->parser->parse(file_get_contents($this->path.'/index.yml'));
  34. foreach ($files as $file) {
  35. $yamls = file_get_contents($this->path.'/'.$file.'.yml');
  36. // split YAMLs documents
  37. foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
  38. if (!$yaml) {
  39. continue;
  40. }
  41. $test = $this->parser->parse($yaml);
  42. if (isset($test['dump_skip']) && $test['dump_skip']) {
  43. continue;
  44. } elseif (isset($test['todo']) && $test['todo']) {
  45. // TODO
  46. } else {
  47. $expected = eval('return '.trim($test['php']).';');
  48. $this->assertEquals($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']);
  49. }
  50. }
  51. }
  52. }
  53. public function testInlineLevel()
  54. {
  55. // inline level
  56. $array = array(
  57. '' => 'bar',
  58. 'foo' => '#bar',
  59. 'foo\'bar' => array(),
  60. 'bar' => array(1, 'foo'),
  61. 'foobar' => array(
  62. 'foo' => 'bar',
  63. 'bar' => array(1, 'foo'),
  64. 'foobar' => array(
  65. 'foo' => 'bar',
  66. 'bar' => array(1, 'foo'),
  67. ),
  68. ),
  69. );
  70. $expected = <<<EOF
  71. { '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
  72. EOF;
  73. $this->assertEquals($expected, $this->dumper->dump($array, -10), '->dump() takes an inline level argument');
  74. $this->assertEquals($expected, $this->dumper->dump($array, 0), '->dump() takes an inline level argument');
  75. $expected = <<<EOF
  76. '': bar
  77. foo: '#bar'
  78. 'foo''bar': { }
  79. bar: [1, foo]
  80. foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
  81. EOF;
  82. $this->assertEquals($expected, $this->dumper->dump($array, 1), '->dump() takes an inline level argument');
  83. $expected = <<<EOF
  84. '': bar
  85. foo: '#bar'
  86. 'foo''bar': { }
  87. bar:
  88. - 1
  89. - foo
  90. foobar:
  91. foo: bar
  92. bar: [1, foo]
  93. foobar: { foo: bar, bar: [1, foo] }
  94. EOF;
  95. $this->assertEquals($expected, $this->dumper->dump($array, 2), '->dump() takes an inline level argument');
  96. $expected = <<<EOF
  97. '': bar
  98. foo: '#bar'
  99. 'foo''bar': { }
  100. bar:
  101. - 1
  102. - foo
  103. foobar:
  104. foo: bar
  105. bar:
  106. - 1
  107. - foo
  108. foobar:
  109. foo: bar
  110. bar: [1, foo]
  111. EOF;
  112. $this->assertEquals($expected, $this->dumper->dump($array, 3), '->dump() takes an inline level argument');
  113. $expected = <<<EOF
  114. '': bar
  115. foo: '#bar'
  116. 'foo''bar': { }
  117. bar:
  118. - 1
  119. - foo
  120. foobar:
  121. foo: bar
  122. bar:
  123. - 1
  124. - foo
  125. foobar:
  126. foo: bar
  127. bar:
  128. - 1
  129. - foo
  130. EOF;
  131. $this->assertEquals($expected, $this->dumper->dump($array, 4), '->dump() takes an inline level argument');
  132. $this->assertEquals($expected, $this->dumper->dump($array, 10), '->dump() takes an inline level argument');
  133. }
  134. public function testObjectsSupport()
  135. {
  136. $a = array('foo' => new A(), 'bar' => 1);
  137. $this->assertEquals('{ foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\A":1:{s:1:"a";s:3:"foo";}, bar: 1 }', $this->dumper->dump($a), '->dump() is able to dump objects');
  138. }
  139. }
  140. class A
  141. {
  142. public $a = 'foo';
  143. }