AbstractFixerTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\Tests\Fixtures\Test\AbstractFixerTest\SimpleFixer;
  14. use PhpCsFixer\Tests\Fixtures\Test\AbstractFixerTest\UnconfigurableFixer;
  15. use PhpCsFixer\WhitespacesFixerConfig;
  16. /**
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\AbstractFixer
  20. */
  21. final class AbstractFixerTest extends TestCase
  22. {
  23. public function testDefaults(): void
  24. {
  25. $fixer = new UnconfigurableFixer();
  26. self::assertFalse($fixer->isRisky());
  27. self::assertTrue($fixer->supports(new \SplFileInfo(__FILE__)));
  28. }
  29. public function testConfigureUnconfigurable(): void
  30. {
  31. $fixer = new UnconfigurableFixer();
  32. self::assertSame(0, $fixer->getPriority());
  33. self::assertSame('unconfigurable', $fixer->getName());
  34. $this->expectException(\LogicException::class);
  35. $this->expectExceptionMessage('Cannot configure using Abstract parent, child not implementing "PhpCsFixer\Fixer\ConfigurableFixerInterface".');
  36. $fixer->configure(['foo' => 'bar']);
  37. }
  38. public function testGetConfigurationDefinitionUnconfigurable(): void
  39. {
  40. $fixer = new UnconfigurableFixer();
  41. $this->expectException(\LogicException::class);
  42. $this->expectExceptionMessage(sprintf('Cannot get configuration definition using Abstract parent, child "%s" not implementing "PhpCsFixer\Fixer\ConfigurableFixerInterface".', \get_class($fixer)));
  43. $fixer->getConfigurationDefinition();
  44. }
  45. public function testCreateConfigurationDefinitionUnconfigurable(): void
  46. {
  47. $fixer = new UnconfigurableFixer();
  48. $this->expectException(\LogicException::class);
  49. $this->expectExceptionMessage('Cannot create configuration definition using Abstract parent, child not implementing "PhpCsFixer\Fixer\ConfigurableFixerInterface".');
  50. $fixer->doSomethingWithCreateConfigDefinition();
  51. }
  52. public function testSetWhitespacesConfigUnconfigurable(): void
  53. {
  54. $fixer = new UnconfigurableFixer();
  55. $this->expectException(\LogicException::class);
  56. $this->expectExceptionMessage('Cannot run method for class not implementing "PhpCsFixer\Fixer\WhitespacesAwareFixerInterface".');
  57. $fixer->setWhitespacesConfig(new WhitespacesFixerConfig());
  58. }
  59. public function testGetWhitespacesFixerConfig(): void
  60. {
  61. $fixer = new SimpleFixer();
  62. $config = $fixer->getWhitespacesConfig();
  63. self::assertSame(' ', $config->getIndent());
  64. self::assertSame("\n", $config->getLineEnding());
  65. $newConfig = new WhitespacesFixerConfig("\t", "\r\n");
  66. $fixer->setWhitespacesConfig($newConfig);
  67. $config = $fixer->getWhitespacesConfig();
  68. self::assertSame("\t", $config->getIndent());
  69. self::assertSame("\r\n", $config->getLineEnding());
  70. }
  71. }