-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WithoutJobs / WithoutEvents / WithoutNotifications replacement rule
- Loading branch information
Showing
6 changed files
with
206 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/Rector/MethodCall/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Identifier; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \RectorLaravel\Tests\Rector\MethodCall\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRectorTest | ||
*/ | ||
class ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Replace `withoutJobs`, `withoutEvents` and `withoutNotifications` with Facade `fake`', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
$this->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'); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...Call/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector\Fixture; | ||
|
||
use Illuminate\Foundation\Testing\TestCase; | ||
|
||
class SomeTest extends TestCase | ||
{ | ||
public function testSomething() | ||
{ | ||
$this->withoutJobs(); | ||
$this->withoutEvents(); | ||
$this->withoutNotifications(); | ||
|
||
$this->get('/'); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector\Fixture; | ||
|
||
use Illuminate\Foundation\Testing\TestCase; | ||
|
||
class SomeTest extends TestCase | ||
{ | ||
public function testSomething() | ||
{ | ||
\Illuminate\Support\Facades\Bus::fake(); | ||
\Illuminate\Support\Facades\Event::fake(); | ||
\Illuminate\Support\Facades\Notification::fake(); | ||
|
||
$this->get('/'); | ||
} | ||
} | ||
|
||
?> |
18 changes: 18 additions & 0 deletions
18
...eWithoutJobsEventsAndNotificationsWithFacadeFakeRector/Fixture/skip_non_test_case.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector\Fixture; | ||
|
||
|
||
class SkipNonTestCaseTest extends TestCase | ||
{ | ||
public function testSomething() | ||
{ | ||
$this->withoutJobs(); | ||
$this->withoutEvents(); | ||
$this->withoutNotifications(); | ||
|
||
$this->get('/'); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...WithFacadeFakeRector/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRectorTest extends AbstractRectorTestCase | ||
{ | ||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...l/ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use RectorLaravel\Rector\MethodCall\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); | ||
|
||
$rectorConfig->rule(ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector::class); | ||
}; |