Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaces without jobs/events/notifications to the appropriate facade #166

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/sets/laravel100.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use RectorLaravel\Rector\Class_\ReplaceExpectsMethodsInTestsRector;
use RectorLaravel\Rector\Class_\UnifyModelDatesWithCastsRector;
use RectorLaravel\Rector\MethodCall\DatabaseExpressionToStringToMethodCallRector;
use RectorLaravel\Rector\MethodCall\ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector;
use RectorLaravel\Rector\StaticCall\ReplaceAssertTimesSendWithAssertSentTimesRector;

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

$rectorConfig
->ruleWithConfiguration(RenamePropertyRector::class, [
Expand Down
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);
};