BlankLinesBeforeNamespaceFixerTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\Fixer\NamespaceNotation;
  13. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Fixer\NamespaceNotation\BlankLinesBeforeNamespaceFixer;
  15. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  16. use PhpCsFixer\WhitespacesFixerConfig;
  17. /**
  18. * @author Greg Korba <greg@codito.dev>
  19. *
  20. * @internal
  21. *
  22. * @covers \PhpCsFixer\Fixer\NamespaceNotation\BlankLinesBeforeNamespaceFixer
  23. *
  24. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\NamespaceNotation\BlankLinesBeforeNamespaceFixer>
  25. *
  26. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\NamespaceNotation\BlankLinesBeforeNamespaceFixer
  27. */
  28. final class BlankLinesBeforeNamespaceFixerTest extends AbstractFixerTestCase
  29. {
  30. /**
  31. * @param _AutogeneratedInputConfiguration $config
  32. *
  33. * @dataProvider provideFixCases
  34. */
  35. public function testFix(
  36. string $expected,
  37. ?string $input = null,
  38. ?array $config = [],
  39. ?WhitespacesFixerConfig $whitespaces = null
  40. ): void {
  41. if (null !== $whitespaces) {
  42. $this->fixer->setWhitespacesConfig($whitespaces);
  43. }
  44. $this->fixer->configure($config);
  45. $this->doTest($expected, $input);
  46. }
  47. public static function provideFixCases(): iterable
  48. {
  49. yield 'multiple blank lines between namespace declaration and PHP opening tag' => [
  50. "<?php\n\n\n\nnamespace X;",
  51. "<?php\nnamespace X;",
  52. ['min_line_breaks' => 4, 'max_line_breaks' => 4],
  53. ];
  54. yield 'multiple blank lines between namespace declaration and comment' => [
  55. "<?php\n/* Foo */\n\n\nnamespace X;",
  56. "<?php\n/* Foo */\nnamespace X;",
  57. ['min_line_breaks' => 3, 'max_line_breaks' => 3],
  58. ];
  59. yield 'multiple blank lines within min and max line breaks range' => [
  60. "<?php\n\n\n\nnamespace X;",
  61. null,
  62. ['min_line_breaks' => 3, 'max_line_breaks' => 5],
  63. ];
  64. yield 'multiple blank lines with fewer line breaks than minimum' => [
  65. "<?php\n\n\nnamespace X;",
  66. "<?php\n\nnamespace X;",
  67. ['min_line_breaks' => 3, 'max_line_breaks' => 5],
  68. ];
  69. yield 'multiple blank lines with more line breaks than maximum' => [
  70. "<?php\n\n\nnamespace X;",
  71. "<?php\n\n\n\n\nnamespace X;",
  72. ['min_line_breaks' => 1, 'max_line_breaks' => 3],
  73. ];
  74. yield 'enforce namespace at the same line as opening tag' => [
  75. '<?php namespace X;',
  76. "<?php\n\n\n\n\nnamespace X;",
  77. ['min_line_breaks' => 0, 'max_line_breaks' => 0],
  78. ];
  79. }
  80. /**
  81. * @dataProvider provideMinMaxConfigurationCases
  82. */
  83. public function testMinMaxConfiguration(int $min, int $max, bool $valid): void
  84. {
  85. if (true === $valid) {
  86. $this->expectNotToPerformAssertions();
  87. } else {
  88. $this->expectException(InvalidFixerConfigurationException::class);
  89. }
  90. $fixer = new BlankLinesBeforeNamespaceFixer();
  91. $fixer->configure(['min_line_breaks' => $min, 'max_line_breaks' => $max]);
  92. }
  93. /**
  94. * @return iterable<array{int, int, bool}>
  95. */
  96. public static function provideMinMaxConfigurationCases(): iterable
  97. {
  98. yield 'same min and max' => [2, 2, true];
  99. yield 'correct min and max range' => [2, 4, true];
  100. yield 'min higher than max' => [4, 2, false];
  101. yield 'min lower than 0' => [-2, 2, false];
  102. yield 'max lower than 0' => [-4, -2, false];
  103. }
  104. }