Skip to content

Commit 48defc3

Browse files
authored
Replaces without jobs/events/notifications to the appropriate facade (#166)
* WithoutJobs / WithoutEvents / WithoutNotifications replacement rule * Add to Laravel100 config
1 parent 4a1934a commit 48defc3

File tree

7 files changed

+208
-2
lines changed

7 files changed

+208
-2
lines changed

config/sets/laravel100.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use RectorLaravel\Rector\Class_\ReplaceExpectsMethodsInTestsRector;
1313
use RectorLaravel\Rector\Class_\UnifyModelDatesWithCastsRector;
1414
use RectorLaravel\Rector\MethodCall\DatabaseExpressionToStringToMethodCallRector;
15+
use RectorLaravel\Rector\MethodCall\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector;
1516
use RectorLaravel\Rector\StaticCall\ReplaceAssertTimesSendWithAssertSentTimesRector;
1617

1718
// see https://laravel.com/docs/10.x/upgrade
@@ -28,6 +29,7 @@
2829
// https://github.com/laravel/framework/pull/41136/files
2930
$rectorConfig->rule(ReplaceExpectsMethodsInTestsRector::class);
3031
$rectorConfig->rule(ReplaceAssertTimesSendWithAssertSentTimesRector::class);
32+
$rectorConfig->rule(ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector::class);
3133

3234
$rectorConfig
3335
->ruleWithConfiguration(RenamePropertyRector::class, [

docs/rector_rules_overview.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 47 Rules Overview
1+
# 50 Rules Overview
22

33
## AddArgumentDefaultValueRector
44

@@ -678,6 +678,19 @@ Change `app()` func calls to facade calls
678678

679679
<br>
680680

681+
## JsonCallToExplicitJsonCallRector
682+
683+
Change method calls from `$this->json` to `$this->postJson,` `$this->putJson,` etc.
684+
685+
- class: [`RectorLaravel\Rector\MethodCall\JsonCallToExplicitJsonCallRector`](../src/Rector/MethodCall/JsonCallToExplicitJsonCallRector.php)
686+
687+
```diff
688+
-$this->json("POST", "/api/v1/users", $data);
689+
+§this->postJson("/api/v1/users", $data);
690+
```
691+
692+
<br>
693+
681694
## LumenRoutesStringActionToUsesArrayRector
682695

683696
Changes action in rule definitions from string to array notation.
@@ -978,7 +991,6 @@ Replace assertTimesSent with assertSentTimes
978991

979992
<br>
980993

981-
982994
## ReplaceExpectsMethodsInTestsRector
983995

984996
Replace expectJobs and expectEvents methods in tests
@@ -1031,6 +1043,23 @@ Replace `$this->faker` with the `fake()` helper function in Factories
10311043

10321044
<br>
10331045

1046+
## ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector
1047+
1048+
Replace `withoutJobs`, `withoutEvents` and `withoutNotifications` with Facade `fake`
1049+
1050+
- class: [`RectorLaravel\Rector\MethodCall\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector`](../src/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector.php)
1051+
1052+
```diff
1053+
-$this->withoutJobs();
1054+
-$this->withoutEvents();
1055+
-$this->withoutNotifications();
1056+
+\Illuminate\Support\Facades\Bus::fake();
1057+
+\Illuminate\Support\Facades\Event::fake();
1058+
+\Illuminate\Support\Facades\Notification::fake();
1059+
```
1060+
1061+
<br>
1062+
10341063
## RequestStaticValidateToInjectRector
10351064

10361065
Change static `validate()` method to `$request->validate()`
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace RectorLaravel\Rector\MethodCall;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\MethodCall;
7+
use PhpParser\Node\Expr\StaticCall;
8+
use PhpParser\Node\Identifier;
9+
use PHPStan\Type\ObjectType;
10+
use Rector\Core\Rector\AbstractRector;
11+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
12+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
13+
14+
/**
15+
* @see \RectorLaravel\Tests\Rector\MethodCall\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRectorTest
16+
*/
17+
class ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector extends AbstractRector
18+
{
19+
public function getRuleDefinition(): RuleDefinition
20+
{
21+
return new RuleDefinition(
22+
'Replace `withoutJobs`, `withoutEvents` and `withoutNotifications` with Facade `fake`',
23+
[
24+
new CodeSample(
25+
<<<'CODE_SAMPLE'
26+
$this->withoutJobs();
27+
$this->withoutEvents();
28+
$this->withoutNotifications();
29+
CODE_SAMPLE,
30+
<<<'CODE_SAMPLE'
31+
\Illuminate\Support\Facades\Bus::fake();
32+
\Illuminate\Support\Facades\Event::fake();
33+
\Illuminate\Support\Facades\Notification::fake();
34+
CODE_SAMPLE,
35+
),
36+
]
37+
);
38+
}
39+
40+
public function getNodeTypes(): array
41+
{
42+
return [MethodCall::class];
43+
}
44+
45+
/**
46+
* @param Node\Expr\MethodCall $node
47+
*/
48+
public function refactor(Node $node): ?StaticCall
49+
{
50+
if (! $this->isNames($node->name, ['withoutJobs', 'withoutEvents', 'withoutNotifications'])) {
51+
return null;
52+
}
53+
54+
if (! $this->isObjectType($node->var, new ObjectType('Illuminate\Foundation\Testing\TestCase'))) {
55+
return null;
56+
}
57+
58+
if (! $node->name instanceof Identifier) {
59+
return null;
60+
}
61+
62+
$facade = match ($node->name->name) {
63+
'withoutJobs' => 'Bus',
64+
'withoutEvents' => 'Event',
65+
'withoutNotifications' => 'Notification',
66+
default => null,
67+
};
68+
69+
if ($facade === null) {
70+
return null;
71+
}
72+
73+
return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\\' . $facade, 'fake');
74+
}
75+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\MethodCall\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector\Fixture;
4+
5+
use Illuminate\Foundation\Testing\TestCase;
6+
7+
class SomeTest extends TestCase
8+
{
9+
public function testSomething()
10+
{
11+
$this->withoutJobs();
12+
$this->withoutEvents();
13+
$this->withoutNotifications();
14+
15+
$this->get('/');
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace RectorLaravel\Tests\Rector\MethodCall\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector\Fixture;
24+
25+
use Illuminate\Foundation\Testing\TestCase;
26+
27+
class SomeTest extends TestCase
28+
{
29+
public function testSomething()
30+
{
31+
\Illuminate\Support\Facades\Bus::fake();
32+
\Illuminate\Support\Facades\Event::fake();
33+
\Illuminate\Support\Facades\Notification::fake();
34+
35+
$this->get('/');
36+
}
37+
}
38+
39+
?>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\MethodCall\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector\Fixture;
4+
5+
6+
class SkipNonTestCaseTest extends TestCase
7+
{
8+
public function testSomething()
9+
{
10+
$this->withoutJobs();
11+
$this->withoutEvents();
12+
$this->withoutNotifications();
13+
14+
$this->get('/');
15+
}
16+
}
17+
18+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RectorLaravel\Tests\Rector\MethodCall\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRectorTest extends AbstractRectorTestCase
12+
{
13+
public static function provideData(): Iterator
14+
{
15+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
16+
}
17+
18+
/**
19+
* @test
20+
*/
21+
#[DataProvider('provideData')]
22+
public function test(string $filePath): void
23+
{
24+
$this->doTestFile($filePath);
25+
}
26+
27+
public function provideConfigFilePath(): string
28+
{
29+
return __DIR__ . '/config/configured_rule.php';
30+
}
31+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use RectorLaravel\Rector\MethodCall\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');
10+
11+
$rectorConfig->rule(ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector::class);
12+
};

0 commit comments

Comments
 (0)