ConstantCaseFixerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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\ConstantCaseFixer
  20. */
  21. final class ConstantCaseFixerTest 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 if (true) if (false) if (null) {}',
  34. '<?php if (TRUE) if (FALSE) if (NULL) {}',
  35. ];
  36. yield [
  37. '<?php if (!true) if (!false) if (!null) {}',
  38. '<?php if (!TRUE) if (!FALSE) if (!NULL) {}',
  39. ];
  40. yield [
  41. '<?php if ($a == true) if ($a == false) if ($a == null) {}',
  42. '<?php if ($a == TRUE) if ($a == FALSE) if ($a == NULL) {}',
  43. ];
  44. yield [
  45. '<?php if ($a === true) if ($a === false) if ($a === null) {}',
  46. '<?php if ($a === TRUE) if ($a === FALSE) if ($a === NULL) {}',
  47. ];
  48. yield [
  49. '<?php if ($a != true) if ($a != false) if ($a != null) {}',
  50. '<?php if ($a != TRUE) if ($a != FALSE) if ($a != NULL) {}',
  51. ];
  52. yield [
  53. '<?php if ($a !== true) if ($a !== false) if ($a !== null) {}',
  54. '<?php if ($a !== TRUE) if ($a !== FALSE) if ($a !== NULL) {}',
  55. ];
  56. yield [
  57. '<?php if (true && true and true AND true || false or false OR false xor null XOR null) {}',
  58. '<?php if (TRUE && TRUE and TRUE AND TRUE || FALSE or FALSE OR FALSE xor NULL XOR NULL) {}',
  59. ];
  60. yield [
  61. '<?php /* foo */ true; /** bar */ false;',
  62. '<?php /* foo */ TRUE; /** bar */ FALSE;',
  63. ];
  64. yield ['<?php echo $null;'];
  65. yield ['<?php $x = False::foo();'];
  66. yield ['<?php namespace Foo\Null;'];
  67. yield ['<?php class Foo extends True {}'];
  68. yield ['<?php class Foo implements False {}'];
  69. yield ['<?php $foo instanceof True; $foo instanceof False; $foo instanceof Null;'];
  70. yield [
  71. '<?php
  72. class Foo
  73. {
  74. const TRUE = 1;
  75. const FALSE = true;
  76. const NULL = null;
  77. }',
  78. '<?php
  79. class Foo
  80. {
  81. const TRUE = 1;
  82. const FALSE = TRUE;
  83. const NULL = NULL;
  84. }',
  85. ];
  86. yield ['<?php $x = new /**/False?>'];
  87. yield ['<?php Null/**/::test();'];
  88. yield ['<?php True//
  89. ::test();'];
  90. yield ['<?php class Foo { public function Bar() { $this->False = 1; $this->True = 2; $this->Null = 3; } }'];
  91. }
  92. /**
  93. * @dataProvider provideFixToLowerCases
  94. */
  95. public function testFixToLower(string $expected, ?string $input = null): void
  96. {
  97. $this->fixer->configure(['case' => 'lower']);
  98. $this->doTest($expected, $input);
  99. }
  100. public static function provideFixToLowerCases(): iterable
  101. {
  102. foreach (['true', 'false', 'null'] as $case) {
  103. yield [
  104. sprintf('<?php $x = %s;', $case),
  105. sprintf('<?php $x = %s;', strtoupper($case)),
  106. ];
  107. yield [
  108. sprintf('<?php $x = %s;', $case),
  109. sprintf('<?php $x = %s;', ucfirst($case)),
  110. ];
  111. yield [sprintf('<?php $x = new %s;', ucfirst($case))];
  112. yield [sprintf('<?php $x = new %s;', strtoupper($case))];
  113. yield [sprintf('<?php $x = "%s story";', $case)];
  114. yield [sprintf('<?php $x = "%s";', $case)];
  115. }
  116. }
  117. /**
  118. * @dataProvider provideFixToUpperCases
  119. */
  120. public function testFixToUpper(string $expected, ?string $input = null): void
  121. {
  122. $this->fixer->configure(['case' => 'upper']);
  123. $this->doTest($expected, $input);
  124. }
  125. public static function provideFixToUpperCases(): iterable
  126. {
  127. foreach (['true', 'false', 'null'] as $case) {
  128. yield [
  129. sprintf('<?php $x = %s;', strtoupper($case)),
  130. sprintf('<?php $x = %s;', $case),
  131. ];
  132. yield [
  133. sprintf('<?php $x = %s;', strtoupper($case)),
  134. sprintf('<?php $x = %s;', ucfirst($case)),
  135. ];
  136. yield [sprintf('<?php $x = new %s;', ucfirst($case))];
  137. yield [sprintf('<?php $x = new %s;', strtoupper($case))];
  138. yield [sprintf('<?php $x = "%s story";', $case)];
  139. yield [sprintf('<?php $x = "%s";', $case)];
  140. }
  141. }
  142. /**
  143. * @dataProvider provideFix80Cases
  144. *
  145. * @requires PHP 8.0
  146. */
  147. public function testFix80(string $expected, ?string $input = null): void
  148. {
  149. $this->doTest($expected, $input);
  150. }
  151. public static function provideFix80Cases(): iterable
  152. {
  153. yield ' nullsafe operator' => ['<?php class Foo { public function Bar() { return $this?->False; } }'];
  154. }
  155. /**
  156. * @dataProvider provideFix81Cases
  157. *
  158. * @requires PHP 8.1
  159. */
  160. public function testFix81(string $expected, ?string $input = null): void
  161. {
  162. $this->doTest($expected, $input);
  163. }
  164. public static function provideFix81Cases(): iterable
  165. {
  166. yield 'final class const' => [
  167. '<?php
  168. class Foo
  169. {
  170. final const TRUE = 1;
  171. public final const FALSE = true;
  172. final public const NULL = null;
  173. }
  174. ',
  175. '<?php
  176. class Foo
  177. {
  178. final const TRUE = 1;
  179. public final const FALSE = TRUE;
  180. final public const NULL = NULL;
  181. }
  182. ',
  183. ];
  184. yield 'enum and switch' => [
  185. '<?php
  186. enum Foo
  187. {
  188. case True;
  189. case False;
  190. case Null;
  191. public function methodWithSwitch(mixed $value): void
  192. {
  193. switch ($value) {
  194. case true:
  195. case false:
  196. case null:
  197. break;
  198. }
  199. }
  200. }
  201. ',
  202. '<?php
  203. enum Foo
  204. {
  205. case True;
  206. case False;
  207. case Null;
  208. public function methodWithSwitch(mixed $value): void
  209. {
  210. switch ($value) {
  211. case TRUE:
  212. case FALSE:
  213. case NULL:
  214. break;
  215. }
  216. }
  217. }
  218. ',
  219. ];
  220. yield 'enum' => [
  221. '<?php
  222. $y = false;
  223. enum Foo: string { case FALSE = "false"; }
  224. $x = true;
  225. ',
  226. '<?php
  227. $y = FALSE;
  228. enum Foo: string { case FALSE = "false"; }
  229. $x = TRUE;
  230. ',
  231. ];
  232. }
  233. }