ParserTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\OutputEscaper;
  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. $this->path = __DIR__.'/../../../../fixtures/Symfony/Components/Yaml';
  25. }
  26. public function testSpecifications()
  27. {
  28. $files = $this->parser->parse(file_get_contents($this->path.'/index.yml'));
  29. foreach ($files as $file)
  30. {
  31. $yamls = file_get_contents($this->path.'/'.$file.'.yml');
  32. // split YAMLs documents
  33. foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml)
  34. {
  35. if (!$yaml)
  36. {
  37. continue;
  38. }
  39. $test = $this->parser->parse($yaml);
  40. if (isset($test['todo']) && $test['todo'])
  41. {
  42. // TODO
  43. }
  44. else
  45. {
  46. $expected = var_export(eval('return '.trim($test['php']).';'), true);
  47. $this->assertEquals($expected, var_export($this->parser->parse($test['yaml']), true), $test['test']);
  48. }
  49. }
  50. }
  51. }
  52. public function testTabsInYaml()
  53. {
  54. // test tabs in YAML
  55. $yamls = array(
  56. "foo:\n bar",
  57. "foo:\n bar",
  58. "foo:\n bar",
  59. "foo:\n bar",
  60. );
  61. foreach ($yamls as $yaml)
  62. {
  63. try
  64. {
  65. $content = $this->parser->parse($yaml);
  66. $this->fail('YAML files must not contain tabs');
  67. }
  68. catch (ParserException $e)
  69. {
  70. }
  71. }
  72. }
  73. public function testObjectsSupport()
  74. {
  75. $b = array('foo' => new B(), 'bar' => 1);
  76. $this->assertEquals($this->parser->parse(<<<EOF
  77. foo: !!php/object:O:40:"Symfony\Tests\Components\OutputEscaper\B":1:{s:1:"b";s:3:"foo";}
  78. bar: 1
  79. EOF
  80. ), $b, '->parse() is able to dump objects');
  81. }
  82. }
  83. class B
  84. {
  85. public $b = 'foo';
  86. }