ParserTest.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165
  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. /**
  444. * @group legacy
  445. * @expectedDeprecation Support for mapping keys in multi-line blocks is deprecated since Symfony 4.3 and will throw a ParseException in 5.0.
  446. */
  447. public function testMappingKeyInMultiLineStringTriggersDeprecationNotice()
  448. {
  449. $yaml = <<<'EOF'
  450. data:
  451. dbal:wrong
  452. default_connection: monolith
  453. EOF;
  454. $this->parser->parse($yaml);
  455. }
  456. public function testCanParseContentWithTrailingSpaces()
  457. {
  458. $yaml = "items: \n foo: bar";
  459. $expected = [
  460. 'items' => ['foo' => 'bar'],
  461. ];
  462. $this->assertSame($expected, $this->parser->parse($yaml));
  463. }
  464. /**
  465. * @requires extension iconv
  466. */
  467. public function testNonUtf8Exception()
  468. {
  469. $yamls = [
  470. iconv('UTF-8', 'ISO-8859-1', "foo: 'äöüß'"),
  471. iconv('UTF-8', 'ISO-8859-15', "euro: '€'"),
  472. iconv('UTF-8', 'CP1252', "cp1252: '©ÉÇáñ'"),
  473. ];
  474. foreach ($yamls as $yaml) {
  475. try {
  476. $this->parser->parse($yaml);
  477. $this->fail('charsets other than UTF-8 are rejected.');
  478. } catch (\Exception $e) {
  479. $this->assertInstanceOf('Symfony\Component\Yaml\Exception\ParseException', $e, 'charsets other than UTF-8 are rejected.');
  480. }
  481. }
  482. }
  483. /**
  484. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  485. */
  486. public function testUnindentedCollectionException()
  487. {
  488. $yaml = <<<'EOF'
  489. collection:
  490. -item1
  491. -item2
  492. -item3
  493. EOF;
  494. $this->parser->parse($yaml);
  495. }
  496. /**
  497. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  498. */
  499. public function testShortcutKeyUnindentedCollectionException()
  500. {
  501. $yaml = <<<'EOF'
  502. collection:
  503. - key: foo
  504. foo: bar
  505. EOF;
  506. $this->parser->parse($yaml);
  507. }
  508. /**
  509. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  510. * @expectedExceptionMessageRegExp /^Multiple documents are not supported.+/
  511. */
  512. public function testMultipleDocumentsNotSupportedException()
  513. {
  514. Yaml::parse(<<<'EOL'
  515. # Ranking of 1998 home runs
  516. ---
  517. - Mark McGwire
  518. - Sammy Sosa
  519. - Ken Griffey
  520. # Team ranking
  521. ---
  522. - Chicago Cubs
  523. - St Louis Cardinals
  524. EOL
  525. );
  526. }
  527. /**
  528. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  529. */
  530. public function testSequenceInAMapping()
  531. {
  532. Yaml::parse(<<<'EOF'
  533. yaml:
  534. hash: me
  535. - array stuff
  536. EOF
  537. );
  538. }
  539. public function testSequenceInMappingStartedBySingleDashLine()
  540. {
  541. $yaml = <<<'EOT'
  542. a:
  543. -
  544. b:
  545. -
  546. bar: baz
  547. - foo
  548. d: e
  549. EOT;
  550. $expected = [
  551. 'a' => [
  552. [
  553. 'b' => [
  554. [
  555. 'bar' => 'baz',
  556. ],
  557. ],
  558. ],
  559. 'foo',
  560. ],
  561. 'd' => 'e',
  562. ];
  563. $this->assertSame($expected, $this->parser->parse($yaml));
  564. }
  565. public function testSequenceFollowedByCommentEmbeddedInMapping()
  566. {
  567. $yaml = <<<'EOT'
  568. a:
  569. b:
  570. - c
  571. # comment
  572. d: e
  573. EOT;
  574. $expected = [
  575. 'a' => [
  576. 'b' => ['c'],
  577. 'd' => 'e',
  578. ],
  579. ];
  580. $this->assertSame($expected, $this->parser->parse($yaml));
  581. }
  582. public function testNonStringFollowedByCommentEmbeddedInMapping()
  583. {
  584. $yaml = <<<'EOT'
  585. a:
  586. b:
  587. {}
  588. # comment
  589. d:
  590. 1.1
  591. # another comment
  592. EOT;
  593. $expected = [
  594. 'a' => [
  595. 'b' => [],
  596. 'd' => 1.1,
  597. ],
  598. ];
  599. $this->assertSame($expected, $this->parser->parse($yaml));
  600. }
  601. public function getParseExceptionNotAffectedMultiLineStringLastResortParsing()
  602. {
  603. $tests = [];
  604. $yaml = <<<'EOT'
  605. a
  606. b:
  607. EOT;
  608. $tests['parse error on first line'] = [$yaml];
  609. $yaml = <<<'EOT'
  610. a
  611. b
  612. c:
  613. EOT;
  614. $tests['parse error due to inconsistent indentation'] = [$yaml];
  615. $yaml = <<<'EOT'
  616. & * ! | > ' " % @ ` #, { asd a;sdasd }-@^qw3
  617. EOT;
  618. $tests['symfony/symfony/issues/22967#issuecomment-322067742'] = [$yaml];
  619. return $tests;
  620. }
  621. /**
  622. * @dataProvider getParseExceptionNotAffectedMultiLineStringLastResortParsing
  623. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  624. */
  625. public function testParseExceptionNotAffectedByMultiLineStringLastResortParsing($yaml)
  626. {
  627. $this->parser->parse($yaml);
  628. }
  629. public function testMultiLineStringLastResortParsing()
  630. {
  631. $yaml = <<<'EOT'
  632. test:
  633. You can have things that don't look like strings here
  634. true
  635. yes you can
  636. EOT;
  637. $expected = [
  638. 'test' => 'You can have things that don\'t look like strings here true yes you can',
  639. ];
  640. $this->assertSame($expected, $this->parser->parse($yaml));
  641. $yaml = <<<'EOT'
  642. a:
  643. b
  644. c
  645. EOT;
  646. $expected = [
  647. 'a' => 'b c',
  648. ];
  649. $this->assertSame($expected, $this->parser->parse($yaml));
  650. }
  651. /**
  652. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  653. */
  654. public function testMappingInASequence()
  655. {
  656. Yaml::parse(<<<'EOF'
  657. yaml:
  658. - array stuff
  659. hash: me
  660. EOF
  661. );
  662. }
  663. /**
  664. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  665. * @expectedExceptionMessage missing colon
  666. */
  667. public function testScalarInSequence()
  668. {
  669. Yaml::parse(<<<'EOF'
  670. foo:
  671. - bar
  672. "missing colon"
  673. foo: bar
  674. EOF
  675. );
  676. }
  677. /**
  678. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  679. * @expectedExceptionMessage Duplicate key "child" detected
  680. *
  681. * > It is an error for two equal keys to appear in the same mapping node.
  682. * > In such a case the YAML processor may continue, ignoring the second
  683. * > "key: value" pair and issuing an appropriate warning. This strategy
  684. * > preserves a consistent information model for one-pass and random access
  685. * > applications.
  686. *
  687. * @see http://yaml.org/spec/1.2/spec.html#id2759572
  688. * @see http://yaml.org/spec/1.1/#id932806
  689. */
  690. public function testMappingDuplicateKeyBlock()
  691. {
  692. $input = <<<'EOD'
  693. parent:
  694. child: first
  695. child: duplicate
  696. parent:
  697. child: duplicate
  698. child: duplicate
  699. EOD;
  700. $expected = [
  701. 'parent' => [
  702. 'child' => 'first',
  703. ],
  704. ];
  705. $this->assertSame($expected, Yaml::parse($input));
  706. }
  707. /**
  708. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  709. * @expectedExceptionMessage Duplicate key "child" detected
  710. */
  711. public function testMappingDuplicateKeyFlow()
  712. {
  713. $input = <<<'EOD'
  714. parent: { child: first, child: duplicate }
  715. parent: { child: duplicate, child: duplicate }
  716. EOD;
  717. $expected = [
  718. 'parent' => [
  719. 'child' => 'first',
  720. ],
  721. ];
  722. $this->assertSame($expected, Yaml::parse($input));
  723. }
  724. /**
  725. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  726. * @dataProvider getParseExceptionOnDuplicateData
  727. */
  728. public function testParseExceptionOnDuplicate($input, $duplicateKey, $lineNumber)
  729. {
  730. $this->expectExceptionMessage(sprintf('Duplicate key "%s" detected at line %d', $duplicateKey, $lineNumber));
  731. Yaml::parse($input);
  732. }
  733. public function getParseExceptionOnDuplicateData()
  734. {
  735. $tests = [];
  736. $yaml = <<<EOD
  737. parent: { child: first, child: duplicate }
  738. EOD;
  739. $tests[] = [$yaml, 'child', 1];
  740. $yaml = <<<EOD
  741. parent:
  742. child: first,
  743. child: duplicate
  744. EOD;
  745. $tests[] = [$yaml, 'child', 3];
  746. $yaml = <<<EOD
  747. parent: { child: foo }
  748. parent: { child: bar }
  749. EOD;
  750. $tests[] = [$yaml, 'parent', 2];
  751. $yaml = <<<EOD
  752. parent: { child_mapping: { value: bar}, child_mapping: { value: bar} }
  753. EOD;
  754. $tests[] = [$yaml, 'child_mapping', 1];
  755. $yaml = <<<EOD
  756. parent:
  757. child_mapping:
  758. value: bar
  759. child_mapping:
  760. value: bar
  761. EOD;
  762. $tests[] = [$yaml, 'child_mapping', 4];
  763. $yaml = <<<EOD
  764. parent: { child_sequence: ['key1', 'key2', 'key3'], child_sequence: ['key1', 'key2', 'key3'] }
  765. EOD;
  766. $tests[] = [$yaml, 'child_sequence', 1];
  767. $yaml = <<<EOD
  768. parent:
  769. child_sequence:
  770. - key1
  771. - key2
  772. - key3
  773. child_sequence:
  774. - key1
  775. - key2
  776. - key3
  777. EOD;
  778. $tests[] = [$yaml, 'child_sequence', 6];
  779. return $tests;
  780. }
  781. public function testEmptyValue()
  782. {
  783. $input = <<<'EOF'
  784. hash:
  785. EOF;
  786. $this->assertEquals(['hash' => null], Yaml::parse($input));
  787. }
  788. public function testCommentAtTheRootIndent()
  789. {
  790. $this->assertEquals([
  791. 'services' => [
  792. 'app.foo_service' => [
  793. 'class' => 'Foo',
  794. ],
  795. 'app/bar_service' => [
  796. 'class' => 'Bar',
  797. ],
  798. ],
  799. ], Yaml::parse(<<<'EOF'
  800. # comment 1
  801. services:
  802. # comment 2
  803. # comment 3
  804. app.foo_service:
  805. class: Foo
  806. # comment 4
  807. # comment 5
  808. app/bar_service:
  809. class: Bar
  810. EOF
  811. ));
  812. }
  813. public function testStringBlockWithComments()
  814. {
  815. $this->assertEquals(['content' => <<<'EOT'
  816. # comment 1
  817. header
  818. # comment 2
  819. <body>
  820. <h1>title</h1>
  821. </body>
  822. footer # comment3
  823. EOT
  824. ], Yaml::parse(<<<'EOF'
  825. content: |
  826. # comment 1
  827. header
  828. # comment 2
  829. <body>
  830. <h1>title</h1>
  831. </body>
  832. footer # comment3
  833. EOF
  834. ));
  835. }
  836. public function testFoldedStringBlockWithComments()
  837. {
  838. $this->assertEquals([['content' => <<<'EOT'
  839. # comment 1
  840. header
  841. # comment 2
  842. <body>
  843. <h1>title</h1>
  844. </body>
  845. footer # comment3
  846. EOT
  847. ]], Yaml::parse(<<<'EOF'
  848. -
  849. content: |
  850. # comment 1
  851. header
  852. # comment 2
  853. <body>
  854. <h1>title</h1>
  855. </body>
  856. footer # comment3
  857. EOF
  858. ));
  859. }
  860. public function testNestedFoldedStringBlockWithComments()
  861. {
  862. $this->assertEquals([[
  863. 'title' => 'some title',
  864. 'content' => <<<'EOT'
  865. # comment 1
  866. header
  867. # comment 2
  868. <body>
  869. <h1>title</h1>
  870. </body>
  871. footer # comment3
  872. EOT
  873. ]], Yaml::parse(<<<'EOF'
  874. -
  875. title: some title
  876. content: |
  877. # comment 1
  878. header
  879. # comment 2
  880. <body>
  881. <h1>title</h1>
  882. </body>
  883. footer # comment3
  884. EOF
  885. ));
  886. }
  887. public function testReferenceResolvingInInlineStrings()
  888. {
  889. $this->assertEquals([
  890. 'var' => 'var-value',
  891. 'scalar' => 'var-value',
  892. 'list' => ['var-value'],
  893. 'list_in_list' => [['var-value']],
  894. 'map_in_list' => [['key' => 'var-value']],
  895. 'embedded_mapping' => [['key' => 'var-value']],
  896. 'map' => ['key' => 'var-value'],
  897. 'list_in_map' => ['key' => ['var-value']],
  898. 'map_in_map' => ['foo' => ['bar' => 'var-value']],
  899. ], Yaml::parse(<<<'EOF'
  900. var: &var var-value
  901. scalar: *var
  902. list: [ *var ]
  903. list_in_list: [[ *var ]]
  904. map_in_list: [ { key: *var } ]
  905. embedded_mapping: [ key: *var ]
  906. map: { key: *var }
  907. list_in_map: { key: [*var] }
  908. map_in_map: { foo: { bar: *var } }
  909. EOF
  910. ));
  911. }
  912. public function testYamlDirective()
  913. {
  914. $yaml = <<<'EOF'
  915. %YAML 1.2
  916. ---
  917. foo: 1
  918. bar: 2
  919. EOF;
  920. $this->assertEquals(['foo' => 1, 'bar' => 2], $this->parser->parse($yaml));
  921. }
  922. /**
  923. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  924. * @expectedExceptionMessage Numeric keys are not supported. Quote your evaluable mapping keys instead
  925. */
  926. public function testFloatKeys()
  927. {
  928. $yaml = <<<'EOF'
  929. foo:
  930. 1.2: "bar"
  931. 1.3: "baz"
  932. EOF;
  933. $this->parser->parse($yaml);
  934. }
  935. /**
  936. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  937. * @expectedExceptionMessage Non-string keys are not supported. Quote your evaluable mapping keys instead
  938. */
  939. public function testBooleanKeys()
  940. {
  941. $yaml = <<<'EOF'
  942. true: foo
  943. false: bar
  944. EOF;
  945. $this->parser->parse($yaml);
  946. }
  947. public function testExplicitStringCasting()
  948. {
  949. $yaml = <<<'EOF'
  950. '1.2': "bar"
  951. !!str 1.3: "baz"
  952. 'true': foo
  953. !!str false: bar
  954. !!str null: 'null'
  955. '~': 'null'
  956. EOF;
  957. $expected = [
  958. '1.2' => 'bar',
  959. '1.3' => 'baz',
  960. 'true' => 'foo',
  961. 'false' => 'bar',
  962. 'null' => 'null',
  963. '~' => 'null',
  964. ];
  965. $this->assertEquals($expected, $this->parser->parse($yaml));
  966. }
  967. /**
  968. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  969. * @expectedExceptionMessage A colon cannot be used in an unquoted mapping value
  970. */
  971. public function testColonInMappingValueException()
  972. {
  973. $yaml = <<<'EOF'
  974. foo: bar: baz
  975. EOF;
  976. $this->parser->parse($yaml);
  977. }
  978. public function testColonInMappingValueExceptionNotTriggeredByColonInComment()
  979. {
  980. $yaml = <<<'EOT'
  981. foo:
  982. bar: foobar # Note: a comment after a colon
  983. EOT;
  984. $this->assertSame(['foo' => ['bar' => 'foobar']], $this->parser->parse($yaml));
  985. }
  986. /**
  987. * @dataProvider getCommentLikeStringInScalarBlockData
  988. */
  989. public function testCommentLikeStringsAreNotStrippedInBlockScalars($yaml, $expectedParserResult)
  990. {
  991. $this->assertSame($expectedParserResult, $this->parser->parse($yaml));
  992. }
  993. public function getCommentLikeStringInScalarBlockData()
  994. {
  995. $tests = [];
  996. $yaml = <<<'EOT'
  997. pages:
  998. -
  999. title: some title
  1000. content: |
  1001. # comment 1
  1002. header
  1003. # comment 2
  1004. <body>
  1005. <h1>title</h1>
  1006. </body>
  1007. footer # comment3
  1008. EOT;
  1009. $expected = [
  1010. 'pages' => [
  1011. [
  1012. 'title' => 'some title',
  1013. 'content' => <<<'EOT'
  1014. # comment 1
  1015. header
  1016. # comment 2
  1017. <body>
  1018. <h1>title</h1>
  1019. </body>
  1020. footer # comment3
  1021. EOT
  1022. ,
  1023. ],
  1024. ],
  1025. ];
  1026. $tests[] = [$yaml, $expected];
  1027. $yaml = <<<'EOT'
  1028. test: |
  1029. foo
  1030. # bar
  1031. baz
  1032. collection:
  1033. - one: |
  1034. foo
  1035. # bar
  1036. baz
  1037. - two: |
  1038. foo
  1039. # bar
  1040. baz
  1041. EOT;
  1042. $expected = [
  1043. 'test' => <<<'EOT'
  1044. foo
  1045. # bar
  1046. baz
  1047. EOT
  1048. ,
  1049. 'collection' => [
  1050. [
  1051. 'one' => <<<'EOT'
  1052. foo
  1053. # bar
  1054. baz
  1055. EOT
  1056. ,
  1057. ],
  1058. [
  1059. 'two' => <<<'EOT'
  1060. foo
  1061. # bar
  1062. baz
  1063. EOT
  1064. ,
  1065. ],
  1066. ],
  1067. ];
  1068. $tests[] = [$yaml, $expected];
  1069. $yaml = <<<'EOT'
  1070. foo:
  1071. bar:
  1072. scalar-block: >
  1073. line1
  1074. line2>
  1075. baz:
  1076. # comment
  1077. foobar: ~
  1078. EOT;
  1079. $expected = [
  1080. 'foo' => [
  1081. 'bar' => [
  1082. 'scalar-block' => "line1 line2>\n",
  1083. ],
  1084. 'baz' => [
  1085. 'foobar' => null,
  1086. ],
  1087. ],
  1088. ];
  1089. $tests[] = [$yaml, $expected];
  1090. $yaml = <<<'EOT'
  1091. a:
  1092. b: hello
  1093. # c: |
  1094. # first row
  1095. # second row
  1096. d: hello
  1097. EOT;
  1098. $expected = [
  1099. 'a' => [
  1100. 'b' => 'hello',
  1101. 'd' => 'hello',
  1102. ],
  1103. ];
  1104. $tests[] = [$yaml, $expected];
  1105. return $tests;
  1106. }
  1107. public function testBlankLinesAreParsedAsNewLinesInFoldedBlocks()
  1108. {
  1109. $yaml = <<<'EOT'
  1110. test: >
  1111. <h2>A heading</h2>
  1112. <ul>
  1113. <li>a list</li>
  1114. <li>may be a good example</li>
  1115. </ul>
  1116. EOT;
  1117. $this->assertSame(
  1118. [
  1119. 'test' => <<<'EOT'
  1120. <h2>A heading</h2>
  1121. <ul> <li>a list</li> <li>may be a good example</li> </ul>
  1122. EOT
  1123. ,
  1124. ],
  1125. $this->parser->parse($yaml)
  1126. );
  1127. }
  1128. public function testAdditionallyIndentedLinesAreParsedAsNewLinesInFoldedBlocks()
  1129. {
  1130. $yaml = <<<'EOT'
  1131. test: >
  1132. <h2>A heading</h2>
  1133. <ul>
  1134. <li>a list</li>
  1135. <li>may be a good example</li>
  1136. </ul>
  1137. EOT;
  1138. $this->assertSame(
  1139. [
  1140. 'test' => <<<'EOT'
  1141. <h2>A heading</h2>
  1142. <ul>
  1143. <li>a list</li>
  1144. <li>may be a good example</li>
  1145. </ul>
  1146. EOT
  1147. ,
  1148. ],
  1149. $this->parser->parse($yaml)
  1150. );
  1151. }
  1152. /**
  1153. * @dataProvider getBinaryData
  1154. */
  1155. public function testParseBinaryData($data)
  1156. {
  1157. $this->assertSame(['data' => 'Hello world'], $this->parser->parse($data));
  1158. }
  1159. public function getBinaryData()
  1160. {
  1161. return [
  1162. 'enclosed with double quotes' => ['data: !!binary "SGVsbG8gd29ybGQ="'],
  1163. 'enclosed with single quotes' => ["data: !!binary 'SGVsbG8gd29ybGQ='"],
  1164. 'containing spaces' => ['data: !!binary "SGVs bG8gd 29ybGQ="'],
  1165. 'in block scalar' => [
  1166. <<<'EOT'
  1167. data: !!binary |
  1168. SGVsbG8gd29ybGQ=
  1169. EOT
  1170. ],
  1171. 'containing spaces in block scalar' => [
  1172. <<<'EOT'
  1173. data: !!binary |
  1174. SGVs bG8gd 29ybGQ=
  1175. EOT
  1176. ],
  1177. ];
  1178. }
  1179. /**
  1180. * @dataProvider getInvalidBinaryData
  1181. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1182. */
  1183. public function testParseInvalidBinaryData($data, $expectedMessage)
  1184. {
  1185. if (method_exists($this, 'expectException')) {
  1186. $this->expectExceptionMessageRegExp($expectedMessage);
  1187. } else {
  1188. $this->setExpectedExceptionRegExp(ParseException::class, $expectedMessage);
  1189. }
  1190. $this->parser->parse($data);
  1191. }
  1192. public function getInvalidBinaryData()
  1193. {
  1194. return [
  1195. '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\)/'],
  1196. 'invalid characters' => ['!!binary "SGVsbG8#d29ybGQ="', '/The base64 encoded data \(.*\) contains invalid characters/'],
  1197. 'too many equals characters' => ['data: !!binary "SGVsbG8gd29yb==="', '/The base64 encoded data \(.*\) contains invalid characters/'],
  1198. 'misplaced equals character' => ['data: !!binary "SGVsbG8gd29ybG=Q"', '/The base64 encoded data \(.*\) contains invalid characters/'],
  1199. 'length not a multiple of four in block scalar' => [
  1200. <<<'EOT'
  1201. data: !!binary |
  1202. SGVsbG8d29ybGQ=
  1203. EOT
  1204. ,
  1205. '/The normalized base64 encoded data \(data without whitespace characters\) length must be a multiple of four \(\d+ bytes given\)/',
  1206. ],
  1207. 'invalid characters in block scalar' => [
  1208. <<<'EOT'
  1209. data: !!binary |
  1210. SGVsbG8#d29ybGQ=
  1211. EOT
  1212. ,
  1213. '/The base64 encoded data \(.*\) contains invalid characters/',
  1214. ],
  1215. 'too many equals characters in block scalar' => [
  1216. <<<'EOT'
  1217. data: !!binary |
  1218. SGVsbG8gd29yb===
  1219. EOT
  1220. ,
  1221. '/The base64 encoded data \(.*\) contains invalid characters/',
  1222. ],
  1223. 'misplaced equals character in block scalar' => [
  1224. <<<'EOT'
  1225. data: !!binary |
  1226. SGVsbG8gd29ybG=Q
  1227. EOT
  1228. ,
  1229. '/The base64 encoded data \(.*\) contains invalid characters/',
  1230. ],
  1231. ];
  1232. }
  1233. public function testParseDateAsMappingValue()
  1234. {
  1235. $yaml = <<<'EOT'
  1236. date: 2002-12-14
  1237. EOT;
  1238. $expectedDate = new \DateTime();
  1239. $expectedDate->setTimeZone(new \DateTimeZone('UTC'));
  1240. $expectedDate->setDate(2002, 12, 14);
  1241. $expectedDate->setTime(0, 0, 0);
  1242. $this->assertEquals(['date' => $expectedDate], $this->parser->parse($yaml, Yaml::PARSE_DATETIME));
  1243. }
  1244. /**
  1245. * @param $lineNumber
  1246. * @param $yaml
  1247. * @dataProvider parserThrowsExceptionWithCorrectLineNumberProvider
  1248. */
  1249. public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml)
  1250. {
  1251. if (method_exists($this, 'expectException')) {
  1252. $this->expectException('\Symfony\Component\Yaml\Exception\ParseException');
  1253. $this->expectExceptionMessage(sprintf('Unexpected characters near "," at line %d (near "bar: "123",").', $lineNumber));
  1254. } else {
  1255. $this->setExpectedException('\Symfony\Component\Yaml\Exception\ParseException', sprintf('Unexpected characters near "," at line %d (near "bar: "123",").', $lineNumber));
  1256. }
  1257. $this->parser->parse($yaml);
  1258. }
  1259. public function parserThrowsExceptionWithCorrectLineNumberProvider()
  1260. {
  1261. return [
  1262. [
  1263. 4,
  1264. <<<'YAML'
  1265. foo:
  1266. -
  1267. # bar
  1268. bar: "123",
  1269. YAML
  1270. ],
  1271. [
  1272. 5,
  1273. <<<'YAML'
  1274. foo:
  1275. -
  1276. # bar
  1277. # bar
  1278. bar: "123",
  1279. YAML
  1280. ],
  1281. [
  1282. 8,
  1283. <<<'YAML'
  1284. foo:
  1285. -
  1286. # foobar
  1287. baz: 123
  1288. bar:
  1289. -
  1290. # bar
  1291. bar: "123",
  1292. YAML
  1293. ],
  1294. [
  1295. 10,
  1296. <<<'YAML'
  1297. foo:
  1298. -
  1299. # foobar
  1300. # foobar
  1301. baz: 123
  1302. bar:
  1303. -
  1304. # bar
  1305. # bar
  1306. bar: "123",
  1307. YAML
  1308. ],
  1309. ];
  1310. }
  1311. public function testParseMultiLineQuotedString()
  1312. {
  1313. $yaml = <<<EOT
  1314. foo: "bar
  1315. baz
  1316. foobar
  1317. foo"
  1318. bar: baz
  1319. EOT;
  1320. $this->assertSame(['foo' => 'bar baz foobar foo', 'bar' => 'baz'], $this->parser->parse($yaml));
  1321. }
  1322. public function testMultiLineQuotedStringWithTrailingBackslash()
  1323. {
  1324. $yaml = <<<YAML
  1325. foobar:
  1326. "foo\
  1327. bar"
  1328. YAML;
  1329. $this->assertSame(['foobar' => 'foobar'], $this->parser->parse($yaml));
  1330. }
  1331. public function testCommentCharactersInMultiLineQuotedStrings()
  1332. {
  1333. $yaml = <<<YAML
  1334. foo:
  1335. foobar: 'foo
  1336. #bar'
  1337. bar: baz
  1338. YAML;
  1339. $expected = [
  1340. 'foo' => [
  1341. 'foobar' => 'foo #bar',
  1342. 'bar' => 'baz',
  1343. ],
  1344. ];
  1345. $this->assertSame($expected, $this->parser->parse($yaml));
  1346. }
  1347. public function testBlankLinesInQuotedMultiLineString()
  1348. {
  1349. $yaml = <<<YAML
  1350. foobar: 'foo
  1351. bar'
  1352. YAML;
  1353. $expected = [
  1354. 'foobar' => "foo\nbar",
  1355. ];
  1356. $this->assertSame($expected, $this->parser->parse($yaml));
  1357. }
  1358. public function testParseMultiLineUnquotedString()
  1359. {
  1360. $yaml = <<<EOT
  1361. foo: bar
  1362. baz
  1363. foobar
  1364. foo
  1365. bar: baz
  1366. EOT;
  1367. $this->assertSame(['foo' => 'bar baz foobar foo', 'bar' => 'baz'], $this->parser->parse($yaml));
  1368. }
  1369. public function testParseMultiLineString()
  1370. {
  1371. $this->assertEquals("foo bar\nbaz", $this->parser->parse("foo\nbar\n\nbaz"));
  1372. }
  1373. /**
  1374. * @dataProvider multiLineDataProvider
  1375. */
  1376. public function testParseMultiLineMappingValue($yaml, $expected, $parseError)
  1377. {
  1378. $this->assertEquals($expected, $this->parser->parse($yaml));
  1379. }
  1380. public function multiLineDataProvider()
  1381. {
  1382. $tests = [];
  1383. $yaml = <<<'EOF'
  1384. foo:
  1385. - bar:
  1386. one
  1387. two
  1388. three
  1389. EOF;
  1390. $expected = [
  1391. 'foo' => [
  1392. [
  1393. 'bar' => "one\ntwo three",
  1394. ],
  1395. ],
  1396. ];
  1397. $tests[] = [$yaml, $expected, false];
  1398. $yaml = <<<'EOF'
  1399. bar
  1400. "foo"
  1401. EOF;
  1402. $expected = 'bar "foo"';
  1403. $tests[] = [$yaml, $expected, false];
  1404. $yaml = <<<'EOF'
  1405. bar
  1406. "foo
  1407. EOF;
  1408. $expected = 'bar "foo';
  1409. $tests[] = [$yaml, $expected, false];
  1410. $yaml = <<<'EOF'
  1411. bar
  1412. 'foo'
  1413. EOF;
  1414. $expected = "bar\n'foo'";
  1415. $tests[] = [$yaml, $expected, false];
  1416. $yaml = <<<'EOF'
  1417. bar
  1418. foo'
  1419. EOF;
  1420. $expected = "bar\nfoo'";
  1421. $tests[] = [$yaml, $expected, false];
  1422. return $tests;
  1423. }
  1424. public function testTaggedInlineMapping()
  1425. {
  1426. $this->assertEquals(new TaggedValue('foo', ['foo' => 'bar']), $this->parser->parse('!foo {foo: bar}', Yaml::PARSE_CUSTOM_TAGS));
  1427. }
  1428. /**
  1429. * @dataProvider taggedValuesProvider
  1430. */
  1431. public function testCustomTagSupport($expected, $yaml)
  1432. {
  1433. $this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_CUSTOM_TAGS));
  1434. }
  1435. public function taggedValuesProvider()
  1436. {
  1437. return [
  1438. 'scalars' => [
  1439. [
  1440. 'foo' => new TaggedValue('inline', 'bar'),
  1441. 'quz' => new TaggedValue('long', 'this is a long text'),
  1442. ],
  1443. <<<YAML
  1444. foo: !inline bar
  1445. quz: !long >
  1446. this is a long
  1447. text
  1448. YAML
  1449. ],
  1450. 'sequences' => [
  1451. [new TaggedValue('foo', ['yaml']), new TaggedValue('quz', ['bar'])],
  1452. <<<YAML
  1453. - !foo
  1454. - yaml
  1455. - !quz [bar]
  1456. YAML
  1457. ],
  1458. 'mappings' => [
  1459. new TaggedValue('foo', ['foo' => new TaggedValue('quz', ['bar']), 'quz' => new TaggedValue('foo', ['quz' => 'bar'])]),
  1460. <<<YAML
  1461. !foo
  1462. foo: !quz [bar]
  1463. quz: !foo
  1464. quz: bar
  1465. YAML
  1466. ],
  1467. 'inline' => [
  1468. [new TaggedValue('foo', ['foo', 'bar']), new TaggedValue('quz', ['foo' => 'bar', 'quz' => new TaggedValue('bar', ['one' => 'bar'])])],
  1469. <<<YAML
  1470. - !foo [foo, bar]
  1471. - !quz {foo: bar, quz: !bar {one: bar}}
  1472. YAML
  1473. ],
  1474. 'spaces-around-tag-value-in-sequence' => [
  1475. [new TaggedValue('foo', 'bar')],
  1476. '[ !foo bar ]',
  1477. ],
  1478. ];
  1479. }
  1480. public function testNonSpecificTagSupport()
  1481. {
  1482. $this->assertSame('12', $this->parser->parse('! 12'));
  1483. }
  1484. /**
  1485. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1486. * @expectedExceptionMessage Tags support is not enabled. Enable the "Yaml::PARSE_CUSTOM_TAGS" flag to use "!iterator" at line 1 (near "!iterator [foo]").
  1487. */
  1488. public function testCustomTagsDisabled()
  1489. {
  1490. $this->parser->parse('!iterator [foo]');
  1491. }
  1492. /**
  1493. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1494. * @expectedExceptionMessage Tags support is not enabled. Enable the "Yaml::PARSE_CUSTOM_TAGS" flag to use "!iterator" at line 1 (near "!iterator foo").
  1495. */
  1496. public function testUnsupportedTagWithScalar()
  1497. {
  1498. $this->parser->parse('!iterator foo');
  1499. }
  1500. /**
  1501. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1502. * @expectedExceptionMessage The string "!!iterator foo" could not be parsed as it uses an unsupported built-in tag at line 1 (near "!!iterator foo").
  1503. */
  1504. public function testUnsupportedBuiltInTagWithScalar()
  1505. {
  1506. $this->parser->parse('!!iterator foo');
  1507. }
  1508. /**
  1509. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1510. * @expectedExceptionMessage The built-in tag "!!foo" is not implemented at line 1 (near "!!foo").
  1511. */
  1512. public function testExceptionWhenUsingUnsuportedBuiltInTags()
  1513. {
  1514. $this->parser->parse('!!foo');
  1515. }
  1516. /**
  1517. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1518. * @expectedExceptionMessage Complex mappings are not supported at line 1 (near "? "1"").
  1519. */
  1520. public function testComplexMappingThrowsParseException()
  1521. {
  1522. $yaml = <<<YAML
  1523. ? "1"
  1524. :
  1525. name: végétalien
  1526. YAML;
  1527. $this->parser->parse($yaml);
  1528. }
  1529. /**
  1530. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1531. * @expectedExceptionMessage Complex mappings are not supported at line 2 (near "? "1"").
  1532. */
  1533. public function testComplexMappingNestedInMappingThrowsParseException()
  1534. {
  1535. $yaml = <<<YAML
  1536. diet:
  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 Complex mappings are not supported at line 1 (near "- ? "1"").
  1546. */
  1547. public function testComplexMappingNestedInSequenceThrowsParseException()
  1548. {
  1549. $yaml = <<<YAML
  1550. - ? "1"
  1551. :
  1552. name: végétalien
  1553. YAML;
  1554. $this->parser->parse($yaml);
  1555. }
  1556. /**
  1557. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1558. * @expectedExceptionMessage Unable to parse at line 1 (near "[parameters]").
  1559. */
  1560. public function testParsingIniThrowsException()
  1561. {
  1562. $ini = <<<INI
  1563. [parameters]
  1564. foo = bar
  1565. bar = %foo%
  1566. INI;
  1567. $this->parser->parse($ini);
  1568. }
  1569. private function loadTestsFromFixtureFiles($testsFile)
  1570. {
  1571. $parser = new Parser();
  1572. $tests = [];
  1573. $files = $parser->parseFile(__DIR__.'/Fixtures/'.$testsFile);
  1574. foreach ($files as $file) {
  1575. $yamls = file_get_contents(__DIR__.'/Fixtures/'.$file.'.yml');
  1576. // split YAMLs documents
  1577. foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) {
  1578. if (!$yaml) {
  1579. continue;
  1580. }
  1581. $test = $parser->parse($yaml);
  1582. if (isset($test['todo']) && $test['todo']) {
  1583. // TODO
  1584. } else {
  1585. eval('$expected = '.trim($test['php']).';');
  1586. $tests[] = [var_export($expected, true), $test['yaml'], $test['test']];
  1587. }
  1588. }
  1589. }
  1590. return $tests;
  1591. }
  1592. public function testCanParseVeryLongValue()
  1593. {
  1594. $longStringWithSpaces = str_repeat('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ', 20000);
  1595. $trickyVal = ['x' => $longStringWithSpaces];
  1596. $yamlString = Yaml::dump($trickyVal);
  1597. $arrayFromYaml = $this->parser->parse($yamlString);
  1598. $this->assertEquals($trickyVal, $arrayFromYaml);
  1599. }
  1600. /**
  1601. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1602. * @expectedExceptionMessage Reference "foo" does not exist at line 2
  1603. */
  1604. public function testParserCleansUpReferencesBetweenRuns()
  1605. {
  1606. $yaml = <<<YAML
  1607. foo: &foo
  1608. baz: foobar
  1609. bar:
  1610. <<: *foo
  1611. YAML;
  1612. $this->parser->parse($yaml);
  1613. $yaml = <<<YAML
  1614. bar:
  1615. <<: *foo
  1616. YAML;
  1617. $this->parser->parse($yaml);
  1618. }
  1619. public function testPhpConstantTagMappingKey()
  1620. {
  1621. $yaml = <<<YAML
  1622. transitions:
  1623. !php/const 'Symfony\Component\Yaml\Tests\B::FOO':
  1624. from:
  1625. - !php/const 'Symfony\Component\Yaml\Tests\B::BAR'
  1626. to: !php/const 'Symfony\Component\Yaml\Tests\B::BAZ'
  1627. YAML;
  1628. $expected = [
  1629. 'transitions' => [
  1630. 'foo' => [
  1631. 'from' => [
  1632. 'bar',
  1633. ],
  1634. 'to' => 'baz',
  1635. ],
  1636. ],
  1637. ];
  1638. $this->assertSame($expected, $this->parser->parse($yaml, Yaml::PARSE_CONSTANT));
  1639. }
  1640. public function testMergeKeysWhenMappingsAreParsedAsObjects()
  1641. {
  1642. $yaml = <<<YAML
  1643. foo: &FOO
  1644. bar: 1
  1645. bar: &BAR
  1646. baz: 2
  1647. <<: *FOO
  1648. baz:
  1649. baz_foo: 3
  1650. <<:
  1651. baz_bar: 4
  1652. foobar:
  1653. bar: ~
  1654. <<: [*FOO, *BAR]
  1655. YAML;
  1656. $expected = (object) [
  1657. 'foo' => (object) [
  1658. 'bar' => 1,
  1659. ],
  1660. 'bar' => (object) [
  1661. 'baz' => 2,
  1662. 'bar' => 1,
  1663. ],
  1664. 'baz' => (object) [
  1665. 'baz_foo' => 3,
  1666. 'baz_bar' => 4,
  1667. ],
  1668. 'foobar' => (object) [
  1669. 'bar' => null,
  1670. 'baz' => 2,
  1671. ],
  1672. ];
  1673. $this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
  1674. }
  1675. public function testFilenamesAreParsedAsStringsWithoutFlag()
  1676. {
  1677. $file = __DIR__.'/Fixtures/index.yml';
  1678. $this->assertSame($file, $this->parser->parse($file));
  1679. }
  1680. public function testParseFile()
  1681. {
  1682. $this->assertInternalType('array', $this->parser->parseFile(__DIR__.'/Fixtures/index.yml'));
  1683. }
  1684. /**
  1685. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1686. * @expectedExceptionMessageRegExp #^File ".+/Fixtures/nonexistent.yml" does not exist\.$#
  1687. */
  1688. public function testParsingNonExistentFilesThrowsException()
  1689. {
  1690. $this->parser->parseFile(__DIR__.'/Fixtures/nonexistent.yml');
  1691. }
  1692. /**
  1693. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1694. * @expectedExceptionMessageRegExp #^File ".+/Fixtures/not_readable.yml" cannot be read\.$#
  1695. */
  1696. public function testParsingNotReadableFilesThrowsException()
  1697. {
  1698. if ('\\' === \DIRECTORY_SEPARATOR) {
  1699. $this->markTestSkipped('chmod is not supported on Windows');
  1700. }
  1701. if (!getenv('USER') || 'root' === getenv('USER')) {
  1702. $this->markTestSkipped('This test will fail if run under superuser');
  1703. }
  1704. $file = __DIR__.'/Fixtures/not_readable.yml';
  1705. chmod($file, 0200);
  1706. $this->parser->parseFile($file);
  1707. }
  1708. public function testParseReferencesOnMergeKeys()
  1709. {
  1710. $yaml = <<<YAML
  1711. mergekeyrefdef:
  1712. a: foo
  1713. <<: &quux
  1714. b: bar
  1715. c: baz
  1716. mergekeyderef:
  1717. d: quux
  1718. <<: *quux
  1719. YAML;
  1720. $expected = [
  1721. 'mergekeyrefdef' => [
  1722. 'a' => 'foo',
  1723. 'b' => 'bar',
  1724. 'c' => 'baz',
  1725. ],
  1726. 'mergekeyderef' => [
  1727. 'd' => 'quux',
  1728. 'b' => 'bar',
  1729. 'c' => 'baz',
  1730. ],
  1731. ];
  1732. $this->assertSame($expected, $this->parser->parse($yaml));
  1733. }
  1734. public function testParseReferencesOnMergeKeysWithMappingsParsedAsObjects()
  1735. {
  1736. $yaml = <<<YAML
  1737. mergekeyrefdef:
  1738. a: foo
  1739. <<: &quux
  1740. b: bar
  1741. c: baz
  1742. mergekeyderef:
  1743. d: quux
  1744. <<: *quux
  1745. YAML;
  1746. $expected = (object) [
  1747. 'mergekeyrefdef' => (object) [
  1748. 'a' => 'foo',
  1749. 'b' => 'bar',
  1750. 'c' => 'baz',
  1751. ],
  1752. 'mergekeyderef' => (object) [
  1753. 'd' => 'quux',
  1754. 'b' => 'bar',
  1755. 'c' => 'baz',
  1756. ],
  1757. ];
  1758. $this->assertEquals($expected, $this->parser->parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
  1759. }
  1760. /**
  1761. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1762. * @expectedExceptionMessage Reference "foo" does not exist
  1763. */
  1764. public function testEvalRefException()
  1765. {
  1766. $yaml = <<<EOE
  1767. foo: { &foo { a: Steve, <<: *foo} }
  1768. EOE;
  1769. $this->parser->parse($yaml);
  1770. }
  1771. /**
  1772. * @dataProvider circularReferenceProvider
  1773. * @expectedException \Symfony\Component\Yaml\Exception\ParseException
  1774. * @expectedExceptionMessage Circular reference [foo, bar, foo] detected
  1775. */
  1776. public function testDetectCircularReferences($yaml)
  1777. {
  1778. $this->parser->parse($yaml, Yaml::PARSE_CUSTOM_TAGS);
  1779. }
  1780. public function circularReferenceProvider()
  1781. {
  1782. $tests = [];
  1783. $yaml = <<<YAML
  1784. foo:
  1785. - &foo
  1786. - &bar
  1787. bar: foobar
  1788. baz: *foo
  1789. YAML;
  1790. $tests['sequence'] = [$yaml];
  1791. $yaml = <<<YAML
  1792. foo: &foo
  1793. bar: &bar
  1794. foobar: baz
  1795. baz: *foo
  1796. YAML;
  1797. $tests['mapping'] = [$yaml];
  1798. $yaml = <<<YAML
  1799. foo: &foo
  1800. bar: &bar
  1801. foobar: baz
  1802. <<: *foo
  1803. YAML;
  1804. $tests['mapping with merge key'] = [$yaml];
  1805. return $tests;
  1806. }
  1807. /**
  1808. * @dataProvider indentedMappingData
  1809. */
  1810. public function testParseIndentedMappings($yaml, $expected)
  1811. {
  1812. $this->assertSame($expected, $this->parser->parse($yaml));
  1813. }
  1814. public function indentedMappingData()
  1815. {
  1816. $tests = [];
  1817. $yaml = <<<YAML
  1818. foo:
  1819. - bar: "foobar"
  1820. # A comment
  1821. baz: "foobaz"
  1822. YAML;
  1823. $expected = [
  1824. 'foo' => [
  1825. [
  1826. 'bar' => 'foobar',
  1827. 'baz' => 'foobaz',
  1828. ],
  1829. ],
  1830. ];
  1831. $tests['comment line is first line in indented block'] = [$yaml, $expected];
  1832. $yaml = <<<YAML
  1833. foo:
  1834. - bar:
  1835. # comment
  1836. baz: [1, 2, 3]
  1837. YAML;
  1838. $expected = [
  1839. 'foo' => [
  1840. [
  1841. 'bar' => [
  1842. 'baz' => [1, 2, 3],
  1843. ],
  1844. ],
  1845. ],
  1846. ];
  1847. $tests['mapping value on new line starting with a comment line'] = [$yaml, $expected];
  1848. $yaml = <<<YAML
  1849. foo:
  1850. -
  1851. bar: foobar
  1852. YAML;
  1853. $expected = [
  1854. 'foo' => [
  1855. [
  1856. 'bar' => 'foobar',
  1857. ],
  1858. ],
  1859. ];
  1860. $tests['mapping in sequence starting on a new line'] = [$yaml, $expected];
  1861. $yaml = <<<YAML
  1862. foo:
  1863. bar: baz
  1864. YAML;
  1865. $expected = [
  1866. 'foo' => [
  1867. 'bar' => 'baz',
  1868. ],
  1869. ];
  1870. $tests['blank line at the beginning of an indented mapping value'] = [$yaml, $expected];
  1871. return $tests;
  1872. }
  1873. }
  1874. class B
  1875. {
  1876. public $b = 'foo';
  1877. const FOO = 'foo';
  1878. const BAR = 'bar';
  1879. const BAZ = 'baz';
  1880. }