Skip to content

Commit

Permalink
this param fixture (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Jan 19, 2024
1 parent 12dec83 commit 43230f3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\PhpSpecToPHPUnit\Tests\Rector\Expression\ExpectedMockDeclarationRector\Fixture;

use PhpSpec\ObjectBehavior;
use Rector\PhpSpecToPHPUnit\Tests\Rector\Expression\ExpectedMockDeclarationRector\Source\SomeMethodType;

class WithThisArgumentSpec extends ObjectBehavior
{
public function it_should_succeed()
{
$someMock = $this->createMock(SomeMethodType::class);
$someMock->hasFailed($this)->shouldBeCalled();
}
}

?>
-----
<?php

namespace Rector\PhpSpecToPHPUnit\Tests\Rector\Expression\ExpectedMockDeclarationRector\Fixture;

use PhpSpec\ObjectBehavior;
use Rector\PhpSpecToPHPUnit\Tests\Rector\Expression\ExpectedMockDeclarationRector\Source\SomeMethodType;

class WithThisArgumentSpec extends ObjectBehavior
{
public function it_should_succeed()
{
$someMock = $this->createMock(SomeMethodType::class);
$someMock->expects($this->once())->method('hasFailed')->with($this->withThisArgument)->shouldBeCalled();
}
}

?>
24 changes: 19 additions & 5 deletions rules/Rector/Expression/ExpectedMockDeclarationRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
use PhpParser\NodeFinder;
use PhpParser\NodeTraverser;
use PHPStan\Type\Generic\GenericClassStringType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpSpecToPHPUnit\Enum\PhpSpecMethodName;
use Rector\PhpSpecToPHPUnit\Enum\PHPUnitMethodName;
use Rector\PhpSpecToPHPUnit\Naming\PhpSpecRenaming;
use Rector\PhpSpecToPHPUnit\Naming\SystemMethodDetector;
use Rector\PhpSpecToPHPUnit\NodeFactory\ExpectsCallFactory;
use Rector\PhpSpecToPHPUnit\NodeFactory\WillCallableAssertFactory;
Expand All @@ -34,7 +36,8 @@
final class ExpectedMockDeclarationRector extends AbstractRector
{
public function __construct(
private readonly WillCallableAssertFactory $willCallableAssertFactory
private readonly WillCallableAssertFactory $willCallableAssertFactory,
private readonly PhpSpecRenaming $phpSpecRenaming,
) {
}

Expand All @@ -56,9 +59,6 @@ public function refactor(Node $node): ?Node
return null;
}

// handled in another rule
$hasShouldNotBeCalled = MethodCallFinder::hasByName($node, PhpSpecMethodName::SHOULD_NOT_BE_CALLED);

$firstMethodCall = $node->expr;

// usually a chain method call
Expand All @@ -69,6 +69,9 @@ public function refactor(Node $node): ?Node
// replace ->method('...') with expects('...')->method('methodName')
$hasChanged = false;

// handled in another rule
$hasShouldNotBeCalled = MethodCallFinder::hasByName($node, PhpSpecMethodName::SHOULD_NOT_BE_CALLED);

$this->traverseNodesWithCallable($firstMethodCall, function (Node $node) use (
&$hasChanged,
$hasShouldNotBeCalled
Expand All @@ -83,7 +86,7 @@ public function refactor(Node $node): ?Node
}

// rename method
if ($node->name->toString() === PhpSpecMethodName::WILL_THROW) {
if ($this->isName($node->name, PhpSpecMethodName::WILL_THROW)) {
$node->name = new Identifier(PHPUnitMethodName::WILL_THROW_EXCEPTION);
return $node;
}
Expand Down Expand Up @@ -162,6 +165,17 @@ public function it_returns()
private function appendWithMethodCall(MethodCall $methodCall, array $args): MethodCall
{
foreach ($args as $arg) {
// flip $this to tested object property fetch
if ($arg->value instanceof Variable && $this->isName($arg->value, 'this')) {
$scope = $arg->getAttribute(AttributeKey::SCOPE);

/** @var string $testedObjectPropertyName */
$testedObjectPropertyName = $this->phpSpecRenaming->resolveTestedObjectPropertyNameFromScope($scope);

$arg->value = new PropertyFetch($arg->value, $testedObjectPropertyName);
continue;
}

if ($arg->value instanceof StaticCall) {
$staticCall = $arg->value;

Expand Down
6 changes: 2 additions & 4 deletions src/Naming/SystemMethodDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ public static function detect(string $methodName): bool
foreach (self::METHOD_NAME_CLASSES as $methodNameClass) {
$reflectionClass = new ReflectionClass($methodNameClass);

foreach ($reflectionClass->getConstants() as $constantValue) {
if ($methodName === $constantValue) {
return true;
}
if (in_array($methodName, $reflectionClass->getConstants(), true)) {
return true;
}
}

Expand Down

0 comments on commit 43230f3

Please sign in to comment.