-
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 new rule AssertSeeToAssertSeeHtmlRector (#251)
* Add new rule AssertSeeToAssertSeeHtmlRector * Add AssertSeeToAssertSeeHtmlRector to Laravel 11 set * Refactored methods to replace into a protected property
- Loading branch information
Showing
9 changed files
with
251 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
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
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,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RectorLaravel\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\PhpParser\Node\Value\ValueResolver; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \RectorLaravel\Tests\Rector\MethodCall\AssertSeeToAssertSeeHtmlRector\AssertSeeToAssertSeeHtmlRectorTest | ||
*/ | ||
final class AssertSeeToAssertSeeHtmlRector extends AbstractRector | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
protected array $methodsToReplace = [ | ||
'assertSee' => 'assertSeeHtml', | ||
'assertDontSee' => 'assertDontSeeHtml', | ||
'assertSeeInOrder' => 'assertSeeHtmlInOrder', | ||
]; | ||
|
||
public function __construct( | ||
private readonly ValueResolver $valueResolver | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Replace assertSee with assertSeeHtml when testing HTML with escape set to false', | ||
[ | ||
new CodeSample( | ||
'$response->assertSee("<li>foo</li>", false);', | ||
'$response->assertSeeHtml("<li>foo</li>");' | ||
), | ||
new CodeSample( | ||
'$response->assertDontSee("<li>foo</li>", false);', | ||
'$response->assertDontSeeHtml("<li>foo</li>");' | ||
), | ||
new CodeSample( | ||
'$response->assertSeeInOrder(["<li>foo</li>", "<li>bar</li>"], false);', | ||
'$response->assertSeeHtmlInOrder(["<li>foo</li>", "<li>bar</li>"]);' | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isObjectType($node->var, new ObjectType('Illuminate\Testing\TestResponse'))) { | ||
return null; | ||
} | ||
|
||
$methodCallName = (string) $this->getName($node->name); | ||
|
||
if (! array_key_exists($methodCallName, $this->methodsToReplace)) { | ||
return null; | ||
} | ||
|
||
if (count($node->getArgs()) !== 2) { | ||
return null; | ||
} | ||
|
||
if (! $this->valueResolver->isFalse($node->getArgs()[1]->value)) { | ||
return null; | ||
} | ||
|
||
return $this->nodeFactory->createMethodCall( | ||
$node->var, | ||
$this->methodsToReplace[$methodCallName], | ||
[$node->getArgs()[0]] | ||
); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...s/Rector/MethodCall/AssertSeeToAssertSeeHtmlRector/AssertSeeToAssertSeeHtmlRectorTest.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\AssertSeeToAssertSeeHtmlRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class AssertSeeToAssertSeeHtmlRectorTest 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'; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
.../MethodCall/AssertSeeToAssertSeeHtmlRector/Fixture/fixture_with_html_escape_false.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,33 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\AssertStatusToAssertMethodRector\Fixture; | ||
|
||
use Illuminate\Testing\TestResponse; | ||
|
||
class FixtureWithHtmlEscapeFalse | ||
{ | ||
public function testSeeHtml(TestResponse $response) | ||
{ | ||
$response->assertSee("<li>foo</li>", false); | ||
$response->assertDontSee("<li>bar</li>", false); | ||
$response->assertSeeInOrder(["<li>foo</li>", "<li>bar</li>", "<li>baz</li>"], false); | ||
} | ||
} | ||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\AssertStatusToAssertMethodRector\Fixture; | ||
|
||
use Illuminate\Testing\TestResponse; | ||
|
||
class FixtureWithHtmlEscapeFalse | ||
{ | ||
public function testSeeHtml(TestResponse $response) | ||
{ | ||
$response->assertSeeHtml("<li>foo</li>"); | ||
$response->assertDontSeeHtml("<li>bar</li>"); | ||
$response->assertSeeHtmlInOrder(["<li>foo</li>", "<li>bar</li>", "<li>baz</li>"]); | ||
} | ||
} | ||
?> |
22 changes: 22 additions & 0 deletions
22
...Call/AssertSeeToAssertSeeHtmlRector/Fixture/skip_with_html_escape_true_or_missing.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,22 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\AssertStatusToAssertMethodRector\Fixture; | ||
|
||
use Illuminate\Testing\TestResponse; | ||
|
||
class FixtureWithHtmlEscapeTrueOrMissing | ||
{ | ||
public function testSeeHtml(TestResponse $response) | ||
{ | ||
$response->assertSee("<li>foo</li>", true); | ||
$response->assertSee("<li>foo</li>", 1); | ||
$response->assertSee("<li>bar</li>"); | ||
$response->assertDontSeeHtml("<li>bar</li>", true); | ||
$response->assertDontSeeHtml("<li>bar</li>", 1); | ||
$response->assertDontSeeHtml("<li>bar</li>"); | ||
$response->assertSeeInOrder(["<li>foo</li>", "<li>bar</li>", "<li>baz</li>"], true); | ||
$response->assertSeeInOrder(["<li>foo</li>", "<li>bar</li>", "<li>baz</li>"], 1); | ||
$response->assertSeeInOrder(["<li>foo</li>", "<li>bar</li>", "<li>baz</li>"]); | ||
} | ||
} | ||
?> |
14 changes: 14 additions & 0 deletions
14
...tor/MethodCall/AssertSeeToAssertSeeHtmlRector/Fixture/skip_with_non_test_response.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,14 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\AssertStatusToAssertMethodRector\Fixture; | ||
|
||
class FixtureWithNonTestResponse | ||
{ | ||
public function testSeeHtml($response) | ||
{ | ||
$response->assertSee("<li>foo</li>", false); | ||
$response->assertSee("<li>foo</li>", false); | ||
$response->assertDontSee("<li>bar</li>", false); | ||
} | ||
} | ||
?> |
15 changes: 15 additions & 0 deletions
15
...or/MethodCall/AssertSeeToAssertSeeHtmlRector/Fixture/skip_with_other_method_names.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,15 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\AssertStatusToAssertMethodRector\Fixture; | ||
|
||
use Illuminate\Testing\TestResponse; | ||
|
||
class FixtureWithOtherMethodNames | ||
{ | ||
public function testSeeHtml(TestResponse $response) | ||
{ | ||
$response->assertSomethingElse("<li>foo</li>", false); | ||
$response->assertSeeText("<li>foo</li>", false); | ||
} | ||
} | ||
?> |
12 changes: 12 additions & 0 deletions
12
tests/Rector/MethodCall/AssertSeeToAssertSeeHtmlRector/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\AssertSeeToAssertSeeHtmlRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); | ||
|
||
$rectorConfig->rule(AssertSeeToAssertSeeHtmlRector::class); | ||
}; |