AbstractFixerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\AbstractFixer;
  14. use PhpCsFixer\AccessibleObject\AccessibleObject;
  15. use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
  16. use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
  17. use PhpCsFixer\Tokenizer\Tokens;
  18. use PhpCsFixer\WhitespacesFixerConfig;
  19. /**
  20. * @internal
  21. *
  22. * @covers \PhpCsFixer\AbstractFixer
  23. */
  24. final class AbstractFixerTest extends TestCase
  25. {
  26. public function testDefaults(): void
  27. {
  28. $fixer = $this->createUnconfigurableFixerDouble();
  29. self::assertFalse($fixer->isRisky());
  30. self::assertTrue($fixer->supports(new \SplFileInfo(__FILE__)));
  31. }
  32. public function testSetWhitespacesConfigUnconfigurable(): void
  33. {
  34. $fixer = $this->createUnconfigurableFixerDouble();
  35. $this->expectException(\LogicException::class);
  36. $this->expectExceptionMessage('Cannot run method for class not implementing "PhpCsFixer\Fixer\WhitespacesAwareFixerInterface".');
  37. $fixer->setWhitespacesConfig(new WhitespacesFixerConfig());
  38. }
  39. public function testGetWhitespacesFixerConfig(): void
  40. {
  41. $fixer = $this->createWhitespacesAwareFixerDouble();
  42. $config = AccessibleObject::create($fixer)->whitespacesConfig;
  43. self::assertSame(' ', $config->getIndent());
  44. self::assertSame("\n", $config->getLineEnding());
  45. $newConfig = new WhitespacesFixerConfig("\t", "\r\n");
  46. $fixer->setWhitespacesConfig($newConfig);
  47. $config = AccessibleObject::create($fixer)->whitespacesConfig;
  48. self::assertSame("\t", $config->getIndent());
  49. self::assertSame("\r\n", $config->getLineEnding());
  50. }
  51. private function createWhitespacesAwareFixerDouble(): WhitespacesAwareFixerInterface
  52. {
  53. return new class extends AbstractFixer implements WhitespacesAwareFixerInterface {
  54. public function getDefinition(): FixerDefinitionInterface
  55. {
  56. throw new \BadMethodCallException('Not implemented.');
  57. }
  58. public function isCandidate(Tokens $tokens): bool
  59. {
  60. throw new \BadMethodCallException('Not implemented.');
  61. }
  62. protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
  63. {
  64. throw new \BadMethodCallException('Not implemented.');
  65. }
  66. };
  67. }
  68. private function createUnconfigurableFixerDouble(): AbstractFixer
  69. {
  70. return new class extends AbstractFixer {
  71. public function getDefinition(): FixerDefinitionInterface
  72. {
  73. throw new \LogicException('Not implemented.');
  74. }
  75. public function isCandidate(Tokens $tokens): bool
  76. {
  77. return true;
  78. }
  79. protected function applyFix(\SplFileInfo $file, Tokens $tokens): void {}
  80. };
  81. }
  82. }