From 84a9478da19abb98f61fda4d16faf1f0600547d5 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 9 Dec 2025 21:43:13 +0100 Subject: [PATCH 1/2] Use Stubs instead of Mocks No expectations were configured for the mock object for PHPStan\Analyser\Scope. You should refactor your test code and use a test stub instead. --- .../Command/AnalyseApplicationIntegrationTest.php | 4 ++-- tests/PHPStan/Parser/CachedParserTest.php | 15 ++++++++------- ...tationsMethodsClassReflectionExtensionTest.php | 2 +- ...ionsPropertiesClassReflectionExtensionTest.php | 2 +- .../Annotations/DeprecatedAnnotationsTest.php | 2 +- .../Annotations/FinalAnnotationsTest.php | 2 +- .../Annotations/InternalAnnotationsTest.php | 2 +- .../Annotations/ThrowsAnnotationsTest.php | 2 +- .../PHPStan/Reflection/FunctionReflectionTest.php | 4 ++-- .../Type/IntersectionTypeMethodReflectionTest.php | 2 +- .../Type/UnionTypeMethodReflectionTest.php | 2 +- tests/PHPStan/Rules/Api/ApiRuleHelperTest.php | 2 +- 12 files changed, 21 insertions(+), 20 deletions(-) diff --git a/tests/PHPStan/Command/AnalyseApplicationIntegrationTest.php b/tests/PHPStan/Command/AnalyseApplicationIntegrationTest.php index 05962e07a6..cee47e2038 100644 --- a/tests/PHPStan/Command/AnalyseApplicationIntegrationTest.php +++ b/tests/PHPStan/Command/AnalyseApplicationIntegrationTest.php @@ -62,7 +62,7 @@ private function runPath(string $path, int $expectedStatusCode): string $symfonyOutput = new SymfonyOutput( $output, - new \PHPStan\Command\Symfony\SymfonyStyle(new SymfonyStyle($this->createMock(InputInterface::class), $output)), + new \PHPStan\Command\Symfony\SymfonyStyle(new SymfonyStyle($this->createStub(InputInterface::class), $output)), ); $relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), __DIR__, [], DIRECTORY_SEPARATOR); @@ -88,7 +88,7 @@ private function runPath(string $path, int $expectedStatusCode): string null, null, null, - $this->createMock(InputInterface::class), + $this->createStub(InputInterface::class), ); $statusCode = $errorFormatter->formatErrors($analysisResult, $symfonyOutput); diff --git a/tests/PHPStan/Parser/CachedParserTest.php b/tests/PHPStan/Parser/CachedParserTest.php index a0df7a1b2a..5b898809b9 100644 --- a/tests/PHPStan/Parser/CachedParserTest.php +++ b/tests/PHPStan/Parser/CachedParserTest.php @@ -10,6 +10,7 @@ use PHPStan\Testing\PHPStanTestCase; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\MockObject\Stub; class CachedParserTest extends PHPStanTestCase { @@ -21,7 +22,7 @@ public function testParseFileClearCache( ): void { $parser = new CachedParser( - $this->getParserMock(), + $this->getParserStub(), $cachedNodesByStringCountMax, ); @@ -62,19 +63,19 @@ public static function dataParseFileClearCache(): Generator ]; } - private function getParserMock(): Parser&MockObject + private function getParserStub(): Parser&Stub { - $mock = $this->createMock(Parser::class); + $mock = $this->createStub(Parser::class); - $mock->method('parseFile')->willReturn([$this->getPhpParserNodeMock()]); - $mock->method('parseString')->willReturn([$this->getPhpParserNodeMock()]); + $mock->method('parseFile')->willReturn([$this->getPhpParserNodeStub()]); + $mock->method('parseString')->willReturn([$this->getPhpParserNodeStub()]); return $mock; } - private function getPhpParserNodeMock(): Node&MockObject + private function getPhpParserNodeStub(): Node&Stub { - return $this->createMock(Node::class); + return $this->createStub(Node::class); } public function testParseTheSameFileWithDifferentMethod(): void diff --git a/tests/PHPStan/Reflection/Annotations/AnnotationsMethodsClassReflectionExtensionTest.php b/tests/PHPStan/Reflection/Annotations/AnnotationsMethodsClassReflectionExtensionTest.php index 2be55e8284..9aa45aba7a 100644 --- a/tests/PHPStan/Reflection/Annotations/AnnotationsMethodsClassReflectionExtensionTest.php +++ b/tests/PHPStan/Reflection/Annotations/AnnotationsMethodsClassReflectionExtensionTest.php @@ -966,7 +966,7 @@ public function testMethods(string $className, array $methods): void { $reflectionProvider = self::createReflectionProvider(); $class = $reflectionProvider->getClass($className); - $scope = $this->createMock(Scope::class); + $scope = $this->createStub(Scope::class); $scope->method('isInClass')->willReturn(true); $scope->method('getClassReflection')->willReturn($class); $scope->method('canCallMethod')->willReturn(true); diff --git a/tests/PHPStan/Reflection/Annotations/AnnotationsPropertiesClassReflectionExtensionTest.php b/tests/PHPStan/Reflection/Annotations/AnnotationsPropertiesClassReflectionExtensionTest.php index f654fdbb51..8ac7e4b5cc 100644 --- a/tests/PHPStan/Reflection/Annotations/AnnotationsPropertiesClassReflectionExtensionTest.php +++ b/tests/PHPStan/Reflection/Annotations/AnnotationsPropertiesClassReflectionExtensionTest.php @@ -280,7 +280,7 @@ public function testProperties(string $className, array $properties): void { $reflectionProvider = self::createReflectionProvider(); $class = $reflectionProvider->getClass($className); - $scope = $this->createMock(Scope::class); + $scope = $this->createStub(Scope::class); $scope->method('isInClass')->willReturn(true); $scope->method('getClassReflection')->willReturn($class); $scope->method('canAccessProperty')->willReturn(true); diff --git a/tests/PHPStan/Reflection/Annotations/DeprecatedAnnotationsTest.php b/tests/PHPStan/Reflection/Annotations/DeprecatedAnnotationsTest.php index 936d56b4aa..467b2e79b9 100644 --- a/tests/PHPStan/Reflection/Annotations/DeprecatedAnnotationsTest.php +++ b/tests/PHPStan/Reflection/Annotations/DeprecatedAnnotationsTest.php @@ -101,7 +101,7 @@ public function testDeprecatedAnnotations(bool $deprecated, string $className, ? { $reflectionProvider = self::createReflectionProvider(); $class = $reflectionProvider->getClass($className); - $scope = $this->createMock(Scope::class); + $scope = $this->createStub(Scope::class); $scope->method('isInClass')->willReturn(true); $scope->method('getClassReflection')->willReturn($class); $scope->method('canAccessProperty')->willReturn(true); diff --git a/tests/PHPStan/Reflection/Annotations/FinalAnnotationsTest.php b/tests/PHPStan/Reflection/Annotations/FinalAnnotationsTest.php index b24c82fba9..ba4901ee10 100644 --- a/tests/PHPStan/Reflection/Annotations/FinalAnnotationsTest.php +++ b/tests/PHPStan/Reflection/Annotations/FinalAnnotationsTest.php @@ -45,7 +45,7 @@ public function testFinalAnnotations(bool $final, string $className, array $fina { $reflectionProvider = self::createReflectionProvider(); $class = $reflectionProvider->getClass($className); - $scope = $this->createMock(Scope::class); + $scope = $this->createStub(Scope::class); $scope->method('isInClass')->willReturn(true); $scope->method('getClassReflection')->willReturn($class); $scope->method('canAccessProperty')->willReturn(true); diff --git a/tests/PHPStan/Reflection/Annotations/InternalAnnotationsTest.php b/tests/PHPStan/Reflection/Annotations/InternalAnnotationsTest.php index 9e4e36901f..1817e21d1e 100644 --- a/tests/PHPStan/Reflection/Annotations/InternalAnnotationsTest.php +++ b/tests/PHPStan/Reflection/Annotations/InternalAnnotationsTest.php @@ -126,7 +126,7 @@ public function testInternalAnnotations(bool $internal, string $className, array { $reflectionProvider = self::createReflectionProvider(); $class = $reflectionProvider->getClass($className); - $scope = $this->createMock(Scope::class); + $scope = $this->createStub(Scope::class); $scope->method('isInClass')->willReturn(true); $scope->method('getClassReflection')->willReturn($class); $scope->method('canAccessProperty')->willReturn(true); diff --git a/tests/PHPStan/Reflection/Annotations/ThrowsAnnotationsTest.php b/tests/PHPStan/Reflection/Annotations/ThrowsAnnotationsTest.php index b58322ca01..e52ee0f5c6 100644 --- a/tests/PHPStan/Reflection/Annotations/ThrowsAnnotationsTest.php +++ b/tests/PHPStan/Reflection/Annotations/ThrowsAnnotationsTest.php @@ -76,7 +76,7 @@ public function testThrowsAnnotations(string $className, array $throwsAnnotation { $reflectionProvider = self::createReflectionProvider(); $class = $reflectionProvider->getClass($className); - $scope = $this->createMock(Scope::class); + $scope = $this->createStub(Scope::class); foreach ($throwsAnnotations as $methodName => $type) { $methodAnnotation = $class->getMethod($methodName, $scope); diff --git a/tests/PHPStan/Reflection/FunctionReflectionTest.php b/tests/PHPStan/Reflection/FunctionReflectionTest.php index 12dd5ee438..e83f33c7a8 100644 --- a/tests/PHPStan/Reflection/FunctionReflectionTest.php +++ b/tests/PHPStan/Reflection/FunctionReflectionTest.php @@ -119,7 +119,7 @@ public function testMethodHasPhpdoc(string $className, string $methodName, ?stri { $reflectionProvider = self::createReflectionProvider(); $class = $reflectionProvider->getClass($className); - $scope = $this->createMock(Scope::class); + $scope = $this->createStub(Scope::class); $scope->method('isInClass')->willReturn(true); $scope->method('getClassReflection')->willReturn($class); $scope->method('canAccessProperty')->willReturn(true); @@ -180,7 +180,7 @@ public function testMethodReturnsByReference(string $className, string $methodNa { $reflectionProvider = self::createReflectionProvider(); $class = $reflectionProvider->getClass($className); - $scope = $this->createMock(Scope::class); + $scope = $this->createStub(Scope::class); $scope->method('isInClass')->willReturn(true); $scope->method('getClassReflection')->willReturn($class); $scope->method('canAccessProperty')->willReturn(true); diff --git a/tests/PHPStan/Reflection/Type/IntersectionTypeMethodReflectionTest.php b/tests/PHPStan/Reflection/Type/IntersectionTypeMethodReflectionTest.php index 2d7d3ca357..d3d413f29c 100644 --- a/tests/PHPStan/Reflection/Type/IntersectionTypeMethodReflectionTest.php +++ b/tests/PHPStan/Reflection/Type/IntersectionTypeMethodReflectionTest.php @@ -38,7 +38,7 @@ public function testMultipleDeprecationsAreJoined(): void private function createDeprecatedMethod(TrinaryLogic $deprecated, ?string $deprecationText): ExtendedMethodReflection { - $method = $this->createMock(ExtendedMethodReflection::class); + $method = $this->createStub(ExtendedMethodReflection::class); $method->method('isDeprecated')->willReturn($deprecated); $method->method('getDeprecatedDescription')->willReturn($deprecationText); return $method; diff --git a/tests/PHPStan/Reflection/Type/UnionTypeMethodReflectionTest.php b/tests/PHPStan/Reflection/Type/UnionTypeMethodReflectionTest.php index b41d8d9636..2136c1d3f9 100644 --- a/tests/PHPStan/Reflection/Type/UnionTypeMethodReflectionTest.php +++ b/tests/PHPStan/Reflection/Type/UnionTypeMethodReflectionTest.php @@ -38,7 +38,7 @@ public function testMultipleDeprecationsAreJoined(): void private function createDeprecatedMethod(TrinaryLogic $deprecated, ?string $deprecationText): ExtendedMethodReflection { - $method = $this->createMock(ExtendedMethodReflection::class); + $method = $this->createStub(ExtendedMethodReflection::class); $method->method('isDeprecated')->willReturn($deprecated); $method->method('getDeprecatedDescription')->willReturn($deprecationText); return $method; diff --git a/tests/PHPStan/Rules/Api/ApiRuleHelperTest.php b/tests/PHPStan/Rules/Api/ApiRuleHelperTest.php index 81945b88e6..6edb9e9cd8 100644 --- a/tests/PHPStan/Rules/Api/ApiRuleHelperTest.php +++ b/tests/PHPStan/Rules/Api/ApiRuleHelperTest.php @@ -144,7 +144,7 @@ public function testIsPhpStanCode( ): void { $rule = new ApiRuleHelper(); - $scope = $this->createMock(Scope::class); + $scope = $this->createStub(Scope::class); $scope->method('getNamespace')->willReturn($scopeNamespace); $scope->method('getFile')->willReturn($scopeFile); $this->assertSame($expected, $rule->isPhpStanCode($scope, $nameToCheck, $declaringFileNameToCheck)); From 26ec70b80ec418ee8e908da54b0a46a82295a2c3 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 9 Dec 2025 21:49:46 +0100 Subject: [PATCH 2/2] cs --- tests/PHPStan/Parser/CachedParserTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/PHPStan/Parser/CachedParserTest.php b/tests/PHPStan/Parser/CachedParserTest.php index 5b898809b9..29ca21ee3a 100644 --- a/tests/PHPStan/Parser/CachedParserTest.php +++ b/tests/PHPStan/Parser/CachedParserTest.php @@ -9,7 +9,6 @@ use PHPStan\File\FileReader; use PHPStan\Testing\PHPStanTestCase; use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\Stub; class CachedParserTest extends PHPStanTestCase