ParserTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Components\Yaml;
  10. use Symfony\Components\Yaml\Yaml;
  11. use Symfony\Components\Yaml\Parser;
  12. use Symfony\Components\Yaml\ParserException;
  13. class ParserTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $parser;
  16. protected $path;
  17. static public function setUpBeforeClass()
  18. {
  19. Yaml::setSpecVersion('1.1');
  20. }
  21. public function setUp()
  22. {
  23. $this->parser = new Parser();
  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 ('.strpbrk($yaml, "\t").').', $e->getMessage(), 'YAML files must not contain tabs');
  72. }
  73. }
  74. }
  75. public function testObjectsSupport()
  76. {
  77. $b = array('foo' => new B(), 'bar' => 1);
  78. $this->assertEquals($this->parser->parse(<<<EOF
  79. foo: !!php/object:O:31:"Symfony\Tests\Components\Yaml\B":1:{s:1:"b";s:3:"foo";}
  80. bar: 1
  81. EOF
  82. ), $b, '->parse() is able to dump objects');
  83. }
  84. }
  85. class B
  86. {
  87. public $b = 'foo';
  88. }