DateTimeImmutableFixerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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\ClassUsage;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Kuba Werłos <werlos@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ClassUsage\DateTimeImmutableFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassUsage\DateTimeImmutableFixer>
  22. */
  23. final class DateTimeImmutableFixerTest 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 new DateTimeImmutable();',
  39. '<?php new DateTime();',
  40. ];
  41. yield [
  42. '<?php new DateTimeImmutable();',
  43. '<?php new DATETIME();',
  44. ];
  45. yield [
  46. '<?php new \DateTimeImmutable();',
  47. '<?php new \DateTime();',
  48. ];
  49. yield [
  50. '<?php new Foo\DateTime();',
  51. ];
  52. yield [
  53. '<?php namespace Foo; new DateTime();',
  54. ];
  55. yield [
  56. '<?php namespace Foo; new \DateTimeImmutable();',
  57. '<?php namespace Foo; new \DateTime();',
  58. ];
  59. yield [
  60. '<?php namespace Foo; use DateTime; new \DateTimeImmutable();',
  61. '<?php namespace Foo; use DateTime; new DateTime();',
  62. ];
  63. yield [
  64. '<?php namespace Foo; use DateTime; new Bar\DateTime();',
  65. ];
  66. yield [
  67. '<?php namespace Foo; use DateTime\Bar; new DateTime();',
  68. ];
  69. yield [
  70. '<?php namespace Foo; use Bar\DateTime; new DateTime();',
  71. ];
  72. yield [
  73. '<?php namespace Foo; use DateTime\Bar; use DateTime; use Baz\DateTime as BazDateTime; new \DateTimeImmutable();',
  74. '<?php namespace Foo; use DateTime\Bar; use DateTime; use Baz\DateTime as BazDateTime; new DateTime();',
  75. ];
  76. yield [
  77. '<?php $foo = DateTime::ISO8601;',
  78. ];
  79. yield [
  80. '<?php $foo = \datetime::ISO8601 + 24;',
  81. ];
  82. yield [
  83. "<?php DateTimeImmutable::createFromFormat('j-M-Y', '15-Feb-2009');",
  84. "<?php DateTime::createFromFormat('j-M-Y', '15-Feb-2009');",
  85. ];
  86. yield [
  87. '<?php \DateTimeImmutable::getLastErrors();',
  88. '<?php \DateTime::getLastErrors();',
  89. ];
  90. yield [
  91. '<?php Foo\DateTime::createFromFormat();',
  92. ];
  93. yield [
  94. '<?php $foo->DateTime();',
  95. ];
  96. yield [
  97. '<?php Foo::DateTime();',
  98. ];
  99. yield [
  100. '<?php Foo\DateTime();',
  101. ];
  102. yield [
  103. '<?php date_create_immutable("now");',
  104. '<?php date_create("now");',
  105. ];
  106. yield [
  107. '<?php date_create_immutable();',
  108. '<?php Date_Create();',
  109. ];
  110. yield [
  111. '<?php \date_create_immutable();',
  112. '<?php \date_create();',
  113. ];
  114. yield [
  115. '<?php namespace Foo; date_create_immutable();',
  116. '<?php namespace Foo; date_create();',
  117. ];
  118. yield [
  119. '<?php namespace Foo; \date_create_immutable();',
  120. '<?php namespace Foo; \date_create();',
  121. ];
  122. yield [
  123. "<?php date_create_immutable_from_format('j-M-Y', '15-Feb-2009');",
  124. "<?php date_create_from_format('j-M-Y', '15-Feb-2009');",
  125. ];
  126. yield [
  127. '<?php Foo\date_create();',
  128. ];
  129. yield [
  130. '<?php $foo->date_create();',
  131. ];
  132. yield [
  133. '<?php Foo::date_create();',
  134. ];
  135. yield [
  136. '<?php new date_create();',
  137. ];
  138. yield [
  139. '<?php new \date_create();',
  140. ];
  141. yield [
  142. '<?php new Foo\date_create();',
  143. ];
  144. yield [
  145. '<?php class Foo { public function datetime() {} }',
  146. ];
  147. yield [
  148. '<?php class Foo { public function date_create() {} }',
  149. ];
  150. yield [
  151. '<?php namespace Foo; use DateTime; class Bar { public function datetime() {} }',
  152. ];
  153. yield [
  154. '<?php
  155. namespace Foo;
  156. use DateTime\Bar;
  157. use DateTime;
  158. use Baz\DateTime as BazDateTime;
  159. new \DateTimeImmutable();
  160. new \DateTimeImmutable();
  161. new \DateTimeImmutable();
  162. new \DateTimeImmutable();
  163. ',
  164. '<?php
  165. namespace Foo;
  166. use DateTime\Bar;
  167. use DateTime;
  168. use Baz\DateTime as BazDateTime;
  169. new DateTime();
  170. new DateTime();
  171. new DateTime();
  172. new DateTime();
  173. ',
  174. ];
  175. }
  176. /**
  177. * @dataProvider provideFix80Cases
  178. *
  179. * @requires PHP 8.0
  180. */
  181. public function testFix80(string $expected): void
  182. {
  183. $this->doTest($expected);
  184. }
  185. /**
  186. * @return iterable<array{string}>
  187. */
  188. public static function provideFix80Cases(): iterable
  189. {
  190. yield ['<?php $foo?->DateTime();'];
  191. yield ['<?php $foo?->date_create();'];
  192. }
  193. }