ParserTest.php 2.2 KB

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