NoBlankLinesBeforeNamespaceFixerTest.php 2.9 KB

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