Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move stubs to composer-autoload #5890

Merged
merged 3 commits into from
Mar 18, 2021
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
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
"doctrine/annotations": "^1.12",
"doctrine/inflector": "^2.0",
"jean85/pretty-package-versions": "^1.5.1|^2.0.1",
"nette/robot-loader": "^3.2 <=3.3.1",
"nette/utils": "^3.2",
"nikic/php-parser": "^4.10.4",
"nette/robot-loader": "^3.4",
"phpstan/phpdoc-parser": "^0.4.12",
"phpstan/phpstan": "^0.12.81",
"phpstan/phpstan-phpunit": "^0.12.18",
Expand Down Expand Up @@ -99,6 +99,7 @@
"Rector\\Utils\\DoctrineAnnotationParserSyncer\\": "utils/doctrine-annotation-parser-syncer/src"
},
"classmap": [
"stubs/Annotations",
"rules-tests/Autodiscovery/Rector/FileNode/MoveInterfacesToContractNamespaceDirectoryRector/Expected",
"rules-tests/Autodiscovery/Rector/FileNode/MoveServicesBySuffixToDirectoryRector/Expected",
"rules-tests/CakePHP/Rector/FileWithoutNamespace/ImplicitShortClassNameUseStatementRector/Source",
Expand All @@ -109,6 +110,9 @@
"rules-tests/Symfony4/Rector/MethodCall/ContainerGetToConstructorInjectionRector/Source"
],
"files": [
"stubs/Symfony/Component/Form/FormTypeInterface.php",
"stubs/Doctrine/Persistence/ObjectManager.php",
"stubs/Doctrine/Common/Persistence/ObjectManager.php",
"vendor/nette/forms/src/Forms/Controls/SubmitButton.php",
"rules-tests/Restoration/Rector/Use_/RestoreFullyQualifiedNameRector/Source/ShortClassOnly.php",
"rules-tests/DeadCode/Rector/MethodCall/RemoveDefaultArgumentValueRector/Source/UserDefined.php",
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,5 @@ parameters:
message: '#Cannot cast array<string\>\|string\|null to string#'
paths:
- utils/compiler/src/Command/DowngradePathsCommand.php

- '#Method Rector\\Testing\\Finder\\RectorsFinder\:\:findClassesInDirectoriesByName\(\) should return array<class\-string\> but returns array<int, string\>#'

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public function test(SmartFileInfo $fileInfo): void
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
Expand Down
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
Loading