SingleBlankLineBeforeNamespaceFixerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Graham Campbell <hello@gjcampbell.co.uk>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\NamespaceNotation\SingleBlankLineBeforeNamespaceFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\NamespaceNotation\SingleBlankLineBeforeNamespaceFixer>
  23. */
  24. final class SingleBlankLineBeforeNamespaceFixerTest extends AbstractFixerTestCase
  25. {
  26. /**
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix(string $expected, ?string $input = null, ?WhitespacesFixerConfig $whitespaces = null): void
  30. {
  31. if (null !== $whitespaces) {
  32. $this->fixer->setWhitespacesConfig($whitespaces);
  33. }
  34. $this->doTest($expected, $input);
  35. }
  36. /**
  37. * @return iterable<array{0: string, 1?: string, 2?: WhitespacesFixerConfig}>
  38. */
  39. public static function provideFixCases(): iterable
  40. {
  41. yield ["<?php\n\nnamespace X;"];
  42. yield ["<?php\n\nnamespace X;", "<?php\n\n\n\nnamespace X;"];
  43. yield ["<?php\r\n\r\nnamespace X;"];
  44. yield ["<?php\n\nnamespace X;", "<?php\r\n\r\n\r\n\r\nnamespace X;"];
  45. yield ["<?php\n\nfoo();\nnamespace\\bar\\baz();"];
  46. yield ["<?php\n\nnamespace X;", "<?php\nnamespace X;"];
  47. yield ["<?php\n\nnamespace X;", '<?php namespace X;'];
  48. yield ["<?php\n\nnamespace X;", "<?php\t\nnamespace X;"];
  49. yield ["<?php \n\nnamespace X;"];
  50. yield ["<?php\r\n\r\nnamespace X;", '<?php namespace X;', new WhitespacesFixerConfig(' ', "\r\n")];
  51. yield ["<?php\r\n\r\nnamespace X;", "<?php\nnamespace X;", new WhitespacesFixerConfig(' ', "\r\n")];
  52. yield ["<?php\r\n\r\nnamespace X;", "<?php\n\n\n\nnamespace X;", new WhitespacesFixerConfig(' ', "\r\n")];
  53. yield ["<?php\r\n\r\nnamespace X;", "<?php\r\n\n\nnamespace X;", new WhitespacesFixerConfig(' ', "\r\n")];
  54. }
  55. public function testFixExampleWithCommentTooMuch(): void
  56. {
  57. $expected = <<<'EOF'
  58. <?php
  59. /*
  60. * This file is part of the PHP CS utility.
  61. *
  62. * (c) Fabien Potencier <fabien@symfony.com>
  63. *
  64. * This source file is subject to the MIT license that is bundled
  65. * with this source code in the file LICENSE.
  66. */
  67. namespace PhpCsFixer\Fixer\Contrib;
  68. EOF;
  69. $input = <<<'EOF'
  70. <?php
  71. /*
  72. * This file is part of the PHP CS utility.
  73. *
  74. * (c) Fabien Potencier <fabien@symfony.com>
  75. *
  76. * This source file is subject to the MIT license that is bundled
  77. * with this source code in the file LICENSE.
  78. */
  79. namespace PhpCsFixer\Fixer\Contrib;
  80. EOF;
  81. $this->doTest($expected, $input);
  82. }
  83. public function testFixExampleWithCommentTooLittle(): void
  84. {
  85. $expected = <<<'EOF'
  86. <?php
  87. /*
  88. * This file is part of the PHP CS utility.
  89. *
  90. * (c) Fabien Potencier <fabien@symfony.com>
  91. *
  92. * This source file is subject to the MIT license that is bundled
  93. * with this source code in the file LICENSE.
  94. */
  95. namespace PhpCsFixer\Fixer\Contrib;
  96. EOF;
  97. $input = <<<'EOF'
  98. <?php
  99. /*
  100. * This file is part of the PHP CS utility.
  101. *
  102. * (c) Fabien Potencier <fabien@symfony.com>
  103. *
  104. * This source file is subject to the MIT license that is bundled
  105. * with this source code in the file LICENSE.
  106. */
  107. namespace PhpCsFixer\Fixer\Contrib;
  108. EOF;
  109. $this->doTest($expected, $input);
  110. }
  111. }