Skip to content

Commit

Permalink
Add new rule AssertSeeToAssertSeeHtmlRector (#251)
Browse files Browse the repository at this point in the history
* Add new rule AssertSeeToAssertSeeHtmlRector

* Add AssertSeeToAssertSeeHtmlRector to Laravel 11 set

* Refactored methods to replace into a protected property
  • Loading branch information
GeniJaho authored Sep 20, 2024
1 parent 321a7f8 commit e2e7899
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 1 deletion.
4 changes: 4 additions & 0 deletions config/sets/laravel110.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\Class_\ModelCastsPropertyToCastsMethodRector;
use RectorLaravel\Rector\MethodCall\AssertSeeToAssertSeeHtmlRector;
use RectorLaravel\Rector\MethodCall\RefactorBlueprintGeometryColumnsRector;

// see https://laravel.com/docs/11.x/upgrade
Expand All @@ -15,4 +16,7 @@

// https://github.com/laravel/framework/pull/49634
$rectorConfig->rule(RefactorBlueprintGeometryColumnsRector::class);

// https://github.com/laravel/framework/pull/52285
$rectorConfig->rule(AssertSeeToAssertSeeHtmlRector::class);
};
29 changes: 28 additions & 1 deletion docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 66 Rules Overview
# 68 Rules Overview

## AbortIfRector

Expand Down Expand Up @@ -237,6 +237,33 @@ Move help facade-like function calls to constructor injection

<br>

## AssertSeeToAssertSeeHtmlRector

Replace assertSee with assertSeeHtml when testing HTML with escape set to false

- class: [`RectorLaravel\Rector\MethodCall\AssertSeeToAssertSeeHtmlRector`](../src/Rector/MethodCall/AssertSeeToAssertSeeHtmlRector.php)

```diff
-$response->assertSee("<li>foo</li>", false);
+$response->assertSeeHtml("<li>foo</li>");
```

<br>

```diff
-$response->assertDontSee("<li>foo</li>", false);
+$response->assertDontSeeHtml("<li>foo</li>");
```

<br>

```diff
-$response->assertSeeInOrder(["<li>foo</li>", "<li>bar</li>"], false);
+$response->assertSeeHtmlInOrder(["<li>foo</li>", "<li>bar</li>"]);
```

<br>

## AssertStatusToAssertMethodRector

Replace `(new \Illuminate\Testing\TestResponse)->assertStatus(200)` with `(new \Illuminate\Testing\TestResponse)->assertOk()`
Expand Down
92 changes: 92 additions & 0 deletions src/Rector/MethodCall/AssertSeeToAssertSeeHtmlRector.php
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]]
);
}
}
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';
}
}
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>"]);
}
}
?>
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>"]);
}
}
?>
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);
}
}
?>
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);
}
}
?>
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);
};

0 comments on commit e2e7899

Please sign in to comment.