ParserTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Yaml\Tests;
  11. use Symfony\Component\Yaml\Yaml;
  12. use Symfony\Component\Yaml\Parser;
  13. class ParserTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $parser;
  16. protected function setUp()
  17. {
  18. $this->parser = new Parser();
  19. }
  20. protected function tearDown()
  21. {
  22. $this->parser = null;
  23. }
  24. /**
  25. * @dataProvider getDataFormSpecifications
  26. */
  27. public function testSpecifications($file, $expected, $yaml, $comment)
  28. {
  29. if ('escapedCharacters' == $file) {
  30. if (!function_exists('iconv') && !function_exists('mb_convert_encoding')) {
  31. $this->markTestSkipped('The iconv and mbstring extensions are not available.');
  32. }
  33. }
  34. $this->assertEquals($expected, var_export($this->parser->parse($yaml), true), $comment);
  35. }
  36. public function getDataFormSpecifications()
  37. {
  38. $parser = new Parser();
  39. $path = __DIR__.'/Fixtures';
  40. $tests = array();
  41. $files = $parser->parse(file_get_contents($path.'/index.yml'));
  42. foreach ($files as $file) {
  43. $yamls = file_get_contents($path.'/'.$file.'.yml');
  44. // split YAMLs documents
  45. foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
  46. if (!$yaml) {
  47. continue;
  48. }
  49. $test = $parser->parse($yaml);
  50. if (isset($test['todo']) && $test['todo']) {
  51. // TODO
  52. } else {
  53. $expected = var_export(eval('return '.trim($test['php']).';'), true);
  54. $tests[] = array($file, $expected, $test['yaml'], $test['test']);
  55. }
  56. }
  57. }
  58. return $tests;
  59. }
  60. public function testTabsInYaml()
  61. {
  62. // test tabs in YAML
  63. $yamls = array(
  64. "foo:\n bar",
  65. "foo:\n bar",
  66. "foo:\n bar",
  67. "foo:\n bar",
  68. );
  69. foreach ($yamls as $yaml) {
  70. try {
  71. $content = $this->parser->parse($yaml);
  72. $this->fail('YAML files must not contain tabs');
  73. } catch (\Exception $e) {
  74. $this->assertInstanceOf('\Exception', $e, 'YAML files must not contain tabs');
  75. $this->assertEquals('A YAML file cannot contain tabs as indentation at line 2 (near "'.strpbrk($yaml, "\t").'").', $e->getMessage(), 'YAML files must not contain tabs');
  76. }
  77. }
  78. }
  79. public function testEndOfTheDocumentMarker()
  80. {
  81. $yaml = <<<EOF
  82. --- %YAML:1.0
  83. foo
  84. ...
  85. EOF;
  86. $this->assertEquals('foo', $this->parser->parse($yaml));
  87. }
  88. public function getBlockChompingTests()
  89. {
  90. $tests = array();
  91. $yaml = <<<'EOF'
  92. foo: |-
  93. one
  94. two
  95. bar: |-
  96. one
  97. two
  98. EOF;
  99. $expected = array(
  100. 'foo' => "one\ntwo",
  101. 'bar' => "one\ntwo",
  102. );
  103. $tests['Literal block chomping strip with single trailing newline'] = array($expected, $yaml);
  104. $yaml = <<<'EOF'
  105. foo: |-
  106. one
  107. two
  108. bar: |-
  109. one
  110. two
  111. EOF;
  112. $expected = array(
  113. 'foo' => "one\ntwo",
  114. 'bar' => "one\ntwo",
  115. );
  116. $tests['Literal block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
  117. $yaml = <<<'EOF'
  118. foo: |-
  119. one
  120. two
  121. bar: |-
  122. one
  123. two
  124. EOF;
  125. $expected = array(
  126. 'foo' => "one\ntwo",
  127. 'bar' => "one\ntwo",
  128. );
  129. $tests['Literal block chomping strip without trailing newline'] = array($expected, $yaml);
  130. $yaml = <<<'EOF'
  131. foo: |
  132. one
  133. two
  134. bar: |
  135. one
  136. two
  137. EOF;
  138. $expected = array(
  139. 'foo' => "one\ntwo\n",
  140. 'bar' => "one\ntwo\n",
  141. );
  142. $tests['Literal block chomping clip with single trailing newline'] = array($expected, $yaml);
  143. $yaml = <<<'EOF'
  144. foo: |
  145. one
  146. two
  147. bar: |
  148. one
  149. two
  150. EOF;
  151. $expected = array(
  152. 'foo' => "one\ntwo\n",
  153. 'bar' => "one\ntwo\n",
  154. );
  155. $tests['Literal block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
  156. $yaml = <<<'EOF'
  157. foo: |
  158. one
  159. two
  160. bar: |
  161. one
  162. two
  163. EOF;
  164. $expected = array(
  165. 'foo' => "one\ntwo\n",
  166. 'bar' => "one\ntwo",
  167. );
  168. $tests['Literal block chomping clip without trailing newline'] = array($expected, $yaml);
  169. $yaml = <<<'EOF'
  170. foo: |+
  171. one
  172. two
  173. bar: |+
  174. one
  175. two
  176. EOF;
  177. $expected = array(
  178. 'foo' => "one\ntwo\n",
  179. 'bar' => "one\ntwo\n",
  180. );
  181. $tests['Literal block chomping keep with single trailing newline'] = array($expected, $yaml);
  182. $yaml = <<<'EOF'
  183. foo: |+
  184. one
  185. two
  186. bar: |+
  187. one
  188. two
  189. EOF;
  190. $expected = array(
  191. 'foo' => "one\ntwo\n\n",
  192. 'bar' => "one\ntwo\n\n",
  193. );
  194. $tests['Literal block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
  195. $yaml = <<<'EOF'
  196. foo: |+
  197. one
  198. two
  199. bar: |+
  200. one
  201. two
  202. EOF;
  203. $expected = array(
  204. 'foo' => "one\ntwo\n",
  205. 'bar' => "one\ntwo",
  206. );
  207. $tests['Literal block chomping keep without trailing newline'] = array($expected, $yaml);
  208. $yaml = <<<'EOF'
  209. foo: >-
  210. one
  211. two
  212. bar: >-
  213. one
  214. two
  215. EOF;
  216. $expected = array(
  217. 'foo' => "one two",
  218. 'bar' => "one two",
  219. );
  220. $tests['Folded block chomping strip with single trailing newline'] = array($expected, $yaml);
  221. $yaml = <<<'EOF'
  222. foo: >-
  223. one
  224. two
  225. bar: >-
  226. one
  227. two
  228. EOF;
  229. $expected = array(
  230. 'foo' => "one two",
  231. 'bar' => "one two",
  232. );
  233. $tests['Folded block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
  234. $yaml = <<<'EOF'
  235. foo: >-
  236. one
  237. two
  238. bar: >-
  239. one
  240. two
  241. EOF;
  242. $expected = array(
  243. 'foo' => "one two",
  244. 'bar' => "one two",
  245. );
  246. $tests['Folded block chomping strip without trailing newline'] = array($expected, $yaml);
  247. $yaml = <<<'EOF'
  248. foo: >
  249. one
  250. two
  251. bar: >
  252. one
  253. two
  254. EOF;
  255. $expected = array(
  256. 'foo' => "one two\n",
  257. 'bar' => "one two\n",
  258. );
  259. $tests['Folded block chomping clip with single trailing newline'] = array($expected, $yaml);
  260. $yaml = <<<'EOF'
  261. foo: >
  262. one
  263. two
  264. bar: >
  265. one
  266. two
  267. EOF;
  268. $expected = array(
  269. 'foo' => "one two\n",
  270. 'bar' => "one two\n",
  271. );
  272. $tests['Folded block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
  273. $yaml = <<<'EOF'
  274. foo: >
  275. one
  276. two
  277. bar: >
  278. one
  279. two
  280. EOF;
  281. $expected = array(
  282. 'foo' => "one two\n",
  283. 'bar' => "one two",
  284. );
  285. $tests['Folded block chomping clip without trailing newline'] = array($expected, $yaml);
  286. $yaml = <<<'EOF'
  287. foo: >+
  288. one
  289. two
  290. bar: >+
  291. one
  292. two
  293. EOF;
  294. $expected = array(
  295. 'foo' => "one two\n",
  296. 'bar' => "one two\n",
  297. );
  298. $tests['Folded block chomping keep with single trailing newline'] = array($expected, $yaml);
  299. $yaml = <<<'EOF'
  300. foo: >+
  301. one
  302. two
  303. bar: >+
  304. one
  305. two
  306. EOF;
  307. $expected = array(
  308. 'foo' => "one two\n\n",
  309. 'bar' => "one two\n\n",
  310. );
  311. $tests['Folded block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
  312. $yaml = <<<'EOF'
  313. foo: >+
  314. one
  315. two
  316. bar: >+
  317. one
  318. two
  319. EOF;
  320. $expected = array(
  321. 'foo' => "one two\n",
  322. 'bar' => "one two",
  323. );
  324. $tests['Folded block chomping keep without trailing newline'] = array($expected, $yaml);
  325. return $tests;
  326. }
  327. /**
  328. * @dataProvider getBlockChompingTests
  329. */
  330. public function testBlockChomping($expected, $yaml)
  331. {
  332. $this->assertSame($expected, $this->parser->parse($yaml));
  333. }
  334. /**
  335. * Regression test for issue #7989.
  336. *
  337. * @see https://github.com/symfony/symfony/issues/7989
  338. */
  339. public function testBlockLiteralWithLeadingNewlines()
  340. {
  341. $yaml = <<<'EOF'
  342. foo: |-
  343. bar
  344. EOF;
  345. $expected = array(
  346. 'foo' => "\n\nbar"
  347. );
  348. $this->assertSame($expected, $this->parser->parse($yaml));
  349. }
  350. public function testObjectSupportEnabled()
  351. {
  352. $input = <<<EOF
  353. foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
  354. bar: 1
  355. EOF;
  356. $this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
  357. }
  358. public function testObjectSupportDisabledButNoExceptions()
  359. {
  360. $input = <<<EOF
  361. foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
  362. bar: 1
  363. EOF;
  364. $this->assertEquals(array('foo' => null, 'bar' => 1), $this->parser->parse($input), '->parse() does not parse objects');
  365. }
  366. /**
  367. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  368. */
  369. public function testObjectsSupportDisabledWithExceptions()
  370. {
  371. $this->parser->parse('foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}', true, false);
  372. }
  373. public function testNonUtf8Exception()
  374. {
  375. if (!function_exists('mb_detect_encoding') || !function_exists('iconv')) {
  376. $this->markTestSkipped('Exceptions for non-utf8 charsets require the mb_detect_encoding() and iconv() functions.');
  377. return;
  378. }
  379. $yamls = array(
  380. iconv("UTF-8", "ISO-8859-1", "foo: 'äöüß'"),
  381. iconv("UTF-8", "ISO-8859-15", "euro: '€'"),
  382. iconv("UTF-8", "CP1252", "cp1252: '©ÉÇáñ'")
  383. );
  384. foreach ($yamls as $yaml) {
  385. try {
  386. $this->parser->parse($yaml);
  387. $this->fail('charsets other than UTF-8 are rejected.');
  388. } catch (\Exception $e) {
  389. $this->assertInstanceOf('Symfony\Component\Yaml\Exception\ParseException', $e, 'charsets other than UTF-8 are rejected.');
  390. }
  391. }
  392. }
  393. /**
  394. *
  395. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  396. *
  397. */
  398. public function testUnindentedCollectionException()
  399. {
  400. $yaml = <<<EOF
  401. collection:
  402. -item1
  403. -item2
  404. -item3
  405. EOF;
  406. $this->parser->parse($yaml);
  407. }
  408. /**
  409. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  410. */
  411. public function testSequenceInAMapping()
  412. {
  413. Yaml::parse(<<<EOF
  414. yaml:
  415. hash: me
  416. - array stuff
  417. EOF
  418. );
  419. }
  420. /**
  421. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  422. */
  423. public function testMappingInASequence()
  424. {
  425. Yaml::parse(<<<EOF
  426. yaml:
  427. - array stuff
  428. hash: me
  429. EOF
  430. );
  431. }
  432. public function testEmptyValue()
  433. {
  434. $input = <<<EOF
  435. hash:
  436. EOF;
  437. $this->assertEquals(array('hash' => null), Yaml::parse($input));
  438. }
  439. public function testStringBlockWithComments()
  440. {
  441. $this->assertEquals(array('content' => <<<EOT
  442. # comment 1
  443. header
  444. # comment 2
  445. <body>
  446. <h1>title</h1>
  447. </body>
  448. footer # comment3
  449. EOT
  450. ), Yaml::parse(<<<EOF
  451. content: |
  452. # comment 1
  453. header
  454. # comment 2
  455. <body>
  456. <h1>title</h1>
  457. </body>
  458. footer # comment3
  459. EOF
  460. ));
  461. }
  462. public function testFoldedStringBlockWithComments()
  463. {
  464. $this->assertEquals(array(array('content' => <<<EOT
  465. # comment 1
  466. header
  467. # comment 2
  468. <body>
  469. <h1>title</h1>
  470. </body>
  471. footer # comment3
  472. EOT
  473. )), Yaml::parse(<<<EOF
  474. -
  475. content: |
  476. # comment 1
  477. header
  478. # comment 2
  479. <body>
  480. <h1>title</h1>
  481. </body>
  482. footer # comment3
  483. EOF
  484. ));
  485. }
  486. public function testNestedFoldedStringBlockWithComments()
  487. {
  488. $this->assertEquals(array(array(
  489. 'title' => 'some title',
  490. 'content' => <<<EOT
  491. # comment 1
  492. header
  493. # comment 2
  494. <body>
  495. <h1>title</h1>
  496. </body>
  497. footer # comment3
  498. EOT
  499. )), Yaml::parse(<<<EOF
  500. -
  501. title: some title
  502. content: |
  503. # comment 1
  504. header
  505. # comment 2
  506. <body>
  507. <h1>title</h1>
  508. </body>
  509. footer # comment3
  510. EOF
  511. ));
  512. }
  513. }
  514. class B
  515. {
  516. public $b = 'foo';
  517. }