DumperTest.php 4.0 KB

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