ParserTest.php 3.8 KB

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