ParserTest.php 47 KB

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