From e19e8d9af9b110247e1dc6fc7112d259897dd00b Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 2 Feb 2022 18:06:50 +0100 Subject: [PATCH] [MigrateAtToConsecutiveExpectationsRector] Remove rule as too various paths to handle, better handle manually --- docs/rector_rules_overview.md | 32 +-- phpstan.neon | 1 + src/NodeAnalyzer/ExpectationAnalyzer.php | 165 --------------- .../ConsecutiveAssertionFactory.php | 191 ----------------- ...grateAtToConsecutiveExpectationsRector.php | 193 ------------------ src/ValueObject/ExpectationMock.php | 68 ------ src/ValueObject/ExpectationMockCollection.php | 139 ------------- .../Fixture/different_return_values.php.inc | 66 ------ .../Fixture/expects_non_at_is_skipped.php.inc | 18 -- .../Fixture/handle_multiple_variables.php.inc | 57 ------ .../Fixture/handle_property_fetch.php.inc | 57 ------ .../Fixture/ignoring_with.php.inc | 36 ---- .../Fixture/ignoring_with_complex.php.inc | 35 ---- ...pty_array_if_no_return_expectation.php.inc | 40 ---- ...ty_array_if_no_return_expectation2.php.inc | 40 ---- .../Fixture/mixed.php.inc | 42 ---- .../Fixture/mixed2.php.inc | 48 ----- .../Fixture/mixing_arrays.php.inc | 33 --- ...skip_different_method_expectations.php.inc | 41 ---- .../Fixture/skip_dynamic_variable.php.inc | 28 --- .../Fixture/skip_missing_expects.php.inc | 19 -- .../skip_missing_return_expectation.php.inc | 28 --- ..._with_if_return_expectations_exist.php.inc | 26 --- .../Fixture/skip_other_class_types.php.inc | 14 -- .../Fixture/skip_static_call_self.php.inc | 14 -- .../Fixture/will_callbacks_are_kept.php.inc | 40 ---- .../Fixture/will_return_only.php.inc | 48 ----- .../Fixture/with_multiple_arguments.php.inc | 36 ---- .../Fixture/with_only.php.inc | 48 ----- ...eAtToConsecutiveExpectationsRectorTest.php | 33 --- .../Source/SomeObject.php | 17 -- .../config/configured_rule.php | 14 -- 32 files changed, 12 insertions(+), 1655 deletions(-) delete mode 100644 src/NodeAnalyzer/ExpectationAnalyzer.php delete mode 100644 src/NodeFactory/ConsecutiveAssertionFactory.php delete mode 100644 src/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector.php delete mode 100644 src/ValueObject/ExpectationMock.php delete mode 100644 src/ValueObject/ExpectationMockCollection.php delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/different_return_values.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/expects_non_at_is_skipped.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/handle_multiple_variables.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/handle_property_fetch.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/ignoring_with.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/ignoring_with_complex.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/missing_with_is_replaced_by_empty_array_if_no_return_expectation.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/missing_with_is_replaced_by_empty_array_if_no_return_expectation2.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/mixed.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/mixed2.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/mixing_arrays.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_different_method_expectations.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_dynamic_variable.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_missing_expects.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_missing_return_expectation.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_missing_with_if_return_expectations_exist.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_other_class_types.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_static_call_self.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/will_callbacks_are_kept.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/will_return_only.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/with_multiple_arguments.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/with_only.php.inc delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/MigrateAtToConsecutiveExpectationsRectorTest.php delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Source/SomeObject.php delete mode 100644 tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/config/configured_rule.php diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index feec2f12..4805f464 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 39 Rules Overview +# 38 Rules Overview ## AddDoesNotPerformAssertionToNonAssertingTestRector @@ -87,11 +87,16 @@ return static function (ContainerConfigurator $containerConfigurator): void { $services = $containerConfigurator->services(); $services->set(ArrayArgumentToDataProviderRector::class) - ->configure([ -ArrayArgumentToDataProviderRector::ARRAY_ARGUMENTS_TO_DATA_PROVIDERS => ValueObjectInliner::inline([ - new ArrayArgumentToDataProvider('PHPUnit\Framework\TestCase', 'doTestMultiple', 'doTestSingle', 'number'), - ]), -]]); + ->call('configure', [[ + ArrayArgumentToDataProviderRector::ARRAY_ARGUMENTS_TO_DATA_PROVIDERS => ValueObjectInliner::inline([ + new ArrayArgumentToDataProvider( + 'PHPUnit\Framework\TestCase', + 'doTestMultiple', + 'doTestSingle', + 'number' + ), + ]), + ]]); }; ``` @@ -579,21 +584,6 @@ Turns getMock*() methods to `createMock()`
-## MigrateAtToConsecutiveExpectationsRector - -Migrates deprecated `$this->at` to `$this->withConsecutive` and `$this->willReturnOnConsecutiveCalls` - -- class: [`Rector\PHPUnit\Rector\ClassMethod\MigrateAtToConsecutiveExpectationsRector`](../src/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector.php) - -```diff - $mock = $this->createMock(Foo::class); --$mock->expects($this->at(0))->with('0')->method('someMethod')->willReturn('1'); --$mock->expects($this->at(1))->with('1')->method('someMethod')->willReturn('2'); -+$mock->method('someMethod')->withConsecutive(['0'], ['1'])->willReturnOnConsecutiveCalls('1', '2'); -``` - -
- ## RemoveDataProviderTestPrefixRector Data provider methods cannot start with "test" prefix diff --git a/phpstan.neon b/phpstan.neon index f1b47744..cd3e5153 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -34,3 +34,4 @@ parameters: # rector co-variant - '#Parameter \#1 \$node \(PhpParser\\Node\\(.*?) of method Rector\\(.*?)\(\) should be contravariant with parameter \$node \(PhpParser\\Node\) of method Rector\\Core\\Contract\\Rector\\PhpRectorInterface\:\:refactor\(\)#' + - '#Parameter \#2 \$length of function array_chunk expects int<1, max\>, int given#' diff --git a/src/NodeAnalyzer/ExpectationAnalyzer.php b/src/NodeAnalyzer/ExpectationAnalyzer.php deleted file mode 100644 index ca73934b..00000000 --- a/src/NodeAnalyzer/ExpectationAnalyzer.php +++ /dev/null @@ -1,165 +0,0 @@ -expr; - $method = $this->getMethod($expr); - if (! $this->testsNodeAnalyzer->isInPHPUnitMethodCallName($method, 'method')) { - continue; - } - - /** @var MethodCall $expects */ - $expects = $this->getExpects($method->var, $method); - if (! $this->isValidExpectsCall($expects)) { - continue; - } - - $expectsArg = $expects->getArgs()[0]; - - $expectsValue = $expectsArg->value; - if (! $this->isValidAtCall($expectsValue)) { - continue; - } - - /** @var MethodCall|StaticCall $expectsValue */ - $atArg = $expectsValue->getArgs()[0]; - $atValue = $atArg->value; - if (! $atValue instanceof LNumber) { - continue; - } - - if (! $expects->var instanceof Variable && ! $expects->var instanceof PropertyFetch) { - 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|StaticCall $call): bool - { - if (! $this->testsNodeAnalyzer->isInPHPUnitMethodCallName($call, 'expects')) { - return false; - } - - return count($call->getArgs()) === 1; - } - - public function isValidAtCall(Expr $expr): bool - { - if (! $expr instanceof StaticCall && ! $expr instanceof MethodCall) { - return false; - } - - if (! $this->testsNodeAnalyzer->isInPHPUnitMethodCallName($expr, 'at')) { - return false; - } - - return count($expr->getArgs()) === 1; - } - - private function getMethod(MethodCall $methodCall): MethodCall - { - if ($this->testsNodeAnalyzer->isPHPUnitMethodCallNames( - $methodCall, - self::PROCESSABLE_WILL_STATEMENTS - ) && $methodCall->var instanceof MethodCall) { - return $methodCall->var; - } - - return $methodCall; - } - - private function getWill(MethodCall $methodCall): ?Expr - { - if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($methodCall, self::PROCESSABLE_WILL_STATEMENTS)) { - return null; - } - - return $this->consecutiveAssertionFactory->createWillReturn($methodCall); - } - - private function getExpects(Expr $expr, MethodCall $methodCall): Expr - { - if (! $expr instanceof MethodCall) { - return $methodCall; - } - - if (! $this->testsNodeAnalyzer->isInPHPUnitMethodCallName($expr, 'with')) { - return $methodCall->var; - } - - return $expr->var; - } - - /** - * @return array - */ - private function getWithArgs(Expr $expr): array - { - if (! $this->testsNodeAnalyzer->isInPHPUnitMethodCallName($expr, 'with')) { - return [null]; - } - - if (! $expr instanceof MethodCall) { - return [null]; - } - - return array_map(static fn (Arg $arg): Expr => $arg->value, $expr->args); - } -} diff --git a/src/NodeFactory/ConsecutiveAssertionFactory.php b/src/NodeFactory/ConsecutiveAssertionFactory.php deleted file mode 100644 index db509eb1..00000000 --- a/src/NodeFactory/ConsecutiveAssertionFactory.php +++ /dev/null @@ -1,191 +0,0 @@ - - */ - private const REPLACE_WILL_MAP = [ - 'willReturnMap' => 'returnValueMap', - 'willReturnArgument' => 'returnArgument', - 'willReturnCallback' => 'returnCallback', - 'willThrowException' => 'throwException', - ]; - - public function createAssertionFromExpectationMockCollection( - ExpectationMockCollection $expectationMockCollection - ): MethodCall { - $expectationMocks = $expectationMockCollection->getExpectationMocks(); - - $variable = $expectationMocks[0]->getExpectationVariable(); - $methodArguments = $expectationMocks[0]->getMethodArguments(); - - $expectationMocks = $this->sortExpectationMocksByIndex($expectationMocks); - - if (! $expectationMockCollection->hasReturnValues()) { - return $this->createWithConsecutive( - $this->createMethod($variable, $methodArguments), - $this->createWithArgs($expectationMocks) - ); - } - - if ($expectationMockCollection->hasWithValues()) { - return $this->createWillReturnOnConsecutiveCalls( - $this->createWithConsecutive( - $this->createMethod($variable, $methodArguments), - $this->createWithArgs($expectationMocks) - ), - $this->createReturnArgs($expectationMocks) - ); - } - - return $this->createWillReturnOnConsecutiveCalls( - $this->createMethod($variable, $methodArguments), - $this->createReturnArgs($expectationMocks) - ); - } - - /** - * @param Arg[] $args - */ - public function createWillReturnOnConsecutiveCalls(Expr $expr, array $args): MethodCall - { - return $this->createMethodCall($expr, 'willReturnOnConsecutiveCalls', $args); - } - - /** - * @param Arg[] $args - */ - public function createMethod(Expr $expr, array $args): MethodCall - { - return $this->createMethodCall($expr, 'method', $args); - } - - /** - * @param Arg[] $args - */ - public function createWithConsecutive(Expr $expr, array $args): MethodCall - { - return $this->createMethodCall($expr, 'withConsecutive', $args); - } - - public function createWillReturn(MethodCall $methodCall): Expr - { - if (! $methodCall->name instanceof Identifier) { - return $methodCall; - } - - $methodCallName = $methodCall->name->name; - if ($methodCallName === 'will') { - return $methodCall->args[0]->value; - } - - if ($methodCallName === 'willReturnSelf') { - return $this->createWillReturnSelf(); - } - - if ($methodCallName === 'willReturnReference') { - return $this->createWillReturnReference($methodCall); - } - - if (array_key_exists($methodCallName, self::REPLACE_WILL_MAP)) { - return $this->createMappedWillReturn($methodCallName, $methodCall); - } - - return $methodCall->args[0]->value; - } - - /** - * @param ExpectationMock[] $expectationMocks - * @return Arg[] - */ - private function createReturnArgs(array $expectationMocks): array - { - return array_map( - static fn (ExpectationMock $expectationMock): Arg => new Arg( - $expectationMock->getReturn() instanceof Expr ? $expectationMock->getReturn() : new ConstFetch(new Name( - 'null' - )) - ), - $expectationMocks - ); - } - - /** - * @param ExpectationMock[] $expectationMocks - * @return Arg[] - */ - private function createWithArgs(array $expectationMocks): array - { - return array_map(static function (ExpectationMock $expectationMock): Arg { - $arrayItems = array_map( - static fn (?Expr $expr): ArrayItem => new ArrayItem($expr instanceof Expr ? $expr : new ConstFetch( - new Name('null') - )), - $expectationMock->getWithArguments() - ); - return new Arg(new Array_($arrayItems)); - }, $expectationMocks); - } - - private function createWillReturnSelf(): MethodCall - { - return $this->createMethodCall(new Variable('this'), 'returnSelf', []); - } - - private function createWillReturnReference(MethodCall $methodCall): New_ - { - return new New_( - new FullyQualified('PHPUnit\Framework\MockObject\Stub\ReturnReference'), - [new Arg($methodCall->args[0]->value)] - ); - } - - private function createMappedWillReturn(string $methodCallName, MethodCall $methodCall): MethodCall - { - return $this->createMethodCall( - new Variable('this'), - self::REPLACE_WILL_MAP[$methodCallName], - [new Arg($methodCall->args[0]->value)] - ); - } - - /** - * @param Arg[] $args - */ - private function createMethodCall(Expr $expr, string $name, array $args): MethodCall - { - return new MethodCall($expr, new Identifier($name), $args); - } - - /** - * @param ExpectationMock[] $expectationMocks - * @return ExpectationMock[] - */ - private function sortExpectationMocksByIndex(array $expectationMocks): array - { - usort( - $expectationMocks, - static fn (ExpectationMock $expectationMockA, ExpectationMock $expectationMockB): int => $expectationMockA->getIndex() > $expectationMockB->getIndex() ? 1 : -1 - ); - return $expectationMocks; - } -} diff --git a/src/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector.php b/src/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector.php deleted file mode 100644 index 939202da..00000000 --- a/src/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector.php +++ /dev/null @@ -1,193 +0,0 @@ -at to $this->withConsecutive and $this->willReturnOnConsecutiveCalls', - [ - new CodeSample( - <<<'CODE_SAMPLE' -$mock = $this->createMock(Foo::class); -$mock->expects($this->at(0))->with('0')->method('someMethod')->willReturn('1'); -$mock->expects($this->at(1))->with('1')->method('someMethod')->willReturn('2'); -CODE_SAMPLE -, - <<<'CODE_SAMPLE' -$mock = $this->createMock(Foo::class); -$mock->method('someMethod')->withConsecutive(['0'], ['1'])->willReturnOnConsecutiveCalls('1', '2'); -CODE_SAMPLE - ), - ] - ); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [ClassMethod::class]; - } - - /** - * @param ClassMethod $node - */ - public function refactor(Node $node): ?Node - { - $stmts = $node->stmts; - if ($stmts === null) { - return null; - } - - $expressions = array_filter( - $stmts, - fn (Stmt $expr): bool => $expr instanceof Expression && $expr->expr instanceof MethodCall - ); - - $expectationMockCollection = $this->expectationAnalyzer->getExpectationsFromExpressions($expressions); - - if (! $expectationMockCollection->hasExpectationMocks()) { - return null; - } - - $expectationCollections = $this->groupExpectationCollectionsByVariableName($expectationMockCollection); - foreach ($expectationCollections as $expectationCollection) { - $this->replaceExpectationNodes($expectationCollection); - } - - return $node; - } - - private function buildNewExpectation(ExpectationMockCollection $expectationMockCollection): MethodCall - { - $this->fillMissingAtIndexes($expectationMockCollection); - - return $this->consecutiveAssertionFactory->createAssertionFromExpectationMockCollection( - $expectationMockCollection - ); - } - - private function fillMissingAtIndexes(ExpectationMockCollection $expectationMockCollection): void - { - $variable = $expectationMockCollection->getExpectationMocks()[0] - ->getExpectationVariable(); - - // OR - if ($expectationMockCollection->getLowestAtIndex() !== 0) { - for ($i = 0; $i < $expectationMockCollection->getLowestAtIndex(); ++$i) { - $expectationMockCollection->add(new ExpectationMock($variable, [], $i, null, [], null)); - } - } - - if ($expectationMockCollection->isMissingAtIndexBetweenHighestAndLowest()) { - $existingIndexes = array_column($expectationMockCollection->getExpectationMocks(), 'index'); - for ($i = 1; $i < $expectationMockCollection->getHighestAtIndex(); ++$i) { - if (! in_array($i, $existingIndexes, true)) { - $expectationMockCollection->add(new ExpectationMock($variable, [], $i, null, [], null)); - } - } - } - } - - private function replaceExpectationNodes(ExpectationMockCollection $expectationMockCollection): void - { - if ($this->shouldSkipReplacement($expectationMockCollection)) { - return; - } - - $endLines = array_map(static function (ExpectationMock $expectationMock): int { - $originalExpression = $expectationMock->getOriginalExpression(); - return $originalExpression instanceof Expression ? $originalExpression->getEndLine() : 0; - }, $expectationMockCollection->getExpectationMocks()); - $max = max($endLines); - - foreach ($expectationMockCollection->getExpectationMocks() as $expectationMock) { - $originalExpression = $expectationMock->getOriginalExpression(); - if (! $originalExpression instanceof Expression) { - continue; - } - - if ($max > $originalExpression->getEndLine()) { - $this->removeNode($originalExpression); - } else { - $originalExpression->expr = $this->buildNewExpectation($expectationMockCollection); - } - } - } - - private function shouldSkipReplacement(ExpectationMockCollection $expectationMockCollection): bool - { - if (! $expectationMockCollection->hasReturnValues()) { - return false; - } - - if (! $expectationMockCollection->isExpectedMethodAlwaysTheSame()) { - return true; - } - - if ($expectationMockCollection->hasMissingAtIndexes()) { - return true; - } - - return $expectationMockCollection->hasMissingReturnValues(); - } - - /** - * @return ExpectationMockCollection[] - */ - private function groupExpectationCollectionsByVariableName( - ExpectationMockCollection $expectationMockCollection - ): array { - $groupedByVariable = []; - foreach ($expectationMockCollection->getExpectationMocks() as $expectationMock) { - $variable = $expectationMock->getExpectationVariable(); - - // The $variable->name will be a string if the mock object is stored as a local variable (Expr\Variable) - // The $variable->name will be Identifier object when the mock object is stored as a class property (Expr\PropertyFetch). - if ($variable->name instanceof Identifier) { - $variable = $variable->name; - } - - if (! is_string($variable->name)) { - continue; - } - - if (! isset($groupedByVariable[$variable->name])) { - $groupedByVariable[$variable->name] = new ExpectationMockCollection(); - } - - $groupedByVariable[$variable->name]->add($expectationMock); - } - - return array_values($groupedByVariable); - } -} diff --git a/src/ValueObject/ExpectationMock.php b/src/ValueObject/ExpectationMock.php deleted file mode 100644 index 11e73140..00000000 --- a/src/ValueObject/ExpectationMock.php +++ /dev/null @@ -1,68 +0,0 @@ - $withArguments - */ - public function __construct( - private readonly Expr $expectationVariable, - private readonly array $methodArguments, - private readonly int $index, - private readonly ?Expr $expr, - private readonly array $withArguments, - private readonly ?Expression $originalExpression - ) { - } - - /** - * @return Variable|PropertyFetch - */ - public function getExpectationVariable(): Expr - { - return $this->expectationVariable; - } - - /** - * @return Arg[] - */ - public function getMethodArguments(): array - { - return $this->methodArguments; - } - - public function getIndex(): int - { - return $this->index; - } - - public function getReturn(): ?Expr - { - return $this->expr; - } - - /** - * @return array - */ - public function getWithArguments(): array - { - return $this->withArguments; - } - - public function getOriginalExpression(): ?Expression - { - return $this->originalExpression; - } -} diff --git a/src/ValueObject/ExpectationMockCollection.php b/src/ValueObject/ExpectationMockCollection.php deleted file mode 100644 index 964314d8..00000000 --- a/src/ValueObject/ExpectationMockCollection.php +++ /dev/null @@ -1,139 +0,0 @@ -expectationMocks; - } - - public function hasExpectationMocks(): bool - { - return $this->expectationMocks !== []; - } - - public function add(ExpectationMock $expectationMock): void - { - $this->expectationMocks[] = $expectationMock; - } - - public function getHighestAtIndex(): int - { - if (! $this->hasExpectationMocks()) { - return 0; - } - - $indexes = array_map( - static fn (ExpectationMock $expectationMock): int => $expectationMock->getIndex(), - $this->expectationMocks - ); - return (int) max($indexes); - } - - public function getLowestAtIndex(): int - { - if (! $this->hasExpectationMocks()) { - return 0; - } - - $indexes = array_map( - static fn (ExpectationMock $expectationMock): int => $expectationMock->getIndex(), - $this->expectationMocks - ); - - return (int) min($indexes); - } - - public function isMissingAtIndexBetweenHighestAndLowest(): bool - { - $highestAtIndex = $this->getHighestAtIndex(); - $lowestAtIndex = $this->getLowestAtIndex(); - return ($highestAtIndex - $lowestAtIndex + 1) !== count($this->expectationMocks); - } - - public function hasMissingAtIndexes(): bool - { - if ($this->getLowestAtIndex() !== 0) { - return true; - } - - return $this->isMissingAtIndexBetweenHighestAndLowest(); - } - - public function hasWithValues(): bool - { - foreach ($this->expectationMocks as $expectationMock) { - if (count($expectationMock->getWithArguments()) > 1) { - return true; - } - - if (count($expectationMock->getWithArguments()) !== 1) { - continue; - } - - if ($expectationMock->getWithArguments()[0] === null) { - continue; - } - - return true; - } - - return false; - } - - public function hasReturnValues(): bool - { - foreach ($this->expectationMocks as $expectationMock) { - if ($expectationMock->getReturn() !== null) { - return true; - } - } - - return false; - } - - public function hasMissingReturnValues(): bool - { - foreach ($this->expectationMocks as $expectationMock) { - if ($expectationMock->getReturn() === null) { - return true; - } - } - - return false; - } - - public function isExpectedMethodAlwaysTheSame(): bool - { - $previousMethod = ''; - foreach ($this->expectationMocks as $expectationMock) { - $methodArgument = $expectationMock->getMethodArguments()[0]; - - if ($methodArgument->value instanceof String_) { - if ($previousMethod === '') { - $previousMethod = $methodArgument->value->value; - } - - if ($previousMethod !== $methodArgument->value->value) { - return false; - } - } - } - - return true; - } -} diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/different_return_values.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/different_return_values.php.inc deleted file mode 100644 index d51590c7..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/different_return_values.php.inc +++ /dev/null @@ -1,66 +0,0 @@ -createMock(SomeObject::class); - $mock - ->expects($this->at(0)) - ->method('someMethod') - ->willReturn('1'); - $reference = '2'; - $mock - ->expects($this->at(1)) - ->method('someMethod') - ->willReturnReference($reference); - $mock - ->expects($this->at(2)) - ->method('someMethod') - ->willReturnMap(['foo' => 'bar']); - $mock - ->expects($this->at(3)) - ->method('someMethod') - ->willReturnArgument(1); - $mock - ->expects($this->at(4)) - ->method('someMethod') - ->willReturnCallback(static function () { - return null; - }); - $mock - ->expects($this->at(5)) - ->method('someMethod') - ->willReturnSelf(); - $mock - ->expects($this->at(6)) - ->method('someMethod') - ->willThrowException(new \Exception()); - } -} - -?> ------ -createMock(SomeObject::class); - $reference = '2'; - $mock->method('someMethod')->willReturnOnConsecutiveCalls('1', new \PHPUnit\Framework\MockObject\Stub\ReturnReference($reference), $this->returnValueMap(['foo' => 'bar']), $this->returnArgument(1), $this->returnCallback(static function () { - return null; - }), $this->returnSelf(), $this->throwException(new \Exception())); - } -} - -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/expects_non_at_is_skipped.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/expects_non_at_is_skipped.php.inc deleted file mode 100644 index 4b5995ce..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/expects_non_at_is_skipped.php.inc +++ /dev/null @@ -1,18 +0,0 @@ -createMock(SomeObject::class); - $mock - ->expects($this->exactly(1)) - ->method('someMethod'); - } -} - -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/handle_multiple_variables.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/handle_multiple_variables.php.inc deleted file mode 100644 index d89bc800..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/handle_multiple_variables.php.inc +++ /dev/null @@ -1,57 +0,0 @@ -createMock(SomeObject::class); - $mock - ->expects($this->at(0)) - ->with('0') - ->method('someMethod') - ->willReturn('1'); - $mock - ->expects($this->at(1)) - ->with('1') - ->method('someMethod') - ->willReturn('2'); - - $mock2 = $this->createMock(SomeObject::class); - $mock2 - ->expects($this->at(0)) - ->with('0') - ->method('someMethod') - ->willReturn('1'); - $mock2 - ->expects($this->at(1)) - ->with('1') - ->method('someMethod') - ->willReturn('2'); - } -} - -?> ------ -createMock(SomeObject::class); - $mock->method('someMethod')->withConsecutive(['0'], ['1'])->willReturnOnConsecutiveCalls('1', '2'); - - $mock2 = $this->createMock(SomeObject::class); - $mock2->method('someMethod')->withConsecutive(['0'], ['1'])->willReturnOnConsecutiveCalls('1', '2'); - } -} - -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/handle_property_fetch.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/handle_property_fetch.php.inc deleted file mode 100644 index df4528c4..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/handle_property_fetch.php.inc +++ /dev/null @@ -1,57 +0,0 @@ -mock = $this->createMock(SomeObject::class); - } - - public function test(): void - { - $this->mock - ->expects($this->at(0)) - ->method('someMethod') - ->willReturn(1); - - $this->mock - ->expects($this->at(1)) - ->method('someMethod') - ->willReturn(2); - } -} - -?> ------ -mock = $this->createMock(SomeObject::class); - } - - public function test(): void - { - $this->mock->method('someMethod')->willReturnOnConsecutiveCalls(1, 2); - } -} - -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/ignoring_with.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/ignoring_with.php.inc deleted file mode 100644 index a1fee793..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/ignoring_with.php.inc +++ /dev/null @@ -1,36 +0,0 @@ -expects($this->at(0)) - ->method('get') - ->with('signed') - ->will($this->returnValue($signed)); - } -} -?> ------ -method('get') - ->with('signed') - ->willReturnOnConsecutiveCalls($this->returnValue($signed)); - } -} -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/ignoring_with_complex.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/ignoring_with_complex.php.inc deleted file mode 100644 index f8f51171..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/ignoring_with_complex.php.inc +++ /dev/null @@ -1,35 +0,0 @@ -expects($this->at(0))->method('convert')->with( - Money::EUR(100), - new Currency('USD') - )->willReturn(Money::USD(120)); - } -} -?> ------ -method('convert')->with( - Money::EUR(100), - new Currency('USD') - )->willReturnOnConsecutiveCalls(Money::USD(120)); - } -} -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/missing_with_is_replaced_by_empty_array_if_no_return_expectation.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/missing_with_is_replaced_by_empty_array_if_no_return_expectation.php.inc deleted file mode 100644 index 44e230bb..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/missing_with_is_replaced_by_empty_array_if_no_return_expectation.php.inc +++ /dev/null @@ -1,40 +0,0 @@ -createMock(SomeObject::class); - $mock - ->expects($this->at(0)) - ->with('0') - ->method('someMethod'); - $mock - ->expects($this->at(2)) - ->with('2') - ->method('someMethod'); - } -} - -?> ------ -createMock(SomeObject::class); - $mock->method('someMethod')->withConsecutive(['0'], [], ['2']); - } -} - -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/missing_with_is_replaced_by_empty_array_if_no_return_expectation2.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/missing_with_is_replaced_by_empty_array_if_no_return_expectation2.php.inc deleted file mode 100644 index 1038e954..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/missing_with_is_replaced_by_empty_array_if_no_return_expectation2.php.inc +++ /dev/null @@ -1,40 +0,0 @@ -createMock(SomeObject::class); - $mock - ->expects($this->at(1)) - ->with('1') - ->method('someMethod'); - $mock - ->expects($this->at(2)) - ->with('2') - ->method('someMethod'); - } -} - -?> ------ -createMock(SomeObject::class); - $mock->method('someMethod')->withConsecutive([], ['1'], ['2']); - } -} - -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/mixed.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/mixed.php.inc deleted file mode 100644 index f844161e..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/mixed.php.inc +++ /dev/null @@ -1,42 +0,0 @@ -createMock(SomeObject::class); - $mock - ->expects($this->at(0)) - ->with(0) - ->method('someMethod') - ->willReturn('0'); - $mock - ->expects($this->at(1)) - ->with(1) - ->method('someMethod') - ->willReturn('1'); - } -} - -?> ------ -createMock(SomeObject::class); - $mock->method('someMethod')->withConsecutive([0], [1])->willReturnOnConsecutiveCalls('0', '1'); - } -} - -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/mixed2.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/mixed2.php.inc deleted file mode 100644 index 47dca420..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/mixed2.php.inc +++ /dev/null @@ -1,48 +0,0 @@ -createMock(SomeObject::class); - $mock - ->expects($this->exactly(2)) - ->method('someMethod'); - $mock - ->expects($this->at(0)) - ->with(0) - ->method('someMethod') - ->willReturn('0'); - $mock - ->expects($this->at(1)) - ->with(1) - ->method('someMethod') - ->willReturn('1'); - } -} - -?> ------ -createMock(SomeObject::class); - $mock - ->expects($this->exactly(2)) - ->method('someMethod'); - $mock->method('someMethod')->withConsecutive([0], [1])->willReturnOnConsecutiveCalls('0', '1'); - } -} - -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/mixing_arrays.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/mixing_arrays.php.inc deleted file mode 100644 index 2fccbfbc..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/mixing_arrays.php.inc +++ /dev/null @@ -1,33 +0,0 @@ -mocks[Myclass::class]->expects($this->at(0))->method('sasa')->willReturn([ - (new Myclass(new Myclass($winner, new Myclass(2), Myclass::getStandard())))->setUser1Result(Myclass::getByCode(Myclass::WIN)) - ]); - } -} -?> ------ -mocks[Myclass::class]->method('sasa')->willReturnOnConsecutiveCalls([ - (new Myclass(new Myclass($winner, new Myclass(2), Myclass::getStandard())))->setUser1Result(Myclass::getByCode(Myclass::WIN)) - ]); - } -} -?> \ No newline at end of file diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_different_method_expectations.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_different_method_expectations.php.inc deleted file mode 100644 index 80024bf3..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_different_method_expectations.php.inc +++ /dev/null @@ -1,41 +0,0 @@ -createMock(SomeObject::class); - $mock - ->expects($this->at(0)) - ->method('someMethod') - ->willReturn('1'); - - $mock - ->expects($this->at(1)) - ->with('1') - ->method('someOtherMethod'); - - $mock - ->expects($this->at(2)) - ->with('2') - ->method('someMethod') - ->willReturn('3'); - } -} - -?> - diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_dynamic_variable.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_dynamic_variable.php.inc deleted file mode 100644 index 289b2df2..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_dynamic_variable.php.inc +++ /dev/null @@ -1,28 +0,0 @@ -createMock(SomeObject::class); - - $mock - ->expects($this->at($one)) - ->with(0) - ->method('someMethod') - ->willReturn('0'); - $mock - ->expects($this->at($two)) - ->with(1) - ->method('someMethod') - ->willReturn('1'); - } -} diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_missing_expects.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_missing_expects.php.inc deleted file mode 100644 index 5e018b16..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_missing_expects.php.inc +++ /dev/null @@ -1,19 +0,0 @@ -createMock(SomeObject::class); - - $mock - ->method('someMethod') - ->willReturn(1); - } -} - -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_missing_return_expectation.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_missing_return_expectation.php.inc deleted file mode 100644 index 27163ee8..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_missing_return_expectation.php.inc +++ /dev/null @@ -1,28 +0,0 @@ -createMock(SomeObject::class); - $mock - ->expects($this->at(0)) - ->method('someMethod') - ->willReturn('1'); - - $mock - ->expects($this->at(1)) - ->method('someMethod'); - - $mock - ->expects($this->at(2)) - ->method('someMethod') - ->willReturn('3'); - } -} - -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_missing_with_if_return_expectations_exist.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_missing_with_if_return_expectations_exist.php.inc deleted file mode 100644 index cc4cfbf6..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_missing_with_if_return_expectations_exist.php.inc +++ /dev/null @@ -1,26 +0,0 @@ -createMock(SomeObject::class); - $mock - ->expects($this->at(0)) - ->with('0') - ->method('someMethod') - ->willReturn('1'); - - $mock - ->expects($this->at(2)) - ->with('2') - ->method('someMethod') - ->willReturn('3'); - } -} - -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_other_class_types.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_other_class_types.php.inc deleted file mode 100644 index 89595763..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_other_class_types.php.inc +++ /dev/null @@ -1,14 +0,0 @@ -createMock(TestCase::class); - $entity->expects($expecataiton)->method('getId'); - } -} diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_static_call_self.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_static_call_self.php.inc deleted file mode 100644 index 92298c4f..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/skip_static_call_self.php.inc +++ /dev/null @@ -1,14 +0,0 @@ -createMock(TestCase::class); - $entity->expects(self::once())->method('getId')->willReturn(1); - } -} diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/will_callbacks_are_kept.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/will_callbacks_are_kept.php.inc deleted file mode 100644 index 4f7baccd..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/will_callbacks_are_kept.php.inc +++ /dev/null @@ -1,40 +0,0 @@ -createMock(SomeObject::class); - $mock - ->expects($this->at(0)) - ->method('someMethod') - ->will($this->throwException(new \Exception())); - $mock - ->expects($this->at(1)) - ->method('someMethod') - ->will($this->returnValue('1')); - } -} - -?> ------ -createMock(SomeObject::class); - $mock->method('someMethod')->willReturnOnConsecutiveCalls($this->throwException(new \Exception()), $this->returnValue('1')); - } -} - -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/will_return_only.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/will_return_only.php.inc deleted file mode 100644 index 13b4723e..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/will_return_only.php.inc +++ /dev/null @@ -1,48 +0,0 @@ -createMock(SomeObject::class); - $mock - ->expects($this->at(0)) - ->method('someMethod') - ->willReturn('0'); - $mock - ->expects($this->at(1)) - ->method('someMethod') - ->willReturn('1'); - $mock - ->expects($this->at(2)) - ->method('someMethod') - ->willReturn('2'); - $mock - ->expects($this->at(3)) - ->method('someMethod') - ->willReturn(null); - } -} - -?> ------ -createMock(SomeObject::class); - $mock->method('someMethod')->willReturnOnConsecutiveCalls('0', '1', '2', null); - } -} - -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/with_multiple_arguments.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/with_multiple_arguments.php.inc deleted file mode 100644 index 7c5bc60b..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/with_multiple_arguments.php.inc +++ /dev/null @@ -1,36 +0,0 @@ -createMock(SomeObject::class); - $mock - ->expects($this->at(0)) - ->with('0', '1') - ->method('someMethod'); - } -} - -?> ------ -createMock(SomeObject::class); - $mock->method('someMethod')->withConsecutive(['0', '1']); - } -} - -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/with_only.php.inc b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/with_only.php.inc deleted file mode 100644 index 8c588a3f..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Fixture/with_only.php.inc +++ /dev/null @@ -1,48 +0,0 @@ -createMock(SomeObject::class); - $mock - ->expects($this->at(0)) - ->with('0') - ->method('someMethod'); - $mock - ->expects($this->at(1)) - ->with('1') - ->method('someMethod'); - $mock - ->expects($this->at(2)) - ->with('2') - ->method('someMethod'); - $mock - ->expects($this->at(3)) - ->with(null) - ->method('someMethod'); - } -} - -?> ------ -createMock(SomeObject::class); - $mock->method('someMethod')->withConsecutive(['0'], ['1'], ['2'], [null]); - } -} - -?> diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/MigrateAtToConsecutiveExpectationsRectorTest.php b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/MigrateAtToConsecutiveExpectationsRectorTest.php deleted file mode 100644 index 50bcf250..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/MigrateAtToConsecutiveExpectationsRectorTest.php +++ /dev/null @@ -1,33 +0,0 @@ -doTestFileInfo($fileInfo); - } - - /** - * @return Iterator - */ - public function provideData(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Source/SomeObject.php b/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Source/SomeObject.php deleted file mode 100644 index cf6bbfc4..00000000 --- a/tests/Rector/ClassMethod/MigrateAtToConsecutiveExpectationsRector/Source/SomeObject.php +++ /dev/null @@ -1,17 +0,0 @@ -import(__DIR__ . '/../../../../../config/config.php'); - - $services = $containerConfigurator->services(); - - $services->set(MigrateAtToConsecutiveExpectationsRector::class); -};