OptionsResolverTest.php 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  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. public function testResolveFailsIfNonExistingOption()
  27. {
  28. $this->expectException('Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException');
  29. $this->expectExceptionMessage('The option "foo" does not exist. Defined options are: "a", "z".');
  30. $this->resolver->setDefault('z', '1');
  31. $this->resolver->setDefault('a', '2');
  32. $this->resolver->resolve(['foo' => 'bar']);
  33. }
  34. public function testResolveFailsIfMultipleNonExistingOptions()
  35. {
  36. $this->expectException('Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException');
  37. $this->expectExceptionMessage('The options "baz", "foo", "ping" do not exist. Defined options are: "a", "z".');
  38. $this->resolver->setDefault('z', '1');
  39. $this->resolver->setDefault('a', '2');
  40. $this->resolver->resolve(['ping' => 'pong', 'foo' => 'bar', 'baz' => 'bam']);
  41. }
  42. public function testResolveFailsFromLazyOption()
  43. {
  44. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  45. $this->resolver->setDefault('foo', function (Options $options) {
  46. $options->resolve([]);
  47. });
  48. $this->resolver->resolve();
  49. }
  50. public function testSetDefaultReturnsThis()
  51. {
  52. $this->assertSame($this->resolver, $this->resolver->setDefault('foo', 'bar'));
  53. }
  54. public function testSetDefault()
  55. {
  56. $this->resolver->setDefault('one', '1');
  57. $this->resolver->setDefault('two', '20');
  58. $this->assertEquals([
  59. 'one' => '1',
  60. 'two' => '20',
  61. ], $this->resolver->resolve());
  62. }
  63. public function testFailIfSetDefaultFromLazyOption()
  64. {
  65. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  66. $this->resolver->setDefault('lazy', function (Options $options) {
  67. $options->setDefault('default', 42);
  68. });
  69. $this->resolver->resolve();
  70. }
  71. public function testHasDefault()
  72. {
  73. $this->assertFalse($this->resolver->hasDefault('foo'));
  74. $this->resolver->setDefault('foo', 42);
  75. $this->assertTrue($this->resolver->hasDefault('foo'));
  76. }
  77. public function testHasDefaultWithNullValue()
  78. {
  79. $this->assertFalse($this->resolver->hasDefault('foo'));
  80. $this->resolver->setDefault('foo', null);
  81. $this->assertTrue($this->resolver->hasDefault('foo'));
  82. }
  83. public function testSetLazyReturnsThis()
  84. {
  85. $this->assertSame($this->resolver, $this->resolver->setDefault('foo', function (Options $options) {}));
  86. }
  87. public function testSetLazyClosure()
  88. {
  89. $this->resolver->setDefault('foo', function (Options $options) {
  90. return 'lazy';
  91. });
  92. $this->assertEquals(['foo' => 'lazy'], $this->resolver->resolve());
  93. }
  94. public function testClosureWithoutTypeHintNotInvoked()
  95. {
  96. $closure = function ($options) {
  97. Assert::fail('Should not be called');
  98. };
  99. $this->resolver->setDefault('foo', $closure);
  100. $this->assertSame(['foo' => $closure], $this->resolver->resolve());
  101. }
  102. public function testClosureWithoutParametersNotInvoked()
  103. {
  104. $closure = function () {
  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 testAccessPreviousDefaultValue()
  111. {
  112. // defined by superclass
  113. $this->resolver->setDefault('foo', 'bar');
  114. // defined by subclass
  115. $this->resolver->setDefault('foo', function (Options $options, $previousValue) {
  116. Assert::assertEquals('bar', $previousValue);
  117. return 'lazy';
  118. });
  119. $this->assertEquals(['foo' => 'lazy'], $this->resolver->resolve());
  120. }
  121. public function testAccessPreviousLazyDefaultValue()
  122. {
  123. // defined by superclass
  124. $this->resolver->setDefault('foo', function (Options $options) {
  125. return 'bar';
  126. });
  127. // defined by subclass
  128. $this->resolver->setDefault('foo', function (Options $options, $previousValue) {
  129. Assert::assertEquals('bar', $previousValue);
  130. return 'lazy';
  131. });
  132. $this->assertEquals(['foo' => 'lazy'], $this->resolver->resolve());
  133. }
  134. public function testPreviousValueIsNotEvaluatedIfNoSecondArgument()
  135. {
  136. // defined by superclass
  137. $this->resolver->setDefault('foo', function () {
  138. Assert::fail('Should not be called');
  139. });
  140. // defined by subclass, no $previousValue argument defined!
  141. $this->resolver->setDefault('foo', function (Options $options) {
  142. return 'lazy';
  143. });
  144. $this->assertEquals(['foo' => 'lazy'], $this->resolver->resolve());
  145. }
  146. public function testOverwrittenLazyOptionNotEvaluated()
  147. {
  148. $this->resolver->setDefault('foo', function (Options $options) {
  149. Assert::fail('Should not be called');
  150. });
  151. $this->resolver->setDefault('foo', 'bar');
  152. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  153. }
  154. public function testInvokeEachLazyOptionOnlyOnce()
  155. {
  156. $calls = 0;
  157. $this->resolver->setDefault('lazy1', function (Options $options) use (&$calls) {
  158. Assert::assertSame(1, ++$calls);
  159. $options['lazy2'];
  160. });
  161. $this->resolver->setDefault('lazy2', function (Options $options) use (&$calls) {
  162. Assert::assertSame(2, ++$calls);
  163. });
  164. $this->resolver->resolve();
  165. $this->assertSame(2, $calls);
  166. }
  167. public function testSetRequiredReturnsThis()
  168. {
  169. $this->assertSame($this->resolver, $this->resolver->setRequired('foo'));
  170. }
  171. public function testFailIfSetRequiredFromLazyOption()
  172. {
  173. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  174. $this->resolver->setDefault('foo', function (Options $options) {
  175. $options->setRequired('bar');
  176. });
  177. $this->resolver->resolve();
  178. }
  179. public function testResolveFailsIfRequiredOptionMissing()
  180. {
  181. $this->expectException('Symfony\Component\OptionsResolver\Exception\MissingOptionsException');
  182. $this->resolver->setRequired('foo');
  183. $this->resolver->resolve();
  184. }
  185. public function testResolveSucceedsIfRequiredOptionSet()
  186. {
  187. $this->resolver->setRequired('foo');
  188. $this->resolver->setDefault('foo', 'bar');
  189. $this->assertNotEmpty($this->resolver->resolve());
  190. }
  191. public function testResolveSucceedsIfRequiredOptionPassed()
  192. {
  193. $this->resolver->setRequired('foo');
  194. $this->assertNotEmpty($this->resolver->resolve(['foo' => 'bar']));
  195. }
  196. public function testIsRequired()
  197. {
  198. $this->assertFalse($this->resolver->isRequired('foo'));
  199. $this->resolver->setRequired('foo');
  200. $this->assertTrue($this->resolver->isRequired('foo'));
  201. }
  202. public function testRequiredIfSetBefore()
  203. {
  204. $this->assertFalse($this->resolver->isRequired('foo'));
  205. $this->resolver->setDefault('foo', 'bar');
  206. $this->resolver->setRequired('foo');
  207. $this->assertTrue($this->resolver->isRequired('foo'));
  208. }
  209. public function testStillRequiredAfterSet()
  210. {
  211. $this->assertFalse($this->resolver->isRequired('foo'));
  212. $this->resolver->setRequired('foo');
  213. $this->resolver->setDefault('foo', 'bar');
  214. $this->assertTrue($this->resolver->isRequired('foo'));
  215. }
  216. public function testIsNotRequiredAfterRemove()
  217. {
  218. $this->assertFalse($this->resolver->isRequired('foo'));
  219. $this->resolver->setRequired('foo');
  220. $this->resolver->remove('foo');
  221. $this->assertFalse($this->resolver->isRequired('foo'));
  222. }
  223. public function testIsNotRequiredAfterClear()
  224. {
  225. $this->assertFalse($this->resolver->isRequired('foo'));
  226. $this->resolver->setRequired('foo');
  227. $this->resolver->clear();
  228. $this->assertFalse($this->resolver->isRequired('foo'));
  229. }
  230. public function testGetRequiredOptions()
  231. {
  232. $this->resolver->setRequired(['foo', 'bar']);
  233. $this->resolver->setDefault('bam', 'baz');
  234. $this->resolver->setDefault('foo', 'boo');
  235. $this->assertSame(['foo', 'bar'], $this->resolver->getRequiredOptions());
  236. }
  237. public function testIsMissingIfNotSet()
  238. {
  239. $this->assertFalse($this->resolver->isMissing('foo'));
  240. $this->resolver->setRequired('foo');
  241. $this->assertTrue($this->resolver->isMissing('foo'));
  242. }
  243. public function testIsNotMissingIfSet()
  244. {
  245. $this->resolver->setDefault('foo', 'bar');
  246. $this->assertFalse($this->resolver->isMissing('foo'));
  247. $this->resolver->setRequired('foo');
  248. $this->assertFalse($this->resolver->isMissing('foo'));
  249. }
  250. public function testIsNotMissingAfterRemove()
  251. {
  252. $this->resolver->setRequired('foo');
  253. $this->resolver->remove('foo');
  254. $this->assertFalse($this->resolver->isMissing('foo'));
  255. }
  256. public function testIsNotMissingAfterClear()
  257. {
  258. $this->resolver->setRequired('foo');
  259. $this->resolver->clear();
  260. $this->assertFalse($this->resolver->isRequired('foo'));
  261. }
  262. public function testGetMissingOptions()
  263. {
  264. $this->resolver->setRequired(['foo', 'bar']);
  265. $this->resolver->setDefault('bam', 'baz');
  266. $this->resolver->setDefault('foo', 'boo');
  267. $this->assertSame(['bar'], $this->resolver->getMissingOptions());
  268. }
  269. public function testFailIfSetDefinedFromLazyOption()
  270. {
  271. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  272. $this->resolver->setDefault('foo', function (Options $options) {
  273. $options->setDefined('bar');
  274. });
  275. $this->resolver->resolve();
  276. }
  277. public function testDefinedOptionsNotIncludedInResolvedOptions()
  278. {
  279. $this->resolver->setDefined('foo');
  280. $this->assertSame([], $this->resolver->resolve());
  281. }
  282. public function testDefinedOptionsIncludedIfDefaultSetBefore()
  283. {
  284. $this->resolver->setDefault('foo', 'bar');
  285. $this->resolver->setDefined('foo');
  286. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  287. }
  288. public function testDefinedOptionsIncludedIfDefaultSetAfter()
  289. {
  290. $this->resolver->setDefined('foo');
  291. $this->resolver->setDefault('foo', 'bar');
  292. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  293. }
  294. public function testDefinedOptionsIncludedIfPassedToResolve()
  295. {
  296. $this->resolver->setDefined('foo');
  297. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve(['foo' => 'bar']));
  298. }
  299. public function testIsDefined()
  300. {
  301. $this->assertFalse($this->resolver->isDefined('foo'));
  302. $this->resolver->setDefined('foo');
  303. $this->assertTrue($this->resolver->isDefined('foo'));
  304. }
  305. public function testLazyOptionsAreDefined()
  306. {
  307. $this->assertFalse($this->resolver->isDefined('foo'));
  308. $this->resolver->setDefault('foo', function (Options $options) {});
  309. $this->assertTrue($this->resolver->isDefined('foo'));
  310. }
  311. public function testRequiredOptionsAreDefined()
  312. {
  313. $this->assertFalse($this->resolver->isDefined('foo'));
  314. $this->resolver->setRequired('foo');
  315. $this->assertTrue($this->resolver->isDefined('foo'));
  316. }
  317. public function testSetOptionsAreDefined()
  318. {
  319. $this->assertFalse($this->resolver->isDefined('foo'));
  320. $this->resolver->setDefault('foo', 'bar');
  321. $this->assertTrue($this->resolver->isDefined('foo'));
  322. }
  323. public function testGetDefinedOptions()
  324. {
  325. $this->resolver->setDefined(['foo', 'bar']);
  326. $this->resolver->setDefault('baz', 'bam');
  327. $this->resolver->setRequired('boo');
  328. $this->assertSame(['foo', 'bar', 'baz', 'boo'], $this->resolver->getDefinedOptions());
  329. }
  330. public function testRemovedOptionsAreNotDefined()
  331. {
  332. $this->assertFalse($this->resolver->isDefined('foo'));
  333. $this->resolver->setDefined('foo');
  334. $this->assertTrue($this->resolver->isDefined('foo'));
  335. $this->resolver->remove('foo');
  336. $this->assertFalse($this->resolver->isDefined('foo'));
  337. }
  338. public function testClearedOptionsAreNotDefined()
  339. {
  340. $this->assertFalse($this->resolver->isDefined('foo'));
  341. $this->resolver->setDefined('foo');
  342. $this->assertTrue($this->resolver->isDefined('foo'));
  343. $this->resolver->clear();
  344. $this->assertFalse($this->resolver->isDefined('foo'));
  345. }
  346. public function testSetAllowedTypesFailsIfUnknownOption()
  347. {
  348. $this->expectException('Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException');
  349. $this->resolver->setAllowedTypes('foo', 'string');
  350. }
  351. public function testResolveTypedArray()
  352. {
  353. $this->resolver->setDefined('foo');
  354. $this->resolver->setAllowedTypes('foo', 'string[]');
  355. $options = $this->resolver->resolve(['foo' => ['bar', 'baz']]);
  356. $this->assertSame(['foo' => ['bar', 'baz']], $options);
  357. }
  358. public function testFailIfSetAllowedTypesFromLazyOption()
  359. {
  360. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  361. $this->resolver->setDefault('foo', function (Options $options) {
  362. $options->setAllowedTypes('bar', 'string');
  363. });
  364. $this->resolver->setDefault('bar', 'baz');
  365. $this->resolver->resolve();
  366. }
  367. public function testResolveFailsIfInvalidTypedArray()
  368. {
  369. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  370. $this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[]", but one of the elements is of type "DateTime[]".');
  371. $this->resolver->setDefined('foo');
  372. $this->resolver->setAllowedTypes('foo', 'int[]');
  373. $this->resolver->resolve(['foo' => [new \DateTime()]]);
  374. }
  375. public function testResolveFailsWithNonArray()
  376. {
  377. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  378. $this->expectExceptionMessage('The option "foo" with value "bar" is expected to be of type "int[]", but is of type "string".');
  379. $this->resolver->setDefined('foo');
  380. $this->resolver->setAllowedTypes('foo', 'int[]');
  381. $this->resolver->resolve(['foo' => 'bar']);
  382. }
  383. public function testResolveFailsIfTypedArrayContainsInvalidTypes()
  384. {
  385. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  386. $this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[]", but one of the elements is of type "stdClass[]".');
  387. $this->resolver->setDefined('foo');
  388. $this->resolver->setAllowedTypes('foo', 'int[]');
  389. $values = range(1, 5);
  390. $values[] = new \stdClass();
  391. $values[] = [];
  392. $values[] = new \DateTime();
  393. $values[] = 123;
  394. $this->resolver->resolve(['foo' => $values]);
  395. }
  396. public function testResolveFailsWithCorrectLevelsButWrongScalar()
  397. {
  398. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  399. $this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "double[][]".');
  400. $this->resolver->setDefined('foo');
  401. $this->resolver->setAllowedTypes('foo', 'int[][]');
  402. $this->resolver->resolve([
  403. 'foo' => [
  404. [1.2],
  405. ],
  406. ]);
  407. }
  408. /**
  409. * @dataProvider provideInvalidTypes
  410. */
  411. public function testResolveFailsIfInvalidType($actualType, $allowedType, $exceptionMessage)
  412. {
  413. $this->resolver->setDefined('option');
  414. $this->resolver->setAllowedTypes('option', $allowedType);
  415. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  416. $this->expectExceptionMessage($exceptionMessage);
  417. $this->resolver->resolve(['option' => $actualType]);
  418. }
  419. public function provideInvalidTypes()
  420. {
  421. return [
  422. [true, 'string', 'The option "option" with value true is expected to be of type "string", but is of type "boolean".'],
  423. [false, 'string', 'The option "option" with value false is expected to be of type "string", but is of type "boolean".'],
  424. [fopen(__FILE__, 'r'), 'string', 'The option "option" with value resource is expected to be of type "string", but is of type "resource".'],
  425. [[], 'string', 'The option "option" with value array is expected to be of type "string", but is of type "array".'],
  426. [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".'],
  427. [42, 'string', 'The option "option" with value 42 is expected to be of type "string", but is of type "integer".'],
  428. [null, 'string', 'The option "option" with value null is expected to be of type "string", but is of type "NULL".'],
  429. ['bar', '\stdClass', 'The option "option" with value "bar" is expected to be of type "\stdClass", but is of type "string".'],
  430. ];
  431. }
  432. public function testResolveSucceedsIfValidType()
  433. {
  434. $this->resolver->setDefault('foo', 'bar');
  435. $this->resolver->setAllowedTypes('foo', 'string');
  436. $this->assertNotEmpty($this->resolver->resolve());
  437. }
  438. public function testResolveFailsIfInvalidTypeMultiple()
  439. {
  440. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  441. $this->expectExceptionMessage('The option "foo" with value 42 is expected to be of type "string" or "bool", but is of type "integer".');
  442. $this->resolver->setDefault('foo', 42);
  443. $this->resolver->setAllowedTypes('foo', ['string', 'bool']);
  444. $this->resolver->resolve();
  445. }
  446. public function testResolveSucceedsIfValidTypeMultiple()
  447. {
  448. $this->resolver->setDefault('foo', true);
  449. $this->resolver->setAllowedTypes('foo', ['string', 'bool']);
  450. $this->assertNotEmpty($this->resolver->resolve());
  451. }
  452. public function testResolveSucceedsIfInstanceOfClass()
  453. {
  454. $this->resolver->setDefault('foo', new \stdClass());
  455. $this->resolver->setAllowedTypes('foo', '\stdClass');
  456. $this->assertNotEmpty($this->resolver->resolve());
  457. }
  458. public function testResolveSucceedsIfTypedArray()
  459. {
  460. $this->resolver->setDefault('foo', null);
  461. $this->resolver->setAllowedTypes('foo', ['null', 'DateTime[]']);
  462. $data = [
  463. 'foo' => [
  464. new \DateTime(),
  465. new \DateTime(),
  466. ],
  467. ];
  468. $result = $this->resolver->resolve($data);
  469. $this->assertEquals($data, $result);
  470. }
  471. public function testResolveFailsIfNotInstanceOfClass()
  472. {
  473. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  474. $this->resolver->setDefault('foo', 'bar');
  475. $this->resolver->setAllowedTypes('foo', '\stdClass');
  476. $this->resolver->resolve();
  477. }
  478. public function testAddAllowedTypesFailsIfUnknownOption()
  479. {
  480. $this->expectException('Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException');
  481. $this->resolver->addAllowedTypes('foo', 'string');
  482. }
  483. public function testFailIfAddAllowedTypesFromLazyOption()
  484. {
  485. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  486. $this->resolver->setDefault('foo', function (Options $options) {
  487. $options->addAllowedTypes('bar', 'string');
  488. });
  489. $this->resolver->setDefault('bar', 'baz');
  490. $this->resolver->resolve();
  491. }
  492. public function testResolveFailsIfInvalidAddedType()
  493. {
  494. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  495. $this->resolver->setDefault('foo', 42);
  496. $this->resolver->addAllowedTypes('foo', 'string');
  497. $this->resolver->resolve();
  498. }
  499. public function testResolveSucceedsIfValidAddedType()
  500. {
  501. $this->resolver->setDefault('foo', 'bar');
  502. $this->resolver->addAllowedTypes('foo', 'string');
  503. $this->assertNotEmpty($this->resolver->resolve());
  504. }
  505. public function testResolveFailsIfInvalidAddedTypeMultiple()
  506. {
  507. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  508. $this->resolver->setDefault('foo', 42);
  509. $this->resolver->addAllowedTypes('foo', ['string', 'bool']);
  510. $this->resolver->resolve();
  511. }
  512. public function testResolveSucceedsIfValidAddedTypeMultiple()
  513. {
  514. $this->resolver->setDefault('foo', 'bar');
  515. $this->resolver->addAllowedTypes('foo', ['string', 'bool']);
  516. $this->assertNotEmpty($this->resolver->resolve());
  517. }
  518. public function testAddAllowedTypesDoesNotOverwrite()
  519. {
  520. $this->resolver->setDefault('foo', 'bar');
  521. $this->resolver->setAllowedTypes('foo', 'string');
  522. $this->resolver->addAllowedTypes('foo', 'bool');
  523. $this->resolver->setDefault('foo', 'bar');
  524. $this->assertNotEmpty($this->resolver->resolve());
  525. }
  526. public function testAddAllowedTypesDoesNotOverwrite2()
  527. {
  528. $this->resolver->setDefault('foo', 'bar');
  529. $this->resolver->setAllowedTypes('foo', 'string');
  530. $this->resolver->addAllowedTypes('foo', 'bool');
  531. $this->resolver->setDefault('foo', false);
  532. $this->assertNotEmpty($this->resolver->resolve());
  533. }
  534. public function testSetAllowedValuesFailsIfUnknownOption()
  535. {
  536. $this->expectException('Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException');
  537. $this->resolver->setAllowedValues('foo', 'bar');
  538. }
  539. public function testFailIfSetAllowedValuesFromLazyOption()
  540. {
  541. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  542. $this->resolver->setDefault('foo', function (Options $options) {
  543. $options->setAllowedValues('bar', 'baz');
  544. });
  545. $this->resolver->setDefault('bar', 'baz');
  546. $this->resolver->resolve();
  547. }
  548. public function testResolveFailsIfInvalidValue()
  549. {
  550. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  551. $this->expectExceptionMessage('The option "foo" with value 42 is invalid. Accepted values are: "bar".');
  552. $this->resolver->setDefined('foo');
  553. $this->resolver->setAllowedValues('foo', 'bar');
  554. $this->resolver->resolve(['foo' => 42]);
  555. }
  556. public function testResolveFailsIfInvalidValueIsNull()
  557. {
  558. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  559. $this->expectExceptionMessage('The option "foo" with value null is invalid. Accepted values are: "bar".');
  560. $this->resolver->setDefault('foo', null);
  561. $this->resolver->setAllowedValues('foo', 'bar');
  562. $this->resolver->resolve();
  563. }
  564. public function testResolveFailsIfInvalidValueStrict()
  565. {
  566. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  567. $this->resolver->setDefault('foo', 42);
  568. $this->resolver->setAllowedValues('foo', '42');
  569. $this->resolver->resolve();
  570. }
  571. public function testResolveSucceedsIfValidValue()
  572. {
  573. $this->resolver->setDefault('foo', 'bar');
  574. $this->resolver->setAllowedValues('foo', 'bar');
  575. $this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
  576. }
  577. public function testResolveSucceedsIfValidValueIsNull()
  578. {
  579. $this->resolver->setDefault('foo', null);
  580. $this->resolver->setAllowedValues('foo', null);
  581. $this->assertEquals(['foo' => null], $this->resolver->resolve());
  582. }
  583. public function testResolveFailsIfInvalidValueMultiple()
  584. {
  585. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  586. $this->expectExceptionMessage('The option "foo" with value 42 is invalid. Accepted values are: "bar", false, null.');
  587. $this->resolver->setDefault('foo', 42);
  588. $this->resolver->setAllowedValues('foo', ['bar', false, null]);
  589. $this->resolver->resolve();
  590. }
  591. public function testResolveSucceedsIfValidValueMultiple()
  592. {
  593. $this->resolver->setDefault('foo', 'baz');
  594. $this->resolver->setAllowedValues('foo', ['bar', 'baz']);
  595. $this->assertEquals(['foo' => 'baz'], $this->resolver->resolve());
  596. }
  597. public function testResolveFailsIfClosureReturnsFalse()
  598. {
  599. $this->resolver->setDefault('foo', 42);
  600. $this->resolver->setAllowedValues('foo', function ($value) use (&$passedValue) {
  601. $passedValue = $value;
  602. return false;
  603. });
  604. try {
  605. $this->resolver->resolve();
  606. $this->fail('Should fail');
  607. } catch (InvalidOptionsException $e) {
  608. }
  609. $this->assertSame(42, $passedValue);
  610. }
  611. public function testResolveSucceedsIfClosureReturnsTrue()
  612. {
  613. $this->resolver->setDefault('foo', 'bar');
  614. $this->resolver->setAllowedValues('foo', function ($value) use (&$passedValue) {
  615. $passedValue = $value;
  616. return true;
  617. });
  618. $this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
  619. $this->assertSame('bar', $passedValue);
  620. }
  621. public function testResolveFailsIfAllClosuresReturnFalse()
  622. {
  623. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  624. $this->resolver->setDefault('foo', 42);
  625. $this->resolver->setAllowedValues('foo', [
  626. function () { return false; },
  627. function () { return false; },
  628. function () { return false; },
  629. ]);
  630. $this->resolver->resolve();
  631. }
  632. public function testResolveSucceedsIfAnyClosureReturnsTrue()
  633. {
  634. $this->resolver->setDefault('foo', 'bar');
  635. $this->resolver->setAllowedValues('foo', [
  636. function () { return false; },
  637. function () { return true; },
  638. function () { return false; },
  639. ]);
  640. $this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
  641. }
  642. public function testAddAllowedValuesFailsIfUnknownOption()
  643. {
  644. $this->expectException('Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException');
  645. $this->resolver->addAllowedValues('foo', 'bar');
  646. }
  647. public function testFailIfAddAllowedValuesFromLazyOption()
  648. {
  649. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  650. $this->resolver->setDefault('foo', function (Options $options) {
  651. $options->addAllowedValues('bar', 'baz');
  652. });
  653. $this->resolver->setDefault('bar', 'baz');
  654. $this->resolver->resolve();
  655. }
  656. public function testResolveFailsIfInvalidAddedValue()
  657. {
  658. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  659. $this->resolver->setDefault('foo', 42);
  660. $this->resolver->addAllowedValues('foo', 'bar');
  661. $this->resolver->resolve();
  662. }
  663. public function testResolveSucceedsIfValidAddedValue()
  664. {
  665. $this->resolver->setDefault('foo', 'bar');
  666. $this->resolver->addAllowedValues('foo', 'bar');
  667. $this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
  668. }
  669. public function testResolveSucceedsIfValidAddedValueIsNull()
  670. {
  671. $this->resolver->setDefault('foo', null);
  672. $this->resolver->addAllowedValues('foo', null);
  673. $this->assertEquals(['foo' => null], $this->resolver->resolve());
  674. }
  675. public function testResolveFailsIfInvalidAddedValueMultiple()
  676. {
  677. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  678. $this->resolver->setDefault('foo', 42);
  679. $this->resolver->addAllowedValues('foo', ['bar', 'baz']);
  680. $this->resolver->resolve();
  681. }
  682. public function testResolveSucceedsIfValidAddedValueMultiple()
  683. {
  684. $this->resolver->setDefault('foo', 'bar');
  685. $this->resolver->addAllowedValues('foo', ['bar', 'baz']);
  686. $this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
  687. }
  688. public function testAddAllowedValuesDoesNotOverwrite()
  689. {
  690. $this->resolver->setDefault('foo', 'bar');
  691. $this->resolver->setAllowedValues('foo', 'bar');
  692. $this->resolver->addAllowedValues('foo', 'baz');
  693. $this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
  694. }
  695. public function testAddAllowedValuesDoesNotOverwrite2()
  696. {
  697. $this->resolver->setDefault('foo', 'baz');
  698. $this->resolver->setAllowedValues('foo', 'bar');
  699. $this->resolver->addAllowedValues('foo', 'baz');
  700. $this->assertEquals(['foo' => 'baz'], $this->resolver->resolve());
  701. }
  702. public function testResolveFailsIfAllAddedClosuresReturnFalse()
  703. {
  704. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  705. $this->resolver->setDefault('foo', 42);
  706. $this->resolver->setAllowedValues('foo', function () { return false; });
  707. $this->resolver->addAllowedValues('foo', function () { return false; });
  708. $this->resolver->resolve();
  709. }
  710. public function testResolveSucceedsIfAnyAddedClosureReturnsTrue()
  711. {
  712. $this->resolver->setDefault('foo', 'bar');
  713. $this->resolver->setAllowedValues('foo', function () { return false; });
  714. $this->resolver->addAllowedValues('foo', function () { return true; });
  715. $this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
  716. }
  717. public function testResolveSucceedsIfAnyAddedClosureReturnsTrue2()
  718. {
  719. $this->resolver->setDefault('foo', 'bar');
  720. $this->resolver->setAllowedValues('foo', function () { return true; });
  721. $this->resolver->addAllowedValues('foo', function () { return false; });
  722. $this->assertEquals(['foo' => 'bar'], $this->resolver->resolve());
  723. }
  724. public function testSetNormalizerReturnsThis()
  725. {
  726. $this->resolver->setDefault('foo', 'bar');
  727. $this->assertSame($this->resolver, $this->resolver->setNormalizer('foo', function () {}));
  728. }
  729. public function testSetNormalizerClosure()
  730. {
  731. $this->resolver->setDefault('foo', 'bar');
  732. $this->resolver->setNormalizer('foo', function () {
  733. return 'normalized';
  734. });
  735. $this->assertEquals(['foo' => 'normalized'], $this->resolver->resolve());
  736. }
  737. public function testSetNormalizerFailsIfUnknownOption()
  738. {
  739. $this->expectException('Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException');
  740. $this->resolver->setNormalizer('foo', function () {});
  741. }
  742. public function testFailIfSetNormalizerFromLazyOption()
  743. {
  744. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  745. $this->resolver->setDefault('foo', function (Options $options) {
  746. $options->setNormalizer('foo', function () {});
  747. });
  748. $this->resolver->setDefault('bar', 'baz');
  749. $this->resolver->resolve();
  750. }
  751. public function testNormalizerReceivesSetOption()
  752. {
  753. $this->resolver->setDefault('foo', 'bar');
  754. $this->resolver->setNormalizer('foo', function (Options $options, $value) {
  755. return 'normalized['.$value.']';
  756. });
  757. $this->assertEquals(['foo' => 'normalized[bar]'], $this->resolver->resolve());
  758. }
  759. public function testNormalizerReceivesPassedOption()
  760. {
  761. $this->resolver->setDefault('foo', 'bar');
  762. $this->resolver->setNormalizer('foo', function (Options $options, $value) {
  763. return 'normalized['.$value.']';
  764. });
  765. $resolved = $this->resolver->resolve(['foo' => 'baz']);
  766. $this->assertEquals(['foo' => 'normalized[baz]'], $resolved);
  767. }
  768. public function testValidateTypeBeforeNormalization()
  769. {
  770. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  771. $this->resolver->setDefault('foo', 'bar');
  772. $this->resolver->setAllowedTypes('foo', 'int');
  773. $this->resolver->setNormalizer('foo', function () {
  774. Assert::fail('Should not be called.');
  775. });
  776. $this->resolver->resolve();
  777. }
  778. public function testValidateValueBeforeNormalization()
  779. {
  780. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  781. $this->resolver->setDefault('foo', 'bar');
  782. $this->resolver->setAllowedValues('foo', 'baz');
  783. $this->resolver->setNormalizer('foo', function () {
  784. Assert::fail('Should not be called.');
  785. });
  786. $this->resolver->resolve();
  787. }
  788. public function testNormalizerCanAccessOtherOptions()
  789. {
  790. $this->resolver->setDefault('default', 'bar');
  791. $this->resolver->setDefault('norm', 'baz');
  792. $this->resolver->setNormalizer('norm', function (Options $options) {
  793. /* @var TestCase $test */
  794. Assert::assertSame('bar', $options['default']);
  795. return 'normalized';
  796. });
  797. $this->assertEquals([
  798. 'default' => 'bar',
  799. 'norm' => 'normalized',
  800. ], $this->resolver->resolve());
  801. }
  802. public function testNormalizerCanAccessLazyOptions()
  803. {
  804. $this->resolver->setDefault('lazy', function (Options $options) {
  805. return 'bar';
  806. });
  807. $this->resolver->setDefault('norm', 'baz');
  808. $this->resolver->setNormalizer('norm', function (Options $options) {
  809. /* @var TestCase $test */
  810. Assert::assertEquals('bar', $options['lazy']);
  811. return 'normalized';
  812. });
  813. $this->assertEquals([
  814. 'lazy' => 'bar',
  815. 'norm' => 'normalized',
  816. ], $this->resolver->resolve());
  817. }
  818. public function testFailIfCyclicDependencyBetweenNormalizers()
  819. {
  820. $this->expectException('Symfony\Component\OptionsResolver\Exception\OptionDefinitionException');
  821. $this->resolver->setDefault('norm1', 'bar');
  822. $this->resolver->setDefault('norm2', 'baz');
  823. $this->resolver->setNormalizer('norm1', function (Options $options) {
  824. $options['norm2'];
  825. });
  826. $this->resolver->setNormalizer('norm2', function (Options $options) {
  827. $options['norm1'];
  828. });
  829. $this->resolver->resolve();
  830. }
  831. public function testFailIfCyclicDependencyBetweenNormalizerAndLazyOption()
  832. {
  833. $this->expectException('Symfony\Component\OptionsResolver\Exception\OptionDefinitionException');
  834. $this->resolver->setDefault('lazy', function (Options $options) {
  835. $options['norm'];
  836. });
  837. $this->resolver->setDefault('norm', 'baz');
  838. $this->resolver->setNormalizer('norm', function (Options $options) {
  839. $options['lazy'];
  840. });
  841. $this->resolver->resolve();
  842. }
  843. public function testCaughtExceptionFromNormalizerDoesNotCrashOptionResolver()
  844. {
  845. $throw = true;
  846. $this->resolver->setDefaults(['catcher' => null, 'thrower' => null]);
  847. $this->resolver->setNormalizer('catcher', function (Options $options) {
  848. try {
  849. return $options['thrower'];
  850. } catch (\Exception $e) {
  851. return false;
  852. }
  853. });
  854. $this->resolver->setNormalizer('thrower', function () use (&$throw) {
  855. if ($throw) {
  856. $throw = false;
  857. throw new \UnexpectedValueException('throwing');
  858. }
  859. return true;
  860. });
  861. $this->assertSame(['catcher' => false, 'thrower' => true], $this->resolver->resolve());
  862. }
  863. public function testCaughtExceptionFromLazyDoesNotCrashOptionResolver()
  864. {
  865. $throw = true;
  866. $this->resolver->setDefault('catcher', function (Options $options) {
  867. try {
  868. return $options['thrower'];
  869. } catch (\Exception $e) {
  870. return false;
  871. }
  872. });
  873. $this->resolver->setDefault('thrower', function (Options $options) use (&$throw) {
  874. if ($throw) {
  875. $throw = false;
  876. throw new \UnexpectedValueException('throwing');
  877. }
  878. return true;
  879. });
  880. $this->assertSame(['catcher' => false, 'thrower' => true], $this->resolver->resolve());
  881. }
  882. public function testInvokeEachNormalizerOnlyOnce()
  883. {
  884. $calls = 0;
  885. $this->resolver->setDefault('norm1', 'bar');
  886. $this->resolver->setDefault('norm2', 'baz');
  887. $this->resolver->setNormalizer('norm1', function ($options) use (&$calls) {
  888. Assert::assertSame(1, ++$calls);
  889. $options['norm2'];
  890. });
  891. $this->resolver->setNormalizer('norm2', function () use (&$calls) {
  892. Assert::assertSame(2, ++$calls);
  893. });
  894. $this->resolver->resolve();
  895. $this->assertSame(2, $calls);
  896. }
  897. public function testNormalizerNotCalledForUnsetOptions()
  898. {
  899. $this->resolver->setDefined('norm');
  900. $this->resolver->setNormalizer('norm', function () {
  901. Assert::fail('Should not be called.');
  902. });
  903. $this->assertEmpty($this->resolver->resolve());
  904. }
  905. public function testSetDefaultsReturnsThis()
  906. {
  907. $this->assertSame($this->resolver, $this->resolver->setDefaults(['foo', 'bar']));
  908. }
  909. public function testSetDefaults()
  910. {
  911. $this->resolver->setDefault('one', '1');
  912. $this->resolver->setDefault('two', 'bar');
  913. $this->resolver->setDefaults([
  914. 'two' => '2',
  915. 'three' => '3',
  916. ]);
  917. $this->assertEquals([
  918. 'one' => '1',
  919. 'two' => '2',
  920. 'three' => '3',
  921. ], $this->resolver->resolve());
  922. }
  923. public function testFailIfSetDefaultsFromLazyOption()
  924. {
  925. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  926. $this->resolver->setDefault('foo', function (Options $options) {
  927. $options->setDefaults(['two' => '2']);
  928. });
  929. $this->resolver->resolve();
  930. }
  931. public function testRemoveReturnsThis()
  932. {
  933. $this->resolver->setDefault('foo', 'bar');
  934. $this->assertSame($this->resolver, $this->resolver->remove('foo'));
  935. }
  936. public function testRemoveSingleOption()
  937. {
  938. $this->resolver->setDefault('foo', 'bar');
  939. $this->resolver->setDefault('baz', 'boo');
  940. $this->resolver->remove('foo');
  941. $this->assertSame(['baz' => 'boo'], $this->resolver->resolve());
  942. }
  943. public function testRemoveMultipleOptions()
  944. {
  945. $this->resolver->setDefault('foo', 'bar');
  946. $this->resolver->setDefault('baz', 'boo');
  947. $this->resolver->setDefault('doo', 'dam');
  948. $this->resolver->remove(['foo', 'doo']);
  949. $this->assertSame(['baz' => 'boo'], $this->resolver->resolve());
  950. }
  951. public function testRemoveLazyOption()
  952. {
  953. $this->resolver->setDefault('foo', function (Options $options) {
  954. return 'lazy';
  955. });
  956. $this->resolver->remove('foo');
  957. $this->assertSame([], $this->resolver->resolve());
  958. }
  959. public function testRemoveNormalizer()
  960. {
  961. $this->resolver->setDefault('foo', 'bar');
  962. $this->resolver->setNormalizer('foo', function (Options $options, $value) {
  963. return 'normalized';
  964. });
  965. $this->resolver->remove('foo');
  966. $this->resolver->setDefault('foo', 'bar');
  967. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  968. }
  969. public function testRemoveAllowedTypes()
  970. {
  971. $this->resolver->setDefault('foo', 'bar');
  972. $this->resolver->setAllowedTypes('foo', 'int');
  973. $this->resolver->remove('foo');
  974. $this->resolver->setDefault('foo', 'bar');
  975. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  976. }
  977. public function testRemoveAllowedValues()
  978. {
  979. $this->resolver->setDefault('foo', 'bar');
  980. $this->resolver->setAllowedValues('foo', ['baz', 'boo']);
  981. $this->resolver->remove('foo');
  982. $this->resolver->setDefault('foo', 'bar');
  983. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  984. }
  985. public function testFailIfRemoveFromLazyOption()
  986. {
  987. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  988. $this->resolver->setDefault('foo', function (Options $options) {
  989. $options->remove('bar');
  990. });
  991. $this->resolver->setDefault('bar', 'baz');
  992. $this->resolver->resolve();
  993. }
  994. public function testRemoveUnknownOptionIgnored()
  995. {
  996. $this->assertNotNull($this->resolver->remove('foo'));
  997. }
  998. public function testClearReturnsThis()
  999. {
  1000. $this->assertSame($this->resolver, $this->resolver->clear());
  1001. }
  1002. public function testClearRemovesAllOptions()
  1003. {
  1004. $this->resolver->setDefault('one', 1);
  1005. $this->resolver->setDefault('two', 2);
  1006. $this->resolver->clear();
  1007. $this->assertEmpty($this->resolver->resolve());
  1008. }
  1009. public function testClearLazyOption()
  1010. {
  1011. $this->resolver->setDefault('foo', function (Options $options) {
  1012. return 'lazy';
  1013. });
  1014. $this->resolver->clear();
  1015. $this->assertSame([], $this->resolver->resolve());
  1016. }
  1017. public function testClearNormalizer()
  1018. {
  1019. $this->resolver->setDefault('foo', 'bar');
  1020. $this->resolver->setNormalizer('foo', function (Options $options, $value) {
  1021. return 'normalized';
  1022. });
  1023. $this->resolver->clear();
  1024. $this->resolver->setDefault('foo', 'bar');
  1025. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  1026. }
  1027. public function testClearAllowedTypes()
  1028. {
  1029. $this->resolver->setDefault('foo', 'bar');
  1030. $this->resolver->setAllowedTypes('foo', 'int');
  1031. $this->resolver->clear();
  1032. $this->resolver->setDefault('foo', 'bar');
  1033. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  1034. }
  1035. public function testClearAllowedValues()
  1036. {
  1037. $this->resolver->setDefault('foo', 'bar');
  1038. $this->resolver->setAllowedValues('foo', 'baz');
  1039. $this->resolver->clear();
  1040. $this->resolver->setDefault('foo', 'bar');
  1041. $this->assertSame(['foo' => 'bar'], $this->resolver->resolve());
  1042. }
  1043. public function testFailIfClearFromLazyption()
  1044. {
  1045. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  1046. $this->resolver->setDefault('foo', function (Options $options) {
  1047. $options->clear();
  1048. });
  1049. $this->resolver->setDefault('bar', 'baz');
  1050. $this->resolver->resolve();
  1051. }
  1052. public function testClearOptionAndNormalizer()
  1053. {
  1054. $this->resolver->setDefault('foo1', 'bar');
  1055. $this->resolver->setNormalizer('foo1', function (Options $options) {
  1056. return '';
  1057. });
  1058. $this->resolver->setDefault('foo2', 'bar');
  1059. $this->resolver->setNormalizer('foo2', function (Options $options) {
  1060. return '';
  1061. });
  1062. $this->resolver->clear();
  1063. $this->assertEmpty($this->resolver->resolve());
  1064. }
  1065. public function testArrayAccess()
  1066. {
  1067. $this->resolver->setDefault('default1', 0);
  1068. $this->resolver->setDefault('default2', 1);
  1069. $this->resolver->setRequired('required');
  1070. $this->resolver->setDefined('defined');
  1071. $this->resolver->setDefault('lazy1', function (Options $options) {
  1072. return 'lazy';
  1073. });
  1074. $this->resolver->setDefault('lazy2', function (Options $options) {
  1075. Assert::assertArrayHasKey('default1', $options);
  1076. Assert::assertArrayHasKey('default2', $options);
  1077. Assert::assertArrayHasKey('required', $options);
  1078. Assert::assertArrayHasKey('lazy1', $options);
  1079. Assert::assertArrayHasKey('lazy2', $options);
  1080. Assert::assertArrayNotHasKey('defined', $options);
  1081. Assert::assertSame(0, $options['default1']);
  1082. Assert::assertSame(42, $options['default2']);
  1083. Assert::assertSame('value', $options['required']);
  1084. Assert::assertSame('lazy', $options['lazy1']);
  1085. // Obviously $options['lazy'] and $options['defined'] cannot be
  1086. // accessed
  1087. });
  1088. $this->resolver->resolve(['default2' => 42, 'required' => 'value']);
  1089. }
  1090. public function testArrayAccessGetFailsOutsideResolve()
  1091. {
  1092. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  1093. $this->resolver->setDefault('default', 0);
  1094. $this->resolver['default'];
  1095. }
  1096. public function testArrayAccessExistsFailsOutsideResolve()
  1097. {
  1098. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  1099. $this->resolver->setDefault('default', 0);
  1100. isset($this->resolver['default']);
  1101. }
  1102. public function testArrayAccessSetNotSupported()
  1103. {
  1104. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  1105. $this->resolver['default'] = 0;
  1106. }
  1107. public function testArrayAccessUnsetNotSupported()
  1108. {
  1109. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  1110. $this->resolver->setDefault('default', 0);
  1111. unset($this->resolver['default']);
  1112. }
  1113. public function testFailIfGetNonExisting()
  1114. {
  1115. $this->expectException('Symfony\Component\OptionsResolver\Exception\NoSuchOptionException');
  1116. $this->expectExceptionMessage('The option "undefined" does not exist. Defined options are: "foo", "lazy".');
  1117. $this->resolver->setDefault('foo', 'bar');
  1118. $this->resolver->setDefault('lazy', function (Options $options) {
  1119. $options['undefined'];
  1120. });
  1121. $this->resolver->resolve();
  1122. }
  1123. public function testFailIfGetDefinedButUnset()
  1124. {
  1125. $this->expectException('Symfony\Component\OptionsResolver\Exception\NoSuchOptionException');
  1126. $this->expectExceptionMessage('The optional option "defined" has no value set. You should make sure it is set with "isset" before reading it.');
  1127. $this->resolver->setDefined('defined');
  1128. $this->resolver->setDefault('lazy', function (Options $options) {
  1129. $options['defined'];
  1130. });
  1131. $this->resolver->resolve();
  1132. }
  1133. public function testFailIfCyclicDependency()
  1134. {
  1135. $this->expectException('Symfony\Component\OptionsResolver\Exception\OptionDefinitionException');
  1136. $this->resolver->setDefault('lazy1', function (Options $options) {
  1137. $options['lazy2'];
  1138. });
  1139. $this->resolver->setDefault('lazy2', function (Options $options) {
  1140. $options['lazy1'];
  1141. });
  1142. $this->resolver->resolve();
  1143. }
  1144. public function testCount()
  1145. {
  1146. $this->resolver->setDefault('default', 0);
  1147. $this->resolver->setRequired('required');
  1148. $this->resolver->setDefined('defined');
  1149. $this->resolver->setDefault('lazy1', function () {});
  1150. $this->resolver->setDefault('lazy2', function (Options $options) {
  1151. Assert::assertCount(4, $options);
  1152. });
  1153. $this->assertCount(4, $this->resolver->resolve(['required' => 'value']));
  1154. }
  1155. /**
  1156. * In resolve() we count the options that are actually set (which may be
  1157. * only a subset of the defined options). Outside of resolve(), it's not
  1158. * clear what is counted.
  1159. */
  1160. public function testCountFailsOutsideResolve()
  1161. {
  1162. $this->expectException('Symfony\Component\OptionsResolver\Exception\AccessException');
  1163. $this->resolver->setDefault('foo', 0);
  1164. $this->resolver->setRequired('bar');
  1165. $this->resolver->setDefined('bar');
  1166. $this->resolver->setDefault('lazy1', function () {});
  1167. \count($this->resolver);
  1168. }
  1169. public function testNestedArrays()
  1170. {
  1171. $this->resolver->setDefined('foo');
  1172. $this->resolver->setAllowedTypes('foo', 'int[][]');
  1173. $this->assertEquals([
  1174. 'foo' => [
  1175. [
  1176. 1, 2,
  1177. ],
  1178. ],
  1179. ], $this->resolver->resolve([
  1180. 'foo' => [
  1181. [1, 2],
  1182. ],
  1183. ]));
  1184. }
  1185. public function testNested2Arrays()
  1186. {
  1187. $this->resolver->setDefined('foo');
  1188. $this->resolver->setAllowedTypes('foo', 'int[][][][]');
  1189. $this->assertEquals([
  1190. 'foo' => [
  1191. [
  1192. [
  1193. [
  1194. 1, 2,
  1195. ],
  1196. ],
  1197. ],
  1198. ],
  1199. ], $this->resolver->resolve(
  1200. [
  1201. 'foo' => [
  1202. [
  1203. [
  1204. [1, 2],
  1205. ],
  1206. ],
  1207. ],
  1208. ]
  1209. ));
  1210. }
  1211. public function testNestedArraysException()
  1212. {
  1213. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  1214. $this->expectExceptionMessage('The option "foo" with value array is expected to be of type "float[][][][]", but one of the elements is of type "integer[][][][]".');
  1215. $this->resolver->setDefined('foo');
  1216. $this->resolver->setAllowedTypes('foo', 'float[][][][]');
  1217. $this->resolver->resolve([
  1218. 'foo' => [
  1219. [
  1220. [
  1221. [1, 2],
  1222. ],
  1223. ],
  1224. ],
  1225. ]);
  1226. }
  1227. public function testNestedArrayException1()
  1228. {
  1229. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  1230. $this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "boolean[][]".');
  1231. $this->resolver->setDefined('foo');
  1232. $this->resolver->setAllowedTypes('foo', 'int[][]');
  1233. $this->resolver->resolve([
  1234. 'foo' => [
  1235. [1, true, 'str', [2, 3]],
  1236. ],
  1237. ]);
  1238. }
  1239. public function testNestedArrayException2()
  1240. {
  1241. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  1242. $this->expectExceptionMessage('The option "foo" with value array is expected to be of type "int[][]", but one of the elements is of type "boolean[][]".');
  1243. $this->resolver->setDefined('foo');
  1244. $this->resolver->setAllowedTypes('foo', 'int[][]');
  1245. $this->resolver->resolve([
  1246. 'foo' => [
  1247. [true, 'str', [2, 3]],
  1248. ],
  1249. ]);
  1250. }
  1251. public function testNestedArrayException3()
  1252. {
  1253. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  1254. $this->expectExceptionMessage('The option "foo" with value array is expected to be of type "string[][][]", but one of the elements is of type "string[][]".');
  1255. $this->resolver->setDefined('foo');
  1256. $this->resolver->setAllowedTypes('foo', 'string[][][]');
  1257. $this->resolver->resolve([
  1258. 'foo' => [
  1259. ['str', [1, 2]],
  1260. ],
  1261. ]);
  1262. }
  1263. public function testNestedArrayException4()
  1264. {
  1265. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  1266. $this->expectExceptionMessage('The option "foo" with value array is expected to be of type "string[][][]", but one of the elements is of type "integer[][][]".');
  1267. $this->resolver->setDefined('foo');
  1268. $this->resolver->setAllowedTypes('foo', 'string[][][]');
  1269. $this->resolver->resolve([
  1270. 'foo' => [
  1271. [
  1272. ['str'], [1, 2], ],
  1273. ],
  1274. ]);
  1275. }
  1276. public function testNestedArrayException5()
  1277. {
  1278. $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
  1279. $this->expectExceptionMessage('The option "foo" with value array is expected to be of type "string[]", but one of the elements is of type "array[]".');
  1280. $this->resolver->setDefined('foo');
  1281. $this->resolver->setAllowedTypes('foo', 'string[]');
  1282. $this->resolver->resolve([
  1283. 'foo' => [
  1284. [
  1285. ['str'], [1, 2], ],
  1286. ],
  1287. ]);
  1288. }
  1289. }