ConstantCaseFixerTest.php 7.0 KB

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