PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/vendor/fabpot/php-cs-fixer/Symfony/CS/Tests/ConfigurationResolverTest.php

https://gitlab.com/I-NOZex/quiz
PHP | 306 lines | 230 code | 64 blank | 12 comment | 4 complexity | c94620e8a7e19a90a31352263f9a5790 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS\Tests;
  11. use Symfony\CS\ConfigurationResolver;
  12. use Symfony\CS\Fixer;
  13. use Symfony\CS\FixerInterface;
  14. /**
  15. * @author Katsuhiro Ogawa <ko.fivestar@gmail.com>
  16. * @author Dariusz RumiƄski <dariusz.ruminski@gmail.com>
  17. */
  18. class ConfigurationResolverTest extends \PHPUnit_Framework_TestCase
  19. {
  20. protected $config;
  21. protected $resolver;
  22. protected $fixersMap;
  23. protected function setUp()
  24. {
  25. $fixer = new Fixer();
  26. $fixer->registerBuiltInFixers();
  27. $fixer->registerBuiltInConfigs();
  28. $fixersMap = array();
  29. foreach ($fixer->getFixers() as $singleFixer) {
  30. $level = $singleFixer->getLevel();
  31. if (!isset($fixersMap[$level])) {
  32. $fixersMap[$level] = array();
  33. }
  34. $fixersMap[$level][$singleFixer->getName()] = $singleFixer;
  35. }
  36. $this->fixersMap = $fixersMap;
  37. $this->config = $this->getMock('Symfony\CS\ConfigInterface');
  38. $this->resolver = new ConfigurationResolver();
  39. $this->resolver
  40. ->setAllFixers($fixer->getFixers())
  41. ->setConfig($this->config);
  42. }
  43. protected function makeFixersTest($expectedFixers, $resolvedFixers)
  44. {
  45. $this->assertCount(count($expectedFixers), $resolvedFixers);
  46. foreach ($expectedFixers as $fixer) {
  47. $this->assertContains($fixer, $resolvedFixers);
  48. }
  49. }
  50. public function testResolveFixersReturnsEmptyArrayByDefault()
  51. {
  52. $this->makeFixersTest(array(), $this->resolver->getFixers());
  53. }
  54. public function testResolveFixersWithLevelConfig()
  55. {
  56. $this->config->expects($this->any())->method('getLevel')
  57. ->will($this->returnValue(FixerInterface::PSR1_LEVEL));
  58. $this->resolver->resolve();
  59. $this->makeFixersTest(
  60. array_merge($this->fixersMap[FixerInterface::PSR0_LEVEL], $this->fixersMap[FixerInterface::PSR1_LEVEL]),
  61. $this->resolver->getFixers()
  62. );
  63. }
  64. public function testResolveFixersWithPositiveFixersConfig()
  65. {
  66. $this->config->expects($this->any())->method('getLevel')
  67. ->will($this->returnValue(FixerInterface::SYMFONY_LEVEL));
  68. $this->config->expects($this->any())->method('getFixers')
  69. ->will($this->returnValue(array('strict', 'strict_param')));
  70. $this->resolver->resolve();
  71. $expectedFixers = array_merge(
  72. $this->fixersMap[FixerInterface::PSR0_LEVEL],
  73. $this->fixersMap[FixerInterface::PSR1_LEVEL],
  74. $this->fixersMap[FixerInterface::PSR2_LEVEL],
  75. $this->fixersMap[FixerInterface::SYMFONY_LEVEL],
  76. array($this->fixersMap[FixerInterface::CONTRIB_LEVEL]['strict'], $this->fixersMap[FixerInterface::CONTRIB_LEVEL]['strict_param'])
  77. );
  78. $this->makeFixersTest(
  79. $expectedFixers,
  80. $this->resolver->getFixers()
  81. );
  82. }
  83. public function testResolveFixersWithNegativeFixersConfig()
  84. {
  85. $this->config->expects($this->any())->method('getLevel')
  86. ->will($this->returnValue(FixerInterface::SYMFONY_LEVEL));
  87. $this->config->expects($this->any())->method('getFixers')
  88. ->will($this->returnValue(array('strict', '-include', 'strict_param')));
  89. $this->resolver->resolve();
  90. $expectedFixers = array_merge(
  91. $this->fixersMap[FixerInterface::PSR0_LEVEL],
  92. $this->fixersMap[FixerInterface::PSR1_LEVEL],
  93. $this->fixersMap[FixerInterface::PSR2_LEVEL],
  94. $this->fixersMap[FixerInterface::SYMFONY_LEVEL],
  95. array($this->fixersMap[FixerInterface::CONTRIB_LEVEL]['strict'], $this->fixersMap[FixerInterface::CONTRIB_LEVEL]['strict_param'])
  96. );
  97. foreach ($expectedFixers as $key => $fixer) {
  98. if ($fixer->getName() === 'include') {
  99. unset($expectedFixers[$key]);
  100. break;
  101. }
  102. }
  103. $this->makeFixersTest(
  104. $expectedFixers,
  105. $this->resolver->getFixers()
  106. );
  107. }
  108. public function testResolveFixersWithLevelOption()
  109. {
  110. $this->resolver
  111. ->setOption('level', 'psr1')
  112. ->resolve();
  113. $this->makeFixersTest(
  114. array_merge($this->fixersMap[FixerInterface::PSR0_LEVEL], $this->fixersMap[FixerInterface::PSR1_LEVEL]),
  115. $this->resolver->getFixers()
  116. );
  117. }
  118. public function testResolveFixersWithLevelConfigAndFixersConfigAndLevelOption()
  119. {
  120. $this->config->expects($this->any())->method('getLevel')
  121. ->will($this->returnValue(FixerInterface::PSR2_LEVEL));
  122. $this->config->expects($this->any())->method('getFixers')
  123. ->will($this->returnValue(array('strict')));
  124. $this->resolver
  125. ->setOption('level', 'psr1')
  126. ->resolve();
  127. $this->makeFixersTest(
  128. array_merge($this->fixersMap[FixerInterface::PSR0_LEVEL], $this->fixersMap[FixerInterface::PSR1_LEVEL]),
  129. $this->resolver->getFixers()
  130. );
  131. }
  132. public function testResolveFixersWithLevelConfigAndFixersConfigAndPositiveFixersOption()
  133. {
  134. $this->config->expects($this->any())->method('getLevel')
  135. ->will($this->returnValue(FixerInterface::PSR2_LEVEL));
  136. $this->config->expects($this->any())->method('getFixers')
  137. ->will($this->returnValue(array('strict')));
  138. $this->resolver
  139. ->setOption('fixers', 'psr0')
  140. ->resolve();
  141. $this->makeFixersTest(
  142. array($this->fixersMap[FixerInterface::PSR0_LEVEL]['psr0']),
  143. $this->resolver->getFixers()
  144. );
  145. }
  146. public function testResolveFixersWithLevelConfigAndFixersConfigAndNegativeFixersOption()
  147. {
  148. $this->config->expects($this->any())->method('getLevel')
  149. ->will($this->returnValue(FixerInterface::SYMFONY_LEVEL));
  150. $this->config->expects($this->any())->method('getFixers')
  151. ->will($this->returnValue(array('strict')));
  152. $this->resolver
  153. ->setOption('fixers', 'strict, -include,strict_param ')
  154. ->resolve();
  155. $expectedFixers = array_merge(
  156. $this->fixersMap[FixerInterface::PSR0_LEVEL],
  157. $this->fixersMap[FixerInterface::PSR1_LEVEL],
  158. $this->fixersMap[FixerInterface::PSR2_LEVEL],
  159. $this->fixersMap[FixerInterface::SYMFONY_LEVEL],
  160. array($this->fixersMap[FixerInterface::CONTRIB_LEVEL]['strict'], $this->fixersMap[FixerInterface::CONTRIB_LEVEL]['strict_param'])
  161. );
  162. foreach ($expectedFixers as $key => $fixer) {
  163. if ($fixer->getName() === 'include') {
  164. unset($expectedFixers[$key]);
  165. break;
  166. }
  167. }
  168. $this->makeFixersTest(
  169. $expectedFixers,
  170. $this->resolver->getFixers()
  171. );
  172. }
  173. public function testResolveFixersWithLevelConfigAndFixersConfigAndLevelOptionsAndFixersOption()
  174. {
  175. $this->config->expects($this->any())->method('getLevel')
  176. ->will($this->returnValue(FixerInterface::PSR2_LEVEL));
  177. $this->config->expects($this->any())->method('getFixers')
  178. ->will($this->returnValue(array('concat_with_spaces')));
  179. $this->resolver
  180. ->setOption('level', 'symfony')
  181. ->setOption('fixers', 'strict, -include,strict_param ')
  182. ->resolve();
  183. $expectedFixers = array_merge(
  184. $this->fixersMap[FixerInterface::PSR0_LEVEL],
  185. $this->fixersMap[FixerInterface::PSR1_LEVEL],
  186. $this->fixersMap[FixerInterface::PSR2_LEVEL],
  187. $this->fixersMap[FixerInterface::SYMFONY_LEVEL],
  188. array($this->fixersMap[FixerInterface::CONTRIB_LEVEL]['strict'], $this->fixersMap[FixerInterface::CONTRIB_LEVEL]['strict_param'])
  189. );
  190. foreach ($expectedFixers as $key => $fixer) {
  191. if ($fixer->getName() === 'include') {
  192. unset($expectedFixers[$key]);
  193. break;
  194. }
  195. }
  196. $this->makeFixersTest(
  197. $expectedFixers,
  198. $this->resolver->getFixers()
  199. );
  200. }
  201. public function testResolveProgressWithPositiveConfigAndPositiveOption()
  202. {
  203. $config = $this->getMock('Symfony\CS\Config\Config');
  204. $config->expects($this->any())->method('getHideProgress')
  205. ->will($this->returnValue(true));
  206. $this->resolver->setConfig($config);
  207. $this->resolver->setOption('progress', true);
  208. $this->assertFalse($this->resolver->getProgress());
  209. }
  210. public function testResolveProgressWithPositiveConfigAndNegativeOption()
  211. {
  212. $config = $this->getMock('Symfony\CS\Config\Config');
  213. $config->expects($this->any())->method('getHideProgress')
  214. ->will($this->returnValue(true));
  215. $this->resolver->setConfig($config);
  216. $this->resolver->setOption('progress', false);
  217. $this->assertFalse($this->resolver->getProgress());
  218. }
  219. public function testResolveProgressWithNegativeConfigAndPositiveOption()
  220. {
  221. $config = $this->getMock('Symfony\CS\Config\Config');
  222. $config->expects($this->any())->method('getHideProgress')
  223. ->will($this->returnValue(false));
  224. $this->resolver->setConfig($config);
  225. $this->resolver->setOption('progress', true);
  226. $this->assertTrue($this->resolver->getProgress());
  227. }
  228. public function testResolveProgressWithNegativeConfigAndNegativeOption()
  229. {
  230. $config = $this->getMock('Symfony\CS\Config\Config');
  231. $config->expects($this->any())->method('getHideProgress')
  232. ->will($this->returnValue(false));
  233. $this->resolver->setConfig($config);
  234. $this->resolver->setOption('progress', false);
  235. $this->assertFalse($this->resolver->getProgress());
  236. }
  237. public function testResolveProgressRespectsConfigInterface()
  238. {
  239. $this->resolver->setOption('progress', true);
  240. $this->assertTrue($this->resolver->getProgress());
  241. }
  242. }