Skip to content

Commit

Permalink
WithoutJobs / WithoutEvents / WithoutNotifications replacement rule
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfox committed Jan 4, 2024
1 parent 4a1934a commit 1031a06
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 2 deletions.
33 changes: 31 additions & 2 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 47 Rules Overview
# 50 Rules Overview

## AddArgumentDefaultValueRector

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

<br>

## 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);
```

<br>

## LumenRoutesStringActionToUsesArrayRector

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

<br>


## ReplaceExpectsMethodsInTestsRector

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

<br>

## 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();
```

<br>

## RequestStaticValidateToInjectRector

Change static `validate()` method to `$request->validate()`
Expand Down
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');
}
}
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('/');
}
}

?>
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('/');
}
}

?>
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';
}
}
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);
};

0 comments on commit 1031a06

Please sign in to comment.