OptionsResolverIntrospectorTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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\Debug;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\OptionsResolver\Debug\OptionsResolverIntrospector;
  13. use Symfony\Component\OptionsResolver\Options;
  14. use Symfony\Component\OptionsResolver\OptionsResolver;
  15. class OptionsResolverIntrospectorTest extends TestCase
  16. {
  17. public function testGetDefault()
  18. {
  19. $resolver = new OptionsResolver();
  20. $resolver->setDefault($option = 'foo', 'bar');
  21. $debug = new OptionsResolverIntrospector($resolver);
  22. $this->assertSame('bar', $debug->getDefault($option));
  23. }
  24. public function testGetDefaultNull()
  25. {
  26. $resolver = new OptionsResolver();
  27. $resolver->setDefault($option = 'foo', null);
  28. $debug = new OptionsResolverIntrospector($resolver);
  29. $this->assertNull($debug->getDefault($option));
  30. }
  31. /**
  32. * @expectedException \Symfony\Component\OptionsResolver\Exception\NoConfigurationException
  33. * @expectedExceptionMessage No default value was set for the "foo" option.
  34. */
  35. public function testGetDefaultThrowsOnNoConfiguredValue()
  36. {
  37. $resolver = new OptionsResolver();
  38. $resolver->setDefined($option = 'foo');
  39. $debug = new OptionsResolverIntrospector($resolver);
  40. $this->assertSame('bar', $debug->getDefault($option));
  41. }
  42. /**
  43. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  44. * @expectedExceptionMessage The option "foo" does not exist.
  45. */
  46. public function testGetDefaultThrowsOnNotDefinedOption()
  47. {
  48. $resolver = new OptionsResolver();
  49. $debug = new OptionsResolverIntrospector($resolver);
  50. $this->assertSame('bar', $debug->getDefault('foo'));
  51. }
  52. public function testGetLazyClosures()
  53. {
  54. $resolver = new OptionsResolver();
  55. $closures = [];
  56. $resolver->setDefault($option = 'foo', $closures[] = function (Options $options) {});
  57. $debug = new OptionsResolverIntrospector($resolver);
  58. $this->assertSame($closures, $debug->getLazyClosures($option));
  59. }
  60. /**
  61. * @expectedException \Symfony\Component\OptionsResolver\Exception\NoConfigurationException
  62. * @expectedExceptionMessage No lazy closures were set for the "foo" option.
  63. */
  64. public function testGetLazyClosuresThrowsOnNoConfiguredValue()
  65. {
  66. $resolver = new OptionsResolver();
  67. $resolver->setDefined($option = 'foo');
  68. $debug = new OptionsResolverIntrospector($resolver);
  69. $this->assertSame('bar', $debug->getLazyClosures($option));
  70. }
  71. /**
  72. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  73. * @expectedExceptionMessage The option "foo" does not exist.
  74. */
  75. public function testGetLazyClosuresThrowsOnNotDefinedOption()
  76. {
  77. $resolver = new OptionsResolver();
  78. $debug = new OptionsResolverIntrospector($resolver);
  79. $this->assertSame('bar', $debug->getLazyClosures('foo'));
  80. }
  81. public function testGetAllowedTypes()
  82. {
  83. $resolver = new OptionsResolver();
  84. $resolver->setDefined($option = 'foo');
  85. $resolver->setAllowedTypes($option = 'foo', $allowedTypes = ['string', 'bool']);
  86. $debug = new OptionsResolverIntrospector($resolver);
  87. $this->assertSame($allowedTypes, $debug->getAllowedTypes($option));
  88. }
  89. /**
  90. * @expectedException \Symfony\Component\OptionsResolver\Exception\NoConfigurationException
  91. * @expectedExceptionMessage No allowed types were set for the "foo" option.
  92. */
  93. public function testGetAllowedTypesThrowsOnNoConfiguredValue()
  94. {
  95. $resolver = new OptionsResolver();
  96. $resolver->setDefined($option = 'foo');
  97. $debug = new OptionsResolverIntrospector($resolver);
  98. $this->assertSame('bar', $debug->getAllowedTypes($option));
  99. }
  100. /**
  101. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  102. * @expectedExceptionMessage The option "foo" does not exist.
  103. */
  104. public function testGetAllowedTypesThrowsOnNotDefinedOption()
  105. {
  106. $resolver = new OptionsResolver();
  107. $debug = new OptionsResolverIntrospector($resolver);
  108. $this->assertSame('bar', $debug->getAllowedTypes('foo'));
  109. }
  110. public function testGetAllowedValues()
  111. {
  112. $resolver = new OptionsResolver();
  113. $resolver->setDefined($option = 'foo');
  114. $resolver->setAllowedValues($option = 'foo', $allowedValues = ['bar', 'baz']);
  115. $debug = new OptionsResolverIntrospector($resolver);
  116. $this->assertSame($allowedValues, $debug->getAllowedValues($option));
  117. }
  118. /**
  119. * @expectedException \Symfony\Component\OptionsResolver\Exception\NoConfigurationException
  120. * @expectedExceptionMessage No allowed values were set for the "foo" option.
  121. */
  122. public function testGetAllowedValuesThrowsOnNoConfiguredValue()
  123. {
  124. $resolver = new OptionsResolver();
  125. $resolver->setDefined($option = 'foo');
  126. $debug = new OptionsResolverIntrospector($resolver);
  127. $this->assertSame('bar', $debug->getAllowedValues($option));
  128. }
  129. /**
  130. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  131. * @expectedExceptionMessage The option "foo" does not exist.
  132. */
  133. public function testGetAllowedValuesThrowsOnNotDefinedOption()
  134. {
  135. $resolver = new OptionsResolver();
  136. $debug = new OptionsResolverIntrospector($resolver);
  137. $this->assertSame('bar', $debug->getAllowedValues('foo'));
  138. }
  139. public function testGetNormalizer()
  140. {
  141. $resolver = new OptionsResolver();
  142. $resolver->setDefined($option = 'foo');
  143. $resolver->setNormalizer($option = 'foo', $normalizer = function () {});
  144. $debug = new OptionsResolverIntrospector($resolver);
  145. $this->assertSame($normalizer, $debug->getNormalizer($option));
  146. }
  147. /**
  148. * @expectedException \Symfony\Component\OptionsResolver\Exception\NoConfigurationException
  149. * @expectedExceptionMessage No normalizer was set for the "foo" option.
  150. */
  151. public function testGetNormalizerThrowsOnNoConfiguredValue()
  152. {
  153. $resolver = new OptionsResolver();
  154. $resolver->setDefined($option = 'foo');
  155. $debug = new OptionsResolverIntrospector($resolver);
  156. $this->assertSame('bar', $debug->getNormalizer($option));
  157. }
  158. /**
  159. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  160. * @expectedExceptionMessage The option "foo" does not exist.
  161. */
  162. public function testGetNormalizerThrowsOnNotDefinedOption()
  163. {
  164. $resolver = new OptionsResolver();
  165. $debug = new OptionsResolverIntrospector($resolver);
  166. $this->assertSame('bar', $debug->getNormalizer('foo'));
  167. }
  168. public function testGetNormalizers()
  169. {
  170. $resolver = new OptionsResolver();
  171. $resolver->setDefined('foo');
  172. $resolver->addNormalizer('foo', $normalizer1 = function () {});
  173. $resolver->addNormalizer('foo', $normalizer2 = function () {});
  174. $debug = new OptionsResolverIntrospector($resolver);
  175. $this->assertSame([$normalizer1, $normalizer2], $debug->getNormalizers('foo'));
  176. }
  177. /**
  178. * @expectedException \Symfony\Component\OptionsResolver\Exception\NoConfigurationException
  179. * @expectedExceptionMessage No normalizer was set for the "foo" option.
  180. */
  181. public function testGetNormalizersThrowsOnNoConfiguredValue()
  182. {
  183. $resolver = new OptionsResolver();
  184. $resolver->setDefined('foo');
  185. $debug = new OptionsResolverIntrospector($resolver);
  186. $debug->getNormalizers('foo');
  187. }
  188. /**
  189. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  190. * @expectedExceptionMessage The option "foo" does not exist.
  191. */
  192. public function testGetNormalizersThrowsOnNotDefinedOption()
  193. {
  194. $resolver = new OptionsResolver();
  195. $debug = new OptionsResolverIntrospector($resolver);
  196. $debug->getNormalizers('foo');
  197. }
  198. public function testGetDeprecationMessage()
  199. {
  200. $resolver = new OptionsResolver();
  201. $resolver->setDefined('foo');
  202. $resolver->setDeprecated('foo', 'The option "foo" is deprecated.');
  203. $debug = new OptionsResolverIntrospector($resolver);
  204. $this->assertSame('The option "foo" is deprecated.', $debug->getDeprecationMessage('foo'));
  205. }
  206. public function testGetClosureDeprecationMessage()
  207. {
  208. $resolver = new OptionsResolver();
  209. $resolver->setDefined('foo');
  210. $resolver->setDeprecated('foo', $closure = function (Options $options, $value) {});
  211. $debug = new OptionsResolverIntrospector($resolver);
  212. $this->assertSame($closure, $debug->getDeprecationMessage('foo'));
  213. }
  214. /**
  215. * @expectedException \Symfony\Component\OptionsResolver\Exception\NoConfigurationException
  216. * @expectedExceptionMessage No deprecation was set for the "foo" option.
  217. */
  218. public function testGetDeprecationMessageThrowsOnNoConfiguredValue()
  219. {
  220. $resolver = new OptionsResolver();
  221. $resolver->setDefined('foo');
  222. $debug = new OptionsResolverIntrospector($resolver);
  223. $this->assertSame('bar', $debug->getDeprecationMessage('foo'));
  224. }
  225. /**
  226. * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
  227. * @expectedExceptionMessage The option "foo" does not exist.
  228. */
  229. public function testGetDeprecationMessageThrowsOnNotDefinedOption()
  230. {
  231. $resolver = new OptionsResolver();
  232. $debug = new OptionsResolverIntrospector($resolver);
  233. $this->assertSame('bar', $debug->getDeprecationMessage('foo'));
  234. }
  235. }