Skip to content

Commit

Permalink
Add CarbonSetTestNowToTravelToRector rule (#240)
Browse files Browse the repository at this point in the history
* Add `CarbonSetTestNowToTravelToRector`

* Rename fixture

* Add more fixtures

* Add fixture

* Add fixture

* Add more tests

* Update doc

---------

Co-authored-by: Geni Jaho <[email protected]>
  • Loading branch information
tamiroh and GeniJaho authored Aug 11, 2024
1 parent 1d57d99 commit 8d39c5b
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 1 deletion.
24 changes: 23 additions & 1 deletion docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 64 Rules Overview
# 65 Rules Overview

## AbortIfRector

Expand Down Expand Up @@ -396,6 +396,28 @@ Replace magical call on `$this->app["something"]` to standalone type assign vari

<br>

## CarbonSetTestNowToTravelToRector

Use the `$this->travelTo()` method in Laravel's `TestCase` class instead of the `Carbon::setTestNow()` method.

- class: [`RectorLaravel\Rector\StaticCall\CarbonSetTestNowToTravelToRector`](../src/Rector/StaticCall/CarbonSetTestNowToTravelToRector.php)

```diff
use Illuminate\Support\Carbon;
use Illuminate\Foundation\Testing\TestCase;

class SomeTest extends TestCase
{
public function test()
{
- Carbon::setTestNow('2024-08-11');
+ $this->travelTo('2024-08-11');
}
}
```

<br>

## CashierStripeOptionsToStripeRector

Renames the Billable `stripeOptions()` to `stripe().`
Expand Down
99 changes: 99 additions & 0 deletions src/Rector/StaticCall/CarbonSetTestNowToTravelToRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace RectorLaravel\Rector\StaticCall;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractScopeAwareRector;
use Symplify\RuleDocGenerator\Exception\PoorDocumentationException;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \RectorLaravel\Tests\Rector\StaticCall\CarbonSetTestNowToTravelToRector\CarbonSetTestNowToTravelToRectorTest
*/
final class CarbonSetTestNowToTravelToRector extends AbstractScopeAwareRector
{
/**
* @throws PoorDocumentationException
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Use the `$this->travelTo()` method in Laravel\'s `TestCase` class instead of the `Carbon::setTestNow()` method.',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Illuminate\Support\Carbon;
use Illuminate\Foundation\Testing\TestCase;
class SomeTest extends TestCase
{
public function test()
{
Carbon::setTestNow('2024-08-11');
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Illuminate\Support\Carbon;
use Illuminate\Foundation\Testing\TestCase;
class SomeTest extends TestCase
{
public function test()
{
$this->travelTo('2024-08-11');
}
}
CODE_SAMPLE
),
],
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StaticCall::class];
}

public function refactorWithScope(Node $node, Scope $scope): ?MethodCall
{
if (! $node instanceof StaticCall) {
return null;
}

if (! $scope->isInClass()) {
return null;
}

if (! $scope->getClassReflection()->isSubclassOf('Illuminate\Foundation\Testing\TestCase')) {
return null;
}

if (! $this->isName($node->name, 'setTestNow')) {
return null;
}

if (! $this->isCarbon($node->class)) {
return null;
}

return $this->nodeFactory->createMethodCall(new Variable('this'), 'travelTo', $node->args);
}

private function isCarbon(Node $node): bool
{
return $this->isObjectType($node, new ObjectType('Carbon\Carbon')) ||
$this->isObjectType($node, new ObjectType('Carbon\CarbonImmutable')) ||
$this->isObjectType($node, new ObjectType('Illuminate\Support\Carbon'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Tests\Rector\StaticCall\CarbonSetTestNowToTravelToRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Exception\ShouldNotHappenException;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class CarbonSetTestNowToTravelToRectorTest extends AbstractRectorTestCase
{
public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

/**
* @test
*
* @throws ShouldNotHappenException
*/
#[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,35 @@
<?php

namespace RectorLaravel\Tests\Rector\StaticCall\CarbonSetTestNowToTravelToRector\Fixture;

use Foo\TestCase;
use Carbon\Carbon;

class SomeTest extends TestCase
{
public function test()
{
Carbon::setTestNow('2024-08-11');
Carbon::setTestNow(Carbon::parse('2024-08-11'));
}
}

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\StaticCall\CarbonSetTestNowToTravelToRector\Fixture;

use Foo\TestCase;
use Carbon\Carbon;

class SomeTest extends TestCase
{
public function test()
{
Carbon::setTestNow('2024-08-11');
Carbon::setTestNow(Carbon::parse('2024-08-11'));
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace RectorLaravel\Tests\Rector\StaticCall\CarbonSetTestNowToTravelToRector\Fixture;

use Illuminate\Foundation\Testing\TestCase;
use Carbon\Carbon;

class SomeTest extends TestCase
{
public function test()
{
Carbon::setTestNow('2024-08-11');
Carbon::setTestNow(Carbon::parse('2024-08-11'));
}
}

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\StaticCall\CarbonSetTestNowToTravelToRector\Fixture;

use Illuminate\Foundation\Testing\TestCase;
use Carbon\Carbon;

class SomeTest extends TestCase
{
public function test()
{
$this->travelTo('2024-08-11');
$this->travelTo(Carbon::parse('2024-08-11'));
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace RectorLaravel\Tests\Rector\StaticCall\CarbonSetTestNowToTravelToRector\Fixture;

use Illuminate\Foundation\Testing\TestCase;
use Carbon\CarbonImmutable;

class SomeTest extends TestCase
{
public function test()
{
CarbonImmutable::setTestNow('2024-08-11');
CarbonImmutable::setTestNow(CarbonImmutable::parse('2024-08-11'));
}
}

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\StaticCall\CarbonSetTestNowToTravelToRector\Fixture;

use Illuminate\Foundation\Testing\TestCase;
use Carbon\CarbonImmutable;

class SomeTest extends TestCase
{
public function test()
{
$this->travelTo('2024-08-11');
$this->travelTo(CarbonImmutable::parse('2024-08-11'));
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace RectorLaravel\Tests\Rector\StaticCall\CarbonSetTestNowToTravelToRector\Fixture;

use Illuminate\Foundation\Testing\TestCase;
use Illuminate\Support\Carbon;

class SomeTest extends TestCase
{
public function test()
{
Carbon::setTestNow('2024-08-11');
Carbon::setTestNow(Carbon::parse('2024-08-11'));
}
}

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\StaticCall\CarbonSetTestNowToTravelToRector\Fixture;

use Illuminate\Foundation\Testing\TestCase;
use Illuminate\Support\Carbon;

class SomeTest extends TestCase
{
public function test()
{
$this->travelTo('2024-08-11');
$this->travelTo(Carbon::parse('2024-08-11'));
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace RectorLaravel\Tests\Rector\StaticCall\CarbonSetTestNowToTravelToRector\Fixture;

use Carbon\Carbon;
use Illuminate\Foundation\Testing\TestCase;
use Foo\Bar;

class SomeTest extends TestCase
{
public function test()
{
Bar::setTestNow('2024-08-11');
Bar::setTestNow(Carbon::parse('2024-08-11'));
}
}

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\StaticCall\CarbonSetTestNowToTravelToRector\Fixture;

use Carbon\Carbon;
use Illuminate\Foundation\Testing\TestCase;
use Foo\Bar;

class SomeTest extends TestCase
{
public function test()
{
Bar::setTestNow('2024-08-11');
Bar::setTestNow(Carbon::parse('2024-08-11'));
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\StaticCall\CarbonSetTestNowToTravelToRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');
$rectorConfig->rule(CarbonSetTestNowToTravelToRector::class);
};

0 comments on commit 8d39c5b

Please sign in to comment.