DateTimeCreateFromFormatCallFixerTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\FunctionNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\FunctionNotation\DateTimeCreateFromFormatCallFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\DateTimeCreateFromFormatCallFixer>
  20. */
  21. final class DateTimeCreateFromFormatCallFixerTest 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. /**
  31. * @return iterable<int|string, array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. foreach ([\DateTime::class, \DateTimeImmutable::class] as $class) {
  36. $lowerCaseClass = strtolower($class);
  37. $upperCaseClass = strtoupper($class);
  38. yield [
  39. "<?php \\{$class}::createFromFormat('!Y-m-d', '2022-02-11');",
  40. "<?php \\{$class}::createFromFormat('Y-m-d', '2022-02-11');",
  41. ];
  42. yield [
  43. "<?php use {$class}; {$class}::createFromFormat('!Y-m-d', '2022-02-11');",
  44. "<?php use {$class}; {$class}::createFromFormat('Y-m-d', '2022-02-11');",
  45. ];
  46. yield [
  47. "<?php {$class}::createFromFormat('!Y-m-d', '2022-02-11');",
  48. "<?php {$class}::createFromFormat('Y-m-d', '2022-02-11');",
  49. ];
  50. yield [
  51. "<?php use \\Example\\{$class}; {$class}::createFromFormat('Y-m-d', '2022-02-11');",
  52. ];
  53. yield [
  54. "<?php use \\Example\\{$lowerCaseClass}; {$upperCaseClass}::createFromFormat('Y-m-d', '2022-02-11');",
  55. ];
  56. yield [
  57. "<?php \\{$class}::createFromFormat(\"!Y-m-d\", '2022-02-11');",
  58. "<?php \\{$class}::createFromFormat(\"Y-m-d\", '2022-02-11');",
  59. ];
  60. yield [
  61. "<?php \\{$class}::createFromFormat(\$foo, '2022-02-11');",
  62. ];
  63. yield [
  64. "<?php \\{$upperCaseClass}::createFromFormat( \"!Y-m-d\", '2022-02-11');",
  65. "<?php \\{$upperCaseClass}::createFromFormat( \"Y-m-d\", '2022-02-11');",
  66. ];
  67. yield [
  68. "<?php \\{$class}::createFromFormat(/* aaa */ '!Y-m-d', '2022-02-11');",
  69. "<?php \\{$class}::createFromFormat(/* aaa */ 'Y-m-d', '2022-02-11');",
  70. ];
  71. yield [
  72. "<?php /*1*//*2*/{$class}/*3*/::/*4*/createFromFormat/*5*/(/*6*/\"!Y-m-d\"/*7*/,/*8*/\"2022-02-11\"/*9*/)/*10*/ ?>",
  73. "<?php /*1*//*2*/{$class}/*3*/::/*4*/createFromFormat/*5*/(/*6*/\"Y-m-d\"/*7*/,/*8*/\"2022-02-11\"/*9*/)/*10*/ ?>",
  74. ];
  75. yield [
  76. "<?php \\{$class}::createFromFormat('Y-m-d');",
  77. ];
  78. yield [
  79. "<?php \\{$class}::createFromFormat(\$a, \$b);",
  80. ];
  81. yield [
  82. "<?php \\{$class}::createFromFormat('Y-m-d', \$b, \$c);",
  83. ];
  84. yield [
  85. "<?php A\\{$class}::createFromFormat('Y-m-d', '2022-02-11');",
  86. ];
  87. yield [
  88. "<?php A\\{$class}::createFromFormat('Y-m-d'.\"a\", '2022-02-11');",
  89. ];
  90. yield ["<?php \\{$class}::createFromFormat(123, '2022-02-11');"];
  91. yield [
  92. "<?php namespace {
  93. \\{$class}::createFromFormat('!Y-m-d', '2022-02-11');
  94. }
  95. namespace Bar {
  96. class {$class} extends Foo {}
  97. {$class}::createFromFormat('Y-m-d', '2022-02-11');
  98. }
  99. ",
  100. "<?php namespace {
  101. \\{$class}::createFromFormat('Y-m-d', '2022-02-11');
  102. }
  103. namespace Bar {
  104. class {$class} extends Foo {}
  105. {$class}::createFromFormat('Y-m-d', '2022-02-11');
  106. }
  107. ",
  108. ];
  109. yield $class.': binary string' => [
  110. "<?php \\{$class}::createFromFormat(b'!Y-m-d', '2022-02-11');",
  111. "<?php \\{$class}::createFromFormat(b'Y-m-d', '2022-02-11');",
  112. ];
  113. yield $class.': empty string' => [
  114. "<?php \\{$class}::createFromFormat('', '2022-02-11');",
  115. ];
  116. }
  117. }
  118. }