ParserTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. require_once __DIR__.'/../../../bootstrap.php';
  10. use Symfony\Components\Yaml\Yaml;
  11. use Symfony\Components\Yaml\Parser;
  12. use Symfony\Components\Yaml\ParserException;
  13. Yaml::setSpecVersion('1.1');
  14. $t = new LimeTest(151);
  15. $parser = new Parser();
  16. $path = __DIR__.'/../../../../fixtures/Symfony/Components/Yaml';
  17. $files = $parser->parse(file_get_contents($path.'/index.yml'));
  18. foreach ($files as $file)
  19. {
  20. $t->diag($file);
  21. $yamls = file_get_contents($path.'/'.$file.'.yml');
  22. // split YAMLs documents
  23. foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml)
  24. {
  25. if (!$yaml)
  26. {
  27. continue;
  28. }
  29. $test = $parser->parse($yaml);
  30. if (isset($test['todo']) && $test['todo'])
  31. {
  32. $t->todo($test['test']);
  33. }
  34. else
  35. {
  36. $expected = var_export(eval('return '.trim($test['php']).';'), true);
  37. $t->is(var_export($parser->parse($test['yaml']), true), $expected, $test['test']);
  38. }
  39. }
  40. }
  41. // test tabs in YAML
  42. $yamls = array(
  43. "foo:\n bar",
  44. "foo:\n bar",
  45. "foo:\n bar",
  46. "foo:\n bar",
  47. );
  48. foreach ($yamls as $yaml)
  49. {
  50. try
  51. {
  52. $content = $parser->parse($yaml);
  53. $t->fail('YAML files must not contain tabs');
  54. }
  55. catch (ParserException $e)
  56. {
  57. $t->pass('YAML files must not contain tabs');
  58. }
  59. }
  60. // objects
  61. $t->diag('Objects support');
  62. class A
  63. {
  64. public $a = 'foo';
  65. }
  66. $a = array('foo' => new A(), 'bar' => 1);
  67. $t->is($parser->parse(<<<EOF
  68. foo: !!php/object:O:1:"A":1:{s:1:"a";s:3:"foo";}
  69. bar: 1
  70. EOF
  71. ), $a, '->parse() is able to dump objects');