ParserTest.php 1.7 KB

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