LegacyOptionsResolverTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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\TestCase;
  12. use Symfony\Component\OptionsResolver\Options;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. /**
  15. * @group legacy
  16. */
  17. class LegacyOptionsResolverTest extends TestCase
  18. {
  19. /**
  20. * @var OptionsResolver
  21. */
  22. private $resolver;
  23. protected function setUp()
  24. {
  25. $this->resolver = new OptionsResolver();
  26. }
  27. public function testResolve()
  28. {
  29. $this->resolver->setDefaults(array(
  30. 'one' => '1',
  31. 'two' => '2',
  32. ));
  33. $options = array(
  34. 'two' => '20',
  35. );
  36. $this->assertEquals(array(
  37. 'one' => '1',
  38. 'two' => '20',
  39. ), $this->resolver->resolve($options));
  40. }
  41. public function testResolveNumericOptions()
  42. {
  43. $this->resolver->setDefaults(array(
  44. '1' => '1',
  45. '2' => '2',
  46. ));
  47. $options = array(
  48. '2' => '20',
  49. );
  50. $this->assertEquals(array(
  51. '1' => '1',
  52. '2' => '20',
  53. ), $this->resolver->resolve($options));
  54. }
  55. public function testResolveLazy()
  56. {
  57. $this->resolver->setDefaults(array(
  58. 'one' => '1',
  59. 'two' => function (Options $options) {
  60. return '20';
  61. },
  62. ));
  63. $this->assertEquals(array(
  64. 'one' => '1',
  65. 'two' => '20',
  66. ), $this->resolver->resolve(array()));
  67. }
  68. public function testTypeAliasesForAllowedTypes()
  69. {
  70. $this->resolver->setDefaults(array(
  71. 'force' => false,
  72. ));
  73. $this->resolver->setAllowedTypes(array(
  74. 'force' => 'boolean',
  75. ));
  76. $this->assertSame(array('force' => true), $this->resolver->resolve(array(
  77. 'force' => true,
  78. )));
  79. }
  80. public function testResolveLazyDependencyOnOptional()
  81. {
  82. $this->resolver->setDefaults(array(
  83. 'one' => '1',
  84. 'two' => function (Options $options) {
  85. return $options['one'].'2';
  86. },
  87. ));
  88. $options = array(
  89. 'one' => '10',
  90. );
  91. $this->assertEquals(array(
  92. 'one' => '10',
  93. 'two' => '102',
  94. ), $this->resolver->resolve($options));
  95. }
  96. public function testResolveLazyDependencyOnMissingOptionalWithoutDefault()
  97. {
  98. $test = $this;
  99. $this->resolver->setOptional(array(
  100. 'one',
  101. ));
  102. $this->resolver->setDefaults(array(
  103. 'two' => function (Options $options) use ($test) {
  104. /* @var TestCase $test */
  105. $test->assertArrayNotHasKey('one', $options);
  106. return '2';
  107. },
  108. ));
  109. $options = array();
  110. $this->assertEquals(array(
  111. 'two' => '2',
  112. ), $this->resolver->resolve($options));
  113. }
  114. public function testResolveLazyDependencyOnOptionalWithoutDefault()
  115. {
  116. $test = $this;
  117. $this->resolver->setOptional(array(
  118. 'one',
  119. ));
  120. $this->resolver->setDefaults(array(
  121. 'two' => function (Options $options) use ($test) {
  122. /* @var TestCase $test */
  123. $test->assertArrayHasKey('one', $options);
  124. return $options['one'].'2';
  125. },
  126. ));
  127. $options = array(
  128. 'one' => '10',
  129. );
  130. $this->assertEquals(array(
  131. 'one' => '10',
  132. 'two' => '102',
  133. ), $this->resolver->resolve($options));
  134. }
  135. public function testResolveLazyDependencyOnRequired()
  136. {
  137. $this->resolver->setRequired(array(
  138. 'one',
  139. ));
  140. $this->resolver->setDefaults(array(
  141. 'two' => function (Options $options) {
  142. return $options['one'].'2';
  143. },
  144. ));
  145. $options = array(
  146. 'one' => '10',
  147. );
  148. $this->assertEquals(array(
  149. 'one' => '10',
  150. 'two' => '102',
  151. ), $this->resolver->resolve($options));
  152. }
  153. public function testResolveLazyReplaceDefaults()
  154. {
  155. $test = $this;
  156. $this->resolver->setDefaults(array(
  157. 'one' => function (Options $options) use ($test) {
  158. /* @var TestCase $test */
  159. $test->fail('Previous closure should not be executed');
  160. },
  161. ));
  162. $this->resolver->replaceDefaults(array(
  163. 'one' => function (Options $options, $previousValue) {
  164. return '1';
  165. },
  166. ));
  167. $this->assertEquals(array(
  168. 'one' => '1',
  169. ), $this->resolver->resolve(array()));
  170. }
  171. /**
  172. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  173. * @expectedExceptionMessage The option "foo" does not exist. Defined options are: "one", "three", "two".
  174. */
  175. public function testResolveFailsIfNonExistingOption()
  176. {
  177. $this->resolver->setDefaults(array(
  178. 'one' => '1',
  179. ));
  180. $this->resolver->setRequired(array(
  181. 'two',
  182. ));
  183. $this->resolver->setOptional(array(
  184. 'three',
  185. ));
  186. $this->resolver->resolve(array(
  187. 'foo' => 'bar',
  188. ));
  189. }
  190. /**
  191. * @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
  192. */
  193. public function testResolveFailsIfMissingRequiredOption()
  194. {
  195. $this->resolver->setRequired(array(
  196. 'one',
  197. ));
  198. $this->resolver->setDefaults(array(
  199. 'two' => '2',
  200. ));
  201. $this->resolver->resolve(array(
  202. 'two' => '20',
  203. ));
  204. }
  205. public function testResolveSucceedsIfOptionValueAllowed()
  206. {
  207. $this->resolver->setDefaults(array(
  208. 'one' => '1',
  209. ));
  210. $this->resolver->setAllowedValues(array(
  211. 'one' => array('1', 'one'),
  212. ));
  213. $options = array(
  214. 'one' => 'one',
  215. );
  216. $this->assertEquals(array(
  217. 'one' => 'one',
  218. ), $this->resolver->resolve($options));
  219. }
  220. public function testResolveSucceedsIfOptionValueAllowed2()
  221. {
  222. $this->resolver->setDefaults(array(
  223. 'one' => '1',
  224. 'two' => '2',
  225. ));
  226. $this->resolver->setAllowedValues(array(
  227. 'one' => '1',
  228. 'two' => '2',
  229. ));
  230. $this->resolver->addAllowedValues(array(
  231. 'one' => 'one',
  232. 'two' => 'two',
  233. ));
  234. $options = array(
  235. 'one' => '1',
  236. 'two' => 'two',
  237. );
  238. $this->assertEquals(array(
  239. 'one' => '1',
  240. 'two' => 'two',
  241. ), $this->resolver->resolve($options));
  242. }
  243. public function testResolveSucceedsIfOptionalWithAllowedValuesNotSet()
  244. {
  245. $this->resolver->setRequired(array(
  246. 'one',
  247. ));
  248. $this->resolver->setOptional(array(
  249. 'two',
  250. ));
  251. $this->resolver->setAllowedValues(array(
  252. 'one' => array('1', 'one'),
  253. 'two' => array('2', 'two'),
  254. ));
  255. $options = array(
  256. 'one' => '1',
  257. );
  258. $this->assertEquals(array(
  259. 'one' => '1',
  260. ), $this->resolver->resolve($options));
  261. }
  262. /**
  263. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  264. */
  265. public function testResolveFailsIfOptionValueNotAllowed()
  266. {
  267. $this->resolver->setDefaults(array(
  268. 'one' => '1',
  269. ));
  270. $this->resolver->setAllowedValues(array(
  271. 'one' => array('1', 'one'),
  272. ));
  273. $this->resolver->resolve(array(
  274. 'one' => '2',
  275. ));
  276. }
  277. public function testResolveSucceedsIfOptionTypeAllowed()
  278. {
  279. $this->resolver->setDefaults(array(
  280. 'one' => '1',
  281. ));
  282. $this->resolver->setAllowedTypes(array(
  283. 'one' => 'string',
  284. ));
  285. $options = array(
  286. 'one' => 'one',
  287. );
  288. $this->assertEquals(array(
  289. 'one' => 'one',
  290. ), $this->resolver->resolve($options));
  291. }
  292. public function testResolveSucceedsIfOptionTypeAllowedPassArray()
  293. {
  294. $this->resolver->setDefaults(array(
  295. 'one' => '1',
  296. ));
  297. $this->resolver->setAllowedTypes(array(
  298. 'one' => array('string', 'bool'),
  299. ));
  300. $options = array(
  301. 'one' => true,
  302. );
  303. $this->assertEquals(array(
  304. 'one' => true,
  305. ), $this->resolver->resolve($options));
  306. }
  307. public function testResolveSucceedsIfOptionTypeAllowedPassObject()
  308. {
  309. $this->resolver->setDefaults(array(
  310. 'one' => '1',
  311. ));
  312. $this->resolver->setAllowedTypes(array(
  313. 'one' => 'object',
  314. ));
  315. $object = new \stdClass();
  316. $options = array(
  317. 'one' => $object,
  318. );
  319. $this->assertEquals(array(
  320. 'one' => $object,
  321. ), $this->resolver->resolve($options));
  322. }
  323. public function testResolveSucceedsIfOptionTypeAllowedPassClass()
  324. {
  325. $this->resolver->setDefaults(array(
  326. 'one' => '1',
  327. ));
  328. $this->resolver->setAllowedTypes(array(
  329. 'one' => '\stdClass',
  330. ));
  331. $object = new \stdClass();
  332. $options = array(
  333. 'one' => $object,
  334. );
  335. $this->assertEquals(array(
  336. 'one' => $object,
  337. ), $this->resolver->resolve($options));
  338. }
  339. public function testResolveSucceedsIfOptionTypeAllowedAddTypes()
  340. {
  341. $this->resolver->setDefaults(array(
  342. 'one' => '1',
  343. 'two' => '2',
  344. ));
  345. $this->resolver->setAllowedTypes(array(
  346. 'one' => 'string',
  347. 'two' => 'bool',
  348. ));
  349. $this->resolver->addAllowedTypes(array(
  350. 'one' => 'float',
  351. 'two' => 'integer',
  352. ));
  353. $options = array(
  354. 'one' => 1.23,
  355. 'two' => false,
  356. );
  357. $this->assertEquals(array(
  358. 'one' => 1.23,
  359. 'two' => false,
  360. ), $this->resolver->resolve($options));
  361. }
  362. public function testResolveSucceedsIfOptionalWithTypeAndWithoutValue()
  363. {
  364. $this->resolver->setOptional(array(
  365. 'one',
  366. 'two',
  367. ));
  368. $this->resolver->setAllowedTypes(array(
  369. 'one' => 'string',
  370. 'two' => 'int',
  371. ));
  372. $options = array(
  373. 'two' => 1,
  374. );
  375. $this->assertEquals(array(
  376. 'two' => 1,
  377. ), $this->resolver->resolve($options));
  378. }
  379. /**
  380. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  381. */
  382. public function testResolveFailsIfOptionTypeNotAllowed()
  383. {
  384. $this->resolver->setDefaults(array(
  385. 'one' => '1',
  386. ));
  387. $this->resolver->setAllowedTypes(array(
  388. 'one' => array('string', 'bool'),
  389. ));
  390. $this->resolver->resolve(array(
  391. 'one' => 1.23,
  392. ));
  393. }
  394. /**
  395. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  396. */
  397. public function testResolveFailsIfOptionTypeNotAllowedMultipleOptions()
  398. {
  399. $this->resolver->setDefaults(array(
  400. 'one' => '1',
  401. 'two' => '2',
  402. ));
  403. $this->resolver->setAllowedTypes(array(
  404. 'one' => 'string',
  405. 'two' => 'bool',
  406. ));
  407. $this->resolver->resolve(array(
  408. 'one' => 'foo',
  409. 'two' => 1.23,
  410. ));
  411. }
  412. /**
  413. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  414. */
  415. public function testResolveFailsIfOptionTypeNotAllowedAddTypes()
  416. {
  417. $this->resolver->setDefaults(array(
  418. 'one' => '1',
  419. ));
  420. $this->resolver->setAllowedTypes(array(
  421. 'one' => 'string',
  422. ));
  423. $this->resolver->addAllowedTypes(array(
  424. 'one' => 'bool',
  425. ));
  426. $this->resolver->resolve(array(
  427. 'one' => 1.23,
  428. ));
  429. }
  430. public function testFluidInterface()
  431. {
  432. $this->resolver->setDefaults(array('one' => '1'))
  433. ->replaceDefaults(array('one' => '2'))
  434. ->setAllowedValues(array('one' => array('1', '2')))
  435. ->addAllowedValues(array('one' => array('3')))
  436. ->setRequired(array('two'))
  437. ->setOptional(array('three'));
  438. $options = array(
  439. 'two' => '2',
  440. );
  441. $this->assertEquals(array(
  442. 'one' => '2',
  443. 'two' => '2',
  444. ), $this->resolver->resolve($options));
  445. }
  446. public function testKnownIfDefaultWasSet()
  447. {
  448. $this->assertFalse($this->resolver->isKnown('foo'));
  449. $this->resolver->setDefaults(array(
  450. 'foo' => 'bar',
  451. ));
  452. $this->assertTrue($this->resolver->isKnown('foo'));
  453. }
  454. public function testKnownIfRequired()
  455. {
  456. $this->assertFalse($this->resolver->isKnown('foo'));
  457. $this->resolver->setRequired(array(
  458. 'foo',
  459. ));
  460. $this->assertTrue($this->resolver->isKnown('foo'));
  461. }
  462. public function testKnownIfOptional()
  463. {
  464. $this->assertFalse($this->resolver->isKnown('foo'));
  465. $this->resolver->setOptional(array(
  466. 'foo',
  467. ));
  468. $this->assertTrue($this->resolver->isKnown('foo'));
  469. }
  470. public function testRequiredIfRequired()
  471. {
  472. $this->assertFalse($this->resolver->isRequired('foo'));
  473. $this->resolver->setRequired(array(
  474. 'foo',
  475. ));
  476. $this->assertTrue($this->resolver->isRequired('foo'));
  477. }
  478. public function testNormalizersTransformFinalOptions()
  479. {
  480. $this->resolver->setDefaults(array(
  481. 'foo' => 'bar',
  482. 'bam' => 'baz',
  483. ));
  484. $this->resolver->setNormalizers(array(
  485. 'foo' => function (Options $options, $value) {
  486. return $options['bam'].'['.$value.']';
  487. },
  488. ));
  489. $expected = array(
  490. 'foo' => 'baz[bar]',
  491. 'bam' => 'baz',
  492. );
  493. $this->assertEquals($expected, $this->resolver->resolve(array()));
  494. $expected = array(
  495. 'foo' => 'boo[custom]',
  496. 'bam' => 'boo',
  497. );
  498. $this->assertEquals($expected, $this->resolver->resolve(array(
  499. 'foo' => 'custom',
  500. 'bam' => 'boo',
  501. )));
  502. }
  503. public function testResolveWithoutOptionSucceedsIfRequiredAndDefaultValue()
  504. {
  505. $this->resolver->setRequired(array(
  506. 'foo',
  507. ));
  508. $this->resolver->setDefaults(array(
  509. 'foo' => 'bar',
  510. ));
  511. $this->assertEquals(array(
  512. 'foo' => 'bar',
  513. ), $this->resolver->resolve(array()));
  514. }
  515. public function testResolveWithoutOptionSucceedsIfDefaultValueAndRequired()
  516. {
  517. $this->resolver->setDefaults(array(
  518. 'foo' => 'bar',
  519. ));
  520. $this->resolver->setRequired(array(
  521. 'foo',
  522. ));
  523. $this->assertEquals(array(
  524. 'foo' => 'bar',
  525. ), $this->resolver->resolve(array()));
  526. }
  527. public function testResolveSucceedsIfOptionRequiredAndValueAllowed()
  528. {
  529. $this->resolver->setRequired(array(
  530. 'one', 'two',
  531. ));
  532. $this->resolver->setAllowedValues(array(
  533. 'two' => array('2'),
  534. ));
  535. $options = array(
  536. 'one' => '1',
  537. 'two' => '2',
  538. );
  539. $this->assertEquals($options, $this->resolver->resolve($options));
  540. }
  541. public function testResolveSucceedsIfValueAllowedCallbackReturnsTrue()
  542. {
  543. $this->resolver->setRequired(array(
  544. 'test',
  545. ));
  546. $this->resolver->setAllowedValues(array(
  547. 'test' => function ($value) {
  548. return true;
  549. },
  550. ));
  551. $options = array(
  552. 'test' => true,
  553. );
  554. $this->assertEquals($options, $this->resolver->resolve($options));
  555. }
  556. /**
  557. * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  558. */
  559. public function testResolveFailsIfValueAllowedCallbackReturnsFalse()
  560. {
  561. $this->resolver->setRequired(array(
  562. 'test',
  563. ));
  564. $this->resolver->setAllowedValues(array(
  565. 'test' => function ($value) {
  566. return false;
  567. },
  568. ));
  569. $options = array(
  570. 'test' => true,
  571. );
  572. $this->assertEquals($options, $this->resolver->resolve($options));
  573. }
  574. public function testClone()
  575. {
  576. $this->resolver->setDefaults(array('one' => '1'));
  577. $clone = clone $this->resolver;
  578. // Changes after cloning don't affect each other
  579. $this->resolver->setDefaults(array('two' => '2'));
  580. $clone->setDefaults(array('three' => '3'));
  581. $this->assertEquals(array(
  582. 'one' => '1',
  583. 'two' => '2',
  584. ), $this->resolver->resolve());
  585. $this->assertEquals(array(
  586. 'one' => '1',
  587. 'three' => '3',
  588. ), $clone->resolve());
  589. }
  590. public function testOverloadReturnsThis()
  591. {
  592. $this->assertSame($this->resolver, $this->resolver->overload('foo', 'bar'));
  593. }
  594. public function testOverloadCallsSet()
  595. {
  596. $this->resolver->overload('foo', 'bar');
  597. $this->assertSame(array('foo' => 'bar'), $this->resolver->resolve());
  598. }
  599. }