OptionsTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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\Options;
  12. class OptionsTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @var Options
  16. */
  17. private $options;
  18. protected function setUp()
  19. {
  20. $this->options = new Options();
  21. }
  22. public function testArrayAccess()
  23. {
  24. $this->assertFalse(isset($this->options['foo']));
  25. $this->assertFalse(isset($this->options['bar']));
  26. $this->options['foo'] = 0;
  27. $this->options['bar'] = 1;
  28. $this->assertTrue(isset($this->options['foo']));
  29. $this->assertTrue(isset($this->options['bar']));
  30. unset($this->options['bar']);
  31. $this->assertTrue(isset($this->options['foo']));
  32. $this->assertFalse(isset($this->options['bar']));
  33. $this->assertEquals(0, $this->options['foo']);
  34. }
  35. public function testCountable()
  36. {
  37. $this->options->set('foo', 0);
  38. $this->options->set('bar', 1);
  39. $this->assertCount(2, $this->options);
  40. }
  41. /**
  42. * @expectedException \OutOfBoundsException
  43. */
  44. public function testGetNonExisting()
  45. {
  46. $this->options->get('foo');
  47. }
  48. /**
  49. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  50. */
  51. public function testSetNotSupportedAfterGet()
  52. {
  53. $this->options->set('foo', 'bar');
  54. $this->options->get('foo');
  55. $this->options->set('foo', 'baz');
  56. }
  57. /**
  58. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  59. */
  60. public function testRemoveNotSupportedAfterGet()
  61. {
  62. $this->options->set('foo', 'bar');
  63. $this->options->get('foo');
  64. $this->options->remove('foo');
  65. }
  66. /**
  67. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  68. */
  69. public function testSetNormalizerNotSupportedAfterGet()
  70. {
  71. $this->options->set('foo', 'bar');
  72. $this->options->get('foo');
  73. $this->options->setNormalizer('foo', function () {});
  74. }
  75. public function testSetLazyOption()
  76. {
  77. $test = $this;
  78. $this->options->set('foo', function (Options $options) use ($test) {
  79. return 'dynamic';
  80. });
  81. $this->assertEquals('dynamic', $this->options->get('foo'));
  82. }
  83. public function testSetDiscardsPreviousValue()
  84. {
  85. $test = $this;
  86. // defined by superclass
  87. $this->options->set('foo', 'bar');
  88. // defined by subclass
  89. $this->options->set('foo', function (Options $options, $previousValue) use ($test) {
  90. /* @var \PHPUnit_Framework_TestCase $test */
  91. $test->assertNull($previousValue);
  92. return 'dynamic';
  93. });
  94. $this->assertEquals('dynamic', $this->options->get('foo'));
  95. }
  96. public function testOverloadKeepsPreviousValue()
  97. {
  98. $test = $this;
  99. // defined by superclass
  100. $this->options->set('foo', 'bar');
  101. // defined by subclass
  102. $this->options->overload('foo', function (Options $options, $previousValue) use ($test) {
  103. /* @var \PHPUnit_Framework_TestCase $test */
  104. $test->assertEquals('bar', $previousValue);
  105. return 'dynamic';
  106. });
  107. $this->assertEquals('dynamic', $this->options->get('foo'));
  108. }
  109. public function testPreviousValueIsEvaluatedIfLazy()
  110. {
  111. $test = $this;
  112. // defined by superclass
  113. $this->options->set('foo', function (Options $options) {
  114. return 'bar';
  115. });
  116. // defined by subclass
  117. $this->options->overload('foo', function (Options $options, $previousValue) use ($test) {
  118. /* @var \PHPUnit_Framework_TestCase $test */
  119. $test->assertEquals('bar', $previousValue);
  120. return 'dynamic';
  121. });
  122. $this->assertEquals('dynamic', $this->options->get('foo'));
  123. }
  124. public function testPreviousValueIsNotEvaluatedIfNoSecondArgument()
  125. {
  126. $test = $this;
  127. // defined by superclass
  128. $this->options->set('foo', function (Options $options) use ($test) {
  129. $test->fail('Should not be called');
  130. });
  131. // defined by subclass, no $previousValue argument defined!
  132. $this->options->overload('foo', function (Options $options) use ($test) {
  133. return 'dynamic';
  134. });
  135. $this->assertEquals('dynamic', $this->options->get('foo'));
  136. }
  137. public function testLazyOptionCanAccessOtherOptions()
  138. {
  139. $test = $this;
  140. $this->options->set('foo', 'bar');
  141. $this->options->set('bam', function (Options $options) use ($test) {
  142. /* @var \PHPUnit_Framework_TestCase $test */
  143. $test->assertEquals('bar', $options->get('foo'));
  144. return 'dynamic';
  145. });
  146. $this->assertEquals('bar', $this->options->get('foo'));
  147. $this->assertEquals('dynamic', $this->options->get('bam'));
  148. }
  149. public function testLazyOptionCanAccessOtherLazyOptions()
  150. {
  151. $test = $this;
  152. $this->options->set('foo', function (Options $options) {
  153. return 'bar';
  154. });
  155. $this->options->set('bam', function (Options $options) use ($test) {
  156. /* @var \PHPUnit_Framework_TestCase $test */
  157. $test->assertEquals('bar', $options->get('foo'));
  158. return 'dynamic';
  159. });
  160. $this->assertEquals('bar', $this->options->get('foo'));
  161. $this->assertEquals('dynamic', $this->options->get('bam'));
  162. }
  163. public function testNormalizer()
  164. {
  165. $this->options->set('foo', 'bar');
  166. $this->options->setNormalizer('foo', function () {
  167. return 'normalized';
  168. });
  169. $this->assertEquals('normalized', $this->options->get('foo'));
  170. }
  171. public function testNormalizerReceivesUnnormalizedValue()
  172. {
  173. $this->options->set('foo', 'bar');
  174. $this->options->setNormalizer('foo', function (Options $options, $value) {
  175. return 'normalized['.$value.']';
  176. });
  177. $this->assertEquals('normalized[bar]', $this->options->get('foo'));
  178. }
  179. public function testNormalizerCanAccessOtherOptions()
  180. {
  181. $test = $this;
  182. $this->options->set('foo', 'bar');
  183. $this->options->set('bam', 'baz');
  184. $this->options->setNormalizer('bam', function (Options $options) use ($test) {
  185. /* @var \PHPUnit_Framework_TestCase $test */
  186. $test->assertEquals('bar', $options->get('foo'));
  187. return 'normalized';
  188. });
  189. $this->assertEquals('bar', $this->options->get('foo'));
  190. $this->assertEquals('normalized', $this->options->get('bam'));
  191. }
  192. public function testNormalizerCanAccessOtherLazyOptions()
  193. {
  194. $test = $this;
  195. $this->options->set('foo', function (Options $options) {
  196. return 'bar';
  197. });
  198. $this->options->set('bam', 'baz');
  199. $this->options->setNormalizer('bam', function (Options $options) use ($test) {
  200. /* @var \PHPUnit_Framework_TestCase $test */
  201. $test->assertEquals('bar', $options->get('foo'));
  202. return 'normalized';
  203. });
  204. $this->assertEquals('bar', $this->options->get('foo'));
  205. $this->assertEquals('normalized', $this->options->get('bam'));
  206. }
  207. /**
  208. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  209. */
  210. public function testFailForCyclicDependencies()
  211. {
  212. $this->options->set('foo', function (Options $options) {
  213. $options->get('bam');
  214. });
  215. $this->options->set('bam', function (Options $options) {
  216. $options->get('foo');
  217. });
  218. $this->options->get('foo');
  219. }
  220. /**
  221. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  222. */
  223. public function testFailForCyclicDependenciesBetweenNormalizers()
  224. {
  225. $this->options->set('foo', 'bar');
  226. $this->options->set('bam', 'baz');
  227. $this->options->setNormalizer('foo', function (Options $options) {
  228. $options->get('bam');
  229. });
  230. $this->options->setNormalizer('bam', function (Options $options) {
  231. $options->get('foo');
  232. });
  233. $this->options->get('foo');
  234. }
  235. /**
  236. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  237. */
  238. public function testFailForCyclicDependenciesBetweenNormalizerAndLazyOption()
  239. {
  240. $this->options->set('foo', function (Options $options) {
  241. $options->get('bam');
  242. });
  243. $this->options->set('bam', 'baz');
  244. $this->options->setNormalizer('bam', function (Options $options) {
  245. $options->get('foo');
  246. });
  247. $this->options->get('foo');
  248. }
  249. public function testAllInvokesEachLazyOptionOnlyOnce()
  250. {
  251. $test = $this;
  252. $i = 1;
  253. $this->options->set('foo', function (Options $options) use ($test, &$i) {
  254. $test->assertSame(1, $i);
  255. ++$i;
  256. // Implicitly invoke lazy option for "bam"
  257. $options->get('bam');
  258. });
  259. $this->options->set('bam', function (Options $options) use ($test, &$i) {
  260. $test->assertSame(2, $i);
  261. ++$i;
  262. });
  263. $this->options->all();
  264. }
  265. public function testAllInvokesEachNormalizerOnlyOnce()
  266. {
  267. $test = $this;
  268. $i = 1;
  269. $this->options->set('foo', 'bar');
  270. $this->options->set('bam', 'baz');
  271. $this->options->setNormalizer('foo', function (Options $options) use ($test, &$i) {
  272. $test->assertSame(1, $i);
  273. ++$i;
  274. // Implicitly invoke normalizer for "bam"
  275. $options->get('bam');
  276. });
  277. $this->options->setNormalizer('bam', function (Options $options) use ($test, &$i) {
  278. $test->assertSame(2, $i);
  279. ++$i;
  280. });
  281. $this->options->all();
  282. }
  283. public function testReplaceClearsAndSets()
  284. {
  285. $this->options->set('one', '1');
  286. $this->options->replace(array(
  287. 'two' => '2',
  288. 'three' => function (Options $options) {
  289. return '2' === $options['two'] ? '3' : 'foo';
  290. }
  291. ));
  292. $this->assertEquals(array(
  293. 'two' => '2',
  294. 'three' => '3',
  295. ), $this->options->all());
  296. }
  297. public function testClearRemovesAllOptions()
  298. {
  299. $this->options->set('one', 1);
  300. $this->options->set('two', 2);
  301. $this->options->clear();
  302. $this->assertEmpty($this->options->all());
  303. }
  304. /**
  305. * @covers Symfony\Component\OptionsResolver\Options::replace
  306. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  307. */
  308. public function testCannotReplaceAfterOptionWasRead()
  309. {
  310. $this->options->set('one', 1);
  311. $this->options->all();
  312. $this->options->replace(array(
  313. 'two' => '2',
  314. ));
  315. }
  316. /**
  317. * @covers Symfony\Component\OptionsResolver\Options::overload
  318. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  319. */
  320. public function testCannotOverloadAfterOptionWasRead()
  321. {
  322. $this->options->set('one', 1);
  323. $this->options->all();
  324. $this->options->overload('one', 2);
  325. }
  326. /**
  327. * @covers Symfony\Component\OptionsResolver\Options::clear
  328. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  329. */
  330. public function testCannotClearAfterOptionWasRead()
  331. {
  332. $this->options->set('one', 1);
  333. $this->options->all();
  334. $this->options->clear();
  335. }
  336. public function testOverloadCannotBeEvaluatedLazilyWithoutExpectedClosureParams()
  337. {
  338. $this->options->set('foo', 'bar');
  339. $this->options->overload('foo', function () {
  340. return 'test';
  341. });
  342. $this->assertNotEquals('test', $this->options->get('foo'));
  343. $this->assertTrue(is_callable($this->options->get('foo')));
  344. }
  345. public function testOverloadCannotBeEvaluatedLazilyWithoutFirstParamTypeHint()
  346. {
  347. $this->options->set('foo', 'bar');
  348. $this->options->overload('foo', function ($object) {
  349. return 'test';
  350. });
  351. $this->assertNotEquals('test', $this->options->get('foo'));
  352. $this->assertTrue(is_callable($this->options->get('foo')));
  353. }
  354. public function testOptionsIteration()
  355. {
  356. $this->options->set('foo', 'bar');
  357. $this->options->set('foo1', 'bar1');
  358. $expectedResult = array('foo' => 'bar', 'foo1' => 'bar1');
  359. $this->assertEquals($expectedResult, iterator_to_array($this->options, true));
  360. }
  361. public function testHasWithNullValue()
  362. {
  363. $this->options->set('foo', null);
  364. $this->assertTrue($this->options->has('foo'));
  365. }
  366. public function testRemoveOptionAndNormalizer()
  367. {
  368. $this->options->set('foo1', 'bar');
  369. $this->options->setNormalizer('foo1', function (Options $options) {
  370. return '';
  371. });
  372. $this->options->set('foo2', 'bar');
  373. $this->options->setNormalizer('foo2', function (Options $options) {
  374. return '';
  375. });
  376. $this->options->remove('foo2');
  377. $this->assertEquals(array('foo1' => ''), $this->options->all());
  378. }
  379. public function testReplaceOptionAndNormalizer()
  380. {
  381. $this->options->set('foo1', 'bar');
  382. $this->options->setNormalizer('foo1', function (Options $options) {
  383. return '';
  384. });
  385. $this->options->set('foo2', 'bar');
  386. $this->options->setNormalizer('foo2', function (Options $options) {
  387. return '';
  388. });
  389. $this->options->replace(array('foo1' => 'new'));
  390. $this->assertEquals(array('foo1' => 'new'), $this->options->all());
  391. }
  392. public function testClearOptionAndNormalizer()
  393. {
  394. $this->options->set('foo1', 'bar');
  395. $this->options->setNormalizer('foo1', function (Options $options) {
  396. return '';
  397. });
  398. $this->options->set('foo2', 'bar');
  399. $this->options->setNormalizer('foo2', function (Options $options) {
  400. return '';
  401. });
  402. $this->options->clear();
  403. $this->assertEmpty($this->options->all());
  404. }
  405. public function testNormalizerWithoutCorrespondingOption()
  406. {
  407. $test = $this;
  408. $this->options->setNormalizer('foo', function (Options $options, $previousValue) use ($test) {
  409. $test->assertNull($previousValue);
  410. return '';
  411. });
  412. $this->assertEquals(array('foo' => ''), $this->options->all());
  413. }
  414. }