AbstractProxyFixerTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests;
  13. use PhpCsFixer\AbstractProxyFixer;
  14. use PhpCsFixer\Fixer\FixerInterface;
  15. use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
  16. use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
  17. use PhpCsFixer\Tests\Fixtures\Test\AbstractProxyFixerTest\SimpleFixer;
  18. use PhpCsFixer\Tests\Fixtures\Test\AbstractProxyFixerTest\SimpleWhitespacesAwareFixer;
  19. use PhpCsFixer\Tokenizer\Tokens;
  20. use PhpCsFixer\WhitespacesFixerConfig;
  21. /**
  22. * @internal
  23. *
  24. * @covers \PhpCsFixer\AbstractProxyFixer
  25. */
  26. final class AbstractProxyFixerTest extends TestCase
  27. {
  28. public function testCandidate(): void
  29. {
  30. $proxyFixer = $this->buildProxyFixer([new SimpleFixer(true)]);
  31. self::assertTrue($proxyFixer->isCandidate(new Tokens()));
  32. $proxyFixer = $this->buildProxyFixer([new SimpleFixer(false)]);
  33. self::assertFalse($proxyFixer->isCandidate(new Tokens()));
  34. $proxyFixer = $this->buildProxyFixer([
  35. new SimpleFixer(false),
  36. new SimpleFixer(true),
  37. ]);
  38. self::assertTrue($proxyFixer->isCandidate(new Tokens()));
  39. }
  40. public function testRisky(): void
  41. {
  42. $proxyFixer = $this->buildProxyFixer([new SimpleFixer(true, false)]);
  43. self::assertFalse($proxyFixer->isRisky());
  44. $proxyFixer = $this->buildProxyFixer([new SimpleFixer(true, true)]);
  45. self::assertTrue($proxyFixer->isRisky());
  46. $proxyFixer = $this->buildProxyFixer([
  47. new SimpleFixer(true, false),
  48. new SimpleFixer(true, true),
  49. new SimpleFixer(true, false),
  50. ]);
  51. self::assertTrue($proxyFixer->isRisky());
  52. }
  53. public function testSupports(): void
  54. {
  55. $file = new \SplFileInfo(__FILE__);
  56. $proxyFixer = $this->buildProxyFixer([new SimpleFixer(true, false, false)]);
  57. self::assertFalse($proxyFixer->supports($file));
  58. $proxyFixer = $this->buildProxyFixer([new SimpleFixer(true, true, true)]);
  59. self::assertTrue($proxyFixer->supports($file));
  60. $proxyFixer = $this->buildProxyFixer([
  61. new SimpleFixer(true, false, false),
  62. new SimpleFixer(true, true, false),
  63. new SimpleFixer(true, false, true),
  64. ]);
  65. self::assertTrue($proxyFixer->supports($file));
  66. }
  67. public function testPrioritySingleFixer(): void
  68. {
  69. $proxyFixer = $this->buildProxyFixer([new SimpleFixer(true, false, false, 123)]);
  70. self::assertSame(123, $proxyFixer->getPriority());
  71. }
  72. public function testPriorityMultipleFixersNotSet(): void
  73. {
  74. $proxyFixer = $this->buildProxyFixer([
  75. new SimpleFixer(true),
  76. new SimpleFixer(true, true),
  77. new SimpleFixer(true, false, true),
  78. ]);
  79. $this->expectException(\LogicException::class);
  80. $this->expectExceptionMessage('You need to override this method to provide the priority of combined fixers.');
  81. $proxyFixer->getPriority();
  82. }
  83. public function testWhitespacesConfig(): void
  84. {
  85. $config = new WhitespacesFixerConfig();
  86. $whitespacesAwareFixer = new SimpleWhitespacesAwareFixer();
  87. $proxyFixer = $this->buildProxyFixer([
  88. new SimpleFixer(true, true),
  89. $whitespacesAwareFixer,
  90. new SimpleFixer(true, false, true),
  91. ]);
  92. $proxyFixer->setWhitespacesConfig($config);
  93. self::assertSame($config, $whitespacesAwareFixer->getWhitespacesFixerConfig());
  94. }
  95. public function testApplyFixInPriorityOrder(): void
  96. {
  97. $fixer1 = new SimpleFixer(true, false, true, 1);
  98. $fixer2 = new SimpleFixer(true, false, true, 10);
  99. $proxyFixer = $this->buildProxyFixer([$fixer1, $fixer2]);
  100. $proxyFixer->fix(new \SplFileInfo(__FILE__), Tokens::fromCode('<?php echo 1;'));
  101. self::assertSame(2, $fixer1->isFixCalled());
  102. self::assertSame(1, $fixer2->isFixCalled());
  103. }
  104. /**
  105. * @param FixerInterface[] $fixers
  106. */
  107. private function buildProxyFixer(array $fixers): AbstractProxyFixer
  108. {
  109. return new class($fixers) extends AbstractProxyFixer implements WhitespacesAwareFixerInterface {
  110. /**
  111. * @var list<FixerInterface>
  112. */
  113. private array $fixers;
  114. /**
  115. * @param list<FixerInterface> $fixers
  116. */
  117. public function __construct(array $fixers)
  118. {
  119. $this->fixers = $fixers;
  120. parent::__construct();
  121. }
  122. public function getDefinition(): FixerDefinitionInterface
  123. {
  124. throw new \BadMethodCallException('Not implemented.');
  125. }
  126. protected function createProxyFixers(): array
  127. {
  128. return $this->fixers;
  129. }
  130. };
  131. }
  132. }