ParserTest.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Yaml\Exception\ParseException;
  13. use Symfony\Component\Yaml\Parser;
  14. use Symfony\Component\Yaml\Tag\TaggedValue;
  15. use Symfony\Component\Yaml\Yaml;
  16. class ParserTest extends TestCase
  17. {
  18. /** @var Parser */
  19. protected $parser;
  20. protected function setUp()
  21. {
  22. $this->parser = new Parser();
  23. }
  24. protected function tearDown()
  25. {
  26. $this->parser = null;
  27. chmod(__DIR__.'/Fixtures/not_readable.yml', 0644);
  28. }
  29. /**
  30. * @dataProvider getDataFormSpecifications
  31. */
  32. public function testSpecifications($expected, $yaml, $comment)
  33. {
  34. $this->assertEquals($expected, var_export($this->parser->parse($yaml), true), $comment);
  35. }
  36. public function getDataFormSpecifications()
  37. {
  38. return $this->loadTestsFromFixtureFiles('index.yml');
  39. }
  40. public function getNonStringMappingKeysData()
  41. {
  42. return $this->loadTestsFromFixtureFiles('nonStringKeys.yml');
  43. }
  44. public function testTabsInYaml()
  45. {
  46. // test tabs in YAML
  47. $yamls = [
  48. "foo:\n bar",
  49. "foo:\n bar",
  50. "foo:\n bar",
  51. "foo:\n bar",
  52. ];
  53. foreach ($yamls as $yaml) {
  54. try {
  55. $content = $this->parser->parse($yaml);
  56. $this->fail('YAML files must not contain tabs');
  57. } catch (\Exception $e) {
  58. $this->assertInstanceOf('\Exception', $e, 'YAML files must not contain tabs');
  59. $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');
  60. }
  61. }
  62. }
  63. public function testEndOfTheDocumentMarker()
  64. {
  65. $yaml = <<<'EOF'
  66. --- %YAML:1.0
  67. foo
  68. ...
  69. EOF;
  70. $this->assertEquals('foo', $this->parser->parse($yaml));
  71. }
  72. public function getBlockChompingTests()
  73. {
  74. $tests = [];
  75. $yaml = <<<'EOF'
  76. foo: |-
  77. one
  78. two
  79. bar: |-
  80. one
  81. two
  82. EOF;
  83. $expected = [
  84. 'foo' => "one\ntwo",
  85. 'bar' => "one\ntwo",
  86. ];
  87. $tests['Literal block chomping strip with single trailing newline'] = [$expected, $yaml];
  88. $yaml = <<<'EOF'
  89. foo: |-
  90. one
  91. two
  92. bar: |-
  93. one
  94. two
  95. EOF;
  96. $expected = [
  97. 'foo' => "one\ntwo",
  98. 'bar' => "one\ntwo",
  99. ];
  100. $tests['Literal block chomping strip with multiple trailing newlines'] = [$expected, $yaml];
  101. $yaml = <<<'EOF'
  102. {}
  103. EOF;
  104. $expected = [];
  105. $tests['Literal block chomping strip with multiple trailing newlines after a 1-liner'] = [$expected, $yaml];
  106. $yaml = <<<'EOF'
  107. foo: |-
  108. one
  109. two
  110. bar: |-
  111. one
  112. two
  113. EOF;
  114. $expected = [
  115. 'foo' => "one\ntwo",
  116. 'bar' => "one\ntwo",
  117. ];
  118. $tests['Literal block chomping strip without trailing newline'] = [$expected, $yaml];
  119. $yaml = <<<'EOF'
  120. foo: |
  121. one
  122. two
  123. bar: |
  124. one
  125. two
  126. EOF;
  127. $expected = [
  128. 'foo' => "one\ntwo\n",
  129. 'bar' => "one\ntwo\n",
  130. ];
  131. $tests['Literal block chomping clip with single trailing newline'] = [$expected, $yaml];
  132. $yaml = <<<'EOF'
  133. foo: |
  134. one
  135. two
  136. bar: |
  137. one
  138. two
  139. EOF;
  140. $expected = [
  141. 'foo' => "one\ntwo\n",
  142. 'bar' => "one\ntwo\n",
  143. ];
  144. $tests['Literal block chomping clip with multiple trailing newlines'] = [$expected, $yaml];
  145. $yaml = <<<'EOF'
  146. foo:
  147. - bar: |
  148. one
  149. two
  150. EOF;
  151. $expected = [
  152. 'foo' => [
  153. [
  154. 'bar' => "one\n\ntwo",
  155. ],
  156. ],
  157. ];
  158. $tests['Literal block chomping clip with embedded blank line inside unindented collection'] = [$expected, $yaml];
  159. $yaml = <<<'EOF'
  160. foo: |
  161. one
  162. two
  163. bar: |
  164. one
  165. two
  166. EOF;
  167. $expected = [
  168. 'foo' => "one\ntwo\n",
  169. 'bar' => "one\ntwo",
  170. ];
  171. $tests['Literal block chomping clip without trailing newline'] = [$expected, $yaml];
  172. $yaml = <<<'EOF'
  173. foo: |+
  174. one
  175. two
  176. bar: |+
  177. one
  178. two
  179. EOF;
  180. $expected = [
  181. 'foo' => "one\ntwo\n",
  182. 'bar' => "one\ntwo\n",
  183. ];
  184. $tests['Literal block chomping keep with single trailing newline'] = [$expected, $yaml];
  185. $yaml = <<<'EOF'
  186. foo: |+
  187. one
  188. two
  189. bar: |+
  190. one
  191. two
  192. EOF;
  193. $expected = [
  194. 'foo' => "one\ntwo\n\n",
  195. 'bar' => "one\ntwo\n\n",
  196. ];
  197. $tests['Literal block chomping keep with multiple trailing newlines'] = [$expected, $yaml];
  198. $yaml = <<<'EOF'
  199. foo: |+
  200. one
  201. two
  202. bar: |+
  203. one
  204. two
  205. EOF;
  206. $expected = [
  207. 'foo' => "one\ntwo\n",
  208. 'bar' => "one\ntwo",
  209. ];
  210. $tests['Literal block chomping keep without trailing newline'] = [$expected, $yaml];
  211. $yaml = <<<'EOF'
  212. foo: >-
  213. one
  214. two
  215. bar: >-
  216. one
  217. two
  218. EOF;
  219. $expected = [
  220. 'foo' => 'one two',
  221. 'bar' => 'one two',
  222. ];
  223. $tests['Folded block chomping strip with single trailing newline'] = [$expected, $yaml];
  224. $yaml = <<<'EOF'
  225. foo: >-
  226. one
  227. two
  228. bar: >-
  229. one
  230. two
  231. EOF;
  232. $expected = [
  233. 'foo' => 'one two',
  234. 'bar' => 'one two',
  235. ];
  236. $tests['Folded block chomping strip with multiple trailing newlines'] = [$expected, $yaml];
  237. $yaml = <<<'EOF'
  238. foo: >-
  239. one
  240. two
  241. bar: >-
  242. one
  243. two
  244. EOF;
  245. $expected = [
  246. 'foo' => 'one two',
  247. 'bar' => 'one two',
  248. ];
  249. $tests['Folded block chomping strip without trailing newline'] = [$expected, $yaml];
  250. $yaml = <<<'EOF'
  251. foo: >
  252. one
  253. two
  254. bar: >
  255. one
  256. two
  257. EOF;
  258. $expected = [
  259. 'foo' => "one two\n",
  260. 'bar' => "one two\n",
  261. ];
  262. $tests['Folded block chomping clip with single trailing newline'] = [$expected, $yaml];
  263. $yaml = <<<'EOF'
  264. foo: >
  265. one
  266. two
  267. bar: >
  268. one
  269. two
  270. EOF;
  271. $expected = [
  272. 'foo' => "one two\n",
  273. 'bar' => "one two\n",
  274. ];
  275. $tests['Folded block chomping clip with multiple trailing newlines'] = [$expected, $yaml];
  276. $yaml = <<<'EOF'
  277. foo: >
  278. one
  279. two
  280. bar: >
  281. one
  282. two
  283. EOF;
  284. $expected = [
  285. 'foo' => "one two\n",
  286. 'bar' => 'one two',
  287. ];
  288. $tests['Folded block chomping clip without trailing newline'] = [$expected, $yaml];
  289. $yaml = <<<'EOF'
  290. foo: >+
  291. one
  292. two
  293. bar: >+
  294. one
  295. two
  296. EOF;
  297. $expected = [
  298. 'foo' => "one two\n",
  299. 'bar' => "one two\n",
  300. ];
  301. $tests['Folded block chomping keep with single trailing newline'] = [$expected, $yaml];
  302. $yaml = <<<'EOF'
  303. foo: >+
  304. one
  305. two
  306. bar: >+
  307. one
  308. two
  309. EOF;
  310. $expected = [
  311. 'foo' => "one two\n\n",
  312. 'bar' => "one two\n\n",
  313. ];
  314. $tests['Folded block chomping keep with multiple trailing newlines'] = [$expected, $yaml];
  315. $yaml = <<<'EOF'
  316. foo: >+
  317. one
  318. two
  319. bar: >+
  320. one
  321. two
  322. EOF;
  323. $expected = [
  324. 'foo' => "one two\n",
  325. 'bar' => 'one two',
  326. ];
  327. $tests['Folded block chomping keep without trailing newline'] = [$expected, $yaml];
  328. return $tests;
  329. }
  330. /**
  331. * @dataProvider getBlockChompingTests
  332. */
  333. public function testBlockChomping($expected, $yaml)
  334. {
  335. $this->assertSame($expected, $this->parser->parse($yaml));
  336. }
  337. /**
  338. * Regression test for issue #7989.
  339. *
  340. * @see https://github.com/symfony/symfony/issues/7989
  341. */
  342. public function testBlockLiteralWithLeadingNewlines()
  343. {
  344. $yaml = <<<'EOF'
  345. foo: |-
  346. bar
  347. EOF;
  348. $expected = [
  349. 'foo' => "\n\nbar",
  350. ];
  351. $this->assertSame($expected, $this->parser->parse($yaml));
  352. }
  353. public function testObjectSupportEnabled()
  354. {
  355. $input = <<<'EOF'
  356. foo: !php/object O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
  357. bar: 1
  358. EOF;
  359. $this->assertEquals(['foo' => new B(), 'bar' => 1], $this->parser->parse($input, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
  360. }
  361. public function testObjectSupportDisabledButNoExceptions()
  362. {
  363. $input = <<<'EOF'
  364. foo: !php/object O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
  365. bar: 1
  366. EOF;
  367. $this->assertEquals(['foo' => null, 'bar' => 1], $this->parser->parse($input), '->parse() does not parse objects');
  368. }
  369. /**
  370. * @dataProvider getObjectForMapTests
  371. */
  372. public function testObjectForMap($yaml, $expected)
  373. {
  374. $flags = Yaml::PARSE_OBJECT_FOR_MAP;
  375. $this->assertEquals($expected, $this->parser->parse($yaml, $flags));
  376. }
  377. public function getObjectForMapTests()
  378. {
  379. $tests = [];
  380. $yaml = <<<'EOF'
  381. foo:
  382. fiz: [cat]
  383. EOF;
  384. $expected = new \stdClass();
  385. $expected->foo = new \stdClass();
  386. $expected->foo->fiz = ['cat'];
  387. $tests['mapping'] = [$yaml, $expected];
  388. $yaml = '{ "foo": "bar", "fiz": "cat" }';
  389. $expected = new \stdClass();
  390. $expected->foo = 'bar';
  391. $expected->fiz = 'cat';
  392. $tests['inline-mapping'] = [$yaml, $expected];
  393. $yaml = "foo: bar\nbaz: foobar";
  394. $expected = new \stdClass();
  395. $expected->foo = 'bar';
  396. $expected->baz = 'foobar';
  397. $tests['object-for-map-is-applied-after-parsing'] = [$yaml, $expected];
  398. $yaml = <<<'EOT'
  399. array:
  400. - key: one
  401. - key: two
  402. EOT;
  403. $expected = new \stdClass();
  404. $expected->array = [];
  405. $expected->array[0] = new \stdClass();
  406. $expected->array[0]->key = 'one';
  407. $expected->array[1] = new \stdClass();
  408. $expected->array[1]->key = 'two';
  409. $tests['nest-map-and-sequence'] = [$yaml, $expected];
  410. $yaml = <<<'YAML'
  411. map:
  412. 1: one
  413. 2: two
  414. YAML;
  415. $expected = new \stdClass();
  416. $expected->map = new \stdClass();
  417. $expected->map->{1} = 'one';
  418. $expected->map->{2} = 'two';
  419. $tests['numeric-keys'] = [$yaml, $expected];
  420. $yaml = <<<'YAML'
  421. map:
  422. '0': one
  423. '1': two
  424. YAML;
  425. $expected = new \stdClass();
  426. $expected->map = new \stdClass();
  427. $expected->map->{0} = 'one';
  428. $expected->map->{1} = 'two';
  429. $tests['zero-indexed-numeric-keys'] = [$yaml, $expected];
  430. return $tests;
  431. }
  432. /**
  433. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  434. */
  435. public function testObjectsSupportDisabledWithExceptions()
  436. {
  437. $yaml = <<<'EOF'
  438. foo: !php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
  439. bar: 1
  440. EOF;
  441. $this->parser->parse($yaml, Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
  442. }
  443. public function testCanParseContentWithTrailingSpaces()
  444. {
  445. $yaml = "items: \n foo: bar";
  446. $expected = [
  447. 'items' => ['foo' => 'bar'],
  448. ];
  449. $this->assertSame($expected, $this->parser->parse($yaml));
  450. }
  451. /**
  452. * @requires extension iconv
  453. */
  454. public function testNonUtf8Exception()
  455. {
  456. $yamls = [
  457. iconv('UTF-8', 'ISO-8859-1', "foo: 'äöüß'"),
  458. iconv('UTF-8', 'ISO-8859-15', "euro: '€'"),
  459. iconv('UTF-8', 'CP1252', "cp1252: '©ÉÇáñ'"),
  460. ];
  461. foreach ($yamls as $yaml) {
  462. try {
  463. $this->parser->parse($yaml);
  464. $this->fail('charsets other than UTF-8 are rejected.');
  465. } catch (\Exception $e) {
  466. $this->assertInstanceOf('Symfony\Component\Yaml\Exception\ParseException', $e, 'charsets other than UTF-8 are rejected.');
  467. }
  468. }
  469. }
  470. /**
  471. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  472. */
  473. public function testUnindentedCollectionException()
  474. {
  475. $yaml = <<<'EOF'
  476. collection:
  477. -item1
  478. -item2
  479. -item3
  480. EOF;
  481. $this->parser->parse($yaml);
  482. }
  483. /**
  484. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  485. */
  486. public function testShortcutKeyUnindentedCollectionException()
  487. {
  488. $yaml = <<<'EOF'
  489. collection:
  490. - key: foo
  491. foo: bar
  492. EOF;
  493. $this->parser->parse($yaml);
  494. }
  495. /**
  496. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  497. * @expectedExceptionMessageRegExp /^Multiple documents are not supported.+/
  498. */
  499. public function testMultipleDocumentsNotSupportedException()
  500. {
  501. Yaml::parse(<<<'EOL'
  502. # Ranking of 1998 home runs
  503. ---
  504. - Mark McGwire
  505. - Sammy Sosa
  506. - Ken Griffey
  507. # Team ranking
  508. ---
  509. - Chicago Cubs
  510. - St Louis Cardinals
  511. EOL
  512. );
  513. }
  514. /**
  515. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  516. */
  517. public function testSequenceInAMapping()
  518. {
  519. Yaml::parse(<<<'EOF'
  520. yaml:
  521. hash: me
  522. - array stuff
  523. EOF
  524. );
  525. }
  526. public function testSequenceInMappingStartedBySingleDashLine()
  527. {
  528. $yaml = <<<'EOT'
  529. a:
  530. -
  531. b:
  532. -
  533. bar: baz
  534. - foo
  535. d: e
  536. EOT;
  537. $expected = [
  538. 'a' => [
  539. [
  540. 'b' => [
  541. [
  542. 'bar' => 'baz',
  543. ],
  544. ],
  545. ],
  546. 'foo',
  547. ],
  548. 'd' => 'e',
  549. ];
  550. $this->assertSame($expected, $this->parser->parse($yaml));
  551. }
  552. public function testSequenceFollowedByCommentEmbeddedInMapping()
  553. {
  554. $yaml = <<<'EOT'
  555. a:
  556. b:
  557. - c
  558. # comment
  559. d: e
  560. EOT;
  561. $expected = [
  562. 'a' => [
  563. 'b' => ['c'],
  564. 'd' => 'e',
  565. ],
  566. ];
  567. $this->assertSame($expected, $this->parser->parse($yaml));
  568. }
  569. public function testNonStringFollowedByCommentEmbeddedInMapping()
  570. {
  571. $yaml = <<<'EOT'
  572. a:
  573. b:
  574. {}
  575. # comment
  576. d:
  577. 1.1
  578. # another comment
  579. EOT;
  580. $expected = [
  581. 'a' => [
  582. 'b' => [],
  583. 'd' => 1.1,
  584. ],
  585. ];
  586. $this->assertSame($expected, $this->parser->parse($yaml));
  587. }
  588. public function getParseExceptionNotAffectedMultiLineStringLastResortParsing()
  589. {
  590. $tests = [];
  591. $yaml = <<<'EOT'
  592. a
  593. b:
  594. EOT;
  595. $tests['parse error on first line'] = [$yaml];
  596. $yaml = <<<'EOT'
  597. a
  598. b
  599. c:
  600. EOT;
  601. $tests['parse error due to inconsistent indentation'] = [$yaml];
  602. $yaml = <<<'EOT'
  603. & * ! | > ' " % @ ` #, { asd a;sdasd }-@^qw3
  604. EOT;
  605. $tests['symfony/symfony/issues/22967#issuecomment-322067742'] = [$yaml];
  606. return $tests;
  607. }
  608. /**
  609. * @dataProvider getParseExceptionNotAffectedMultiLineStringLastResortParsing
  610. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  611. */
  612. public function testParseExceptionNotAffectedByMultiLineStringLastResortParsing($yaml)
  613. {
  614. $this->parser->parse($yaml);
  615. }
  616. public function testMultiLineStringLastResortParsing()
  617. {
  618. $yaml = <<<'EOT'
  619. test:
  620. You can have things that don't look like strings here
  621. true
  622. yes you can
  623. EOT;
  624. $expected = [
  625. 'test' => 'You can have things that don\'t look like strings here true yes you can',
  626. ];
  627. $this->assertSame($expected, $this->parser->parse($yaml));
  628. $yaml = <<<'EOT'
  629. a:
  630. b
  631. c
  632. EOT;
  633. $expected = [
  634. 'a' => 'b c',
  635. ];
  636. $this->assertSame($expected, $this->parser->parse($yaml));
  637. }
  638. /**
  639. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  640. */
  641. public function testMappingInASequence()
  642. {
  643. Yaml::parse(<<<'EOF'
  644. yaml:
  645. - array stuff
  646. hash: me
  647. EOF
  648. );
  649. }
  650. /**
  651. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  652. * @expectedExceptionMessage missing colon
  653. */
  654. public function testScalarInSequence()
  655. {
  656. Yaml::parse(<<<'EOF'
  657. foo:
  658. - bar
  659. "missing colon"
  660. foo: bar
  661. EOF
  662. );
  663. }
  664. /**
  665. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  666. * @expectedExceptionMessage Duplicate key "child" detected
  667. *
  668. * > It is an error for two equal keys to appear in the same mapping node.
  669. * > In such a case the YAML processor may continue, ignoring the second
  670. * > "key: value" pair and issuing an appropriate warning. This strategy
  671. * > preserves a consistent information model for one-pass and random access
  672. * > applications.
  673. *
  674. * @see http://yaml.org/spec/1.2/spec.html#id2759572
  675. * @see http://yaml.org/spec/1.1/#id932806
  676. */
  677. public function testMappingDuplicateKeyBlock()
  678. {
  679. $input = <<<'EOD'
  680. parent:
  681. child: first
  682. child: duplicate
  683. parent:
  684. child: duplicate
  685. child: duplicate
  686. EOD;
  687. $expected = [
  688. 'parent' => [
  689. 'child' => 'first',
  690. ],
  691. ];
  692. $this->assertSame($expected, Yaml::parse($input));
  693. }
  694. /**
  695. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  696. * @expectedExceptionMessage Duplicate key "child" detected
  697. */
  698. public function testMappingDuplicateKeyFlow()
  699. {
  700. $input = <<<'EOD'
  701. parent: { child: first, child: duplicate }
  702. parent: { child: duplicate, child: duplicate }
  703. EOD;
  704. $expected = [
  705. 'parent' => [
  706. 'child' => 'first',
  707. ],
  708. ];
  709. $this->assertSame($expected, Yaml::parse($input));
  710. }
  711. /**
  712. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  713. * @dataProvider getParseExceptionOnDuplicateData
  714. */
  715. public function testParseExceptionOnDuplicate($input, $duplicateKey, $lineNumber)
  716. {
  717. $this->expectExceptionMessage(sprintf('Duplicate key "%s" detected at line %d', $duplicateKey, $lineNumber));
  718. Yaml::parse($input);
  719. }
  720. public function getParseExceptionOnDuplicateData()
  721. {
  722. $tests = [];
  723. $yaml = <<<EOD
  724. parent: { child: first, child: duplicate }
  725. EOD;
  726. $tests[] = [$yaml, 'child', 1];
  727. $yaml = <<<EOD
  728. parent:
  729. child: first,
  730. child: duplicate
  731. EOD;
  732. $tests[] = [$yaml, 'child', 3];
  733. $yaml = <<<EOD
  734. parent: { child: foo }
  735. parent: { child: bar }
  736. EOD;
  737. $tests[] = [$yaml, 'parent', 2];
  738. $yaml = <<<EOD
  739. parent: { child_mapping: { value: bar}, child_mapping: { value: bar} }
  740. EOD;
  741. $tests[] = [$yaml, 'child_mapping', 1];
  742. $yaml = <<<EOD
  743. parent:
  744. child_mapping:
  745. value: bar
  746. child_mapping:
  747. value: bar
  748. EOD;
  749. $tests[] = [$yaml, 'child_mapping', 4];
  750. $yaml = <<<EOD
  751. parent: { child_sequence: ['key1', 'key2', 'key3'], child_sequence: ['key1', 'key2', 'key3'] }
  752. EOD;
  753. $tests[] = [$yaml, 'child_sequence', 1];
  754. $yaml = <<<EOD
  755. parent:
  756. child_sequence:
  757. - key1
  758. - key2
  759. - key3
  760. child_sequence:
  761. - key1
  762. - key2
  763. - key3
  764. EOD;
  765. $tests[] = [$yaml, 'child_sequence', 6];
  766. return $tests;
  767. }
  768. public function testEmptyValue()
  769. {
  770. $input = <<<'EOF'
  771. hash:
  772. EOF;
  773. $this->assertEquals(['hash' => null], Yaml::parse($input));
  774. }
  775. public function testCommentAtTheRootIndent()
  776. {
  777. $this->assertEquals([
  778. 'services' => [
  779. 'app.foo_service' => [
  780. 'class' => 'Foo',
  781. ],
  782. 'app/bar_service' => [
  783. 'class' => 'Bar',
  784. ],
  785. ],
  786. ], Yaml::parse(<<<'EOF'
  787. # comment 1
  788. services:
  789. # comment 2
  790. # comment 3
  791. app.foo_service:
  792. class: Foo
  793. # comment 4
  794. # comment 5
  795. app/bar_service:
  796. class: Bar
  797. EOF
  798. ));
  799. }
  800. public function testStringBlockWithComments()
  801. {
  802. $this->assertEquals(['content' => <<<'EOT'
  803. # comment 1
  804. header
  805. # comment 2
  806. <body>
  807. <h1>title</h1>
  808. </body>
  809. footer # comment3
  810. EOT
  811. ], Yaml::parse(<<<'EOF'
  812. content: |
  813. # comment 1
  814. header
  815. # comment 2
  816. <body>
  817. <h1>title</h1>
  818. </body>
  819. footer # comment3
  820. EOF
  821. ));
  822. }
  823. public function testFoldedStringBlockWithComments()
  824. {
  825. $this->assertEquals([['content' => <<<'EOT'
  826. # comment 1
  827. header
  828. # comment 2
  829. <body>
  830. <h1>title</h1>
  831. </body>
  832. footer # comment3
  833. EOT
  834. ]], Yaml::parse(<<<'EOF'
  835. -
  836. content: |
  837. # comment 1
  838. header
  839. # comment 2
  840. <body>
  841. <h1>title</h1>
  842. </body>
  843. footer # comment3
  844. EOF
  845. ));
  846. }
  847. public function testNestedFoldedStringBlockWithComments()
  848. {
  849. $this->assertEquals([[
  850. 'title' => 'some title',
  851. 'content' => <<<'EOT'
  852. # comment 1
  853. header
  854. # comment 2
  855. <body>
  856. <h1>title</h1>
  857. </body>
  858. footer # comment3
  859. EOT
  860. ]], Yaml::parse(<<<'EOF'
  861. -
  862. title: some title
  863. content: |
  864. # comment 1
  865. header
  866. # comment 2
  867. <body>
  868. <h1>title</h1>
  869. </body>
  870. footer # comment3
  871. EOF
  872. ));
  873. }
  874. public function testReferenceResolvingInInlineStrings()
  875. {
  876. $this->assertEquals([
  877. 'var' => 'var-value',
  878. 'scalar' => 'var-value',
  879. 'list' => ['var-value'],
  880. 'list_in_list' => [['var-value']],
  881. 'map_in_list' => [['key' => 'var-value']],
  882. 'embedded_mapping' => [['key' => 'var-value']],
  883. 'map' => ['key' => 'var-value'],
  884. 'list_in_map' => ['key' => ['var-value']],
  885. 'map_in_map' => ['foo' => ['bar' => 'var-value']],
  886. ], Yaml::parse(<<<'EOF'
  887. var: &var var-value
  888. scalar: *var
  889. list: [ *var ]
  890. list_in_list: [[ *var ]]
  891. map_in_list: [ { key: *var } ]
  892. embedded_mapping: [ key: *var ]
  893. map: { key: *var }
  894. list_in_map: { key: [*var] }
  895. map_in_map: { foo: { bar: *var } }
  896. EOF
  897. ));
  898. }
  899. public function testYamlDirective()
  900. {
  901. $yaml = <<<'EOF'
  902. %YAML 1.2
  903. ---
  904. foo: 1
  905. bar: 2
  906. EOF;
  907. $this->assertEquals(['foo' => 1, 'bar' => 2], $this->parser->parse($yaml));
  908. }
  909. /**
  910. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  911. * @expectedExceptionMessage Numeric keys are not supported. Quote your evaluable mapping keys instead
  912. */
  913. public function testFloatKeys()
  914. {
  915. $yaml = <<<'EOF'
  916. foo:
  917. 1.2: "bar"
  918. 1.3: "baz"
  919. EOF;
  920. $this->parser->parse($yaml);
  921. }
  922. /**
  923. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  924. * @expectedExceptionMessage Non-string keys are not supported. Quote your evaluable mapping keys instead
  925. */
  926. public function testBooleanKeys()
  927. {
  928. $yaml = <<<'EOF'
  929. true: foo
  930. false: bar
  931. EOF;
  932. $this->parser->parse($yaml);
  933. }
  934. public function testExplicitStringCasting()
  935. {
  936. $yaml = <<<'EOF'
  937. '1.2': "bar"
  938. !!str 1.3: "baz"
  939. 'true': foo
  940. !!str false: bar
  941. !!str null: 'null'
  942. '~': 'null'
  943. EOF;
  944. $expected = [
  945. '1.2' => 'bar',
  946. '1.3' => 'baz',
  947. 'true' => 'foo',
  948. 'false' => 'bar',
  949. 'null' => 'null',
  950. '~' => 'null',
  951. ];
  952. $this->assertEquals($expected, $this->parser->parse($yaml));
  953. }
  954. /**
  955. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  956. * @expectedExceptionMessage A colon cannot be used in an unquoted mapping value
  957. */
  958. public function testColonInMappingValueException()
  959. {
  960. $yaml = <<<'EOF'
  961. foo: bar: baz
  962. EOF;
  963. $this->parser->parse($yaml);
  964. }
  965. public function testColonInMappingValueExceptionNotTriggeredByColonInComment()
  966. {
  967. $yaml = <<<'EOT'
  968. foo:
  969. bar: foobar # Note: a comment after a colon
  970. EOT;
  971. $this->assertSame(['foo' => ['bar' => 'foobar']], $this->parser->parse($yaml));
  972. }
  973. /**
  974. * @dataProvider getCommentLikeStringInScalarBlockData
  975. */
  976. public function testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expectedParserResult)
  977. {
  978. $this->assertSame($expectedParserResult, $this->parser->parse($yaml));
  979. }
  980. public function getCommentLikeStringInScalarBlockData()
  981. {
  982. $tests = [];
  983. $yaml = <<<'EOT'
  984. pages:
  985. -
  986. title: some title
  987. content: |
  988. # comment 1
  989. header
  990. # comment 2
  991. <body>
  992. <h1>title</h1>
  993. </body>
  994. footer # comment3
  995. EOT;
  996. $expected = [
  997. 'pages' => [
  998. [
  999. 'title' => 'some title',
  1000. 'content' => <<<'EOT'
  1001. # comment 1
  1002. header
  1003. # comment 2
  1004. <body>
  1005. <h1>title</h1>
  1006. </body>
  1007. footer # comment3
  1008. EOT
  1009. ,
  1010. ],
  1011. ],
  1012. ];
  1013. $tests[] = [$yaml, $expected];
  1014. $yaml = <<<'EOT'
  1015. test: |
  1016. foo
  1017. # bar
  1018. baz
  1019. collection:
  1020. - one: |
  1021. foo
  1022. # bar
  1023. baz
  1024. - two: |
  1025. foo
  1026. # bar
  1027. baz
  1028. EOT;
  1029. $expected = [
  1030. 'test' => <<<'EOT'
  1031. foo
  1032. # bar
  1033. baz
  1034. EOT
  1035. ,
  1036. 'collection' => [
  1037. [
  1038. 'one' => <<<'EOT'
  1039. foo
  1040. # bar
  1041. baz
  1042. EOT
  1043. ,
  1044. ],
  1045. [
  1046. 'two' => <<<'EOT'
  1047. foo
  1048. # bar
  1049. baz
  1050. EOT
  1051. ,
  1052. ],
  1053. ],
  1054. ];
  1055. $tests[] = [$yaml, $expected];
  1056. $yaml = <<<'EOT'
  1057. foo:
  1058. bar:
  1059. scalar-block: >
  1060. line1
  1061. line2>
  1062. baz:
  1063. # comment
  1064. foobar: ~
  1065. EOT;
  1066. $expected = [
  1067. 'foo' => [
  1068. 'bar' => [
  1069. 'scalar-block' => "line1 line2>\n",
  1070. ],
  1071. 'baz' => [
  1072. 'foobar' => null,
  1073. ],
  1074. ],
  1075. ];
  1076. $tests[] = [$yaml, $expected];
  1077. $yaml = <<<'EOT'
  1078. a:
  1079. b: hello
  1080. # c: |
  1081. # first row
  1082. # second row
  1083. d: hello
  1084. EOT;
  1085. $expected = [
  1086. 'a' => [
  1087. 'b' => 'hello',
  1088. 'd' => 'hello',
  1089. ],
  1090. ];
  1091. $tests[] = [$yaml, $expected];
  1092. return $tests;
  1093. }
  1094. public function testBlankLinesAreParsedAsNewLinesInFoldedBlocks()
  1095. {
  1096. $yaml = <<<'EOT'
  1097. test: >
  1098. <h2>A heading</h2>
  1099. <ul>
  1100. <li>a list</li>
  1101. <li>may be a good example</li>
  1102. </ul>
  1103. EOT;
  1104. $this->assertSame(
  1105. [
  1106. 'test' => <<<'EOT'
  1107. <h2>A heading</h2>
  1108. <ul> <li>a list</li> <li>may be a good example</li> </ul>
  1109. EOT
  1110. ,
  1111. ],
  1112. $this->parser->parse($yaml)
  1113. );
  1114. }
  1115. public function testAdditionallyIndentedLinesAreParsedAsNewLinesInFoldedBlocks()
  1116. {
  1117. $yaml = <<<'EOT'
  1118. test: >
  1119. <h2>A heading</h2>
  1120. <ul>
  1121. <li>a list</li>
  1122. <li>may be a good example</li>
  1123. </ul>
  1124. EOT;
  1125. $this->assertSame(
  1126. [
  1127. 'test' => <<<'EOT'
  1128. <h2>A heading</h2>
  1129. <ul>
  1130. <li>a list</li>
  1131. <li>may be a good example</li>
  1132. </ul>
  1133. EOT
  1134. ,
  1135. ],
  1136. $this->parser->parse($yaml)
  1137. );
  1138. }
  1139. /**
  1140. * @dataProvider getBinaryData
  1141. */
  1142. public function testParseBinaryData($data)
  1143. {
  1144. $this->assertSame(['data' => 'Hello world'], $this->parser->parse($data));
  1145. }
  1146. public function getBinaryData()
  1147. {
  1148. return [
  1149. 'enclosed with double quotes' => ['data: !!binary "SGVsbG8gd29ybGQ="'],
  1150. 'enclosed with single quotes' => ["data: !!binary 'SGVsbG8gd29ybGQ='"],
  1151. 'containing spaces' => ['data: !!binary "SGVs bG8gd 29ybGQ="'],
  1152. 'in block scalar' => [
  1153. <<<'EOT'
  1154. data: !!binary |
  1155. SGVsbG8gd29ybGQ=
  1156. EOT
  1157. ],
  1158. 'containing spaces in block scalar' => [
  1159. <<<'EOT'
  1160. data: !!binary |
  1161. SGVs bG8gd 29ybGQ=
  1162. EOT
  1163. ],
  1164. ];
  1165. }
  1166. /**
  1167. * @dataProvider getInvalidBinaryData
  1168. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1169. */
  1170. public function testParseInvalidBinaryData($data, $expectedMessage)
  1171. {
  1172. if (method_exists($this, 'expectException')) {
  1173. $this->expectExceptionMessageRegExp($expectedMessage);
  1174. } else {
  1175. $this->setExpectedExceptionRegExp(ParseException::class, $expectedMessage);
  1176. }
  1177. $this->parser->parse($data);
  1178. }
  1179. public function getInvalidBinaryData()
  1180. {
  1181. return [
  1182. 'length not a multiple of four' => ['data: !!binary "SGVsbG8d29ybGQ="', '/The normalized base64 encoded data \(data without whitespace characters\) length must be a multiple of four \(\d+ bytes given\)/'],
  1183. 'invalid characters' => ['!!binary "SGVsbG8#d29ybGQ="', '/The base64 encoded data \(.*\) contains invalid characters/'],
  1184. 'too many equals characters' => ['data: !!binary "SGVsbG8gd29yb==="', '/The base64 encoded data \(.*\) contains invalid characters/'],
  1185. 'misplaced equals character' => ['data: !!binary "SGVsbG8gd29ybG=Q"', '/The base64 encoded data \(.*\) contains invalid characters/'],
  1186. 'length not a multiple of four in block scalar' => [
  1187. <<<'EOT'
  1188. data: !!binary |
  1189. SGVsbG8d29ybGQ=
  1190. EOT
  1191. ,
  1192. '/The normalized base64 encoded data \(data without whitespace characters\) length must be a multiple of four \(\d+ bytes given\)/',
  1193. ],
  1194. 'invalid characters in block scalar' => [
  1195. <<<'EOT'
  1196. data: !!binary |
  1197. SGVsbG8#d29ybGQ=
  1198. EOT
  1199. ,
  1200. '/The base64 encoded data \(.*\) contains invalid characters/',
  1201. ],
  1202. 'too many equals characters in block scalar' => [
  1203. <<<'EOT'
  1204. data: !!binary |
  1205. SGVsbG8gd29yb===
  1206. EOT
  1207. ,
  1208. '/The base64 encoded data \(.*\) contains invalid characters/',
  1209. ],
  1210. 'misplaced equals character in block scalar' => [
  1211. <<<'EOT'
  1212. data: !!binary |
  1213. SGVsbG8gd29ybG=Q
  1214. EOT
  1215. ,
  1216. '/The base64 encoded data \(.*\) contains invalid characters/',
  1217. ],
  1218. ];
  1219. }
  1220. public function testParseDateAsMappingValue()
  1221. {
  1222. $yaml = <<<'EOT'
  1223. date: 2002-12-14
  1224. EOT;
  1225. $expectedDate = new \DateTime();
  1226. $expectedDate->setTimeZone(new \DateTimeZone('UTC'));
  1227. $expectedDate->setDate(2002, 12, 14);
  1228. $expectedDate->setTime(0, 0, 0);
  1229. $this->assertEquals(['date' => $expectedDate], $this->parser->parse($yaml, Yaml::PARSE_DATETIME));
  1230. }
  1231. /**
  1232. * @param $lineNumber
  1233. * @param $yaml
  1234. * @dataProvider parserThrowsExceptionWithCorrectLineNumberProvider
  1235. */
  1236. public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml)
  1237. {
  1238. if (method_exists($this, 'expectException')) {
  1239. $this->expectException('\Symfony\Component\Yaml\Exception\ParseException');
  1240. $this->expectExceptionMessage(sprintf('Unexpected characters near "," at line %d (near "bar: "123",").', $lineNumber));
  1241. } else {
  1242. $this->setExpectedException('\Symfony\Component\Yaml\Exception\ParseException', sprintf('Unexpected characters near "," at line %d (near "bar: "123",").', $lineNumber));
  1243. }
  1244. $this->parser->parse($yaml);
  1245. }
  1246. public function parserThrowsExceptionWithCorrectLineNumberProvider()
  1247. {
  1248. return [
  1249. [
  1250. 4,
  1251. <<<'YAML'
  1252. foo:
  1253. -
  1254. # bar
  1255. bar: "123",
  1256. YAML
  1257. ],
  1258. [
  1259. 5,
  1260. <<<'YAML'
  1261. foo:
  1262. -
  1263. # bar
  1264. # bar
  1265. bar: "123",
  1266. YAML
  1267. ],
  1268. [
  1269. 8,
  1270. <<<'YAML'
  1271. foo:
  1272. -
  1273. # foobar
  1274. baz: 123
  1275. bar:
  1276. -
  1277. # bar
  1278. bar: "123",
  1279. YAML
  1280. ],
  1281. [
  1282. 10,
  1283. <<<'YAML'
  1284. foo:
  1285. -
  1286. # foobar
  1287. # foobar
  1288. baz: 123
  1289. bar:
  1290. -
  1291. # bar
  1292. # bar
  1293. bar: "123",
  1294. YAML
  1295. ],
  1296. ];
  1297. }
  1298. public function testParseMultiLineQuotedString()
  1299. {
  1300. $yaml = <<<EOT
  1301. foo: "bar
  1302. baz
  1303. foobar
  1304. foo"
  1305. bar: baz
  1306. EOT;
  1307. $this->assertSame(['foo' => 'bar baz foobar foo', 'bar' => 'baz'], $this->parser->parse($yaml));
  1308. }
  1309. public function testMultiLineQuotedStringWithTrailingBackslash()
  1310. {
  1311. $yaml = <<<YAML
  1312. foobar:
  1313. "foo\
  1314. bar"
  1315. YAML;
  1316. $this->assertSame(['foobar' => 'foobar'], $this->parser->parse($yaml));
  1317. }
  1318. public function testCommentCharactersInMultiLineQuotedStrings()
  1319. {
  1320. $yaml = <<<YAML
  1321. foo:
  1322. foobar: 'foo
  1323. #bar'
  1324. bar: baz
  1325. YAML;
  1326. $expected = [
  1327. 'foo' => [
  1328. 'foobar' => 'foo #bar',
  1329. 'bar' => 'baz',
  1330. ],
  1331. ];
  1332. $this->assertSame($expected, $this->parser->parse($yaml));
  1333. }
  1334. public function testBlankLinesInQuotedMultiLineString()
  1335. {
  1336. $yaml = <<<YAML
  1337. foobar: 'foo
  1338. bar'
  1339. YAML;
  1340. $expected = [
  1341. 'foobar' => "foo\nbar",
  1342. ];
  1343. $this->assertSame($expected, $this->parser->parse($yaml));
  1344. }
  1345. public function testParseMultiLineUnquotedString()
  1346. {
  1347. $yaml = <<<EOT
  1348. foo: bar
  1349. baz
  1350. foobar
  1351. foo
  1352. bar: baz
  1353. EOT;
  1354. $this->assertSame(['foo' => 'bar baz foobar foo', 'bar' => 'baz'], $this->parser->parse($yaml));
  1355. }
  1356. public function testParseMultiLineString()
  1357. {
  1358. $this->assertEquals("foo bar\nbaz", $this->parser->parse("foo\nbar\n\nbaz"));
  1359. }
  1360. /**
  1361. * @dataProvider multiLineDataProvider
  1362. */
  1363. public function testParseMultiLineMappingValue($yaml, $expected, $parseError)
  1364. {
  1365. $this->assertEquals($expected, $this->parser->parse($yaml));
  1366. }
  1367. public function multiLineDataProvider()
  1368. {
  1369. $tests = [];
  1370. $yaml = <<<'EOF'
  1371. foo:
  1372. - bar:
  1373. one
  1374. two
  1375. three
  1376. EOF;
  1377. $expected = [
  1378. 'foo' => [
  1379. [
  1380. 'bar' => "one\ntwo three",
  1381. ],
  1382. ],
  1383. ];
  1384. $tests[] = [$yaml, $expected, false];
  1385. $yaml = <<<'EOF'
  1386. bar
  1387. "foo"
  1388. EOF;
  1389. $expected = 'bar "foo"';
  1390. $tests[] = [$yaml, $expected, false];
  1391. $yaml = <<<'EOF'
  1392. bar
  1393. "foo
  1394. EOF;
  1395. $expected = 'bar "foo';
  1396. $tests[] = [$yaml, $expected, false];
  1397. $yaml = <<<'EOF'
  1398. bar
  1399. 'foo'
  1400. EOF;
  1401. $expected = "bar\n'foo'";
  1402. $tests[] = [$yaml, $expected, false];
  1403. $yaml = <<<'EOF'
  1404. bar
  1405. foo'
  1406. EOF;
  1407. $expected = "bar\nfoo'";
  1408. $tests[] = [$yaml, $expected, false];
  1409. return $tests;
  1410. }
  1411. public function testTaggedInlineMapping()
  1412. {
  1413. $this->assertEquals(new TaggedValue('foo', ['foo' => 'bar']), $this->parser->parse('!foo {foo: bar}', Yaml::PARSE_CUSTOM_TAGS));
  1414. }
  1415. /**
  1416. * @dataProvider taggedValuesProvider
  1417. */
  1418. public function testCustomTagSupport($expected, $yaml)
  1419. {
  1420. $this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_CUSTOM_TAGS));
  1421. }
  1422. public function taggedValuesProvider()
  1423. {
  1424. return [
  1425. 'scalars' => [
  1426. [
  1427. 'foo' => new TaggedValue('inline', 'bar'),
  1428. 'quz' => new TaggedValue('long', 'this is a long text'),
  1429. ],
  1430. <<<YAML
  1431. foo: !inline bar
  1432. quz: !long >
  1433. this is a long
  1434. text
  1435. YAML
  1436. ],
  1437. 'sequences' => [
  1438. [new TaggedValue('foo', ['yaml']), new TaggedValue('quz', ['bar'])],
  1439. <<<YAML
  1440. - !foo
  1441. - yaml
  1442. - !quz [bar]
  1443. YAML
  1444. ],
  1445. 'mappings' => [
  1446. new TaggedValue('foo', ['foo' => new TaggedValue('quz', ['bar']), 'quz' => new TaggedValue('foo', ['quz' => 'bar'])]),
  1447. <<<YAML
  1448. !foo
  1449. foo: !quz [bar]
  1450. quz: !foo
  1451. quz: bar
  1452. YAML
  1453. ],
  1454. 'inline' => [
  1455. [new TaggedValue('foo', ['foo', 'bar']), new TaggedValue('quz', ['foo' => 'bar', 'quz' => new TaggedValue('bar', ['one' => 'bar'])])],
  1456. <<<YAML
  1457. - !foo [foo, bar]
  1458. - !quz {foo: bar, quz: !bar {one: bar}}
  1459. YAML
  1460. ],
  1461. 'spaces-around-tag-value-in-sequence' => [
  1462. [new TaggedValue('foo', 'bar')],
  1463. '[ !foo bar ]',
  1464. ],
  1465. ];
  1466. }
  1467. public function testNonSpecificTagSupport()
  1468. {
  1469. $this->assertSame('12', $this->parser->parse('! 12'));
  1470. }
  1471. /**
  1472. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1473. * @expectedExceptionMessage Tags support is not enabled. Enable the "Yaml::PARSE_CUSTOM_TAGS" flag to use "!iterator" at line 1 (near "!iterator [foo]").
  1474. */
  1475. public function testCustomTagsDisabled()
  1476. {
  1477. $this->parser->parse('!iterator [foo]');
  1478. }
  1479. /**
  1480. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1481. * @expectedExceptionMessage Tags support is not enabled. Enable the "Yaml::PARSE_CUSTOM_TAGS" flag to use "!iterator" at line 1 (near "!iterator foo").
  1482. */
  1483. public function testUnsupportedTagWithScalar()
  1484. {
  1485. $this->parser->parse('!iterator foo');
  1486. }
  1487. /**
  1488. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1489. * @expectedExceptionMessage The string "!!iterator foo" could not be parsed as it uses an unsupported built-in tag at line 1 (near "!!iterator foo").
  1490. */
  1491. public function testUnsupportedBuiltInTagWithScalar()
  1492. {
  1493. $this->parser->parse('!!iterator foo');
  1494. }
  1495. /**
  1496. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1497. * @expectedExceptionMessage The built-in tag "!!foo" is not implemented at line 1 (near "!!foo").
  1498. */
  1499. public function testExceptionWhenUsingUnsuportedBuiltInTags()
  1500. {
  1501. $this->parser->parse('!!foo');
  1502. }
  1503. /**
  1504. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1505. * @expectedExceptionMessage Complex mappings are not supported at line 1 (near "? "1"").
  1506. */
  1507. public function testComplexMappingThrowsParseException()
  1508. {
  1509. $yaml = <<<YAML
  1510. ? "1"
  1511. :
  1512. name: végétalien
  1513. YAML;
  1514. $this->parser->parse($yaml);
  1515. }
  1516. /**
  1517. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1518. * @expectedExceptionMessage Complex mappings are not supported at line 2 (near "? "1"").
  1519. */
  1520. public function testComplexMappingNestedInMappingThrowsParseException()
  1521. {
  1522. $yaml = <<<YAML
  1523. diet:
  1524. ? "1"
  1525. :
  1526. name: végétalien
  1527. YAML;
  1528. $this->parser->parse($yaml);
  1529. }
  1530. /**
  1531. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1532. * @expectedExceptionMessage Complex mappings are not supported at line 1 (near "- ? "1"").
  1533. */
  1534. public function testComplexMappingNestedInSequenceThrowsParseException()
  1535. {
  1536. $yaml = <<<YAML
  1537. - ? "1"
  1538. :
  1539. name: végétalien
  1540. YAML;
  1541. $this->parser->parse($yaml);
  1542. }
  1543. /**
  1544. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1545. * @expectedExceptionMessage Unable to parse at line 1 (near "[parameters]").
  1546. */
  1547. public function testParsingIniThrowsException()
  1548. {
  1549. $ini = <<<INI
  1550. [parameters]
  1551. foo = bar
  1552. bar = %foo%
  1553. INI;
  1554. $this->parser->parse($ini);
  1555. }
  1556. private function loadTestsFromFixtureFiles($testsFile)
  1557. {
  1558. $parser = new Parser();
  1559. $tests = [];
  1560. $files = $parser->parseFile(__DIR__.'/Fixtures/'.$testsFile);
  1561. foreach ($files as $file) {
  1562. $yamls = file_get_contents(__DIR__.'/Fixtures/'.$file.'.yml');
  1563. // split YAMLs documents
  1564. foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
  1565. if (!$yaml) {
  1566. continue;
  1567. }
  1568. $test = $parser->parse($yaml);
  1569. if (isset($test['todo']) && $test['todo']) {
  1570. // TODO
  1571. } else {
  1572. eval('$expected = '.trim($test['php']).';');
  1573. $tests[] = [var_export($expected, true), $test['yaml'], $test['test']];
  1574. }
  1575. }
  1576. }
  1577. return $tests;
  1578. }
  1579. public function testCanParseVeryLongValue()
  1580. {
  1581. $longStringWithSpaces = str_repeat('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ', 20000);
  1582. $trickyVal = ['x' => $longStringWithSpaces];
  1583. $yamlString = Yaml::dump($trickyVal);
  1584. $arrayFromYaml = $this->parser->parse($yamlString);
  1585. $this->assertEquals($trickyVal, $arrayFromYaml);
  1586. }
  1587. /**
  1588. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1589. * @expectedExceptionMessage Reference "foo" does not exist at line 2
  1590. */
  1591. public function testParserCleansUpReferencesBetweenRuns()
  1592. {
  1593. $yaml = <<<YAML
  1594. foo: &foo
  1595. baz: foobar
  1596. bar:
  1597. <<: *foo
  1598. YAML;
  1599. $this->parser->parse($yaml);
  1600. $yaml = <<<YAML
  1601. bar:
  1602. <<: *foo
  1603. YAML;
  1604. $this->parser->parse($yaml);
  1605. }
  1606. public function testPhpConstantTagMappingKey()
  1607. {
  1608. $yaml = <<<YAML
  1609. transitions:
  1610. !php/const 'Symfony\Component\Yaml\Tests\B::FOO':
  1611. from:
  1612. - !php/const 'Symfony\Component\Yaml\Tests\B::BAR'
  1613. to: !php/const 'Symfony\Component\Yaml\Tests\B::BAZ'
  1614. YAML;
  1615. $expected = [
  1616. 'transitions' => [
  1617. 'foo' => [
  1618. 'from' => [
  1619. 'bar',
  1620. ],
  1621. 'to' => 'baz',
  1622. ],
  1623. ],
  1624. ];
  1625. $this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT));
  1626. }
  1627. public function testMergeKeysWhenMappingsAreParsedAsObjects()
  1628. {
  1629. $yaml = <<<YAML
  1630. foo: &FOO
  1631. bar: 1
  1632. bar: &BAR
  1633. baz: 2
  1634. <<: *FOO
  1635. baz:
  1636. baz_foo: 3
  1637. <<:
  1638. baz_bar: 4
  1639. foobar:
  1640. bar: ~
  1641. <<: [*FOO, *BAR]
  1642. YAML;
  1643. $expected = (object) [
  1644. 'foo' => (object) [
  1645. 'bar' => 1,
  1646. ],
  1647. 'bar' => (object) [
  1648. 'baz' => 2,
  1649. 'bar' => 1,
  1650. ],
  1651. 'baz' => (object) [
  1652. 'baz_foo' => 3,
  1653. 'baz_bar' => 4,
  1654. ],
  1655. 'foobar' => (object) [
  1656. 'bar' => null,
  1657. 'baz' => 2,
  1658. ],
  1659. ];
  1660. $this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
  1661. }
  1662. public function testFilenamesAreParsedAsStringsWithoutFlag()
  1663. {
  1664. $file = __DIR__.'/Fixtures/index.yml';
  1665. $this->assertSame($file, $this->parser->parse($file));
  1666. }
  1667. public function testParseFile()
  1668. {
  1669. $this->assertInternalType('array', $this->parser->parseFile(__DIR__.'/Fixtures/index.yml'));
  1670. }
  1671. /**
  1672. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1673. * @expectedExceptionMessageRegExp #^File ".+/Fixtures/nonexistent.yml" does not exist\.$#
  1674. */
  1675. public function testParsingNonExistentFilesThrowsException()
  1676. {
  1677. $this->parser->parseFile(__DIR__.'/Fixtures/nonexistent.yml');
  1678. }
  1679. /**
  1680. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1681. * @expectedExceptionMessageRegExp #^File ".+/Fixtures/not_readable.yml" cannot be read\.$#
  1682. */
  1683. public function testParsingNotReadableFilesThrowsException()
  1684. {
  1685. if ('\\' === \DIRECTORY_SEPARATOR) {
  1686. $this->markTestSkipped('chmod is not supported on Windows');
  1687. }
  1688. if (!getenv('USER') || 'root' === getenv('USER')) {
  1689. $this->markTestSkipped('This test will fail if run under superuser');
  1690. }
  1691. $file = __DIR__.'/Fixtures/not_readable.yml';
  1692. chmod($file, 0200);
  1693. $this->parser->parseFile($file);
  1694. }
  1695. public function testParseReferencesOnMergeKeys()
  1696. {
  1697. $yaml = <<<YAML
  1698. mergekeyrefdef:
  1699. a: foo
  1700. <<: &quux
  1701. b: bar
  1702. c: baz
  1703. mergekeyderef:
  1704. d: quux
  1705. <<: *quux
  1706. YAML;
  1707. $expected = [
  1708. 'mergekeyrefdef' => [
  1709. 'a' => 'foo',
  1710. 'b' => 'bar',
  1711. 'c' => 'baz',
  1712. ],
  1713. 'mergekeyderef' => [
  1714. 'd' => 'quux',
  1715. 'b' => 'bar',
  1716. 'c' => 'baz',
  1717. ],
  1718. ];
  1719. $this->assertSame($expected, $this->parser->parse($yaml));
  1720. }
  1721. public function testParseReferencesOnMergeKeysWithMappingsParsedAsObjects()
  1722. {
  1723. $yaml = <<<YAML
  1724. mergekeyrefdef:
  1725. a: foo
  1726. <<: &quux
  1727. b: bar
  1728. c: baz
  1729. mergekeyderef:
  1730. d: quux
  1731. <<: *quux
  1732. YAML;
  1733. $expected = (object) [
  1734. 'mergekeyrefdef' => (object) [
  1735. 'a' => 'foo',
  1736. 'b' => 'bar',
  1737. 'c' => 'baz',
  1738. ],
  1739. 'mergekeyderef' => (object) [
  1740. 'd' => 'quux',
  1741. 'b' => 'bar',
  1742. 'c' => 'baz',
  1743. ],
  1744. ];
  1745. $this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
  1746. }
  1747. /**
  1748. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1749. * @expectedExceptionMessage Reference "foo" does not exist
  1750. */
  1751. public function testEvalRefException()
  1752. {
  1753. $yaml = <<<EOE
  1754. foo: { &foo { a: Steve, <<: *foo} }
  1755. EOE;
  1756. $this->parser->parse($yaml);
  1757. }
  1758. /**
  1759. * @dataProvider circularReferenceProvider
  1760. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1761. * @expectedExceptionMessage Circular reference [foo, bar, foo] detected
  1762. */
  1763. public function testDetectCircularReferences($yaml)
  1764. {
  1765. $this->parser->parse($yaml, Yaml::PARSE_CUSTOM_TAGS);
  1766. }
  1767. public function circularReferenceProvider()
  1768. {
  1769. $tests = [];
  1770. $yaml = <<<YAML
  1771. foo:
  1772. - &foo
  1773. - &bar
  1774. bar: foobar
  1775. baz: *foo
  1776. YAML;
  1777. $tests['sequence'] = [$yaml];
  1778. $yaml = <<<YAML
  1779. foo: &foo
  1780. bar: &bar
  1781. foobar: baz
  1782. baz: *foo
  1783. YAML;
  1784. $tests['mapping'] = [$yaml];
  1785. $yaml = <<<YAML
  1786. foo: &foo
  1787. bar: &bar
  1788. foobar: baz
  1789. <<: *foo
  1790. YAML;
  1791. $tests['mapping with merge key'] = [$yaml];
  1792. return $tests;
  1793. }
  1794. /**
  1795. * @dataProvider indentedMappingData
  1796. */
  1797. public function testParseIndentedMappings($yaml, $expected)
  1798. {
  1799. $this->assertSame($expected, $this->parser->parse($yaml));
  1800. }
  1801. public function indentedMappingData()
  1802. {
  1803. $tests = [];
  1804. $yaml = <<<YAML
  1805. foo:
  1806. - bar: "foobar"
  1807. # A comment
  1808. baz: "foobaz"
  1809. YAML;
  1810. $expected = [
  1811. 'foo' => [
  1812. [
  1813. 'bar' => 'foobar',
  1814. 'baz' => 'foobaz',
  1815. ],
  1816. ],
  1817. ];
  1818. $tests['comment line is first line in indented block'] = [$yaml, $expected];
  1819. $yaml = <<<YAML
  1820. foo:
  1821. - bar:
  1822. # comment
  1823. baz: [1, 2, 3]
  1824. YAML;
  1825. $expected = [
  1826. 'foo' => [
  1827. [
  1828. 'bar' => [
  1829. 'baz' => [1, 2, 3],
  1830. ],
  1831. ],
  1832. ],
  1833. ];
  1834. $tests['mapping value on new line starting with a comment line'] = [$yaml, $expected];
  1835. $yaml = <<<YAML
  1836. foo:
  1837. -
  1838. bar: foobar
  1839. YAML;
  1840. $expected = [
  1841. 'foo' => [
  1842. [
  1843. 'bar' => 'foobar',
  1844. ],
  1845. ],
  1846. ];
  1847. $tests['mapping in sequence starting on a new line'] = [$yaml, $expected];
  1848. $yaml = <<<YAML
  1849. foo:
  1850. bar: baz
  1851. YAML;
  1852. $expected = [
  1853. 'foo' => [
  1854. 'bar' => 'baz',
  1855. ],
  1856. ];
  1857. $tests['blank line at the beginning of an indented mapping value'] = [$yaml, $expected];
  1858. return $tests;
  1859. }
  1860. }
  1861. class B
  1862. {
  1863. public $b = 'foo';
  1864. const FOO = 'foo';
  1865. const BAR = 'bar';
  1866. const BAZ = 'baz';
  1867. }