AbstractFixerWithAliasedOptionsTestCase.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Test;
  12. use PhpCsFixer\AbstractFixer;
  13. use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
  14. use PhpCsFixer\FixerConfiguration\AliasedFixerOption;
  15. /**
  16. * @author ntzm
  17. *
  18. * @internal
  19. */
  20. abstract class AbstractFixerWithAliasedOptionsTestCase extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @var null|\PhpCsFixer\Fixer\ConfigurableFixerInterface
  24. */
  25. private $fixerWithAliasedConfig;
  26. protected function doTearDown()
  27. {
  28. parent::doTearDown();
  29. $this->fixerWithAliasedConfig = null;
  30. }
  31. protected function doTest($expected, $input = null, \SplFileInfo $file = null)
  32. {
  33. parent::doTest($expected, $input, $file);
  34. if (null !== $this->fixerWithAliasedConfig) {
  35. if (!$this->fixerWithAliasedConfig instanceof AbstractFixer) {
  36. throw new \LogicException();
  37. }
  38. $fixer = $this->fixer;
  39. $fixerWithAliasedConfig = $this->fixerWithAliasedConfig;
  40. $this->fixer = $fixerWithAliasedConfig;
  41. $this->fixerWithAliasedConfig = null;
  42. $this->doTest($expected, $input, $file);
  43. $this->fixerWithAliasedConfig = $fixerWithAliasedConfig;
  44. $this->fixer = $fixer;
  45. }
  46. }
  47. protected function configureFixerWithAliasedOptions(array $configuration)
  48. {
  49. if (!$this->fixer instanceof ConfigurationDefinitionFixerInterface) {
  50. throw new \LogicException('Fixer is not configurable.');
  51. }
  52. $this->fixer->configure($configuration);
  53. $options = $this->fixer->getConfigurationDefinition()->getOptions();
  54. $hasAliasedOptions = false;
  55. foreach ($options as $option) {
  56. if (!$option instanceof AliasedFixerOption) {
  57. continue;
  58. }
  59. $hasAliasedOptions = true;
  60. $alias = $option->getAlias();
  61. if (\array_key_exists($alias, $configuration)) {
  62. $configuration[$option->getName()] = $configuration[$alias];
  63. unset($configuration[$alias]);
  64. }
  65. }
  66. if (!$hasAliasedOptions) {
  67. throw new \LogicException('Fixer has no aliased options.');
  68. }
  69. $this->fixerWithAliasedConfig = clone $this->fixer;
  70. $this->fixerWithAliasedConfig->configure($configuration);
  71. }
  72. }