Skip to content

Commit

Permalink
apply Rector
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Mar 18, 2021
1 parent dd05a4a commit 3b68df0
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 117 deletions.
86 changes: 49 additions & 37 deletions rules/PHPUnit/NodeAnalyzer/ExpectationAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

final class ExpectationAnalyzer
{
/**
* @var string[]
*/
private const PROCESSABLE_WILL_STATEMENTS = [
'will',
'willReturn',
Expand Down Expand Up @@ -74,90 +77,99 @@ public function getExpectationsFromExpressions(array $stmts): ExpectationMockCol

$atArg = $expectsValue->args[0];
$atValue = $atArg->value;
if ($atValue instanceof LNumber && $expects->var instanceof Variable) {
$expectationMockCollection->add(
new ExpectationMock(
$expects->var,
$method->args,
$atValue->value,
$this->getWill($expr),
$this->getWithArgs($method->var),
$stmt
)
);
if (! $atValue instanceof LNumber) {
continue;
}
if (! $expects->var instanceof Variable) {
continue;
}

$expectationMockCollection->add(
new ExpectationMock(
$expects->var,
$method->args,
$atValue->value,
$this->getWill($expr),
$this->getWithArgs($method->var),
$stmt
)
);
}

return $expectationMockCollection;
}

public function isValidExpectsCall(MethodCall $expr): bool
public function isValidExpectsCall(MethodCall $methodCall): bool
{
if (! $this->testsNodeAnalyzer->isPHPUnitMethodName($expr, 'expects')) {
if (! $this->testsNodeAnalyzer->isPHPUnitMethodName($methodCall, 'expects')) {
return false;
}

if (count($expr->args) !== 1) {
if (count($methodCall->args) !== 1) {
return false;
}

return true;
}

public function isValidAtCall(MethodCall $expr): bool
public function isValidAtCall(MethodCall $methodCall): bool
{
if (! $this->testsNodeAnalyzer->isPHPUnitMethodName($expr, 'at')) {
if (! $this->testsNodeAnalyzer->isPHPUnitMethodName($methodCall, 'at')) {
return false;
}

if (count($expr->args) !== 1) {
if (count($methodCall->args) !== 1) {
return false;
}

return true;
}

private function getMethod(MethodCall $expr): MethodCall
private function getMethod(MethodCall $methodCall): MethodCall
{
if ($this->testsNodeAnalyzer->isPHPUnitMethodNames(
$expr,
$methodCall,
self::PROCESSABLE_WILL_STATEMENTS
) && $expr->var instanceof MethodCall) {
return $expr->var;
) && $methodCall->var instanceof MethodCall) {
return $methodCall->var;
}

return $expr;
return $methodCall;
}

private function getWill(MethodCall $expr): ?Expr
private function getWill(MethodCall $methodCall): ?Expr
{
if (! $this->testsNodeAnalyzer->isPHPUnitMethodNames($expr, self::PROCESSABLE_WILL_STATEMENTS)) {
if (! $this->testsNodeAnalyzer->isPHPUnitMethodNames($methodCall, self::PROCESSABLE_WILL_STATEMENTS)) {
return null;
}

return $this->consecutiveAssertionFactory->createWillReturn($expr);
return $this->consecutiveAssertionFactory->createWillReturn($methodCall);
}

private function getExpects(Expr $maybeWith, MethodCall $method): Expr
private function getExpects(Expr $expr, MethodCall $methodCall): Expr
{
if ($this->testsNodeAnalyzer->isPHPUnitMethodName($maybeWith, 'with') && $maybeWith instanceof MethodCall) {
return $maybeWith->var;
if (! $this->testsNodeAnalyzer->isPHPUnitMethodName($expr, 'with')) {
return $methodCall->var;
}

return $method->var;
if (! $expr instanceof MethodCall) {
return $methodCall->var;
}
return $expr->var;
}

/**
* @return array<int, Expr|null>
*/
private function getWithArgs(Expr $maybeWith): array
private function getWithArgs(Expr $expr): array
{
if ($this->testsNodeAnalyzer->isPHPUnitMethodName($maybeWith, 'with') && $maybeWith instanceof MethodCall) {
return array_map(static function (Arg $arg) {
return $arg->value;
}, $maybeWith->args);
if (! $this->testsNodeAnalyzer->isPHPUnitMethodName($expr, 'with')) {
return [null];
}

return [null];
if (! $expr instanceof MethodCall) {
return [null];
}
return array_map(static function (Arg $arg): Expr {
return $arg->value;
}, $expr->args);
}
}
27 changes: 16 additions & 11 deletions rules/PHPUnit/NodeFactory/ConsecutiveAssertionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
Expand All @@ -17,6 +19,9 @@

final class ConsecutiveAssertionFactory
{
/**
* @var array<string, string>
*/
private const REPLACE_WILL_MAP = [
'willReturnMap' => 'returnValueMap',
'willReturnArgument' => 'returnArgument',
Expand All @@ -29,30 +34,30 @@ public function createAssertionFromExpectationMockCollection(
): MethodCall {
$expectationMocks = $expectationMockCollection->getExpectationMocks();

$var = $expectationMocks[0]->getExpectationVariable();
$variable = $expectationMocks[0]->getExpectationVariable();
$methodArguments = $expectationMocks[0]->getMethodArguments();

$expectationMocks = $this->sortExpectationMocksByIndex($expectationMocks);

if (! $expectationMockCollection->hasReturnValues()) {
return $this->createWithConsecutive(
$this->createMethod($var, $methodArguments),
$this->createMethod($variable, $methodArguments),
$this->createWithArgs($expectationMocks)
);
}

if ($expectationMockCollection->hasWithValues()) {
return $this->createWillReturnOnConsecutiveCalls(
$this->createWithConsecutive(
$this->createMethod($var, $methodArguments),
$this->createMethod($variable, $methodArguments),
$this->createWithArgs($expectationMocks)
),
$this->createReturnArgs($expectationMocks)
);
}

return $this->createWillReturnOnConsecutiveCalls(
$this->createMethod($var, $methodArguments),
$this->createMethod($variable, $methodArguments),
$this->createReturnArgs($expectationMocks)
);
}
Expand Down Expand Up @@ -113,7 +118,7 @@ public function createWillReturn(MethodCall $methodCall): Expr
*/
private function createReturnArgs(array $expectationMocks): array
{
return array_map(static function (ExpectationMock $expectationMock) {
return array_map(static function (ExpectationMock $expectationMock): Arg {
return new Arg($expectationMock->getReturn() ?: new ConstFetch(new Name('null')));
}, $expectationMocks);
}
Expand All @@ -124,11 +129,11 @@ private function createReturnArgs(array $expectationMocks): array
*/
private function createWithArgs(array $expectationMocks): array
{
return array_map(static function (ExpectationMock $expectationMock) {
$arrayItems = array_map(static function (?Expr $expr) {
return array_map(static function (ExpectationMock $expectationMock): Arg {
$arrayItems = array_map(static function (?Expr $expr): ArrayItem {
return new ArrayItem($expr ?: new ConstFetch(new Name('null')));
}, $expectationMock->getWithArguments());
return new Arg(new Expr\Array_($arrayItems));
return new Arg(new Array_($arrayItems));
}, $expectationMocks);
}

Expand All @@ -137,9 +142,9 @@ private function createWillReturnSelf(): MethodCall
return $this->createMethodCall(new Variable('this'), 'returnSelf', []);
}

private function createWillReturnReference(MethodCall $methodCall): Expr\New_
private function createWillReturnReference(MethodCall $methodCall): New_
{
return new Expr\New_(
return new New_(
new FullyQualified('PHPUnit\Framework\MockObject\Stub\ReturnReference'),
[new Arg($methodCall->args[0]->value)]
);
Expand Down Expand Up @@ -170,7 +175,7 @@ private function sortExpectationMocksByIndex(array $expectationMocks): array
{
usort(
$expectationMocks,
static function (ExpectationMock $expectationMockA, ExpectationMock $expectationMockB) {
static function (ExpectationMock $expectationMockA, ExpectationMock $expectationMockB): int {
return $expectationMockA->getIndex() > $expectationMockB->getIndex() ? 1 : -1;
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Rector\AbstractRector;
use Rector\PHPUnit\NodeAnalyzer\ExpectationAnalyzer;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\PHPUnit\NodeFactory\ConsecutiveAssertionFactory;
use Rector\PHPUnit\ValueObject\ExpectationMock;
use Rector\PHPUnit\ValueObject\ExpectationMockCollection;
Expand All @@ -28,23 +27,16 @@ final class MigrateAtToConsecutiveExpectationsRector extends AbstractRector
*/
private $consecutiveAssertionFactory;

/**
* @var TestsNodeAnalyzer
*/
private $testsNodeAnalyzer;

/**
* @var ExpectationAnalyzer
*/
private $expectationAnalyzer;

public function __construct(
ConsecutiveAssertionFactory $consecutiveAssertionFactory,
TestsNodeAnalyzer $testsNodeAnalyzer,
ExpectationAnalyzer $expectationAnalyzer
) {
$this->consecutiveAssertionFactory = $consecutiveAssertionFactory;
$this->testsNodeAnalyzer = $testsNodeAnalyzer;
$this->expectationAnalyzer = $expectationAnalyzer;
}

Expand Down Expand Up @@ -88,7 +80,7 @@ public function refactor(Node $node): ?Node
return null;
}

$expressions = array_filter($stmts, function (Stmt $expr) {
$expressions = array_filter($stmts, function (Stmt $expr): bool {
return $expr instanceof Expression && $expr->expr instanceof MethodCall;
});

Expand Down Expand Up @@ -117,7 +109,7 @@ private function buildNewExpectation(ExpectationMockCollection $expectationMockC
private function fillMissingAtIndexes(
ExpectationMockCollection $expectationMockCollection
): ExpectationMockCollection {
$var = $expectationMockCollection->getExpectationMocks()[0]
$variable = $expectationMockCollection->getExpectationMocks()[0]
->getExpectationVariable();

// 0,1,2,3,4
Expand All @@ -135,7 +127,7 @@ private function fillMissingAtIndexes(
// 0,1,2
if ($expectationMockCollection->getLowestAtIndex() !== 0) {
for ($i = 0; $i < $expectationMockCollection->getLowestAtIndex(); ++$i) {
$expectationMockCollection->add(new ExpectationMock($var, [], $i, null, [], null));
$expectationMockCollection->add(new ExpectationMock($variable, [], $i, null, [], null));
}
}

Expand All @@ -146,7 +138,7 @@ private function fillMissingAtIndexes(
$existingIndexes = array_column($expectationMockCollection->getExpectationMocks(), 'index');
for ($i = 1; $i < $expectationMockCollection->getHighestAtIndex(); ++$i) {
if (! in_array($i, $existingIndexes, true)) {
$expectationMockCollection->add(new ExpectationMock($var, [], $i, null, [], null));
$expectationMockCollection->add(new ExpectationMock($variable, [], $i, null, [], null));
}
}
}
Expand All @@ -159,15 +151,15 @@ private function replaceExpectationNodes(ExpectationMockCollection $expectationM
return;
}

$endLines = array_map(static function (ExpectationMock $expectationMock) {
$originalExpression = $expectationMock->originalExpression();
return $originalExpression === null ? 0 : $originalExpression->getEndLine();
$endLines = array_map(static function (ExpectationMock $expectationMock): int {
$originalExpression = $expectationMock->getOriginalExpression();
return ! $originalExpression instanceof Expression ? 0 : $originalExpression->getEndLine();
}, $expectationMockCollection->getExpectationMocks());
$max = max($endLines);

foreach ($expectationMockCollection->getExpectationMocks() as $expectationMock) {
$originalExpression = $expectationMock->originalExpression();
if ($originalExpression === null) {
$originalExpression = $expectationMock->getOriginalExpression();
if (! $originalExpression instanceof Expression) {
continue;
}
if ($max > $originalExpression->getEndLine()) {
Expand All @@ -191,12 +183,7 @@ private function shouldSkipReplacement(ExpectationMockCollection $expectationMoc
if ($expectationMockCollection->hasMissingAtIndexes()) {
return true;
}

if ($expectationMockCollection->hasMissingReturnValues()) {
return true;
}

return false;
return $expectationMockCollection->hasMissingReturnValues();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function provide(): array
$robotLoader->rebuild();

foreach (array_keys($robotLoader->getIndexedClasses()) as $className) {
$this->phpUnitTestCaseClasses[] = (string) $className;
$this->phpUnitTestCaseClasses[] = $className;
}

return $this->phpUnitTestCaseClasses;
Expand Down
Loading

0 comments on commit 3b68df0

Please sign in to comment.