ParserTest.php 49 KB

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