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: 1 addition & 1 deletion config/sets/phpunit-code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

declare(strict_types=1);

use Rector\PHPUnit\CodeQuality\Rector\Expression\DecorateWillReturnMapWithExpectsMockRector;
use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\CallLike\DirectInstanceOverMockArgRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddParamTypeFromDependsRector;
Expand All @@ -26,6 +25,7 @@
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\RemoveStandaloneCreateMockRector;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector;
use Rector\PHPUnit\CodeQuality\Rector\Expression\AssertArrayCastedObjectToAssertSameRector;
use Rector\PHPUnit\CodeQuality\Rector\Expression\DecorateWillReturnMapWithExpectsMockRector;
use Rector\PHPUnit\CodeQuality\Rector\Foreach_\SimplifyForeachInstanceOfRector;
use Rector\PHPUnit\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertCompareOnCountableWithMethodToAssertCountRector;
Expand Down
2 changes: 2 additions & 0 deletions config/sets/phpunit120.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubInCoalesceArgRector;
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\AllowMockObjectsForDataProviderRector;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\AllowMockObjectsWhereParentClassRector;
Expand All @@ -19,6 +20,7 @@

// stubs over mocks
CreateStubOverCreateMockArgRector::class,
CreateStubInCoalesceArgRector::class,
ExpressionCreateMockToCreateStubRector::class,
PropertyCreateMockToCreateStubRector::class,
AllowMockObjectsWhereParentClassRector::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\CallLike\CreateStubInCoalesceArgRector;

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

final class CreateStubInCoalesceArgRectorTest 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,41 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\CallLike\CreateStubInCoalesceArgRector\Fixture;

use PHPUnit\Framework\TestCase;

final class HandleArrayItem extends TestCase
{
public function testThat()
{
$value = mt_rand(0, 1);

$items = [
$value ?? $this->createMock(\stdClass::class),
$value ?? $this->createMock(\stdClass::class),
];
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\CallLike\CreateStubInCoalesceArgRector\Fixture;

use PHPUnit\Framework\TestCase;

final class HandleArrayItem extends TestCase
{
public function testThat()
{
$value = mt_rand(0, 1);

$items = [
$value ?? $this->createStub(\stdClass::class),
$value ?? $this->createStub(\stdClass::class),
];
}
}

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

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\CallLike\CreateStubInCoalesceArgRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipInAssign extends TestCase
{
public function testThat()
{
$value = mt_rand(0, 1);

$result = $value ?? $this->createMock(\stdClass::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\CallLike\CreateStubInCoalesceArgRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SomeFileWithMocks extends TestCase
{
public function testThat()
{
$value = mt_rand(0, 1);

$anyClass = new \stdClass();
$anyClass->run(
$value ?? $this->createMock(\stdClass::class),
$value ?? $this->createMock(\stdClass::class),
);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\CallLike\CreateStubInCoalesceArgRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SomeFileWithMocks extends TestCase
{
public function testThat()
{
$value = mt_rand(0, 1);

$anyClass = new \stdClass();
$anyClass->run(
$value ?? $this->createStub(\stdClass::class),
$value ?? $this->createStub(\stdClass::class),
);
}
}

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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\PHPUnit120\Rector\CallLike\CreateStubInCoalesceArgRector;

return RectorConfig::configure()
->withRules([CreateStubInCoalesceArgRector::class]);
147 changes: 147 additions & 0 deletions rules/PHPUnit120/Rector/CallLike/CreateStubInCoalesceArgRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\PHPUnit120\Rector\CallLike;

use PhpParser\Node;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* Related change in PHPUnit 12 https://phpunit.expert/articles/testing-with-and-without-dependencies.html
*
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\CallLike\CreateStubInCoalesceArgRector\CreateStubInCoalesceArgRectorTest
*/
final class CreateStubInCoalesceArgRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Use createStub() over createMock() when used as argument/array item coalesce ?? fallback',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function test()
{
$mockObject = $this->>get('service');
$this->someMethod($mockObject ?? $this->createMock(SomeClass::class));
}
private function someMethod($someClass)
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function test()
{
$mockObject = $this->>get('service');
$this->someMethod($mockObject ?? $this->createStub(SomeClass::class));
}
private function someMethod($someClass)
{
}
CODE_SAMPLE
),

]
);
}

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

/**
* @param MethodCall|StaticCall|New_|ArrayItem $node
*/
public function refactor(Node $node): MethodCall|StaticCall|New_|ArrayItem|null
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}

if ($node instanceof ArrayItem) {
return $this->refactorArrayItem($node);
}

$hasChanges = false;
if ($node->isFirstClassCallable()) {
return null;
}

foreach ($node->getArgs() as $arg) {
if (! $arg->value instanceof Coalesce) {
continue;
}

$coalesce = $arg->value;
if (! $coalesce->right instanceof MethodCall) {
continue;
}

$methodCall = $coalesce->right;
if (! $this->isName($methodCall->name, 'createMock')) {
continue;
}

$methodCall->name = new Identifier('createStub');
$hasChanges = true;
}

if ($hasChanges) {
return $node;
}

return null;
}

private function refactorArrayItem(ArrayItem $arrayItem): ?ArrayItem
{
if (! $arrayItem->value instanceof Coalesce) {
return null;
}

$coalesce = $arrayItem->value;
if (! $coalesce->right instanceof MethodCall) {
return null;
}

$methodCall = $coalesce->right;
if (! $this->isName($methodCall->name, 'createMock')) {
return null;
}

$methodCall->name = new Identifier('createStub');

return $arrayItem;
}
}