OptionsResolverTest.php 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518
  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\OptionsResolver\Tests;
  11. use PHPUnit\Framework\Assert;
  12. use PHPUnit\Framework\TestCase;
  13. use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
  14. use Symfony\Component\OptionsResolver\Options;
  15. use Symfony\Component\OptionsResolver\OptionsResolver;
  16. class OptionsResolverTest extends TestCase
  17. {
  18. /**
  19. * @var OptionsResolver
  20. */
  21. private $resolver;
  22. protected function setUp()
  23. {
  24. $this->resolver = new OptionsResolver();
  25. }
  26. /**
  27. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  28. * @expectedExceptionMessage The option "foo" does not exist. Defined options are: "a", "z".
  29. */
  30. public function testResolveFailsIfNonExistingOption()
  31. {
  32. $this->resolver->setDefault('z', '1');
  33. $this->resolver->setDefault('a', '2');
  34. $this->resolver->resolve(['foo' => 'bar']);
  35. }
  36. /**
  37. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  38. * @expectedExceptionMessage The options "baz", "foo", "ping" do not exist. Defined options are: "a", "z".
  39. */
  40. public function testResolveFailsIfMultipleNonExistingOptions()
  41. {
  42. $this->resolver->setDefault('z', '1');
  43. $this->resolver->setDefault('a', '2');
  44. $this->resolver->resolve(['ping' => 'pong', 'foo' => 'bar', 'baz' => 'bam']);
  45. }
  46. /**
  47. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  48. */
  49. public function testResolveFailsFromLazyOption()
  50. {
  51. $this->resolver->setDefault('foo', function (Options $options) {
  52. $options->resolve([]);
  53. });
  54. $this->resolver->resolve();
  55. }
  56. public function testSetDefaultReturnsThis()
  57. {
  58. $this->assertSame($this->resolver, $this->resolver->setDefault('foo', 'bar'));
  59. }
  60. public function testSetDefault()
  61. {
  62. $this->resolver->setDefault('one', '1');
  63. $this->resolver->setDefault('two', '20');
  64. $this->assertEquals([
  65. 'one' => '1',
  66. 'two' => '20',
  67. ], $this->resolver->resolve());
  68. }
  69. /**
  70. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  71. */
  72. public function testFailIfSetDefaultFromLazyOption()
  73. {
  74. $this->resolver->setDefault('lazy', function (Options $options) {
  75. $options->setDefault('default', 42);
  76. });
  77. $this->resolver->resolve();
  78. }
  79. public function testHasDefault()
  80. {
  81. $this->assertFalse($this->resolver->hasDefault('foo'));
  82. $this->resolver->setDefault('foo', 42);
  83. $this->assertTrue($this->resolver->hasDefault('foo'));
  84. }
  85. public function testHasDefaultWithNullValue()
  86. {
  87. $this->assertFalse($this->resolver->hasDefault('foo'));
  88. $this->resolver->setDefault('foo', null);
  89. $this->assertTrue($this->resolver->hasDefault('foo'));
  90. }
  91. public function testSetLazyReturnsThis()
  92. {
  93. $this->assertSame($this->resolver, $this->resolver->setDefault('foo', function (Options $options) {}));
  94. }
  95. public function testSetLazyClosure()
  96. {
  97. $this->resolver->setDefault('foo', function (Options $options) {
  98. return 'lazy';
  99. });
  100. $this->assertEquals(['foo' => 'lazy'], $this->resolver->resolve());
  101. }
  102. public function testClosureWithoutTypeHintNotInvoked()
  103. {
  104. $closure = function ($options) {
  105. Assert::fail('Should not be called');
  106. };
  107. $this->resolver->setDefault('foo', $closure);
  108. $this->assertSame(['foo' => $closure], $this->resolver->resolve());
  109. }
  110. public function testClosureWithoutParametersNotInvoked()
  111. {
  112. $closure = function () {
  113. Assert::fail('Should not be called');
  114. };
  115. $this->resolver->setDefault('foo', $closure);
  116. $this->assertSame(['foo' => $closure], $this->resolver->resolve());
  117. }
  118. public function testAccessPreviousDefaultValue()
  119. {
  120. // defined by superclass
  121. $this->resolver->setDefault('foo', 'bar');
  122. // defined by subclass
  123. $this->resolver->setDefault('foo', function (Options $options, $previousValue) {
  124. Assert::assertEquals('bar', $previousValue);
  125. return 'lazy';
  126. });
  127. $this->assertEquals(['foo' => 'lazy'], $this->resolver->resolve());
  128. }
  129. public function testAccessPreviousLazyDefaultValue()
  130. {
  131. // defined by superclass
  132. $this->resolver->setDefault('foo', function (Options $options) {
  133. return 'bar';
  134. });
  135. // defined by subclass
  136. $this->resolver->setDefault('foo', function (Options $options, $previousValue) {
  137. Assert::assertEquals('bar', $previousValue);
  138. return 'lazy';
  139. });
  140. $this->assertEquals(['foo' => 'lazy'], $this->resolver->resolve());
  141. }
  142. public function testPreviousValueIsNotEvaluatedIfNoSecondArgument()
  143. {
  144. // defined by superclass
  145. $this->resolver->setDefault('foo', function () {
  146. Assert::fail('Should not be called');
  147. });
  148. // defined by subclass, no $previousValue argument defined!
  149. $this->resolver->setDefault('foo', function (Options $options) {
  150. return 'lazy';
  151. });
  152. $this->assertEquals(['foo' => 'lazy'], $this->resolver->resolve());
  153. }
  154. public function testOverwrittenLazyOptionNotEvaluated()
  155. {
  156. $this->resolver->setDefault('foo', function (Options $options) {
  157. Assert::fail('Should not be called');
  158. });
  159. $this->resolver->setDefault('foo', 'bar');
  160. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  161. }
  162. public function testInvokeEachLazyOptionOnlyOnce()
  163. {
  164. $calls = 0;
  165. $this->resolver->setDefault('lazy1', function (Options $options) use (&$calls) {
  166. Assert::assertSame(1, ++$calls);
  167. $options['lazy2'];
  168. });
  169. $this->resolver->setDefault('lazy2', function (Options $options) use (&$calls) {
  170. Assert::assertSame(2, ++$calls);
  171. });
  172. $this->resolver->resolve();
  173. $this->assertSame(2, $calls);
  174. }
  175. public function testSetRequiredReturnsThis()
  176. {
  177. $this->assertSame($this->resolver, $this->resolver->setRequired('foo'));
  178. }
  179. /**
  180. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  181. */
  182. public function testFailIfSetRequiredFromLazyOption()
  183. {
  184. $this->resolver->setDefault('foo', function (Options $options) {
  185. $options->setRequired('bar');
  186. });
  187. $this->resolver->resolve();
  188. }
  189. /**
  190. * @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
  191. */
  192. public function testResolveFailsIfRequiredOptionMissing()
  193. {
  194. $this->resolver->setRequired('foo');
  195. $this->resolver->resolve();
  196. }
  197. public function testResolveSucceedsIfRequiredOptionSet()
  198. {
  199. $this->resolver->setRequired('foo');
  200. $this->resolver->setDefault('foo', 'bar');
  201. $this->assertNotEmpty($this->resolver->resolve());
  202. }
  203. public function testResolveSucceedsIfRequiredOptionPassed()
  204. {
  205. $this->resolver->setRequired('foo');
  206. $this->assertNotEmpty($this->resolver->resolve(['foo' => 'bar']));
  207. }
  208. public function testIsRequired()
  209. {
  210. $this->assertFalse($this->resolver->isRequired('foo'));
  211. $this->resolver->setRequired('foo');
  212. $this->assertTrue($this->resolver->isRequired('foo'));
  213. }
  214. public function testRequiredIfSetBefore()
  215. {
  216. $this->assertFalse($this->resolver->isRequired('foo'));
  217. $this->resolver->setDefault('foo', 'bar');
  218. $this->resolver->setRequired('foo');
  219. $this->assertTrue($this->resolver->isRequired('foo'));
  220. }
  221. public function testStillRequiredAfterSet()
  222. {
  223. $this->assertFalse($this->resolver->isRequired('foo'));
  224. $this->resolver->setRequired('foo');
  225. $this->resolver->setDefault('foo', 'bar');
  226. $this->assertTrue($this->resolver->isRequired('foo'));
  227. }
  228. public function testIsNotRequiredAfterRemove()
  229. {
  230. $this->assertFalse($this->resolver->isRequired('foo'));
  231. $this->resolver->setRequired('foo');
  232. $this->resolver->remove('foo');
  233. $this->assertFalse($this->resolver->isRequired('foo'));
  234. }
  235. public function testIsNotRequiredAfterClear()
  236. {
  237. $this->assertFalse($this->resolver->isRequired('foo'));
  238. $this->resolver->setRequired('foo');
  239. $this->resolver->clear();
  240. $this->assertFalse($this->resolver->isRequired('foo'));
  241. }
  242. public function testGetRequiredOptions()
  243. {
  244. $this->resolver->setRequired(['foo', 'bar']);
  245. $this->resolver->setDefault('bam', 'baz');
  246. $this->resolver->setDefault('foo', 'boo');
  247. $this->assertSame(['foo', 'bar'], $this->resolver->getRequiredOptions());
  248. }
  249. public function testIsMissingIfNotSet()
  250. {
  251. $this->assertFalse($this->resolver->isMissing('foo'));
  252. $this->resolver->setRequired('foo');
  253. $this->assertTrue($this->resolver->isMissing('foo'));
  254. }
  255. public function testIsNotMissingIfSet()
  256. {
  257. $this->resolver->setDefault('foo', 'bar');
  258. $this->assertFalse($this->resolver->isMissing('foo'));
  259. $this->resolver->setRequired('foo');
  260. $this->assertFalse($this->resolver->isMissing('foo'));
  261. }
  262. public function testIsNotMissingAfterRemove()
  263. {
  264. $this->resolver->setRequired('foo');
  265. $this->resolver->remove('foo');
  266. $this->assertFalse($this->resolver->isMissing('foo'));
  267. }
  268. public function testIsNotMissingAfterClear()
  269. {
  270. $this->resolver->setRequired('foo');
  271. $this->resolver->clear();
  272. $this->assertFalse($this->resolver->isRequired('foo'));
  273. }
  274. public function testGetMissingOptions()
  275. {
  276. $this->resolver->setRequired(['foo', 'bar']);
  277. $this->resolver->setDefault('bam', 'baz');
  278. $this->resolver->setDefault('foo', 'boo');
  279. $this->assertSame(['bar'], $this->resolver->getMissingOptions());
  280. }
  281. /**
  282. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  283. */
  284. public function testFailIfSetDefinedFromLazyOption()
  285. {
  286. $this->resolver->setDefault('foo', function (Options $options) {
  287. $options->setDefined('bar');
  288. });
  289. $this->resolver->resolve();
  290. }
  291. public function testDefinedOptionsNotIncludedInResolvedOptions()
  292. {
  293. $this->resolver->setDefined('foo');
  294. $this->assertSame([], $this->resolver->resolve());
  295. }
  296. public function testDefinedOptionsIncludedIfDefaultSetBefore()
  297. {
  298. $this->resolver->setDefault('foo', 'bar');
  299. $this->resolver->setDefined('foo');
  300. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  301. }
  302. public function testDefinedOptionsIncludedIfDefaultSetAfter()
  303. {
  304. $this->resolver->setDefined('foo');
  305. $this->resolver->setDefault('foo', 'bar');
  306. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  307. }
  308. public function testDefinedOptionsIncludedIfPassedToResolve()
  309. {
  310. $this->resolver->setDefined('foo');
  311. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve(['foo' => 'bar']));
  312. }
  313. public function testIsDefined()
  314. {
  315. $this->assertFalse($this->resolver->isDefined('foo'));
  316. $this->resolver->setDefined('foo');
  317. $this->assertTrue($this->resolver->isDefined('foo'));
  318. }
  319. public function testLazyOptionsAreDefined()
  320. {
  321. $this->assertFalse($this->resolver->isDefined('foo'));
  322. $this->resolver->setDefault('foo', function (Options $options) {});
  323. $this->assertTrue($this->resolver->isDefined('foo'));
  324. }
  325. public function testRequiredOptionsAreDefined()
  326. {
  327. $this->assertFalse($this->resolver->isDefined('foo'));
  328. $this->resolver->setRequired('foo');
  329. $this->assertTrue($this->resolver->isDefined('foo'));
  330. }
  331. public function testSetOptionsAreDefined()
  332. {
  333. $this->assertFalse($this->resolver->isDefined('foo'));
  334. $this->resolver->setDefault('foo', 'bar');
  335. $this->assertTrue($this->resolver->isDefined('foo'));
  336. }
  337. public function testGetDefinedOptions()
  338. {
  339. $this->resolver->setDefined(['foo', 'bar']);
  340. $this->resolver->setDefault('baz', 'bam');
  341. $this->resolver->setRequired('boo');
  342. $this->assertSame(['foo', 'bar', 'baz', 'boo'], $this->resolver->getDefinedOptions());
  343. }
  344. public function testRemovedOptionsAreNotDefined()
  345. {
  346. $this->assertFalse($this->resolver->isDefined('foo'));
  347. $this->resolver->setDefined('foo');
  348. $this->assertTrue($this->resolver->isDefined('foo'));
  349. $this->resolver->remove('foo');
  350. $this->assertFalse($this->resolver->isDefined('foo'));
  351. }
  352. public function testClearedOptionsAreNotDefined()
  353. {
  354. $this->assertFalse($this->resolver->isDefined('foo'));
  355. $this->resolver->setDefined('foo');
  356. $this->assertTrue($this->resolver->isDefined('foo'));
  357. $this->resolver->clear();
  358. $this->assertFalse($this->resolver->isDefined('foo'));
  359. }
  360. /**
  361. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  362. */
  363. public function testFailIfSetDeprecatedFromLazyOption()
  364. {
  365. $this->resolver
  366. ->setDefault('bar', 'baz')
  367. ->setDefault('foo', function (Options $options) {
  368. $options->setDeprecated('bar');
  369. })
  370. ->resolve()
  371. ;
  372. }
  373. /**
  374. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  375. */
  376. public function testSetDeprecatedFailsIfUnknownOption()
  377. {
  378. $this->resolver->setDeprecated('foo');
  379. }
  380. /**
  381. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidArgumentException
  382. * @expectedExceptionMessage Invalid type for deprecation message argument, expected string or \Closure, but got "boolean".
  383. */
  384. public function testSetDeprecatedFailsIfInvalidDeprecationMessageType()
  385. {
  386. $this->resolver
  387. ->setDefined('foo')
  388. ->setDeprecated('foo', true)
  389. ;
  390. }
  391. /**
  392. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidArgumentException
  393. * @expectedExceptionMessage Invalid type for deprecation message, expected string but got "boolean", return an empty string to ignore.
  394. */
  395. public function testLazyDeprecationFailsIfInvalidDeprecationMessageType()
  396. {
  397. $this->resolver
  398. ->setDefined('foo')
  399. ->setDeprecated('foo', function (Options $options, $value) {
  400. return false;
  401. })
  402. ;
  403. $this->resolver->resolve(['foo' => null]);
  404. }
  405. /**
  406. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  407. * @expectedExceptionMessage The options "foo", "bar" have a cyclic dependency.
  408. */
  409. public function testFailsIfCyclicDependencyBetweenDeprecation()
  410. {
  411. $this->resolver
  412. ->setDefined(['foo', 'bar'])
  413. ->setDeprecated('foo', function (Options $options, $value) {
  414. $options['bar'];
  415. })
  416. ->setDeprecated('bar', function (Options $options, $value) {
  417. $options['foo'];
  418. })
  419. ;
  420. $this->resolver->resolve(['foo' => null, 'bar' => null]);
  421. }
  422. public function testIsDeprecated()
  423. {
  424. $this->resolver
  425. ->setDefined('foo')
  426. ->setDeprecated('foo')
  427. ;
  428. $this->assertTrue($this->resolver->isDeprecated('foo'));
  429. }
  430. public function testIsNotDeprecatedIfEmptyString()
  431. {
  432. $this->resolver
  433. ->setDefined('foo')
  434. ->setDeprecated('foo', '')
  435. ;
  436. $this->assertFalse($this->resolver->isDeprecated('foo'));
  437. }
  438. /**
  439. * @dataProvider provideDeprecationData
  440. */
  441. public function testDeprecationMessages(\Closure $configureOptions, array $options, ?array $expectedError, int $expectedCount)
  442. {
  443. $count = 0;
  444. error_clear_last();
  445. set_error_handler(function () use (&$count) {
  446. ++$count;
  447. return false;
  448. });
  449. $e = error_reporting(0);
  450. $configureOptions($this->resolver);
  451. $this->resolver->resolve($options);
  452. error_reporting($e);
  453. restore_error_handler();
  454. $lastError = error_get_last();
  455. unset($lastError['file'], $lastError['line']);
  456. $this->assertSame($expectedError, $lastError);
  457. $this->assertSame($expectedCount, $count);
  458. }
  459. public function provideDeprecationData()
  460. {
  461. yield 'It deprecates an option with default message' => [
  462. function (OptionsResolver $resolver) {
  463. $resolver
  464. ->setDefined(['foo', 'bar'])
  465. ->setDeprecated('foo')
  466. ;
  467. },
  468. ['foo' => 'baz'],
  469. [
  470. 'type' => E_USER_DEPRECATED,
  471. 'message' => 'The option "foo" is deprecated.',
  472. ],
  473. 1,
  474. ];
  475. yield 'It deprecates an option with custom message' => [
  476. function (OptionsResolver $resolver) {
  477. $resolver
  478. ->setDefined('foo')
  479. ->setDefault('bar', function (Options $options) {
  480. return $options['foo'];
  481. })
  482. ->setDeprecated('foo', 'The option "foo" is deprecated, use "bar" option instead.')
  483. ;
  484. },
  485. ['foo' => 'baz'],
  486. [
  487. 'type' => E_USER_DEPRECATED,
  488. 'message' => 'The option "foo" is deprecated, use "bar" option instead.',
  489. ],
  490. 2,
  491. ];
  492. yield 'It deprecates an option evaluated in another definition' => [
  493. function (OptionsResolver $resolver) {
  494. // defined by superclass
  495. $resolver
  496. ->setDefault('foo', null)
  497. ->setDeprecated('foo')
  498. ;
  499. // defined by subclass
  500. $resolver->setDefault('bar', function (Options $options) {
  501. return $options['foo']; // It triggers a deprecation
  502. });
  503. },
  504. [],
  505. [
  506. 'type' => E_USER_DEPRECATED,
  507. 'message' => 'The option "foo" is deprecated.',
  508. ],
  509. 1,
  510. ];
  511. yield 'It deprecates allowed type and value' => [
  512. function (OptionsResolver $resolver) {
  513. $resolver
  514. ->setDefault('foo', null)
  515. ->setAllowedTypes('foo', ['null', 'string', \stdClass::class])
  516. ->setDeprecated('foo', function (Options $options, $value) {
  517. if ($value instanceof \stdClass) {
  518. return sprintf('Passing an instance of "%s" to option "foo" is deprecated, pass its FQCN instead.', \stdClass::class);
  519. }
  520. return '';
  521. })
  522. ;
  523. },
  524. ['foo' => new \stdClass()],
  525. [
  526. 'type' => E_USER_DEPRECATED,
  527. 'message' => 'Passing an instance of "stdClass" to option "foo" is deprecated, pass its FQCN instead.',
  528. ],
  529. 1,
  530. ];
  531. yield 'It triggers a deprecation based on the value only if option is provided by the user' => [
  532. function (OptionsResolver $resolver) {
  533. $resolver
  534. ->setDefined('foo')
  535. ->setAllowedTypes('foo', ['null', 'bool'])
  536. ->setDeprecated('foo', function (Options $options, $value) {
  537. if (!\is_bool($value)) {
  538. return 'Passing a value different than true or false is deprecated.';
  539. }
  540. return '';
  541. })
  542. ->setDefault('baz', null)
  543. ->setAllowedTypes('baz', ['null', 'int'])
  544. ->setDeprecated('baz', function (Options $options, $value) {
  545. if (!\is_int($value)) {
  546. return 'Not passing an integer is deprecated.';
  547. }
  548. return '';
  549. })
  550. ->setDefault('bar', function (Options $options) {
  551. $options['baz']; // It does not triggers a deprecation
  552. return $options['foo']; // It does not triggers a deprecation
  553. })
  554. ;
  555. },
  556. ['foo' => null], // It triggers a deprecation
  557. [
  558. 'type' => E_USER_DEPRECATED,
  559. 'message' => 'Passing a value different than true or false is deprecated.',
  560. ],
  561. 1,
  562. ];
  563. yield 'It ignores a deprecation if closure returns an empty string' => [
  564. function (OptionsResolver $resolver) {
  565. $resolver
  566. ->setDefault('foo', null)
  567. ->setDeprecated('foo', function (Options $options, $value) {
  568. return '';
  569. })
  570. ;
  571. },
  572. ['foo' => Bar::class],
  573. null,
  574. 0,
  575. ];
  576. yield 'It deprecates value depending on other option value' => [
  577. function (OptionsResolver $resolver) {
  578. $resolver
  579. ->setDefault('widget', null)
  580. ->setDefault('date_format', null)
  581. ->setDeprecated('date_format', function (Options $options, $dateFormat) {
  582. if (null !== $dateFormat && 'single_text' === $options['widget']) {
  583. return 'Using the "date_format" option when the "widget" option is set to "single_text" is deprecated.';
  584. }
  585. return '';
  586. })
  587. ;
  588. },
  589. ['widget' => 'single_text', 'date_format' => 2],
  590. [
  591. 'type' => E_USER_DEPRECATED,
  592. 'message' => 'Using the "date_format" option when the "widget" option is set to "single_text" is deprecated.',
  593. ],
  594. 1,
  595. ];
  596. yield 'It triggers a deprecation for each evaluation' => [
  597. function (OptionsResolver $resolver) {
  598. $resolver
  599. // defined by superclass
  600. ->setDefined('foo')
  601. ->setDeprecated('foo')
  602. // defined by subclass
  603. ->setDefault('bar', function (Options $options) {
  604. return $options['foo']; // It triggers a deprecation
  605. })
  606. ->setNormalizer('bar', function (Options $options, $value) {
  607. $options['foo']; // It triggers a deprecation
  608. $options['foo']; // It triggers a deprecation
  609. return $value;
  610. })
  611. ;
  612. },
  613. ['foo' => 'baz'], // It triggers a deprecation
  614. [
  615. 'type' => E_USER_DEPRECATED,
  616. 'message' => 'The option "foo" is deprecated.',
  617. ],
  618. 4,
  619. ];
  620. yield 'It ignores a deprecation if no option is provided by the user' => [
  621. function (OptionsResolver $resolver) {
  622. $resolver
  623. ->setDefined('foo')
  624. ->setDefault('bar', null)
  625. ->setDeprecated('foo')
  626. ->setDeprecated('bar')
  627. ;
  628. },
  629. [],
  630. null,
  631. 0,
  632. ];
  633. yield 'It explicitly ignores a deprecation' => [
  634. function (OptionsResolver $resolver) {
  635. $resolver
  636. ->setDefault('baz', function (Options $options) {
  637. return $options->offsetGet('foo', false);
  638. })
  639. ->setDefault('foo', null)
  640. ->setDeprecated('foo')
  641. ->setDefault('bar', function (Options $options) {
  642. return $options->offsetGet('foo', false);
  643. })
  644. ;
  645. },
  646. [],
  647. null,
  648. 0,
  649. ];
  650. }
  651. /**
  652. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  653. */
  654. public function testSetAllowedTypesFailsIfUnknownOption()
  655. {
  656. $this->resolver->setAllowedTypes('foo', 'string');
  657. }
  658. public function testResolveTypedArray()
  659. {
  660. $this->resolver->setDefined('foo');
  661. $this->resolver->setAllowedTypes('foo', 'string[]');
  662. $options = $this->resolver->resolve(['foo' => ['bar', 'baz']]);
  663. $this->assertSame(['foo' => ['bar', 'baz']], $options);
  664. }
  665. /**
  666. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  667. */
  668. public function testFailIfSetAllowedTypesFromLazyOption()
  669. {
  670. $this->resolver->setDefault('foo', function (Options $options) {
  671. $options->setAllowedTypes('bar', 'string');
  672. });
  673. $this->resolver->setDefault('bar', 'baz');
  674. $this->resolver->resolve();
  675. }
  676. /**
  677. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  678. * @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[]", but one of the elements is of type "DateTime[]".
  679. */
  680. public function testResolveFailsIfInvalidTypedArray()
  681. {
  682. $this->resolver->setDefined('foo');
  683. $this->resolver->setAllowedTypes('foo', 'int[]');
  684. $this->resolver->resolve(['foo' => [new \DateTime()]]);
  685. }
  686. /**
  687. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  688. * @expectedExceptionMessage The option "foo" with value "bar" is expected to be of type "int[]", but is of type "string".
  689. */
  690. public function testResolveFailsWithNonArray()
  691. {
  692. $this->resolver->setDefined('foo');
  693. $this->resolver->setAllowedTypes('foo', 'int[]');
  694. $this->resolver->resolve(['foo' => 'bar']);
  695. }
  696. /**
  697. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  698. * @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[]", but one of the elements is of type "stdClass[]".
  699. */
  700. public function testResolveFailsIfTypedArrayContainsInvalidTypes()
  701. {
  702. $this->resolver->setDefined('foo');
  703. $this->resolver->setAllowedTypes('foo', 'int[]');
  704. $values = range(1, 5);
  705. $values[] = new \stdClass();
  706. $values[] = [];
  707. $values[] = new \DateTime();
  708. $values[] = 123;
  709. $this->resolver->resolve(['foo' => $values]);
  710. }
  711. /**
  712. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  713. * @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "double[][]".
  714. */
  715. public function testResolveFailsWithCorrectLevelsButWrongScalar()
  716. {
  717. $this->resolver->setDefined('foo');
  718. $this->resolver->setAllowedTypes('foo', 'int[][]');
  719. $this->resolver->resolve([
  720. 'foo' => [
  721. [1.2],
  722. ],
  723. ]);
  724. }
  725. /**
  726. * @dataProvider provideInvalidTypes
  727. */
  728. public function testResolveFailsIfInvalidType($actualType, $allowedType, $exceptionMessage)
  729. {
  730. $this->resolver->setDefined('option');
  731. $this->resolver->setAllowedTypes('option', $allowedType);
  732. if (method_exists($this, 'expectException')) {
  733. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  734. $this->expectExceptionMessage($exceptionMessage);
  735. } else {
  736. $this->setExpectedException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException', $exceptionMessage);
  737. }
  738. $this->resolver->resolve(['option' => $actualType]);
  739. }
  740. public function provideInvalidTypes()
  741. {
  742. return [
  743. [true, 'string', 'The option "option" with value true is expected to be of type "string", but is of type "boolean".'],
  744. [false, 'string', 'The option "option" with value false is expected to be of type "string", but is of type "boolean".'],
  745. [fopen(__FILE__, 'r'), 'string', 'The option "option" with value resource is expected to be of type "string", but is of type "resource".'],
  746. [[], 'string', 'The option "option" with value array is expected to be of type "string", but is of type "array".'],
  747. [new OptionsResolver(), 'string', 'The option "option" with value Symfony\Component\OptionsResolver\OptionsResolver is expected to be of type "string", but is of type "Symfony\Component\OptionsResolver\OptionsResolver".'],
  748. [42, 'string', 'The option "option" with value 42 is expected to be of type "string", but is of type "integer".'],
  749. [null, 'string', 'The option "option" with value null is expected to be of type "string", but is of type "NULL".'],
  750. ['bar', '\stdClass', 'The option "option" with value "bar" is expected to be of type "\stdClass", but is of type "string".'],
  751. ];
  752. }
  753. public function testResolveSucceedsIfValidType()
  754. {
  755. $this->resolver->setDefault('foo', 'bar');
  756. $this->resolver->setAllowedTypes('foo', 'string');
  757. $this->assertNotEmpty($this->resolver->resolve());
  758. }
  759. /**
  760. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  761. * @expectedExceptionMessage The option "foo" with value 42 is expected to be of type "string" or "bool", but is of type "integer".
  762. */
  763. public function testResolveFailsIfInvalidTypeMultiple()
  764. {
  765. $this->resolver->setDefault('foo', 42);
  766. $this->resolver->setAllowedTypes('foo', ['string', 'bool']);
  767. $this->resolver->resolve();
  768. }
  769. public function testResolveSucceedsIfValidTypeMultiple()
  770. {
  771. $this->resolver->setDefault('foo', true);
  772. $this->resolver->setAllowedTypes('foo', ['string', 'bool']);
  773. $this->assertNotEmpty($this->resolver->resolve());
  774. }
  775. public function testResolveSucceedsIfInstanceOfClass()
  776. {
  777. $this->resolver->setDefault('foo', new \stdClass());
  778. $this->resolver->setAllowedTypes('foo', '\stdClass');
  779. $this->assertNotEmpty($this->resolver->resolve());
  780. }
  781. public function testResolveSucceedsIfTypedArray()
  782. {
  783. $this->resolver->setDefault('foo', null);
  784. $this->resolver->setAllowedTypes('foo', ['null', 'DateTime[]']);
  785. $data = [
  786. 'foo' => [
  787. new \DateTime(),
  788. new \DateTime(),
  789. ],
  790. ];
  791. $result = $this->resolver->resolve($data);
  792. $this->assertEquals($data, $result);
  793. }
  794. /**
  795. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  796. */
  797. public function testResolveFailsIfNotInstanceOfClass()
  798. {
  799. $this->resolver->setDefault('foo', 'bar');
  800. $this->resolver->setAllowedTypes('foo', '\stdClass');
  801. $this->resolver->resolve();
  802. }
  803. /**
  804. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  805. */
  806. public function testAddAllowedTypesFailsIfUnknownOption()
  807. {
  808. $this->resolver->addAllowedTypes('foo', 'string');
  809. }
  810. /**
  811. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  812. */
  813. public function testFailIfAddAllowedTypesFromLazyOption()
  814. {
  815. $this->resolver->setDefault('foo', function (Options $options) {
  816. $options->addAllowedTypes('bar', 'string');
  817. });
  818. $this->resolver->setDefault('bar', 'baz');
  819. $this->resolver->resolve();
  820. }
  821. /**
  822. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  823. */
  824. public function testResolveFailsIfInvalidAddedType()
  825. {
  826. $this->resolver->setDefault('foo', 42);
  827. $this->resolver->addAllowedTypes('foo', 'string');
  828. $this->resolver->resolve();
  829. }
  830. public function testResolveSucceedsIfValidAddedType()
  831. {
  832. $this->resolver->setDefault('foo', 'bar');
  833. $this->resolver->addAllowedTypes('foo', 'string');
  834. $this->assertNotEmpty($this->resolver->resolve());
  835. }
  836. /**
  837. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  838. */
  839. public function testResolveFailsIfInvalidAddedTypeMultiple()
  840. {
  841. $this->resolver->setDefault('foo', 42);
  842. $this->resolver->addAllowedTypes('foo', ['string', 'bool']);
  843. $this->resolver->resolve();
  844. }
  845. public function testResolveSucceedsIfValidAddedTypeMultiple()
  846. {
  847. $this->resolver->setDefault('foo', 'bar');
  848. $this->resolver->addAllowedTypes('foo', ['string', 'bool']);
  849. $this->assertNotEmpty($this->resolver->resolve());
  850. }
  851. public function testAddAllowedTypesDoesNotOverwrite()
  852. {
  853. $this->resolver->setDefault('foo', 'bar');
  854. $this->resolver->setAllowedTypes('foo', 'string');
  855. $this->resolver->addAllowedTypes('foo', 'bool');
  856. $this->resolver->setDefault('foo', 'bar');
  857. $this->assertNotEmpty($this->resolver->resolve());
  858. }
  859. public function testAddAllowedTypesDoesNotOverwrite2()
  860. {
  861. $this->resolver->setDefault('foo', 'bar');
  862. $this->resolver->setAllowedTypes('foo', 'string');
  863. $this->resolver->addAllowedTypes('foo', 'bool');
  864. $this->resolver->setDefault('foo', false);
  865. $this->assertNotEmpty($this->resolver->resolve());
  866. }
  867. /**
  868. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  869. */
  870. public function testSetAllowedValuesFailsIfUnknownOption()
  871. {
  872. $this->resolver->setAllowedValues('foo', 'bar');
  873. }
  874. /**
  875. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  876. */
  877. public function testFailIfSetAllowedValuesFromLazyOption()
  878. {
  879. $this->resolver->setDefault('foo', function (Options $options) {
  880. $options->setAllowedValues('bar', 'baz');
  881. });
  882. $this->resolver->setDefault('bar', 'baz');
  883. $this->resolver->resolve();
  884. }
  885. /**
  886. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  887. * @expectedExceptionMessage The option "foo" with value 42 is invalid. Accepted values are: "bar".
  888. */
  889. public function testResolveFailsIfInvalidValue()
  890. {
  891. $this->resolver->setDefined('foo');
  892. $this->resolver->setAllowedValues('foo', 'bar');
  893. $this->resolver->resolve(['foo' => 42]);
  894. }
  895. /**
  896. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  897. * @expectedExceptionMessage The option "foo" with value null is invalid. Accepted values are: "bar".
  898. */
  899. public function testResolveFailsIfInvalidValueIsNull()
  900. {
  901. $this->resolver->setDefault('foo', null);
  902. $this->resolver->setAllowedValues('foo', 'bar');
  903. $this->resolver->resolve();
  904. }
  905. /**
  906. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  907. */
  908. public function testResolveFailsIfInvalidValueStrict()
  909. {
  910. $this->resolver->setDefault('foo', 42);
  911. $this->resolver->setAllowedValues('foo', '42');
  912. $this->resolver->resolve();
  913. }
  914. public function testResolveSucceedsIfValidValue()
  915. {
  916. $this->resolver->setDefault('foo', 'bar');
  917. $this->resolver->setAllowedValues('foo', 'bar');
  918. $this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
  919. }
  920. public function testResolveSucceedsIfValidValueIsNull()
  921. {
  922. $this->resolver->setDefault('foo', null);
  923. $this->resolver->setAllowedValues('foo', null);
  924. $this->assertEquals(['foo' => null], $this->resolver->resolve());
  925. }
  926. /**
  927. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  928. * @expectedExceptionMessage The option "foo" with value 42 is invalid. Accepted values are: "bar", false, null.
  929. */
  930. public function testResolveFailsIfInvalidValueMultiple()
  931. {
  932. $this->resolver->setDefault('foo', 42);
  933. $this->resolver->setAllowedValues('foo', ['bar', false, null]);
  934. $this->resolver->resolve();
  935. }
  936. public function testResolveSucceedsIfValidValueMultiple()
  937. {
  938. $this->resolver->setDefault('foo', 'baz');
  939. $this->resolver->setAllowedValues('foo', ['bar', 'baz']);
  940. $this->assertEquals(['foo' => 'baz'], $this->resolver->resolve());
  941. }
  942. public function testResolveFailsIfClosureReturnsFalse()
  943. {
  944. $this->resolver->setDefault('foo', 42);
  945. $this->resolver->setAllowedValues('foo', function ($value) use (&$passedValue) {
  946. $passedValue = $value;
  947. return false;
  948. });
  949. try {
  950. $this->resolver->resolve();
  951. $this->fail('Should fail');
  952. } catch (InvalidOptionsException $e) {
  953. }
  954. $this->assertSame(42, $passedValue);
  955. }
  956. public function testResolveSucceedsIfClosureReturnsTrue()
  957. {
  958. $this->resolver->setDefault('foo', 'bar');
  959. $this->resolver->setAllowedValues('foo', function ($value) use (&$passedValue) {
  960. $passedValue = $value;
  961. return true;
  962. });
  963. $this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
  964. $this->assertSame('bar', $passedValue);
  965. }
  966. /**
  967. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  968. */
  969. public function testResolveFailsIfAllClosuresReturnFalse()
  970. {
  971. $this->resolver->setDefault('foo', 42);
  972. $this->resolver->setAllowedValues('foo', [
  973. function () { return false; },
  974. function () { return false; },
  975. function () { return false; },
  976. ]);
  977. $this->resolver->resolve();
  978. }
  979. public function testResolveSucceedsIfAnyClosureReturnsTrue()
  980. {
  981. $this->resolver->setDefault('foo', 'bar');
  982. $this->resolver->setAllowedValues('foo', [
  983. function () { return false; },
  984. function () { return true; },
  985. function () { return false; },
  986. ]);
  987. $this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
  988. }
  989. /**
  990. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  991. */
  992. public function testAddAllowedValuesFailsIfUnknownOption()
  993. {
  994. $this->resolver->addAllowedValues('foo', 'bar');
  995. }
  996. /**
  997. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  998. */
  999. public function testFailIfAddAllowedValuesFromLazyOption()
  1000. {
  1001. $this->resolver->setDefault('foo', function (Options $options) {
  1002. $options->addAllowedValues('bar', 'baz');
  1003. });
  1004. $this->resolver->setDefault('bar', 'baz');
  1005. $this->resolver->resolve();
  1006. }
  1007. /**
  1008. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  1009. */
  1010. public function testResolveFailsIfInvalidAddedValue()
  1011. {
  1012. $this->resolver->setDefault('foo', 42);
  1013. $this->resolver->addAllowedValues('foo', 'bar');
  1014. $this->resolver->resolve();
  1015. }
  1016. public function testResolveSucceedsIfValidAddedValue()
  1017. {
  1018. $this->resolver->setDefault('foo', 'bar');
  1019. $this->resolver->addAllowedValues('foo', 'bar');
  1020. $this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
  1021. }
  1022. public function testResolveSucceedsIfValidAddedValueIsNull()
  1023. {
  1024. $this->resolver->setDefault('foo', null);
  1025. $this->resolver->addAllowedValues('foo', null);
  1026. $this->assertEquals(['foo' => null], $this->resolver->resolve());
  1027. }
  1028. /**
  1029. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  1030. */
  1031. public function testResolveFailsIfInvalidAddedValueMultiple()
  1032. {
  1033. $this->resolver->setDefault('foo', 42);
  1034. $this->resolver->addAllowedValues('foo', ['bar', 'baz']);
  1035. $this->resolver->resolve();
  1036. }
  1037. public function testResolveSucceedsIfValidAddedValueMultiple()
  1038. {
  1039. $this->resolver->setDefault('foo', 'bar');
  1040. $this->resolver->addAllowedValues('foo', ['bar', 'baz']);
  1041. $this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
  1042. }
  1043. public function testAddAllowedValuesDoesNotOverwrite()
  1044. {
  1045. $this->resolver->setDefault('foo', 'bar');
  1046. $this->resolver->setAllowedValues('foo', 'bar');
  1047. $this->resolver->addAllowedValues('foo', 'baz');
  1048. $this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
  1049. }
  1050. public function testAddAllowedValuesDoesNotOverwrite2()
  1051. {
  1052. $this->resolver->setDefault('foo', 'baz');
  1053. $this->resolver->setAllowedValues('foo', 'bar');
  1054. $this->resolver->addAllowedValues('foo', 'baz');
  1055. $this->assertEquals(['foo' => 'baz'], $this->resolver->resolve());
  1056. }
  1057. /**
  1058. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  1059. */
  1060. public function testResolveFailsIfAllAddedClosuresReturnFalse()
  1061. {
  1062. $this->resolver->setDefault('foo', 42);
  1063. $this->resolver->setAllowedValues('foo', function () { return false; });
  1064. $this->resolver->addAllowedValues('foo', function () { return false; });
  1065. $this->resolver->resolve();
  1066. }
  1067. public function testResolveSucceedsIfAnyAddedClosureReturnsTrue()
  1068. {
  1069. $this->resolver->setDefault('foo', 'bar');
  1070. $this->resolver->setAllowedValues('foo', function () { return false; });
  1071. $this->resolver->addAllowedValues('foo', function () { return true; });
  1072. $this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
  1073. }
  1074. public function testResolveSucceedsIfAnyAddedClosureReturnsTrue2()
  1075. {
  1076. $this->resolver->setDefault('foo', 'bar');
  1077. $this->resolver->setAllowedValues('foo', function () { return true; });
  1078. $this->resolver->addAllowedValues('foo', function () { return false; });
  1079. $this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
  1080. }
  1081. public function testSetNormalizerReturnsThis()
  1082. {
  1083. $this->resolver->setDefault('foo', 'bar');
  1084. $this->assertSame($this->resolver, $this->resolver->setNormalizer('foo', function () {}));
  1085. }
  1086. public function testSetNormalizerClosure()
  1087. {
  1088. $this->resolver->setDefault('foo', 'bar');
  1089. $this->resolver->setNormalizer('foo', function () {
  1090. return 'normalized';
  1091. });
  1092. $this->assertEquals(['foo' => 'normalized'], $this->resolver->resolve());
  1093. }
  1094. /**
  1095. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  1096. */
  1097. public function testSetNormalizerFailsIfUnknownOption()
  1098. {
  1099. $this->resolver->setNormalizer('foo', function () {});
  1100. }
  1101. /**
  1102. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  1103. */
  1104. public function testFailIfSetNormalizerFromLazyOption()
  1105. {
  1106. $this->resolver->setDefault('foo', function (Options $options) {
  1107. $options->setNormalizer('foo', function () {});
  1108. });
  1109. $this->resolver->setDefault('bar', 'baz');
  1110. $this->resolver->resolve();
  1111. }
  1112. public function testNormalizerReceivesSetOption()
  1113. {
  1114. $this->resolver->setDefault('foo', 'bar');
  1115. $this->resolver->setNormalizer('foo', function (Options $options, $value) {
  1116. return 'normalized['.$value.']';
  1117. });
  1118. $this->assertEquals(['foo' => 'normalized[bar]'], $this->resolver->resolve());
  1119. }
  1120. public function testNormalizerReceivesPassedOption()
  1121. {
  1122. $this->resolver->setDefault('foo', 'bar');
  1123. $this->resolver->setNormalizer('foo', function (Options $options, $value) {
  1124. return 'normalized['.$value.']';
  1125. });
  1126. $resolved = $this->resolver->resolve(['foo' => 'baz']);
  1127. $this->assertEquals(['foo' => 'normalized[baz]'], $resolved);
  1128. }
  1129. /**
  1130. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  1131. */
  1132. public function testValidateTypeBeforeNormalization()
  1133. {
  1134. $this->resolver->setDefault('foo', 'bar');
  1135. $this->resolver->setAllowedTypes('foo', 'int');
  1136. $this->resolver->setNormalizer('foo', function () {
  1137. Assert::fail('Should not be called.');
  1138. });
  1139. $this->resolver->resolve();
  1140. }
  1141. /**
  1142. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  1143. */
  1144. public function testValidateValueBeforeNormalization()
  1145. {
  1146. $this->resolver->setDefault('foo', 'bar');
  1147. $this->resolver->setAllowedValues('foo', 'baz');
  1148. $this->resolver->setNormalizer('foo', function () {
  1149. Assert::fail('Should not be called.');
  1150. });
  1151. $this->resolver->resolve();
  1152. }
  1153. public function testNormalizerCanAccessOtherOptions()
  1154. {
  1155. $this->resolver->setDefault('default', 'bar');
  1156. $this->resolver->setDefault('norm', 'baz');
  1157. $this->resolver->setNormalizer('norm', function (Options $options) {
  1158. /* @var TestCase $test */
  1159. Assert::assertSame('bar', $options['default']);
  1160. return 'normalized';
  1161. });
  1162. $this->assertEquals([
  1163. 'default' => 'bar',
  1164. 'norm' => 'normalized',
  1165. ], $this->resolver->resolve());
  1166. }
  1167. public function testNormalizerCanAccessLazyOptions()
  1168. {
  1169. $this->resolver->setDefault('lazy', function (Options $options) {
  1170. return 'bar';
  1171. });
  1172. $this->resolver->setDefault('norm', 'baz');
  1173. $this->resolver->setNormalizer('norm', function (Options $options) {
  1174. /* @var TestCase $test */
  1175. Assert::assertEquals('bar', $options['lazy']);
  1176. return 'normalized';
  1177. });
  1178. $this->assertEquals([
  1179. 'lazy' => 'bar',
  1180. 'norm' => 'normalized',
  1181. ], $this->resolver->resolve());
  1182. }
  1183. /**
  1184. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  1185. */
  1186. public function testFailIfCyclicDependencyBetweenNormalizers()
  1187. {
  1188. $this->resolver->setDefault('norm1', 'bar');
  1189. $this->resolver->setDefault('norm2', 'baz');
  1190. $this->resolver->setNormalizer('norm1', function (Options $options) {
  1191. $options['norm2'];
  1192. });
  1193. $this->resolver->setNormalizer('norm2', function (Options $options) {
  1194. $options['norm1'];
  1195. });
  1196. $this->resolver->resolve();
  1197. }
  1198. /**
  1199. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  1200. */
  1201. public function testFailIfCyclicDependencyBetweenNormalizerAndLazyOption()
  1202. {
  1203. $this->resolver->setDefault('lazy', function (Options $options) {
  1204. $options['norm'];
  1205. });
  1206. $this->resolver->setDefault('norm', 'baz');
  1207. $this->resolver->setNormalizer('norm', function (Options $options) {
  1208. $options['lazy'];
  1209. });
  1210. $this->resolver->resolve();
  1211. }
  1212. public function testCaughtExceptionFromNormalizerDoesNotCrashOptionResolver()
  1213. {
  1214. $throw = true;
  1215. $this->resolver->setDefaults(['catcher' => null, 'thrower' => null]);
  1216. $this->resolver->setNormalizer('catcher', function (Options $options) {
  1217. try {
  1218. return $options['thrower'];
  1219. } catch (\Exception $e) {
  1220. return false;
  1221. }
  1222. });
  1223. $this->resolver->setNormalizer('thrower', function () use (&$throw) {
  1224. if ($throw) {
  1225. $throw = false;
  1226. throw new \UnexpectedValueException('throwing');
  1227. }
  1228. return true;
  1229. });
  1230. $this->assertSame(['catcher' => false, 'thrower' => true], $this->resolver->resolve());
  1231. }
  1232. public function testCaughtExceptionFromLazyDoesNotCrashOptionResolver()
  1233. {
  1234. $throw = true;
  1235. $this->resolver->setDefault('catcher', function (Options $options) {
  1236. try {
  1237. return $options['thrower'];
  1238. } catch (\Exception $e) {
  1239. return false;
  1240. }
  1241. });
  1242. $this->resolver->setDefault('thrower', function (Options $options) use (&$throw) {
  1243. if ($throw) {
  1244. $throw = false;
  1245. throw new \UnexpectedValueException('throwing');
  1246. }
  1247. return true;
  1248. });
  1249. $this->assertSame(['catcher' => false, 'thrower' => true], $this->resolver->resolve());
  1250. }
  1251. public function testInvokeEachNormalizerOnlyOnce()
  1252. {
  1253. $calls = 0;
  1254. $this->resolver->setDefault('norm1', 'bar');
  1255. $this->resolver->setDefault('norm2', 'baz');
  1256. $this->resolver->setNormalizer('norm1', function ($options) use (&$calls) {
  1257. Assert::assertSame(1, ++$calls);
  1258. $options['norm2'];
  1259. });
  1260. $this->resolver->setNormalizer('norm2', function () use (&$calls) {
  1261. Assert::assertSame(2, ++$calls);
  1262. });
  1263. $this->resolver->resolve();
  1264. $this->assertSame(2, $calls);
  1265. }
  1266. public function testNormalizerNotCalledForUnsetOptions()
  1267. {
  1268. $this->resolver->setDefined('norm');
  1269. $this->resolver->setNormalizer('norm', function () {
  1270. Assert::fail('Should not be called.');
  1271. });
  1272. $this->assertEmpty($this->resolver->resolve());
  1273. }
  1274. public function testAddNormalizerReturnsThis()
  1275. {
  1276. $this->resolver->setDefault('foo', 'bar');
  1277. $this->assertSame($this->resolver, $this->resolver->addNormalizer('foo', function () {}));
  1278. }
  1279. public function testAddNormalizerClosure()
  1280. {
  1281. // defined by superclass
  1282. $this->resolver->setDefault('foo', 'bar');
  1283. $this->resolver->setNormalizer('foo', function (Options $options, $value) {
  1284. return '1st-normalized-'.$value;
  1285. });
  1286. // defined by subclass
  1287. $this->resolver->addNormalizer('foo', function (Options $options, $value) {
  1288. return '2nd-normalized-'.$value;
  1289. });
  1290. $this->assertEquals(['foo' => '2nd-normalized-1st-normalized-bar'], $this->resolver->resolve());
  1291. }
  1292. public function testForcePrependNormalizerClosure()
  1293. {
  1294. // defined by superclass
  1295. $this->resolver->setDefault('foo', 'bar');
  1296. $this->resolver->setNormalizer('foo', function (Options $options, $value) {
  1297. return '2nd-normalized-'.$value;
  1298. });
  1299. // defined by subclass
  1300. $this->resolver->addNormalizer('foo', function (Options $options, $value) {
  1301. return '1st-normalized-'.$value;
  1302. }, true);
  1303. $this->assertEquals(['foo' => '2nd-normalized-1st-normalized-bar'], $this->resolver->resolve());
  1304. }
  1305. /**
  1306. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  1307. */
  1308. public function testAddNormalizerFailsIfUnknownOption()
  1309. {
  1310. $this->resolver->addNormalizer('foo', function () {});
  1311. }
  1312. /**
  1313. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  1314. */
  1315. public function testFailIfAddNormalizerFromLazyOption()
  1316. {
  1317. $this->resolver->setDefault('foo', function (Options $options) {
  1318. $options->addNormalizer('foo', function () {});
  1319. });
  1320. $this->resolver->resolve();
  1321. }
  1322. public function testSetDefaultsReturnsThis()
  1323. {
  1324. $this->assertSame($this->resolver, $this->resolver->setDefaults(['foo', 'bar']));
  1325. }
  1326. public function testSetDefaults()
  1327. {
  1328. $this->resolver->setDefault('one', '1');
  1329. $this->resolver->setDefault('two', 'bar');
  1330. $this->resolver->setDefaults([
  1331. 'two' => '2',
  1332. 'three' => '3',
  1333. ]);
  1334. $this->assertEquals([
  1335. 'one' => '1',
  1336. 'two' => '2',
  1337. 'three' => '3',
  1338. ], $this->resolver->resolve());
  1339. }
  1340. /**
  1341. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  1342. */
  1343. public function testFailIfSetDefaultsFromLazyOption()
  1344. {
  1345. $this->resolver->setDefault('foo', function (Options $options) {
  1346. $options->setDefaults(['two' => '2']);
  1347. });
  1348. $this->resolver->resolve();
  1349. }
  1350. public function testRemoveReturnsThis()
  1351. {
  1352. $this->resolver->setDefault('foo', 'bar');
  1353. $this->assertSame($this->resolver, $this->resolver->remove('foo'));
  1354. }
  1355. public function testRemoveSingleOption()
  1356. {
  1357. $this->resolver->setDefault('foo', 'bar');
  1358. $this->resolver->setDefault('baz', 'boo');
  1359. $this->resolver->remove('foo');
  1360. $this->assertSame(['baz' => 'boo'], $this->resolver->resolve());
  1361. }
  1362. public function testRemoveMultipleOptions()
  1363. {
  1364. $this->resolver->setDefault('foo', 'bar');
  1365. $this->resolver->setDefault('baz', 'boo');
  1366. $this->resolver->setDefault('doo', 'dam');
  1367. $this->resolver->remove(['foo', 'doo']);
  1368. $this->assertSame(['baz' => 'boo'], $this->resolver->resolve());
  1369. }
  1370. public function testRemoveLazyOption()
  1371. {
  1372. $this->resolver->setDefault('foo', function (Options $options) {
  1373. return 'lazy';
  1374. });
  1375. $this->resolver->remove('foo');
  1376. $this->assertSame([], $this->resolver->resolve());
  1377. }
  1378. public function testRemoveNormalizer()
  1379. {
  1380. $this->resolver->setDefault('foo', 'bar');
  1381. $this->resolver->setNormalizer('foo', function (Options $options, $value) {
  1382. return 'normalized';
  1383. });
  1384. $this->resolver->remove('foo');
  1385. $this->resolver->setDefault('foo', 'bar');
  1386. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  1387. }
  1388. public function testRemoveAllowedTypes()
  1389. {
  1390. $this->resolver->setDefault('foo', 'bar');
  1391. $this->resolver->setAllowedTypes('foo', 'int');
  1392. $this->resolver->remove('foo');
  1393. $this->resolver->setDefault('foo', 'bar');
  1394. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  1395. }
  1396. public function testRemoveAllowedValues()
  1397. {
  1398. $this->resolver->setDefault('foo', 'bar');
  1399. $this->resolver->setAllowedValues('foo', ['baz', 'boo']);
  1400. $this->resolver->remove('foo');
  1401. $this->resolver->setDefault('foo', 'bar');
  1402. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  1403. }
  1404. /**
  1405. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  1406. */
  1407. public function testFailIfRemoveFromLazyOption()
  1408. {
  1409. $this->resolver->setDefault('foo', function (Options $options) {
  1410. $options->remove('bar');
  1411. });
  1412. $this->resolver->setDefault('bar', 'baz');
  1413. $this->resolver->resolve();
  1414. }
  1415. public function testRemoveUnknownOptionIgnored()
  1416. {
  1417. $this->assertNotNull($this->resolver->remove('foo'));
  1418. }
  1419. public function testClearReturnsThis()
  1420. {
  1421. $this->assertSame($this->resolver, $this->resolver->clear());
  1422. }
  1423. public function testClearRemovesAllOptions()
  1424. {
  1425. $this->resolver->setDefault('one', 1);
  1426. $this->resolver->setDefault('two', 2);
  1427. $this->resolver->clear();
  1428. $this->assertEmpty($this->resolver->resolve());
  1429. }
  1430. public function testClearLazyOption()
  1431. {
  1432. $this->resolver->setDefault('foo', function (Options $options) {
  1433. return 'lazy';
  1434. });
  1435. $this->resolver->clear();
  1436. $this->assertSame([], $this->resolver->resolve());
  1437. }
  1438. public function testClearNormalizer()
  1439. {
  1440. $this->resolver->setDefault('foo', 'bar');
  1441. $this->resolver->setNormalizer('foo', function (Options $options, $value) {
  1442. return 'normalized';
  1443. });
  1444. $this->resolver->clear();
  1445. $this->resolver->setDefault('foo', 'bar');
  1446. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  1447. }
  1448. public function testClearAllowedTypes()
  1449. {
  1450. $this->resolver->setDefault('foo', 'bar');
  1451. $this->resolver->setAllowedTypes('foo', 'int');
  1452. $this->resolver->clear();
  1453. $this->resolver->setDefault('foo', 'bar');
  1454. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  1455. }
  1456. public function testClearAllowedValues()
  1457. {
  1458. $this->resolver->setDefault('foo', 'bar');
  1459. $this->resolver->setAllowedValues('foo', 'baz');
  1460. $this->resolver->clear();
  1461. $this->resolver->setDefault('foo', 'bar');
  1462. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  1463. }
  1464. /**
  1465. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  1466. */
  1467. public function testFailIfClearFromLazyption()
  1468. {
  1469. $this->resolver->setDefault('foo', function (Options $options) {
  1470. $options->clear();
  1471. });
  1472. $this->resolver->setDefault('bar', 'baz');
  1473. $this->resolver->resolve();
  1474. }
  1475. public function testClearOptionAndNormalizer()
  1476. {
  1477. $this->resolver->setDefault('foo1', 'bar');
  1478. $this->resolver->setNormalizer('foo1', function (Options $options) {
  1479. return '';
  1480. });
  1481. $this->resolver->setDefault('foo2', 'bar');
  1482. $this->resolver->setNormalizer('foo2', function (Options $options) {
  1483. return '';
  1484. });
  1485. $this->resolver->clear();
  1486. $this->assertEmpty($this->resolver->resolve());
  1487. }
  1488. public function testArrayAccess()
  1489. {
  1490. $this->resolver->setDefault('default1', 0);
  1491. $this->resolver->setDefault('default2', 1);
  1492. $this->resolver->setRequired('required');
  1493. $this->resolver->setDefined('defined');
  1494. $this->resolver->setDefault('lazy1', function (Options $options) {
  1495. return 'lazy';
  1496. });
  1497. $this->resolver->setDefault('lazy2', function (Options $options) {
  1498. Assert::assertArrayHasKey('default1', $options);
  1499. Assert::assertArrayHasKey('default2', $options);
  1500. Assert::assertArrayHasKey('required', $options);
  1501. Assert::assertArrayHasKey('lazy1', $options);
  1502. Assert::assertArrayHasKey('lazy2', $options);
  1503. Assert::assertArrayNotHasKey('defined', $options);
  1504. Assert::assertSame(0, $options['default1']);
  1505. Assert::assertSame(42, $options['default2']);
  1506. Assert::assertSame('value', $options['required']);
  1507. Assert::assertSame('lazy', $options['lazy1']);
  1508. // Obviously $options['lazy'] and $options['defined'] cannot be
  1509. // accessed
  1510. });
  1511. $this->resolver->resolve(['default2' => 42, 'required' => 'value']);
  1512. }
  1513. /**
  1514. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  1515. */
  1516. public function testArrayAccessGetFailsOutsideResolve()
  1517. {
  1518. $this->resolver->setDefault('default', 0);
  1519. $this->resolver['default'];
  1520. }
  1521. /**
  1522. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  1523. */
  1524. public function testArrayAccessExistsFailsOutsideResolve()
  1525. {
  1526. $this->resolver->setDefault('default', 0);
  1527. isset($this->resolver['default']);
  1528. }
  1529. /**
  1530. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  1531. */
  1532. public function testArrayAccessSetNotSupported()
  1533. {
  1534. $this->resolver['default'] = 0;
  1535. }
  1536. /**
  1537. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  1538. */
  1539. public function testArrayAccessUnsetNotSupported()
  1540. {
  1541. $this->resolver->setDefault('default', 0);
  1542. unset($this->resolver['default']);
  1543. }
  1544. /**
  1545. * @expectedException \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException
  1546. * @expectedExceptionMessage The option "undefined" does not exist. Defined options are: "foo", "lazy".
  1547. */
  1548. public function testFailIfGetNonExisting()
  1549. {
  1550. $this->resolver->setDefault('foo', 'bar');
  1551. $this->resolver->setDefault('lazy', function (Options $options) {
  1552. $options['undefined'];
  1553. });
  1554. $this->resolver->resolve();
  1555. }
  1556. /**
  1557. * @expectedException \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException
  1558. * @expectedExceptionMessage The optional option "defined" has no value set. You should make sure it is set with "isset" before reading it.
  1559. */
  1560. public function testFailIfGetDefinedButUnset()
  1561. {
  1562. $this->resolver->setDefined('defined');
  1563. $this->resolver->setDefault('lazy', function (Options $options) {
  1564. $options['defined'];
  1565. });
  1566. $this->resolver->resolve();
  1567. }
  1568. /**
  1569. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  1570. */
  1571. public function testFailIfCyclicDependency()
  1572. {
  1573. $this->resolver->setDefault('lazy1', function (Options $options) {
  1574. $options['lazy2'];
  1575. });
  1576. $this->resolver->setDefault('lazy2', function (Options $options) {
  1577. $options['lazy1'];
  1578. });
  1579. $this->resolver->resolve();
  1580. }
  1581. public function testCount()
  1582. {
  1583. $this->resolver->setDefault('default', 0);
  1584. $this->resolver->setRequired('required');
  1585. $this->resolver->setDefined('defined');
  1586. $this->resolver->setDefault('lazy1', function () {});
  1587. $this->resolver->setDefault('lazy2', function (Options $options) {
  1588. Assert::assertCount(4, $options);
  1589. });
  1590. $this->assertCount(4, $this->resolver->resolve(['required' => 'value']));
  1591. }
  1592. /**
  1593. * In resolve() we count the options that are actually set (which may be
  1594. * only a subset of the defined options). Outside of resolve(), it's not
  1595. * clear what is counted.
  1596. *
  1597. * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
  1598. */
  1599. public function testCountFailsOutsideResolve()
  1600. {
  1601. $this->resolver->setDefault('foo', 0);
  1602. $this->resolver->setRequired('bar');
  1603. $this->resolver->setDefined('bar');
  1604. $this->resolver->setDefault('lazy1', function () {});
  1605. \count($this->resolver);
  1606. }
  1607. public function testNestedArrays()
  1608. {
  1609. $this->resolver->setDefined('foo');
  1610. $this->resolver->setAllowedTypes('foo', 'int[][]');
  1611. $this->assertEquals([
  1612. 'foo' => [
  1613. [
  1614. 1, 2,
  1615. ],
  1616. ],
  1617. ], $this->resolver->resolve([
  1618. 'foo' => [
  1619. [1, 2],
  1620. ],
  1621. ]));
  1622. }
  1623. public function testNested2Arrays()
  1624. {
  1625. $this->resolver->setDefined('foo');
  1626. $this->resolver->setAllowedTypes('foo', 'int[][][][]');
  1627. $this->assertEquals([
  1628. 'foo' => [
  1629. [
  1630. [
  1631. [
  1632. 1, 2,
  1633. ],
  1634. ],
  1635. ],
  1636. ],
  1637. ], $this->resolver->resolve(
  1638. [
  1639. 'foo' => [
  1640. [
  1641. [
  1642. [1, 2],
  1643. ],
  1644. ],
  1645. ],
  1646. ]
  1647. ));
  1648. }
  1649. /**
  1650. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  1651. * @expectedExceptionMessage The option "foo" with value array is expected to be of type "float[][][][]", but one of the elements is of type "integer[][][][]".
  1652. */
  1653. public function testNestedArraysException()
  1654. {
  1655. $this->resolver->setDefined('foo');
  1656. $this->resolver->setAllowedTypes('foo', 'float[][][][]');
  1657. $this->resolver->resolve([
  1658. 'foo' => [
  1659. [
  1660. [
  1661. [1, 2],
  1662. ],
  1663. ],
  1664. ],
  1665. ]);
  1666. }
  1667. /**
  1668. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  1669. * @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "boolean[][]".
  1670. */
  1671. public function testNestedArrayException1()
  1672. {
  1673. $this->resolver->setDefined('foo');
  1674. $this->resolver->setAllowedTypes('foo', 'int[][]');
  1675. $this->resolver->resolve([
  1676. 'foo' => [
  1677. [1, true, 'str', [2, 3]],
  1678. ],
  1679. ]);
  1680. }
  1681. /**
  1682. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  1683. * @expectedExceptionMessage The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "boolean[][]".
  1684. */
  1685. public function testNestedArrayException2()
  1686. {
  1687. $this->resolver->setDefined('foo');
  1688. $this->resolver->setAllowedTypes('foo', 'int[][]');
  1689. $this->resolver->resolve([
  1690. 'foo' => [
  1691. [true, 'str', [2, 3]],
  1692. ],
  1693. ]);
  1694. }
  1695. /**
  1696. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  1697. * @expectedExceptionMessage The option "foo" with value array is expected to be of type "string[][][]", but one of the elements is of type "string[][]".
  1698. */
  1699. public function testNestedArrayException3()
  1700. {
  1701. $this->resolver->setDefined('foo');
  1702. $this->resolver->setAllowedTypes('foo', 'string[][][]');
  1703. $this->resolver->resolve([
  1704. 'foo' => [
  1705. ['str', [1, 2]],
  1706. ],
  1707. ]);
  1708. }
  1709. /**
  1710. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  1711. * @expectedExceptionMessage The option "foo" with value array is expected to be of type "string[][][]", but one of the elements is of type "integer[][][]".
  1712. */
  1713. public function testNestedArrayException4()
  1714. {
  1715. $this->resolver->setDefined('foo');
  1716. $this->resolver->setAllowedTypes('foo', 'string[][][]');
  1717. $this->resolver->resolve([
  1718. 'foo' => [
  1719. [
  1720. ['str'], [1, 2], ],
  1721. ],
  1722. ]);
  1723. }
  1724. /**
  1725. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  1726. * @expectedExceptionMessage The option "foo" with value array is expected to be of type "string[]", but one of the elements is of type "array[]".
  1727. */
  1728. public function testNestedArrayException5()
  1729. {
  1730. $this->resolver->setDefined('foo');
  1731. $this->resolver->setAllowedTypes('foo', 'string[]');
  1732. $this->resolver->resolve([
  1733. 'foo' => [
  1734. [
  1735. ['str'], [1, 2], ],
  1736. ],
  1737. ]);
  1738. }
  1739. public function testIsNestedOption()
  1740. {
  1741. $this->resolver->setDefaults([
  1742. 'database' => function (OptionsResolver $resolver) {
  1743. $resolver->setDefined(['host', 'port']);
  1744. },
  1745. ]);
  1746. $this->assertTrue($this->resolver->isNested('database'));
  1747. }
  1748. /**
  1749. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  1750. * @expectedExceptionMessage The option "foo" does not exist. Defined options are: "host", "port".
  1751. */
  1752. public function testFailsIfUndefinedNestedOption()
  1753. {
  1754. $this->resolver->setDefaults([
  1755. 'name' => 'default',
  1756. 'database' => function (OptionsResolver $resolver) {
  1757. $resolver->setDefined(['host', 'port']);
  1758. },
  1759. ]);
  1760. $this->resolver->resolve([
  1761. 'database' => ['foo' => 'bar'],
  1762. ]);
  1763. }
  1764. /**
  1765. * @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
  1766. * @expectedExceptionMessage The required option "host" is missing.
  1767. */
  1768. public function testFailsIfMissingRequiredNestedOption()
  1769. {
  1770. $this->resolver->setDefaults([
  1771. 'name' => 'default',
  1772. 'database' => function (OptionsResolver $resolver) {
  1773. $resolver->setRequired('host');
  1774. },
  1775. ]);
  1776. $this->resolver->resolve([
  1777. 'database' => [],
  1778. ]);
  1779. }
  1780. /**
  1781. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  1782. * @expectedExceptionMessage The option "logging" with value null is expected to be of type "bool", but is of type "NULL".
  1783. */
  1784. public function testFailsIfInvalidTypeNestedOption()
  1785. {
  1786. $this->resolver->setDefaults([
  1787. 'name' => 'default',
  1788. 'database' => function (OptionsResolver $resolver) {
  1789. $resolver
  1790. ->setDefined('logging')
  1791. ->setAllowedTypes('logging', 'bool');
  1792. },
  1793. ]);
  1794. $this->resolver->resolve([
  1795. 'database' => ['logging' => null],
  1796. ]);
  1797. }
  1798. /**
  1799. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  1800. * @expectedExceptionMessage The nested option "database" with value null is expected to be of type array, but is of type "NULL".
  1801. */
  1802. public function testFailsIfNotArrayIsGivenForNestedOptions()
  1803. {
  1804. $this->resolver->setDefaults([
  1805. 'name' => 'default',
  1806. 'database' => function (OptionsResolver $resolver) {
  1807. $resolver->setDefined('host');
  1808. },
  1809. ]);
  1810. $this->resolver->resolve([
  1811. 'database' => null,
  1812. ]);
  1813. }
  1814. public function testResolveNestedOptionsWithoutDefault()
  1815. {
  1816. $this->resolver->setDefaults([
  1817. 'name' => 'default',
  1818. 'database' => function (OptionsResolver $resolver) {
  1819. $resolver->setDefined(['host', 'port']);
  1820. },
  1821. ]);
  1822. $actualOptions = $this->resolver->resolve();
  1823. $expectedOptions = [
  1824. 'name' => 'default',
  1825. 'database' => [],
  1826. ];
  1827. $this->assertSame($expectedOptions, $actualOptions);
  1828. }
  1829. public function testResolveNestedOptionsWithDefault()
  1830. {
  1831. $this->resolver->setDefaults([
  1832. 'name' => 'default',
  1833. 'database' => function (OptionsResolver $resolver) {
  1834. $resolver->setDefaults([
  1835. 'host' => 'localhost',
  1836. 'port' => 3306,
  1837. ]);
  1838. },
  1839. ]);
  1840. $actualOptions = $this->resolver->resolve();
  1841. $expectedOptions = [
  1842. 'name' => 'default',
  1843. 'database' => [
  1844. 'host' => 'localhost',
  1845. 'port' => 3306,
  1846. ],
  1847. ];
  1848. $this->assertSame($expectedOptions, $actualOptions);
  1849. }
  1850. public function testResolveMultipleNestedOptions()
  1851. {
  1852. $this->resolver->setDefaults([
  1853. 'name' => 'default',
  1854. 'database' => function (OptionsResolver $resolver) {
  1855. $resolver
  1856. ->setRequired(['dbname', 'host'])
  1857. ->setDefaults([
  1858. 'port' => 3306,
  1859. 'replicas' => function (OptionsResolver $resolver) {
  1860. $resolver->setDefaults([
  1861. 'host' => 'replica1',
  1862. 'port' => 3306,
  1863. ]);
  1864. },
  1865. ]);
  1866. },
  1867. ]);
  1868. $actualOptions = $this->resolver->resolve([
  1869. 'name' => 'custom',
  1870. 'database' => [
  1871. 'dbname' => 'test',
  1872. 'host' => 'localhost',
  1873. 'port' => null,
  1874. 'replicas' => ['host' => 'replica2'],
  1875. ],
  1876. ]);
  1877. $expectedOptions = [
  1878. 'name' => 'custom',
  1879. 'database' => [
  1880. 'port' => null,
  1881. 'replicas' => ['port' => 3306, 'host' => 'replica2'],
  1882. 'dbname' => 'test',
  1883. 'host' => 'localhost',
  1884. ],
  1885. ];
  1886. $this->assertSame($expectedOptions, $actualOptions);
  1887. }
  1888. public function testResolveLazyOptionUsingNestedOption()
  1889. {
  1890. $this->resolver->setDefaults([
  1891. 'version' => function (Options $options) {
  1892. return $options['database']['server_version'];
  1893. },
  1894. 'database' => function (OptionsResolver $resolver) {
  1895. $resolver->setDefault('server_version', '3.15');
  1896. },
  1897. ]);
  1898. $actualOptions = $this->resolver->resolve();
  1899. $expectedOptions = [
  1900. 'database' => ['server_version' => '3.15'],
  1901. 'version' => '3.15',
  1902. ];
  1903. $this->assertSame($expectedOptions, $actualOptions);
  1904. }
  1905. public function testNormalizeNestedOptionValue()
  1906. {
  1907. $this->resolver
  1908. ->setDefaults([
  1909. 'database' => function (OptionsResolver $resolver) {
  1910. $resolver->setDefaults([
  1911. 'port' => 3306,
  1912. 'host' => 'localhost',
  1913. 'dbname' => 'demo',
  1914. ]);
  1915. },
  1916. ])
  1917. ->setNormalizer('database', function (Options $options, $value) {
  1918. ksort($value);
  1919. return $value;
  1920. });
  1921. $actualOptions = $this->resolver->resolve([
  1922. 'database' => ['dbname' => 'test'],
  1923. ]);
  1924. $expectedOptions = [
  1925. 'database' => ['dbname' => 'test', 'host' => 'localhost', 'port' => 3306],
  1926. ];
  1927. $this->assertSame($expectedOptions, $actualOptions);
  1928. }
  1929. public function testOverwrittenNestedOptionNotEvaluatedIfLazyDefault()
  1930. {
  1931. // defined by superclass
  1932. $this->resolver->setDefault('foo', function (OptionsResolver $resolver) {
  1933. Assert::fail('Should not be called');
  1934. });
  1935. // defined by subclass
  1936. $this->resolver->setDefault('foo', function (Options $options) {
  1937. return 'lazy';
  1938. });
  1939. $this->assertSame(['foo' => 'lazy'], $this->resolver->resolve());
  1940. }
  1941. public function testOverwrittenNestedOptionNotEvaluatedIfScalarDefault()
  1942. {
  1943. // defined by superclass
  1944. $this->resolver->setDefault('foo', function (OptionsResolver $resolver) {
  1945. Assert::fail('Should not be called');
  1946. });
  1947. // defined by subclass
  1948. $this->resolver->setDefault('foo', 'bar');
  1949. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  1950. }
  1951. public function testOverwrittenLazyOptionNotEvaluatedIfNestedOption()
  1952. {
  1953. // defined by superclass
  1954. $this->resolver->setDefault('foo', function (Options $options) {
  1955. Assert::fail('Should not be called');
  1956. });
  1957. // defined by subclass
  1958. $this->resolver->setDefault('foo', function (OptionsResolver $resolver) {
  1959. $resolver->setDefault('bar', 'baz');
  1960. });
  1961. $this->assertSame(['foo' => ['bar' => 'baz']], $this->resolver->resolve());
  1962. }
  1963. public function testResolveAllNestedOptionDefinitions()
  1964. {
  1965. // defined by superclass
  1966. $this->resolver->setDefault('foo', function (OptionsResolver $resolver) {
  1967. $resolver->setRequired('bar');
  1968. });
  1969. // defined by subclass
  1970. $this->resolver->setDefault('foo', function (OptionsResolver $resolver) {
  1971. $resolver->setDefault('bar', 'baz');
  1972. });
  1973. // defined by subclass
  1974. $this->resolver->setDefault('foo', function (OptionsResolver $resolver) {
  1975. $resolver->setDefault('ping', 'pong');
  1976. });
  1977. $this->assertSame(['foo' => ['ping' => 'pong', 'bar' => 'baz']], $this->resolver->resolve());
  1978. }
  1979. public function testNormalizeNestedValue()
  1980. {
  1981. // defined by superclass
  1982. $this->resolver->setDefault('foo', function (OptionsResolver $resolver) {
  1983. $resolver->setDefault('bar', null);
  1984. });
  1985. // defined by subclass
  1986. $this->resolver->setNormalizer('foo', function (Options $options, $resolvedValue) {
  1987. if (null === $resolvedValue['bar']) {
  1988. $resolvedValue['bar'] = 'baz';
  1989. }
  1990. return $resolvedValue;
  1991. });
  1992. $this->assertSame(['foo' => ['bar' => 'baz']], $this->resolver->resolve());
  1993. }
  1994. /**
  1995. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  1996. */
  1997. public function testFailsIfCyclicDependencyBetweenSameNestedOption()
  1998. {
  1999. $this->resolver->setDefault('database', function (OptionsResolver $resolver, Options $parent) {
  2000. $resolver->setDefault('replicas', $parent['database']);
  2001. });
  2002. $this->resolver->resolve();
  2003. }
  2004. /**
  2005. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  2006. */
  2007. public function testFailsIfCyclicDependencyBetweenNestedOptionAndParentLazyOption()
  2008. {
  2009. $this->resolver->setDefaults([
  2010. 'version' => function (Options $options) {
  2011. return $options['database']['server_version'];
  2012. },
  2013. 'database' => function (OptionsResolver $resolver, Options $parent) {
  2014. $resolver->setDefault('server_version', $parent['version']);
  2015. },
  2016. ]);
  2017. $this->resolver->resolve();
  2018. }
  2019. /**
  2020. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  2021. */
  2022. public function testFailsIfCyclicDependencyBetweenNormalizerAndNestedOption()
  2023. {
  2024. $this->resolver
  2025. ->setDefault('name', 'default')
  2026. ->setDefault('database', function (OptionsResolver $resolver, Options $parent) {
  2027. $resolver->setDefault('host', $parent['name']);
  2028. })
  2029. ->setNormalizer('name', function (Options $options, $value) {
  2030. $options['database'];
  2031. });
  2032. $this->resolver->resolve();
  2033. }
  2034. /**
  2035. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  2036. */
  2037. public function testFailsIfCyclicDependencyBetweenNestedOptions()
  2038. {
  2039. $this->resolver->setDefault('database', function (OptionsResolver $resolver, Options $parent) {
  2040. $resolver->setDefault('host', $parent['replica']['host']);
  2041. });
  2042. $this->resolver->setDefault('replica', function (OptionsResolver $resolver, Options $parent) {
  2043. $resolver->setDefault('host', $parent['database']['host']);
  2044. });
  2045. $this->resolver->resolve();
  2046. }
  2047. public function testGetAccessToParentOptionFromNestedOption()
  2048. {
  2049. $this->resolver->setDefaults([
  2050. 'version' => 3.15,
  2051. 'database' => function (OptionsResolver $resolver, Options $parent) {
  2052. $resolver->setDefault('server_version', $parent['version']);
  2053. },
  2054. ]);
  2055. $this->assertSame(['version' => 3.15, 'database' => ['server_version' => 3.15]], $this->resolver->resolve());
  2056. }
  2057. public function testNestedClosureWithoutTypeHintNotInvoked()
  2058. {
  2059. $closure = function ($resolver) {
  2060. Assert::fail('Should not be called');
  2061. };
  2062. $this->resolver->setDefault('foo', $closure);
  2063. $this->assertSame(['foo' => $closure], $this->resolver->resolve());
  2064. }
  2065. public function testNestedClosureWithoutTypeHint2ndArgumentNotInvoked()
  2066. {
  2067. $closure = function (OptionsResolver $resolver, $parent) {
  2068. Assert::fail('Should not be called');
  2069. };
  2070. $this->resolver->setDefault('foo', $closure);
  2071. $this->assertSame(['foo' => $closure], $this->resolver->resolve());
  2072. }
  2073. public function testResolveLazyOptionWithTransitiveDefaultDependency()
  2074. {
  2075. $this->resolver->setDefaults([
  2076. 'ip' => null,
  2077. 'database' => function (OptionsResolver $resolver, Options $parent) {
  2078. $resolver->setDefault('host', $parent['ip']);
  2079. $resolver->setDefault('primary_replica', function (OptionsResolver $resolver, Options $parent) {
  2080. $resolver->setDefault('host', $parent['host']);
  2081. });
  2082. },
  2083. 'secondary_replica' => function (Options $options) {
  2084. return $options['database']['primary_replica']['host'];
  2085. },
  2086. ]);
  2087. $actualOptions = $this->resolver->resolve(['ip' => '127.0.0.1']);
  2088. $expectedOptions = [
  2089. 'ip' => '127.0.0.1',
  2090. 'database' => [
  2091. 'host' => '127.0.0.1',
  2092. 'primary_replica' => ['host' => '127.0.0.1'],
  2093. ],
  2094. 'secondary_replica' => '127.0.0.1',
  2095. ];
  2096. $this->assertSame($expectedOptions, $actualOptions);
  2097. }
  2098. public function testAccessToParentOptionFromNestedNormalizerAndLazyOption()
  2099. {
  2100. $this->resolver->setDefaults([
  2101. 'debug' => true,
  2102. 'database' => function (OptionsResolver $resolver, Options $parent) {
  2103. $resolver
  2104. ->setDefined('logging')
  2105. ->setDefault('profiling', function (Options $options) use ($parent) {
  2106. return $parent['debug'];
  2107. })
  2108. ->setNormalizer('logging', function (Options $options, $value) use ($parent) {
  2109. return false === $parent['debug'] ? true : $value;
  2110. });
  2111. },
  2112. ]);
  2113. $actualOptions = $this->resolver->resolve([
  2114. 'debug' => false,
  2115. 'database' => ['logging' => false],
  2116. ]);
  2117. $expectedOptions = [
  2118. 'debug' => false,
  2119. 'database' => ['profiling' => false, 'logging' => true],
  2120. ];
  2121. $this->assertSame($expectedOptions, $actualOptions);
  2122. }
  2123. }