NoBlankLinesBeforeNamespaceFixerTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\NoBlankLinesBeforeNamespaceFixer
  21. */
  22. final class NoBlankLinesBeforeNamespaceFixerTest 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 namespace Some\Name\Space;'];
  37. yield ["<?php\nnamespace X;"];
  38. yield ["<?php\nnamespace X;", "<?php\n\n\n\nnamespace X;"];
  39. yield ["<?php\r\nnamespace X;"];
  40. yield ["<?php\nnamespace X;", "<?php\r\n\r\n\r\n\r\nnamespace X;"];
  41. yield ["<?php\r\nnamespace X;", "<?php\r\n\r\n\r\n\r\nnamespace X;", new WhitespacesFixerConfig(' ', "\r\n")];
  42. yield ["<?php\n\nnamespace\\Sub\\Foo::bar();"];
  43. yield [
  44. '<?php
  45. // Foo
  46. namespace Foo;
  47. ',
  48. '<?php
  49. // Foo
  50. '.'
  51. namespace Foo;
  52. ',
  53. ];
  54. yield [
  55. '<?php
  56. // Foo
  57. namespace Foo;
  58. ',
  59. '<?php
  60. // Foo
  61. '.'
  62. namespace Foo;
  63. ',
  64. ];
  65. }
  66. public function testFixExampleWithComment(): void
  67. {
  68. $expected = <<<'EOF'
  69. <?php
  70. /*
  71. * This file is part of the PHP CS utility.
  72. *
  73. * (c) Fabien Potencier <fabien@symfony.com>
  74. *
  75. * This source file is subject to the MIT license that is bundled
  76. * with this source code in the file LICENSE.
  77. */
  78. namespace PhpCsFixer\Fixer\Contrib;
  79. EOF;
  80. $input = <<<'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. $this->doTest($expected, $input);
  93. }
  94. }