SingleBlankLineBeforeNamespaceFixerTest.php 3.4 KB

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