-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CarbonSetTestNowToTravelToRector rule (#240)
* 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
Showing
9 changed files
with
344 additions
and
1 deletion.
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
99 changes: 99 additions & 0 deletions
99
src/Rector/StaticCall/CarbonSetTestNowToTravelToRector.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,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')); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...ctor/StaticCall/CarbonSetTestNowToTravelToRector/CarbonSetTestNowToTravelToRectorTest.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,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'; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...StaticCall/CarbonSetTestNowToTravelToRector/Fixture/in_non_laravel_testcase_class.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,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')); | ||
} | ||
} | ||
|
||
?> |
35 changes: 35 additions & 0 deletions
35
tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_carbon_carbon.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,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')); | ||
} | ||
} | ||
|
||
?> |
35 changes: 35 additions & 0 deletions
35
...r/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_carbon_carbonimmutable.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,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')); | ||
} | ||
} | ||
|
||
?> |
35 changes: 35 additions & 0 deletions
35
...taticCall/CarbonSetTestNowToTravelToRector/Fixture/with_illuminate_support_carbon.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,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')); | ||
} | ||
} | ||
|
||
?> |
37 changes: 37 additions & 0 deletions
37
.../Rector/StaticCall/CarbonSetTestNowToTravelToRector/Fixture/with_non_carbon_class.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,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')); | ||
} | ||
} | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
tests/Rector/StaticCall/CarbonSetTestNowToTravelToRector/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,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); | ||
}; |