/vendor/symfony/symfony/src/Symfony/Component/OptionsResolver/Tests/LegacyOptionsResolverTest.php

https://gitlab.com/mohamedchiheb.bida/workshopFOS · PHP · 719 lines · 547 code · 133 blank · 39 comment · 0 complexity · a9f3e96f17c5e7ef885b2dcd1eeefba8 MD5 · raw file

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