Skip to content
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/phpunit-code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\NarrowSingleWillReturnCallbackRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\RemoveExpectAnyFromMockRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWillMethodRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWithMethodRector;

Expand Down Expand Up @@ -63,6 +64,7 @@

// type declarations
TypeWillReturnCallableArrowFunctionRector::class,
StringCastAssertStringContainsStringRector::class,

NarrowUnusedSetUpDefinedPropertyRector::class,

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomasVotruba Was this file added intentionally? Due to its missing .inc extension, it is always removed after running tests locally.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems due to run phpunit, and cut in the middle of process, then next is committed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #507


namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertTrueFalseToSpecificMethodRector\Fixture;

use PHPUnit\Framework\TestCase;

final class MyTest2 extends TestCase
{
public function test()
{
$this->assertTrue(\array_search($foo, $this->bar->toArray()));
$this->assertNotFalse(\array_search($foo, $this->bar->toArray()));
}
}

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

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector\Fixture;

use PHPUnit\Framework\TestCase;

class SomeTest extends TestCase
{
public function testSomething(?string $value)
{
$this->assertStringContainsString('foo', $value);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector\Fixture;

use PHPUnit\Framework\TestCase;

class SomeTest extends TestCase
{
public function testSomething(?string $value)
{
$this->assertStringContainsString('foo', (string) $value);
}
}

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

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector\Fixture;

use PHPUnit\Framework\TestCase;

class SkipKnownString extends TestCase
{
public function testSomething(string $value)
{
$this->assertStringContainsString('foo', $value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector;

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

final class StringCastAssertStringContainsStringRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(StringCastAssertStringContainsStringRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector\StringCastAssertStringContainsStringRectorTest
*/
final class StringCastAssertStringContainsStringRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Cast 2nd argument in assertStringContainsString() to a string if not yet',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

class SomeTest extends TestCase
{
public function testSomething(?string $value)
{
$this->assertStringContainsString('foo', $value);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

class SomeTest extends TestCase
{
public function testSomething(?string $value)
{
$this->assertStringContainsString('foo', (string) $value);
}
}
CODE_SAMPLE
),
]
);
}

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

/**
* @param MethodCall|StaticCall $node
*/
public function refactor(Node $node): MethodCall|StaticCall|null
{
if ($node->isFirstClassCallable()) {
return null;
}

if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames(
$node,
['assertStringContainsString', 'assertStringNotContainsString']
)) {
return null;
}

$secondArg = $node->getArgs()[1];
$secondArgType = $this->getType($secondArg->value);

if ($secondArgType->isString()->yes()) {
return null;
}

$secondArg->value = new String_($secondArg->value);

return null;
}
}