NoTrailingWhitespaceFixerTest.php 3.8 KB

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