ParserTest.php 3.7 KB

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