DumperTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Components\Yaml;
  10. use Symfony\Components\Yaml\Yaml;
  11. use Symfony\Components\Yaml\Parser;
  12. use Symfony\Components\Yaml\Dumper;
  13. class DumperTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $parser;
  16. protected $dumper;
  17. protected $path;
  18. static public function setUpBeforeClass()
  19. {
  20. Yaml::setSpecVersion('1.1');
  21. }
  22. public function setUp()
  23. {
  24. $this->parser = new Parser();
  25. $this->dumper = new Dumper();
  26. $this->path = __DIR__.'/Fixtures';
  27. }
  28. public function testSpecifications()
  29. {
  30. $files = $this->parser->parse(file_get_contents($this->path.'/index.yml'));
  31. foreach ($files as $file) {
  32. $yamls = file_get_contents($this->path.'/'.$file.'.yml');
  33. // split YAMLs documents
  34. foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
  35. if (!$yaml) {
  36. continue;
  37. }
  38. $test = $this->parser->parse($yaml);
  39. if (isset($test['dump_skip']) && $test['dump_skip']) {
  40. continue;
  41. } else if (isset($test['todo']) && $test['todo']) {
  42. // TODO
  43. } else {
  44. $expected = eval('return '.trim($test['php']).';');
  45. $this->assertEquals($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']);
  46. }
  47. }
  48. }
  49. }
  50. public function testInlineLevel()
  51. {
  52. // inline level
  53. $array = array(
  54. '' => 'bar',
  55. 'foo' => '#bar',
  56. 'foo\'bar' => array(),
  57. 'bar' => array(1, 'foo'),
  58. 'foobar' => array(
  59. 'foo' => 'bar',
  60. 'bar' => array(1, 'foo'),
  61. 'foobar' => array(
  62. 'foo' => 'bar',
  63. 'bar' => array(1, 'foo'),
  64. ),
  65. ),
  66. );
  67. $expected = <<<EOF
  68. { '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
  69. EOF;
  70. $this->assertEquals($expected, $this->dumper->dump($array, -10), '->dump() takes an inline level argument');
  71. $this->assertEquals($expected, $this->dumper->dump($array, 0), '->dump() takes an inline level argument');
  72. $expected = <<<EOF
  73. '': bar
  74. foo: '#bar'
  75. 'foo''bar': { }
  76. bar: [1, foo]
  77. foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
  78. EOF;
  79. $this->assertEquals($expected, $this->dumper->dump($array, 1), '->dump() takes an inline level argument');
  80. $expected = <<<EOF
  81. '': bar
  82. foo: '#bar'
  83. 'foo''bar': { }
  84. bar:
  85. - 1
  86. - foo
  87. foobar:
  88. foo: bar
  89. bar: [1, foo]
  90. foobar: { foo: bar, bar: [1, foo] }
  91. EOF;
  92. $this->assertEquals($expected, $this->dumper->dump($array, 2), '->dump() takes an inline level argument');
  93. $expected = <<<EOF
  94. '': bar
  95. foo: '#bar'
  96. 'foo''bar': { }
  97. bar:
  98. - 1
  99. - foo
  100. foobar:
  101. foo: bar
  102. bar:
  103. - 1
  104. - foo
  105. foobar:
  106. foo: bar
  107. bar: [1, foo]
  108. EOF;
  109. $this->assertEquals($expected, $this->dumper->dump($array, 3), '->dump() takes an inline level argument');
  110. $expected = <<<EOF
  111. '': bar
  112. foo: '#bar'
  113. 'foo''bar': { }
  114. bar:
  115. - 1
  116. - foo
  117. foobar:
  118. foo: bar
  119. bar:
  120. - 1
  121. - foo
  122. foobar:
  123. foo: bar
  124. bar:
  125. - 1
  126. - foo
  127. EOF;
  128. $this->assertEquals($expected, $this->dumper->dump($array, 4), '->dump() takes an inline level argument');
  129. $this->assertEquals($expected, $this->dumper->dump($array, 10), '->dump() takes an inline level argument');
  130. }
  131. public function testObjectsSupport()
  132. {
  133. $a = array('foo' => new A(), 'bar' => 1);
  134. $this->assertEquals('{ foo: !!php/object:O:31:"Symfony\Tests\Components\Yaml\A":1:{s:1:"a";s:3:"foo";}, bar: 1 }', $this->dumper->dump($a), '->dump() is able to dump objects');
  135. }
  136. }
  137. class A
  138. {
  139. public $a = 'foo';
  140. }