DumperTest.php 3.9 KB

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