DumperTest.php 3.9 KB

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