ParserTest.php 2.7 KB

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