diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index 945e810c..a65b0829 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 47 Rules Overview +# 50 Rules Overview ## AddArgumentDefaultValueRector @@ -678,6 +678,19 @@ Change `app()` func calls to facade calls
+## JsonCallToExplicitJsonCallRector + +Change method calls from `$this->json` to `$this->postJson,` `$this->putJson,` etc. + +- class: [`RectorLaravel\Rector\MethodCall\JsonCallToExplicitJsonCallRector`](../src/Rector/MethodCall/JsonCallToExplicitJsonCallRector.php) + +```diff +-$this->json("POST", "/api/v1/users", $data); ++§this->postJson("/api/v1/users", $data); +``` + +
+ ## LumenRoutesStringActionToUsesArrayRector Changes action in rule definitions from string to array notation. @@ -978,7 +991,6 @@ Replace assertTimesSent with assertSentTimes
- ## ReplaceExpectsMethodsInTestsRector Replace expectJobs and expectEvents methods in tests @@ -1031,6 +1043,23 @@ Replace `$this->faker` with the `fake()` helper function in Factories
+## ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector + +Replace `withoutJobs`, `withoutEvents` and `withoutNotifications` with Facade `fake` + +- class: [`RectorLaravel\Rector\MethodCall\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector`](../src/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector.php) + +```diff +-$this->withoutJobs(); +-$this->withoutEvents(); +-$this->withoutNotifications(); ++\Illuminate\Support\Facades\Bus::fake(); ++\Illuminate\Support\Facades\Event::fake(); ++\Illuminate\Support\Facades\Notification::fake(); +``` + +
+ ## RequestStaticValidateToInjectRector Change static `validate()` method to `$request->validate()` diff --git a/src/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector.php b/src/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector.php new file mode 100644 index 00000000..ddeeff5d --- /dev/null +++ b/src/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector.php @@ -0,0 +1,75 @@ +withoutJobs(); +$this->withoutEvents(); +$this->withoutNotifications(); +CODE_SAMPLE, + <<<'CODE_SAMPLE' +\Illuminate\Support\Facades\Bus::fake(); +\Illuminate\Support\Facades\Event::fake(); +\Illuminate\Support\Facades\Notification::fake(); +CODE_SAMPLE, + ), + ] + ); + } + + public function getNodeTypes(): array + { + return [MethodCall::class]; + } + + /** + * @param Node\Expr\MethodCall $node + */ + public function refactor(Node $node): ?StaticCall + { + if (! $this->isNames($node->name, ['withoutJobs', 'withoutEvents', 'withoutNotifications'])) { + return null; + } + + if (! $this->isObjectType($node->var, new ObjectType('Illuminate\Foundation\Testing\TestCase'))) { + return null; + } + + if (! $node->name instanceof Identifier) { + return null; + } + + $facade = match ($node->name->name) { + 'withoutJobs' => 'Bus', + 'withoutEvents' => 'Event', + 'withoutNotifications' => 'Notification', + default => null, + }; + + if ($facade === null) { + return null; + } + + return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\\' . $facade, 'fake'); + } +} diff --git a/tests/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector/Fixture/fixture.php.inc b/tests/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..46e0fe02 --- /dev/null +++ b/tests/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector/Fixture/fixture.php.inc @@ -0,0 +1,39 @@ +withoutJobs(); + $this->withoutEvents(); + $this->withoutNotifications(); + + $this->get('/'); + } +} + +?> +----- +get('/'); + } +} + +?> diff --git a/tests/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector/Fixture/skip_non_test_case.php.inc b/tests/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector/Fixture/skip_non_test_case.php.inc new file mode 100644 index 00000000..0acdab19 --- /dev/null +++ b/tests/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector/Fixture/skip_non_test_case.php.inc @@ -0,0 +1,18 @@ +withoutJobs(); + $this->withoutEvents(); + $this->withoutNotifications(); + + $this->get('/'); + } +} + +?> diff --git a/tests/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRectorTest.php b/tests/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRectorTest.php new file mode 100644 index 00000000..1846cb76 --- /dev/null +++ b/tests/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRectorTest.php @@ -0,0 +1,31 @@ +doTestFile($filePath); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector/config/configured_rule.php b/tests/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector/config/configured_rule.php new file mode 100644 index 00000000..4529b5e8 --- /dev/null +++ b/tests/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector/config/configured_rule.php @@ -0,0 +1,12 @@ +import(__DIR__ . '/../../../../../config/config.php'); + + $rectorConfig->rule(ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector::class); +};