ParserTest.php 3.8KB

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