NoTrailingWhitespaceFixerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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\Whitespace;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Whitespace\NoTrailingWhitespaceFixer
  20. */
  21. final class NoTrailingWhitespaceFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. public static function provideFixCases(): iterable
  31. {
  32. yield [
  33. '<?php
  34. $a = 1;',
  35. '<?php
  36. $a = 1; ',
  37. ];
  38. yield [
  39. '<?php
  40. $a = 1 ;',
  41. '<?php
  42. $a = 1 ; ',
  43. ];
  44. yield [
  45. '<?php
  46. $b = 1;',
  47. '<?php
  48. $b = 1; ',
  49. ];
  50. yield [
  51. "<?php \$b = 1;\n ",
  52. "<?php \$b = 1; \n ",
  53. ];
  54. yield [
  55. "<?php \$b = 1;\n\$c = 1;",
  56. "<?php \$b = 1; \n\$c = 1;",
  57. ];
  58. yield [
  59. "<?php\necho 1;\n \necho2;",
  60. ];
  61. yield [
  62. '<?php
  63. $b = 1;
  64. ',
  65. ];
  66. yield [
  67. "<?php\n\$a=1;\n \n\t\n\$b = 1;",
  68. ];
  69. yield [
  70. "<?php\necho 1;\n?>\n\n\n\n",
  71. ];
  72. yield [
  73. "<?php\n\techo 1;\n?>\n\n\t a \r\n b \r\n",
  74. ];
  75. yield [
  76. "<?php
  77. <<<'EOT'
  78. Il y eut un rire éclatant des écoliers qui décontenança le pauvre
  79. garçon, si bien qu'il ne savait s'il fallait garder sa casquette à
  80. la main, la laisser par terre ou la mettre sur sa tête. Il se
  81. rassit et la posa sur ses genoux.
  82. EOT;
  83. ",
  84. ];
  85. yield [
  86. "<?php\n\$string = 'x \ny';\necho (strlen(\$string) === 5);",
  87. ];
  88. yield [
  89. "<?php\necho <<<'EOT'\nInline Il y eut un \r\nrire éclatant \n \n \r\nEOT;\n\n",
  90. ];
  91. yield [
  92. "<?php\necho 'Hello World';",
  93. "<?php \necho 'Hello World';",
  94. ];
  95. yield [
  96. "<?php\n\necho 'Hello World';",
  97. "<?php \n\necho 'Hello World';",
  98. ];
  99. yield [
  100. "<?php\r\necho 'Hello World';",
  101. "<?php \r\necho 'Hello World';",
  102. ];
  103. yield [
  104. "<?php\necho 'Hello World';",
  105. "<?php \necho 'Hello World';",
  106. ];
  107. yield [
  108. "<?php\necho 'Hello World';",
  109. "<?php \necho 'Hello World';",
  110. ];
  111. yield [
  112. '<?php ',
  113. '<?php ',
  114. ];
  115. yield [
  116. "<?php\t",
  117. "<?php\t\t",
  118. ];
  119. yield [
  120. '<?php ', // do not trim this as "<?php" is not valid PHP
  121. ];
  122. yield [
  123. "<?php\n \n \n ",
  124. ];
  125. yield [
  126. "<?php\n \n ",
  127. "<?php \n \n ",
  128. ];
  129. }
  130. /**
  131. * @dataProvider provideFix80Cases
  132. *
  133. * @requires PHP 8.0
  134. */
  135. public function testFix80(string $expected, string $input = null): void
  136. {
  137. $this->doTest($expected, $input);
  138. }
  139. public static function provideFix80Cases(): iterable
  140. {
  141. yield [
  142. '<?php class Foo {
  143. #[Required]
  144. public $bar;
  145. }',
  146. '<?php class Foo {
  147. #[Required] '.'
  148. public $bar;
  149. }',
  150. ];
  151. }
  152. }