LowercaseKeywordsFixerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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\Casing;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Casing\LowercaseKeywordsFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Casing\LowercaseKeywordsFixer>
  22. */
  23. final class LowercaseKeywordsFixerTest 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 ['<?php $x = (1 and 2);', '<?php $x = (1 AND 2);'];
  38. yield ['<?php foreach(array(1, 2, 3) as $val) {}', '<?php FOREACH(array(1, 2, 3) AS $val) {}'];
  39. yield ['<?php echo "GOOD AS NEW";'];
  40. yield ['<?php echo X::class ?>', '<?php echo X::ClASs ?>'];
  41. yield [
  42. '<?php $fn = fn() => true;',
  43. '<?php $fn = FN() => true;',
  44. ];
  45. yield ['<?php __HALT_COMPILER();'];
  46. }
  47. /**
  48. * @dataProvider provideFix80Cases
  49. *
  50. * @requires PHP 8.0
  51. */
  52. public function testFix80(string $expected, string $input): void
  53. {
  54. $this->doTest($expected, $input);
  55. }
  56. /**
  57. * @return iterable<array{string, string}>
  58. */
  59. public static function provideFix80Cases(): iterable
  60. {
  61. yield [
  62. '<?php
  63. echo match (1) {
  64. 1 => 9,
  65. 2 => 7,
  66. };',
  67. '<?php
  68. echo MATCH (1) {
  69. 1 => 9,
  70. 2 => 7,
  71. };',
  72. ];
  73. yield [
  74. '<?php
  75. class Point {
  76. public function __construct(
  77. public float $x = 0.0,
  78. protected float $y = 0.0,
  79. private float $z = 0.0,
  80. ) {}
  81. }
  82. ',
  83. '<?php
  84. class Point {
  85. public function __construct(
  86. PUBLIC float $x = 0.0,
  87. Protected float $y = 0.0,
  88. privatE float $z = 0.0,
  89. ) {}
  90. }
  91. ',
  92. ];
  93. }
  94. /**
  95. * @dataProvider provideFix81Cases
  96. *
  97. * @requires PHP 8.1
  98. */
  99. public function testFix81(string $expected, string $input): void
  100. {
  101. $this->doTest($expected, $input);
  102. }
  103. /**
  104. * @return iterable<int|string, array{string, string}>
  105. */
  106. public static function provideFix81Cases(): iterable
  107. {
  108. yield [
  109. '<?php
  110. final class Foo
  111. {
  112. public readonly string $prop;
  113. }
  114. ',
  115. '<?php
  116. final class Foo
  117. {
  118. public READONLY string $prop;
  119. }
  120. ',
  121. ];
  122. yield [
  123. '<?php
  124. class Point {
  125. public function __construct(
  126. public readonly float $x = 0.0,
  127. readonly protected float $y = 0.0,
  128. private readonly float $z = 0.0,
  129. ) {}
  130. }
  131. ',
  132. '<?php
  133. class Point {
  134. public function __construct(
  135. PUBLIC rEADONLY float $x = 0.0,
  136. READonly Protected float $y = 0.0,
  137. privatE READONLY float $z = 0.0,
  138. ) {}
  139. }
  140. ',
  141. ];
  142. yield 'enum full caps' => [
  143. '<?php
  144. enum Suit {
  145. case Hearts;
  146. }
  147. ',
  148. '<?php
  149. ENUM Suit {
  150. case Hearts;
  151. }
  152. ',
  153. ];
  154. }
  155. }