ConstantCaseFixerTest.php 7.6 KB

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