BlankLinesBeforeNamespaceFixerTest.php 3.6 KB

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