ParserTest.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. class ParserTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $parser;
  16. protected function setUp()
  17. {
  18. $this->parser = new Parser();
  19. }
  20. protected function tearDown()
  21. {
  22. $this->parser = null;
  23. }
  24. /**
  25. * @dataProvider getDataFormSpecifications
  26. */
  27. public function testSpecifications($file, $expected, $yaml, $comment)
  28. {
  29. if ('escapedCharacters' == $file) {
  30. if (!function_exists('iconv') && !function_exists('mb_convert_encoding')) {
  31. $this->markTestSkipped('The iconv and mbstring extensions are not available.');
  32. }
  33. }
  34. $this->assertEquals($expected, var_export($this->parser->parse($yaml), true), $comment);
  35. }
  36. public function getDataFormSpecifications()
  37. {
  38. $parser = new Parser();
  39. $path = __DIR__.'/Fixtures';
  40. $tests = array();
  41. $files = $parser->parse(file_get_contents($path.'/index.yml'));
  42. foreach ($files as $file) {
  43. $yamls = file_get_contents($path.'/'.$file.'.yml');
  44. // split YAMLs documents
  45. foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
  46. if (!$yaml) {
  47. continue;
  48. }
  49. $test = $parser->parse($yaml);
  50. if (isset($test['todo']) && $test['todo']) {
  51. // TODO
  52. } else {
  53. $expected = var_export(eval('return '.trim($test['php']).';'), true);
  54. $tests[] = array($file, $expected, $test['yaml'], $test['test']);
  55. }
  56. }
  57. }
  58. return $tests;
  59. }
  60. public function testTabsInYaml()
  61. {
  62. // test tabs in YAML
  63. $yamls = array(
  64. "foo:\n bar",
  65. "foo:\n bar",
  66. "foo:\n bar",
  67. "foo:\n bar",
  68. );
  69. foreach ($yamls as $yaml) {
  70. try {
  71. $content = $this->parser->parse($yaml);
  72. $this->fail('YAML files must not contain tabs');
  73. } catch (\Exception $e) {
  74. $this->assertInstanceOf('\Exception', $e, 'YAML files must not contain tabs');
  75. $this->assertEquals('A YAML file cannot contain tabs as indentation at line 2 (near "'.strpbrk($yaml, "\t").'").', $e->getMessage(), 'YAML files must not contain tabs');
  76. }
  77. }
  78. }
  79. public function testEndOfTheDocumentMarker()
  80. {
  81. $yaml = <<<EOF
  82. --- %YAML:1.0
  83. foo
  84. ...
  85. EOF;
  86. $this->assertEquals('foo', $this->parser->parse($yaml));
  87. }
  88. public function testObjectSupportEnabled()
  89. {
  90. $input = <<<EOF
  91. foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
  92. bar: 1
  93. EOF;
  94. $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
  95. }
  96. public function testObjectSupportDisabledButNoExceptions()
  97. {
  98. $input = <<<EOF
  99. foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
  100. bar: 1
  101. EOF;
  102. $this->assertEquals(array('foo' => null, 'bar' => 1), $this->parser->parse($input), '->parse() does not parse objects');
  103. }
  104. /**
  105. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  106. */
  107. public function testObjectsSupportDisabledWithExceptions()
  108. {
  109. $this->parser->parse('foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}', true, false);
  110. }
  111. public function testNonUtf8Exception()
  112. {
  113. if (!function_exists('mb_detect_encoding') || !function_exists('iconv')) {
  114. $this->markTestSkipped('Exceptions for non-utf8 charsets require the mb_detect_encoding() and iconv() functions.');
  115. return;
  116. }
  117. $yamls = array(
  118. iconv("UTF-8", "ISO-8859-1", "foo: 'äöüß'"),
  119. iconv("UTF-8", "ISO-8859-15", "euro: '€'"),
  120. iconv("UTF-8", "CP1252", "cp1252: '©ÉÇáñ'")
  121. );
  122. foreach ($yamls as $yaml) {
  123. try {
  124. $this->parser->parse($yaml);
  125. $this->fail('charsets other than UTF-8 are rejected.');
  126. } catch (\Exception $e) {
  127. $this->assertInstanceOf('Symfony\Component\Yaml\Exception\ParseException', $e, 'charsets other than UTF-8 are rejected.');
  128. }
  129. }
  130. }
  131. }
  132. class B
  133. {
  134. public $b = 'foo';
  135. }