ParserTest.php 4.1KB

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