From 8d39c5b8ebfa757179eebc7f0fdbb9c9d2277e66 Mon Sep 17 00:00:00 2001 From: tamiroh Date: Mon, 12 Aug 2024 05:58:32 +0900 Subject: [PATCH] Add CarbonSetTestNowToTravelToRector rule (#240) * Add `CarbonSetTestNowToTravelToRector` * Rename fixture * Add more fixtures * Add fixture * Add fixture * Add more tests * Update doc --------- Co-authored-by: Geni Jaho --- docs/rector_rules_overview.md | 24 ++++- .../CarbonSetTestNowToTravelToRector.php | 99 +++++++++++++++++++ .../CarbonSetTestNowToTravelToRectorTest.php | 34 +++++++ .../in_non_laravel_testcase_class.php.inc | 35 +++++++ .../Fixture/with_carbon_carbon.php.inc | 35 +++++++ .../with_carbon_carbonimmutable.php.inc | 35 +++++++ .../with_illuminate_support_carbon.php.inc | 35 +++++++ .../Fixture/with_non_carbon_class.php.inc | 37 +++++++ .../config/configured_rule.php | 11 +++ 9 files changed, 344 insertions(+), 1 deletion(-) create mode 100644 src/Rector/StaticCall/CarbonSetTestNowToTravelToRector.php create mode 100644 tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/CarbonSetTestNowToTravelToRectorTest.php create mode 100644 tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/in_non_laravel_testcase_class.php.inc create mode 100644 tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_carbon_carbon.php.inc create mode 100644 tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_carbon_carbonimmutable.php.inc create mode 100644 tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_illuminate_support_carbon.php.inc create mode 100644 tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_non_carbon_class.php.inc create mode 100644 tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/config/configured_rule.php diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index 87937b8f..16b4f746 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 64 Rules Overview +# 65 Rules Overview ## AbortIfRector @@ -396,6 +396,28 @@ Replace magical call on `$this->app["something"]` to standalone type assign vari
+## CarbonSetTestNowToTravelToRector + +Use the `$this->travelTo()` method in Laravel's `TestCase` class instead of the `Carbon::setTestNow()` method. + +- class: [`RectorLaravel\Rector\StaticCall\CarbonSetTestNowToTravelToRector`](../src/Rector/StaticCall/CarbonSetTestNowToTravelToRector.php) + +```diff + use Illuminate\Support\Carbon; + use Illuminate\Foundation\Testing\TestCase; + + class SomeTest extends TestCase + { + public function test() + { +- Carbon::setTestNow('2024-08-11'); ++ $this->travelTo('2024-08-11'); + } + } +``` + +
+ ## CashierStripeOptionsToStripeRector Renames the Billable `stripeOptions()` to `stripe().` diff --git a/src/Rector/StaticCall/CarbonSetTestNowToTravelToRector.php b/src/Rector/StaticCall/CarbonSetTestNowToTravelToRector.php new file mode 100644 index 00000000..2284c165 --- /dev/null +++ b/src/Rector/StaticCall/CarbonSetTestNowToTravelToRector.php @@ -0,0 +1,99 @@ +travelTo()` method in Laravel\'s `TestCase` class instead of the `Carbon::setTestNow()` method.', + [ + new CodeSample( + <<<'CODE_SAMPLE' +use Illuminate\Support\Carbon; +use Illuminate\Foundation\Testing\TestCase; + +class SomeTest extends TestCase +{ + public function test() + { + Carbon::setTestNow('2024-08-11'); + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +use Illuminate\Support\Carbon; +use Illuminate\Foundation\Testing\TestCase; + +class SomeTest extends TestCase +{ + public function test() + { + $this->travelTo('2024-08-11'); + } +} +CODE_SAMPLE + ), + ], + ); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [StaticCall::class]; + } + + public function refactorWithScope(Node $node, Scope $scope): ?MethodCall + { + if (! $node instanceof StaticCall) { + return null; + } + + if (! $scope->isInClass()) { + return null; + } + + if (! $scope->getClassReflection()->isSubclassOf('Illuminate\Foundation\Testing\TestCase')) { + return null; + } + + if (! $this->isName($node->name, 'setTestNow')) { + return null; + } + + if (! $this->isCarbon($node->class)) { + return null; + } + + return $this->nodeFactory->createMethodCall(new Variable('this'), 'travelTo', $node->args); + } + + private function isCarbon(Node $node): bool + { + return $this->isObjectType($node, new ObjectType('Carbon\Carbon')) || + $this->isObjectType($node, new ObjectType('Carbon\CarbonImmutable')) || + $this->isObjectType($node, new ObjectType('Illuminate\Support\Carbon')); + } +} diff --git a/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/CarbonSetTestNowToTravelToRectorTest.php b/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/CarbonSetTestNowToTravelToRectorTest.php new file mode 100644 index 00000000..4a2b7ba8 --- /dev/null +++ b/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/CarbonSetTestNowToTravelToRectorTest.php @@ -0,0 +1,34 @@ +doTestFile($filePath); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/in_non_laravel_testcase_class.php.inc b/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/in_non_laravel_testcase_class.php.inc new file mode 100644 index 00000000..ff466cc8 --- /dev/null +++ b/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/in_non_laravel_testcase_class.php.inc @@ -0,0 +1,35 @@ + +----- + diff --git a/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_carbon_carbon.php.inc b/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_carbon_carbon.php.inc new file mode 100644 index 00000000..de6f441d --- /dev/null +++ b/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_carbon_carbon.php.inc @@ -0,0 +1,35 @@ + +----- +travelTo('2024-08-11'); + $this->travelTo(Carbon::parse('2024-08-11')); + } +} + +?> diff --git a/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_carbon_carbonimmutable.php.inc b/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_carbon_carbonimmutable.php.inc new file mode 100644 index 00000000..8ed1e9f4 --- /dev/null +++ b/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_carbon_carbonimmutable.php.inc @@ -0,0 +1,35 @@ + +----- +travelTo('2024-08-11'); + $this->travelTo(CarbonImmutable::parse('2024-08-11')); + } +} + +?> diff --git a/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_illuminate_support_carbon.php.inc b/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_illuminate_support_carbon.php.inc new file mode 100644 index 00000000..2ed85202 --- /dev/null +++ b/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_illuminate_support_carbon.php.inc @@ -0,0 +1,35 @@ + +----- +travelTo('2024-08-11'); + $this->travelTo(Carbon::parse('2024-08-11')); + } +} + +?> diff --git a/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_non_carbon_class.php.inc b/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_non_carbon_class.php.inc new file mode 100644 index 00000000..1602b4f8 --- /dev/null +++ b/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_non_carbon_class.php.inc @@ -0,0 +1,37 @@ + +----- + diff --git a/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/config/configured_rule.php b/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/config/configured_rule.php new file mode 100644 index 00000000..1296cac3 --- /dev/null +++ b/tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/config/configured_rule.php @@ -0,0 +1,11 @@ +import(__DIR__ . '/../../../../../config/config.php'); + $rectorConfig->rule(CarbonSetTestNowToTravelToRector::class); +};