ParserTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. }
  95. class B
  96. {
  97. public $b = 'foo';
  98. }