ParserTest.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104
  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\Yaml;
  14. use Symfony\Component\Yaml\Parser;
  15. use Symfony\Component\Yaml\Tag\TaggedValue;
  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 = array(
  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 = array();
  75. $yaml = <<<'EOF'
  76. foo: |-
  77. one
  78. two
  79. bar: |-
  80. one
  81. two
  82. EOF;
  83. $expected = array(
  84. 'foo' => "one\ntwo",
  85. 'bar' => "one\ntwo",
  86. );
  87. $tests['Literal block chomping strip with single trailing newline'] = array($expected, $yaml);
  88. $yaml = <<<'EOF'
  89. foo: |-
  90. one
  91. two
  92. bar: |-
  93. one
  94. two
  95. EOF;
  96. $expected = array(
  97. 'foo' => "one\ntwo",
  98. 'bar' => "one\ntwo",
  99. );
  100. $tests['Literal block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
  101. $yaml = <<<'EOF'
  102. {}
  103. EOF;
  104. $expected = array();
  105. $tests['Literal block chomping strip with multiple trailing newlines after a 1-liner'] = array($expected, $yaml);
  106. $yaml = <<<'EOF'
  107. foo: |-
  108. one
  109. two
  110. bar: |-
  111. one
  112. two
  113. EOF;
  114. $expected = array(
  115. 'foo' => "one\ntwo",
  116. 'bar' => "one\ntwo",
  117. );
  118. $tests['Literal block chomping strip without trailing newline'] = array($expected, $yaml);
  119. $yaml = <<<'EOF'
  120. foo: |
  121. one
  122. two
  123. bar: |
  124. one
  125. two
  126. EOF;
  127. $expected = array(
  128. 'foo' => "one\ntwo\n",
  129. 'bar' => "one\ntwo\n",
  130. );
  131. $tests['Literal block chomping clip with single trailing newline'] = array($expected, $yaml);
  132. $yaml = <<<'EOF'
  133. foo: |
  134. one
  135. two
  136. bar: |
  137. one
  138. two
  139. EOF;
  140. $expected = array(
  141. 'foo' => "one\ntwo\n",
  142. 'bar' => "one\ntwo\n",
  143. );
  144. $tests['Literal block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
  145. $yaml = <<<'EOF'
  146. foo:
  147. - bar: |
  148. one
  149. two
  150. EOF;
  151. $expected = array(
  152. 'foo' => array(
  153. array(
  154. 'bar' => "one\n\ntwo",
  155. ),
  156. ),
  157. );
  158. $tests['Literal block chomping clip with embedded blank line inside unindented collection'] = array($expected, $yaml);
  159. $yaml = <<<'EOF'
  160. foo: |
  161. one
  162. two
  163. bar: |
  164. one
  165. two
  166. EOF;
  167. $expected = array(
  168. 'foo' => "one\ntwo\n",
  169. 'bar' => "one\ntwo",
  170. );
  171. $tests['Literal block chomping clip without trailing newline'] = array($expected, $yaml);
  172. $yaml = <<<'EOF'
  173. foo: |+
  174. one
  175. two
  176. bar: |+
  177. one
  178. two
  179. EOF;
  180. $expected = array(
  181. 'foo' => "one\ntwo\n",
  182. 'bar' => "one\ntwo\n",
  183. );
  184. $tests['Literal block chomping keep with single trailing newline'] = array($expected, $yaml);
  185. $yaml = <<<'EOF'
  186. foo: |+
  187. one
  188. two
  189. bar: |+
  190. one
  191. two
  192. EOF;
  193. $expected = array(
  194. 'foo' => "one\ntwo\n\n",
  195. 'bar' => "one\ntwo\n\n",
  196. );
  197. $tests['Literal block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
  198. $yaml = <<<'EOF'
  199. foo: |+
  200. one
  201. two
  202. bar: |+
  203. one
  204. two
  205. EOF;
  206. $expected = array(
  207. 'foo' => "one\ntwo\n",
  208. 'bar' => "one\ntwo",
  209. );
  210. $tests['Literal block chomping keep without trailing newline'] = array($expected, $yaml);
  211. $yaml = <<<'EOF'
  212. foo: >-
  213. one
  214. two
  215. bar: >-
  216. one
  217. two
  218. EOF;
  219. $expected = array(
  220. 'foo' => 'one two',
  221. 'bar' => 'one two',
  222. );
  223. $tests['Folded block chomping strip with single trailing newline'] = array($expected, $yaml);
  224. $yaml = <<<'EOF'
  225. foo: >-
  226. one
  227. two
  228. bar: >-
  229. one
  230. two
  231. EOF;
  232. $expected = array(
  233. 'foo' => 'one two',
  234. 'bar' => 'one two',
  235. );
  236. $tests['Folded block chomping strip with multiple trailing newlines'] = array($expected, $yaml);
  237. $yaml = <<<'EOF'
  238. foo: >-
  239. one
  240. two
  241. bar: >-
  242. one
  243. two
  244. EOF;
  245. $expected = array(
  246. 'foo' => 'one two',
  247. 'bar' => 'one two',
  248. );
  249. $tests['Folded block chomping strip without trailing newline'] = array($expected, $yaml);
  250. $yaml = <<<'EOF'
  251. foo: >
  252. one
  253. two
  254. bar: >
  255. one
  256. two
  257. EOF;
  258. $expected = array(
  259. 'foo' => "one two\n",
  260. 'bar' => "one two\n",
  261. );
  262. $tests['Folded block chomping clip with single trailing newline'] = array($expected, $yaml);
  263. $yaml = <<<'EOF'
  264. foo: >
  265. one
  266. two
  267. bar: >
  268. one
  269. two
  270. EOF;
  271. $expected = array(
  272. 'foo' => "one two\n",
  273. 'bar' => "one two\n",
  274. );
  275. $tests['Folded block chomping clip with multiple trailing newlines'] = array($expected, $yaml);
  276. $yaml = <<<'EOF'
  277. foo: >
  278. one
  279. two
  280. bar: >
  281. one
  282. two
  283. EOF;
  284. $expected = array(
  285. 'foo' => "one two\n",
  286. 'bar' => 'one two',
  287. );
  288. $tests['Folded block chomping clip without trailing newline'] = array($expected, $yaml);
  289. $yaml = <<<'EOF'
  290. foo: >+
  291. one
  292. two
  293. bar: >+
  294. one
  295. two
  296. EOF;
  297. $expected = array(
  298. 'foo' => "one two\n",
  299. 'bar' => "one two\n",
  300. );
  301. $tests['Folded block chomping keep with single trailing newline'] = array($expected, $yaml);
  302. $yaml = <<<'EOF'
  303. foo: >+
  304. one
  305. two
  306. bar: >+
  307. one
  308. two
  309. EOF;
  310. $expected = array(
  311. 'foo' => "one two\n\n",
  312. 'bar' => "one two\n\n",
  313. );
  314. $tests['Folded block chomping keep with multiple trailing newlines'] = array($expected, $yaml);
  315. $yaml = <<<'EOF'
  316. foo: >+
  317. one
  318. two
  319. bar: >+
  320. one
  321. two
  322. EOF;
  323. $expected = array(
  324. 'foo' => "one two\n",
  325. 'bar' => 'one two',
  326. );
  327. $tests['Folded block chomping keep without trailing newline'] = array($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 = array(
  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(array('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(array('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 = array();
  380. $yaml = <<<'EOF'
  381. foo:
  382. fiz: [cat]
  383. EOF;
  384. $expected = new \stdClass();
  385. $expected->foo = new \stdClass();
  386. $expected->foo->fiz = array('cat');
  387. $tests['mapping'] = array($yaml, $expected);
  388. $yaml = '{ "foo": "bar", "fiz": "cat" }';
  389. $expected = new \stdClass();
  390. $expected->foo = 'bar';
  391. $expected->fiz = 'cat';
  392. $tests['inline-mapping'] = array($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'] = array($yaml, $expected);
  398. $yaml = <<<'EOT'
  399. array:
  400. - key: one
  401. - key: two
  402. EOT;
  403. $expected = new \stdClass();
  404. $expected->array = 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'] = array($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'] = array($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'] = array($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 = array(
  447. 'items' => array('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 = array(
  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 = array(
  538. 'a' => array(
  539. array(
  540. 'b' => array(
  541. array(
  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 = array(
  562. 'a' => array(
  563. 'b' => array('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 = array(
  581. 'a' => array(
  582. 'b' => array(),
  583. 'd' => 1.1,
  584. ),
  585. );
  586. $this->assertSame($expected, $this->parser->parse($yaml));
  587. }
  588. public function getParseExceptionNotAffectedMultiLineStringLastResortParsing()
  589. {
  590. $tests = array();
  591. $yaml = <<<'EOT'
  592. a
  593. b:
  594. EOT;
  595. $tests['parse error on first line'] = array($yaml);
  596. $yaml = <<<'EOT'
  597. a
  598. b
  599. c:
  600. EOT;
  601. $tests['parse error due to inconsistent indentation'] = array($yaml);
  602. $yaml = <<<'EOT'
  603. & * ! | > ' " % @ ` #, { asd a;sdasd }-@^qw3
  604. EOT;
  605. $tests['symfony/symfony/issues/22967#issuecomment-322067742'] = array($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 = array(
  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 = array(
  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 = array(
  688. 'parent' => array(
  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 = array(
  705. 'parent' => array(
  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 = array();
  723. $yaml = <<<EOD
  724. parent: { child: first, child: duplicate }
  725. EOD;
  726. $tests[] = array($yaml, 'child', 1);
  727. $yaml = <<<EOD
  728. parent:
  729. child: first,
  730. child: duplicate
  731. EOD;
  732. $tests[] = array($yaml, 'child', 3);
  733. $yaml = <<<EOD
  734. parent: { child: foo }
  735. parent: { child: bar }
  736. EOD;
  737. $tests[] = array($yaml, 'parent', 2);
  738. $yaml = <<<EOD
  739. parent: { child_mapping: { value: bar}, child_mapping: { value: bar} }
  740. EOD;
  741. $tests[] = array($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[] = array($yaml, 'child_mapping', 4);
  750. $yaml = <<<EOD
  751. parent: { child_sequence: ['key1', 'key2', 'key3'], child_sequence: ['key1', 'key2', 'key3'] }
  752. EOD;
  753. $tests[] = array($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[] = array($yaml, 'child_sequence', 6);
  766. return $tests;
  767. }
  768. public function testEmptyValue()
  769. {
  770. $input = <<<'EOF'
  771. hash:
  772. EOF;
  773. $this->assertEquals(array('hash' => null), Yaml::parse($input));
  774. }
  775. public function testCommentAtTheRootIndent()
  776. {
  777. $this->assertEquals(array(
  778. 'services' => array(
  779. 'app.foo_service' => array(
  780. 'class' => 'Foo',
  781. ),
  782. 'app/bar_service' => array(
  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(array('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(array(array('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(array(array(
  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(array(
  877. 'var' => 'var-value',
  878. 'scalar' => 'var-value',
  879. 'list' => array('var-value'),
  880. 'list_in_list' => array(array('var-value')),
  881. 'map_in_list' => array(array('key' => 'var-value')),
  882. 'embedded_mapping' => array(array('key' => 'var-value')),
  883. 'map' => array('key' => 'var-value'),
  884. 'list_in_map' => array('key' => array('var-value')),
  885. 'map_in_map' => array('foo' => array('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(array('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 = array(
  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(array('foo' => array('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 = array();
  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 = array(
  997. 'pages' => array(
  998. array(
  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[] = array($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 = array(
  1030. 'test' => <<<'EOT'
  1031. foo
  1032. # bar
  1033. baz
  1034. EOT
  1035. ,
  1036. 'collection' => array(
  1037. array(
  1038. 'one' => <<<'EOT'
  1039. foo
  1040. # bar
  1041. baz
  1042. EOT
  1043. ,
  1044. ),
  1045. array(
  1046. 'two' => <<<'EOT'
  1047. foo
  1048. # bar
  1049. baz
  1050. EOT
  1051. ,
  1052. ),
  1053. ),
  1054. );
  1055. $tests[] = array($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 = array(
  1067. 'foo' => array(
  1068. 'bar' => array(
  1069. 'scalar-block' => "line1 line2>\n",
  1070. ),
  1071. 'baz' => array(
  1072. 'foobar' => null,
  1073. ),
  1074. ),
  1075. );
  1076. $tests[] = array($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 = array(
  1086. 'a' => array(
  1087. 'b' => 'hello',
  1088. 'd' => 'hello',
  1089. ),
  1090. );
  1091. $tests[] = array($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. array(
  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. array(
  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(array('data' => 'Hello world'), $this->parser->parse($data));
  1145. }
  1146. public function getBinaryData()
  1147. {
  1148. return array(
  1149. 'enclosed with double quotes' => array('data: !!binary "SGVsbG8gd29ybGQ="'),
  1150. 'enclosed with single quotes' => array("data: !!binary 'SGVsbG8gd29ybGQ='"),
  1151. 'containing spaces' => array('data: !!binary "SGVs bG8gd 29ybGQ="'),
  1152. 'in block scalar' => array(
  1153. <<<'EOT'
  1154. data: !!binary |
  1155. SGVsbG8gd29ybGQ=
  1156. EOT
  1157. ),
  1158. 'containing spaces in block scalar' => array(
  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 array(
  1182. 'length not a multiple of four' => array('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' => array('!!binary "SGVsbG8#d29ybGQ="', '/The base64 encoded data \(.*\) contains invalid characters/'),
  1184. 'too many equals characters' => array('data: !!binary "SGVsbG8gd29yb==="', '/The base64 encoded data \(.*\) contains invalid characters/'),
  1185. 'misplaced equals character' => array('data: !!binary "SGVsbG8gd29ybG=Q"', '/The base64 encoded data \(.*\) contains invalid characters/'),
  1186. 'length not a multiple of four in block scalar' => array(
  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' => array(
  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' => array(
  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' => array(
  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(array('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 array(
  1249. array(
  1250. 4,
  1251. <<<'YAML'
  1252. foo:
  1253. -
  1254. # bar
  1255. bar: "123",
  1256. YAML
  1257. ),
  1258. array(
  1259. 5,
  1260. <<<'YAML'
  1261. foo:
  1262. -
  1263. # bar
  1264. # bar
  1265. bar: "123",
  1266. YAML
  1267. ),
  1268. array(
  1269. 8,
  1270. <<<'YAML'
  1271. foo:
  1272. -
  1273. # foobar
  1274. baz: 123
  1275. bar:
  1276. -
  1277. # bar
  1278. bar: "123",
  1279. YAML
  1280. ),
  1281. array(
  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(array('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(array('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 = array(
  1327. 'foo' => array(
  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 = array(
  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(array('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 = array();
  1370. $yaml = <<<'EOF'
  1371. foo:
  1372. - bar:
  1373. one
  1374. two
  1375. three
  1376. EOF;
  1377. $expected = array(
  1378. 'foo' => array(
  1379. array(
  1380. 'bar' => "one\ntwo three",
  1381. ),
  1382. ),
  1383. );
  1384. $tests[] = array($yaml, $expected, false);
  1385. $yaml = <<<'EOF'
  1386. bar
  1387. "foo"
  1388. EOF;
  1389. $expected = 'bar "foo"';
  1390. $tests[] = array($yaml, $expected, false);
  1391. $yaml = <<<'EOF'
  1392. bar
  1393. "foo
  1394. EOF;
  1395. $expected = 'bar "foo';
  1396. $tests[] = array($yaml, $expected, false);
  1397. $yaml = <<<'EOF'
  1398. bar
  1399. 'foo'
  1400. EOF;
  1401. $expected = "bar\n'foo'";
  1402. $tests[] = array($yaml, $expected, false);
  1403. $yaml = <<<'EOF'
  1404. bar
  1405. foo'
  1406. EOF;
  1407. $expected = "bar\nfoo'";
  1408. $tests[] = array($yaml, $expected, false);
  1409. return $tests;
  1410. }
  1411. public function testTaggedInlineMapping()
  1412. {
  1413. $this->assertEquals(new TaggedValue('foo', array('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 array(
  1425. 'scalars' => array(
  1426. array(
  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' => array(
  1438. array(new TaggedValue('foo', array('yaml')), new TaggedValue('quz', array('bar'))),
  1439. <<<YAML
  1440. - !foo
  1441. - yaml
  1442. - !quz [bar]
  1443. YAML
  1444. ),
  1445. 'mappings' => array(
  1446. new TaggedValue('foo', array('foo' => new TaggedValue('quz', array('bar')), 'quz' => new TaggedValue('foo', array('quz' => 'bar')))),
  1447. <<<YAML
  1448. !foo
  1449. foo: !quz [bar]
  1450. quz: !foo
  1451. quz: bar
  1452. YAML
  1453. ),
  1454. 'inline' => array(
  1455. array(new TaggedValue('foo', array('foo', 'bar')), new TaggedValue('quz', array('foo' => 'bar', 'quz' => new TaggedValue('bar', array('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' => array(
  1462. array(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 = array();
  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[] = array(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 = array('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 = array(
  1616. 'transitions' => array(
  1617. 'foo' => array(
  1618. 'from' => array(
  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) array(
  1644. 'foo' => (object) array(
  1645. 'bar' => 1,
  1646. ),
  1647. 'bar' => (object) array(
  1648. 'baz' => 2,
  1649. 'bar' => 1,
  1650. ),
  1651. 'baz' => (object) array(
  1652. 'baz_foo' => 3,
  1653. 'baz_bar' => 4,
  1654. ),
  1655. 'foobar' => (object) array(
  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. $file = __DIR__.'/Fixtures/not_readable.yml';
  1689. chmod($file, 0200);
  1690. $this->parser->parseFile($file);
  1691. }
  1692. public function testParseReferencesOnMergeKeys()
  1693. {
  1694. $yaml = <<<YAML
  1695. mergekeyrefdef:
  1696. a: foo
  1697. <<: &quux
  1698. b: bar
  1699. c: baz
  1700. mergekeyderef:
  1701. d: quux
  1702. <<: *quux
  1703. YAML;
  1704. $expected = array(
  1705. 'mergekeyrefdef' => array(
  1706. 'a' => 'foo',
  1707. 'b' => 'bar',
  1708. 'c' => 'baz',
  1709. ),
  1710. 'mergekeyderef' => array(
  1711. 'd' => 'quux',
  1712. 'b' => 'bar',
  1713. 'c' => 'baz',
  1714. ),
  1715. );
  1716. $this->assertSame($expected, $this->parser->parse($yaml));
  1717. }
  1718. public function testParseReferencesOnMergeKeysWithMappingsParsedAsObjects()
  1719. {
  1720. $yaml = <<<YAML
  1721. mergekeyrefdef:
  1722. a: foo
  1723. <<: &quux
  1724. b: bar
  1725. c: baz
  1726. mergekeyderef:
  1727. d: quux
  1728. <<: *quux
  1729. YAML;
  1730. $expected = (object) array(
  1731. 'mergekeyrefdef' => (object) array(
  1732. 'a' => 'foo',
  1733. 'b' => 'bar',
  1734. 'c' => 'baz',
  1735. ),
  1736. 'mergekeyderef' => (object) array(
  1737. 'd' => 'quux',
  1738. 'b' => 'bar',
  1739. 'c' => 'baz',
  1740. ),
  1741. );
  1742. $this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
  1743. }
  1744. /**
  1745. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1746. * @expectedExceptionMessage Reference "foo" does not exist
  1747. */
  1748. public function testEvalRefException()
  1749. {
  1750. $yaml = <<<EOE
  1751. foo: { &foo { a: Steve, <<: *foo} }
  1752. EOE;
  1753. $this->parser->parse($yaml);
  1754. }
  1755. /**
  1756. * @dataProvider indentedMappingData
  1757. */
  1758. public function testParseIndentedMappings($yaml, $expected)
  1759. {
  1760. $this->assertSame($expected, $this->parser->parse($yaml));
  1761. }
  1762. public function indentedMappingData()
  1763. {
  1764. $tests = array();
  1765. $yaml = <<<YAML
  1766. foo:
  1767. - bar: "foobar"
  1768. # A comment
  1769. baz: "foobaz"
  1770. YAML;
  1771. $expected = array(
  1772. 'foo' => array(
  1773. array(
  1774. 'bar' => 'foobar',
  1775. 'baz' => 'foobaz',
  1776. ),
  1777. ),
  1778. );
  1779. $tests['comment line is first line in indented block'] = array($yaml, $expected);
  1780. $yaml = <<<YAML
  1781. foo:
  1782. - bar:
  1783. # comment
  1784. baz: [1, 2, 3]
  1785. YAML;
  1786. $expected = array(
  1787. 'foo' => array(
  1788. array(
  1789. 'bar' => array(
  1790. 'baz' => array(1, 2, 3),
  1791. ),
  1792. ),
  1793. ),
  1794. );
  1795. $tests['mapping value on new line starting with a comment line'] = array($yaml, $expected);
  1796. $yaml = <<<YAML
  1797. foo:
  1798. -
  1799. bar: foobar
  1800. YAML;
  1801. $expected = array(
  1802. 'foo' => array(
  1803. array(
  1804. 'bar' => 'foobar',
  1805. ),
  1806. ),
  1807. );
  1808. $tests['mapping in sequence starting on a new line'] = array($yaml, $expected);
  1809. $yaml = <<<YAML
  1810. foo:
  1811. bar: baz
  1812. YAML;
  1813. $expected = array(
  1814. 'foo' => array(
  1815. 'bar' => 'baz',
  1816. ),
  1817. );
  1818. $tests['blank line at the beginning of an indented mapping value'] = array($yaml, $expected);
  1819. return $tests;
  1820. }
  1821. }
  1822. class B
  1823. {
  1824. public $b = 'foo';
  1825. const FOO = 'foo';
  1826. const BAR = 'bar';
  1827. const BAZ = 'baz';
  1828. }