DumperTest.php 4.1 KB

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