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
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\Expression\DecorateWillReturnMapWithExpectsMockRector;

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

final class DecorateWillReturnMapWithExpectsMockRectorTest 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,33 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\DecorateWillReturnMapWithExpectsMockRector\Fixture;

final class SomeTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$someMock = $this->createMock(\stdClass::class);
$someMock->method('some')->willReturnMap([1, 2]);
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\DecorateWillReturnMapWithExpectsMockRector\Fixture;

final class SomeTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$someMock = $this->createMock(\stdClass::class);
$someMock->expects($this->exactly(2))->method('some')->willReturnMap([1, 2]);
}
}

?>
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\CodeQuality\Rector\Expression\DecorateWillReturnMapWithExpectsMockRector;

return RectorConfig::configure()
->withRules([DecorateWillReturnMapWithExpectsMockRector::class]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\Rector\Expression;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt\Expression;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\DecorateWillReturnMapWithExpectsMockRector\DecorateWillReturnMapWithExpectsMockRectorTest
*/
final class DecorateWillReturnMapWithExpectsMockRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Decorate willReturnMap() calls with expects on the mock object if missing', [
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

final class SomeTest extends TestCase
{
private MockObject $someMock;

protected function setUp(): void
{
$this->someMock = $this->createMock(SomeClass::class);

$this->someMock->method("someMethod")
->willReturnMap([
["arg1", "arg2", "result1"],
["arg3", "arg4", "result2"],
]);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

final class SomeTest extends TestCase
{
private MockObject $someMock;

protected function setUp(): void
{
$this->someMock = $this->createMock(SomeClass::class);

$this->someMock->expects($this->exactly(2))
->method("someMethod")
->willReturnMap([
["arg1", "arg2", "result1"],
["arg3", "arg4", "result2"],
]);
}
}
CODE_SAMPLE
),

]);
}

public function getNodeTypes(): array
{
return [Expression::class];
}

/**
* @param Expression $node
* @return Expression|null
*/
public function refactor(Node $node)
{
if (! $node->expr instanceof MethodCall) {
return null;
}

$methodCall = $node->expr;
if (! $this->isName($methodCall->name, 'willReturnMap')) {
return null;
}

$topmostCall = $this->resolveTopmostCall($methodCall);

// already covered
if ($this->isName($topmostCall->name, 'expects')) {
return null;
}

// count values in will map arg
$willReturnMapArg = $methodCall->getArgs()[0];
if (! $willReturnMapArg->value instanceof Array_) {
return null;
}

$array = $willReturnMapArg->value;
$mapCount = count($array->items);

$topmostCall->var = new MethodCall(
$topmostCall->var,
new Identifier('expects'),
[new Arg(new MethodCall(
new Variable('this'),
new Identifier('exactly'),
[new Arg(new Int_($mapCount))]
))]
);

return $node;
}

private function resolveTopmostCall(MethodCall $methodCall): MethodCall
{
$currentCall = $methodCall;
while ($currentCall->var instanceof MethodCall) {
$currentCall = $currentCall->var;
}

return $currentCall;
}
}