LegacyOptionsTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 LegacyOptionsTest extends TestCase
  18. {
  19. /**
  20. * @var OptionsResolver
  21. */
  22. private $options;
  23. protected function setUp()
  24. {
  25. $this->options = new OptionsResolver();
  26. }
  27. public function testSetLazyOption()
  28. {
  29. $this->options->set('foo', function (Options $options) {
  30. return 'dynamic';
  31. });
  32. $this->assertEquals(array('foo' => 'dynamic'), $this->options->resolve());
  33. }
  34. public function testOverloadKeepsPreviousValue()
  35. {
  36. $test = $this;
  37. // defined by superclass
  38. $this->options->set('foo', 'bar');
  39. // defined by subclass
  40. $this->options->overload('foo', function (Options $options, $previousValue) use ($test) {
  41. /* @var TestCase $test */
  42. $test->assertEquals('bar', $previousValue);
  43. return 'dynamic';
  44. });
  45. $this->assertEquals(array('foo' => 'dynamic'), $this->options->resolve());
  46. }
  47. public function testPreviousValueIsEvaluatedIfLazy()
  48. {
  49. $test = $this;
  50. // defined by superclass
  51. $this->options->set('foo', function (Options $options) {
  52. return 'bar';
  53. });
  54. // defined by subclass
  55. $this->options->overload('foo', function (Options $options, $previousValue) use ($test) {
  56. /* @var TestCase $test */
  57. $test->assertEquals('bar', $previousValue);
  58. return 'dynamic';
  59. });
  60. $this->assertEquals(array('foo' => 'dynamic'), $this->options->resolve());
  61. }
  62. public function testPreviousValueIsNotEvaluatedIfNoSecondArgument()
  63. {
  64. $test = $this;
  65. // defined by superclass
  66. $this->options->set('foo', function (Options $options) use ($test) {
  67. $test->fail('Should not be called');
  68. });
  69. // defined by subclass, no $previousValue argument defined!
  70. $this->options->overload('foo', function (Options $options) {
  71. return 'dynamic';
  72. });
  73. $this->assertEquals(array('foo' => 'dynamic'), $this->options->resolve());
  74. }
  75. public function testLazyOptionCanAccessOtherOptions()
  76. {
  77. $test = $this;
  78. $this->options->set('foo', 'bar');
  79. $this->options->set('bam', function (Options $options) use ($test) {
  80. /* @var TestCase $test */
  81. $test->assertEquals('bar', $options->get('foo'));
  82. return 'dynamic';
  83. });
  84. $this->assertEquals(array('foo' => 'bar', 'bam' => 'dynamic'), $this->options->resolve());
  85. }
  86. public function testLazyOptionCanAccessOtherLazyOptions()
  87. {
  88. $test = $this;
  89. $this->options->set('foo', function (Options $options) {
  90. return 'bar';
  91. });
  92. $this->options->set('bam', function (Options $options) use ($test) {
  93. /* @var TestCase $test */
  94. $test->assertEquals('bar', $options->get('foo'));
  95. return 'dynamic';
  96. });
  97. $this->assertEquals(array('foo' => 'bar', 'bam' => 'dynamic'), $this->options->resolve());
  98. }
  99. public function testNormalizer()
  100. {
  101. $this->options->set('foo', 'bar');
  102. $this->options->setNormalizer('foo', function () {
  103. return 'normalized';
  104. });
  105. $this->assertEquals(array('foo' => 'normalized'), $this->options->resolve());
  106. }
  107. public function testNormalizerReceivesUnnormalizedValue()
  108. {
  109. $this->options->set('foo', 'bar');
  110. $this->options->setNormalizer('foo', function (Options $options, $value) {
  111. return 'normalized['.$value.']';
  112. });
  113. $this->assertEquals(array('foo' => 'normalized[bar]'), $this->options->resolve());
  114. }
  115. public function testNormalizerCanAccessOtherOptions()
  116. {
  117. $test = $this;
  118. $this->options->set('foo', 'bar');
  119. $this->options->set('bam', 'baz');
  120. $this->options->setNormalizer('bam', function (Options $options) use ($test) {
  121. /* @var TestCase $test */
  122. $test->assertEquals('bar', $options->get('foo'));
  123. return 'normalized';
  124. });
  125. $this->assertEquals(array('foo' => 'bar', 'bam' => 'normalized'), $this->options->resolve());
  126. }
  127. public function testNormalizerCanAccessOtherLazyOptions()
  128. {
  129. $test = $this;
  130. $this->options->set('foo', function (Options $options) {
  131. return 'bar';
  132. });
  133. $this->options->set('bam', 'baz');
  134. $this->options->setNormalizer('bam', function (Options $options) use ($test) {
  135. /* @var TestCase $test */
  136. $test->assertEquals('bar', $options->get('foo'));
  137. return 'normalized';
  138. });
  139. $this->assertEquals(array('foo' => 'bar', 'bam' => 'normalized'), $this->options->resolve());
  140. }
  141. /**
  142. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  143. */
  144. public function testFailForCyclicDependencies()
  145. {
  146. $this->options->set('foo', function (Options $options) {
  147. $options->get('bam');
  148. });
  149. $this->options->set('bam', function (Options $options) {
  150. $options->get('foo');
  151. });
  152. $this->options->resolve();
  153. }
  154. /**
  155. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  156. */
  157. public function testFailForCyclicDependenciesBetweenNormalizers()
  158. {
  159. $this->options->set('foo', 'bar');
  160. $this->options->set('bam', 'baz');
  161. $this->options->setNormalizer('foo', function (Options $options) {
  162. $options->get('bam');
  163. });
  164. $this->options->setNormalizer('bam', function (Options $options) {
  165. $options->get('foo');
  166. });
  167. $this->options->resolve();
  168. }
  169. /**
  170. * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
  171. */
  172. public function testFailForCyclicDependenciesBetweenNormalizerAndLazyOption()
  173. {
  174. $this->options->set('foo', function (Options $options) {
  175. $options->get('bam');
  176. });
  177. $this->options->set('bam', 'baz');
  178. $this->options->setNormalizer('bam', function (Options $options) {
  179. $options->get('foo');
  180. });
  181. $this->options->resolve();
  182. }
  183. public function testReplaceClearsAndSets()
  184. {
  185. $this->options->set('one', '1');
  186. $this->options->replace(array(
  187. 'two' => '2',
  188. 'three' => function (Options $options) {
  189. return '2' === $options['two'] ? '3' : 'foo';
  190. },
  191. ));
  192. $this->assertEquals(array(
  193. 'two' => '2',
  194. 'three' => '3',
  195. ), $this->options->resolve());
  196. }
  197. public function testClearRemovesAllOptions()
  198. {
  199. $this->options->set('one', 1);
  200. $this->options->set('two', 2);
  201. $this->options->clear();
  202. $this->assertEmpty($this->options->resolve());
  203. }
  204. public function testOverloadCannotBeEvaluatedLazilyWithoutExpectedClosureParams()
  205. {
  206. $this->options->set('foo', 'bar');
  207. $this->options->overload('foo', function () {
  208. return 'test';
  209. });
  210. $resolved = $this->options->resolve();
  211. $this->assertInternalType('callable', $resolved['foo']);
  212. }
  213. public function testOverloadCannotBeEvaluatedLazilyWithoutFirstParamTypeHint()
  214. {
  215. $this->options->set('foo', 'bar');
  216. $this->options->overload('foo', function ($object) {
  217. return 'test';
  218. });
  219. $resolved = $this->options->resolve();
  220. $this->assertInternalType('callable', $resolved['foo']);
  221. }
  222. public function testRemoveOptionAndNormalizer()
  223. {
  224. $this->options->set('foo1', 'bar');
  225. $this->options->setNormalizer('foo1', function (Options $options) {
  226. return '';
  227. });
  228. $this->options->set('foo2', 'bar');
  229. $this->options->setNormalizer('foo2', function (Options $options) {
  230. return '';
  231. });
  232. $this->options->remove('foo2');
  233. $this->assertEquals(array('foo1' => ''), $this->options->resolve());
  234. }
  235. public function testReplaceOptionAndNormalizer()
  236. {
  237. $this->options->set('foo1', 'bar');
  238. $this->options->setNormalizer('foo1', function (Options $options) {
  239. return '';
  240. });
  241. $this->options->set('foo2', 'bar');
  242. $this->options->setNormalizer('foo2', function (Options $options) {
  243. return '';
  244. });
  245. $this->options->replace(array('foo1' => 'new'));
  246. $this->assertEquals(array('foo1' => 'new'), $this->options->resolve());
  247. }
  248. public function testClearOptionAndNormalizer()
  249. {
  250. $this->options->set('foo1', 'bar');
  251. $this->options->setNormalizer('foo1', function (Options $options) {
  252. return '';
  253. });
  254. $this->options->set('foo2', 'bar');
  255. $this->options->setNormalizer('foo2', function (Options $options) {
  256. return '';
  257. });
  258. $this->options->clear();
  259. $this->assertEmpty($this->options->resolve());
  260. }
  261. }