SingleBlankLineBeforeNamespaceFixerTest.php 3.9 KB

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