From 9f983d938e33879fbe06e0d30e6344e7d5131c9c Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 4 Feb 2026 13:00:11 +0100 Subject: [PATCH] add to setUp with only variables --- ..._to_setup_when_no_expects_variable.php.inc | 50 +++++++++++++++++++ ...ectsWithoutExpectationsAttributeRector.php | 10 ++-- 2 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 rules-tests/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector/Fixture/add_to_setup_when_no_expects_variable.php.inc diff --git a/rules-tests/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector/Fixture/add_to_setup_when_no_expects_variable.php.inc b/rules-tests/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector/Fixture/add_to_setup_when_no_expects_variable.php.inc new file mode 100644 index 00000000..0ce59a7c --- /dev/null +++ b/rules-tests/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector/Fixture/add_to_setup_when_no_expects_variable.php.inc @@ -0,0 +1,50 @@ +createMock(\stdClass::class); + $someMock->method('some')->willReturn(true); + } + + public function testOne() + { + } + + public function testTwo() + { + } +} + +?> +----- +createMock(\stdClass::class); + $someMock->method('some')->willReturn(true); + } + + public function testOne() + { + } + + public function testTwo() + { + } +} + +?> diff --git a/rules/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector.php b/rules/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector.php index bf93ebde..1dce25bb 100644 --- a/rules/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector.php +++ b/rules/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector.php @@ -54,13 +54,9 @@ public function refactor(Node $node): ?Class_ return null; } + // even for 0 mocked properties, the variable in setUp() can be mocked $mockObjectPropertyNames = $this->matchMockObjectPropertyNames($node); - // there are no mock object properties - if ($mockObjectPropertyNames === []) { - return null; - } - $missedTestMethodsByMockPropertyName = []; $usingTestMethodsByMockPropertyName = []; $testMethodCount = 0; @@ -89,7 +85,7 @@ public function refactor(Node $node): ?Class_ // or find a ->method() calls on a setUp() mocked property $hasAnyMethodInSetup = $this->isMissingExpectsOnMockObjectMethodCallInSetUp($node); - if ($hasAnyMethodInSetup && $testMethodCount > 1) { + if ($hasAnyMethodInSetup) { $node->attrGroups[] = new AttributeGroup([ new Attribute(new FullyQualified(PHPUnitAttribute::ALLOW_MOCK_OBJECTS_WITHOUT_EXPECTATIONS)), ]); @@ -284,6 +280,7 @@ private function isAtLeastOneMockPropertyMockedOnce(array $usingTestMethodsByMoc private function isMissingExpectsOnMockObjectMethodCallInSetUp(Class_ $class): bool { $setupClassMethod = $class->getMethod(MethodName::SET_UP); + if (! $setupClassMethod instanceof ClassMethod) { return false; } @@ -293,6 +290,7 @@ private function isMissingExpectsOnMockObjectMethodCallInSetUp(Class_ $class): b (array) $setupClassMethod->stmts, MethodCall::class ); + foreach ($methodCalls as $methodCall) { if (! $this->isName($methodCall->name, 'method')) { continue;