diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 6e540218..9968c69c 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -25,7 +25,7 @@ parameters: path: src/Reflection/EnumReflectionProperty.php - - message: '#^PHPDoc tag @var with type Doctrine\\Persistence\\Mapping\\ClassMetadata\ is not subtype of native type PHPUnit\\Framework\\MockObject\\MockObject\.$#' + message: '#^PHPDoc tag @var with type Doctrine\\Persistence\\Mapping\\ClassMetadata\ is not subtype of native type PHPUnit\\Framework\\MockObject\\Stub\.$#' identifier: varTag.nativeType count: 1 path: tests/Mapping/ClassMetadataFactoryTest.php diff --git a/tests/Event/PreUpdateEventArgsTest.php b/tests/Event/PreUpdateEventArgsTest.php index fef09458..508af502 100644 --- a/tests/Event/PreUpdateEventArgsTest.php +++ b/tests/Event/PreUpdateEventArgsTest.php @@ -70,7 +70,7 @@ private function createTestPreUpdateEventArgs(): PreUpdateEventArgs { $entity = new TestObject(); - $objectManager = $this->createMock(ObjectManager::class); + $objectManager = self::createStub(ObjectManager::class); $entityChangeset = [ 'name' => ['old', 'new'], diff --git a/tests/ManagerRegistryTest.php b/tests/ManagerRegistryTest.php index a32dcc4a..c09a5733 100644 --- a/tests/ManagerRegistryTest.php +++ b/tests/ManagerRegistryTest.php @@ -74,7 +74,7 @@ public function testResetManager(): void public function testGetRepository(): void { - $repository = $this->createMock(ObjectRepository::class); + $repository = self::createStub(ObjectRepository::class); $defaultManager = $this->mr->getManager(); assert($defaultManager instanceof MockObject); @@ -99,7 +99,7 @@ public function testGetRepositoryWithSpecificManagerName(): void $this->getManagerFactory(), ); - $repository = $this->createMock(ObjectRepository::class); + $repository = self::createStub(ObjectRepository::class); $defaultManager = $this->mr->getManager(); assert($defaultManager instanceof MockObject); @@ -130,7 +130,7 @@ public function testGetRepositoryWithManagerDetection(): void $this->getManagerFactory(), ); - $repository = $this->createMock(ObjectRepository::class); + $repository = self::createStub(ObjectRepository::class); $defaultManager = $this->mr->getManager(); assert($defaultManager instanceof MockObject); @@ -153,9 +153,10 @@ private function getManagerFactory(): Closure { return function (string $name) { $mock = $this->createMock(ObjectManager::class); + $mock->expects($this->never())->method('clear'); - $driver = $this->createMock(MappingDriver::class); - $metadata = $this->createMock(ClassMetadata::class); + $driver = self::createStub(MappingDriver::class); + $metadata = self::createStub(ClassMetadata::class); $metadata ->method('getName') diff --git a/tests/Mapping/AbstractClassMetadataFactoryTest.php b/tests/Mapping/AbstractClassMetadataFactoryTest.php index a76e5219..d426c040 100644 --- a/tests/Mapping/AbstractClassMetadataFactoryTest.php +++ b/tests/Mapping/AbstractClassMetadataFactoryTest.php @@ -28,7 +28,7 @@ public function testItSkipsTransientClasses(): void $cmf = $this->createTestFactory($driver); $metadataCallCount = 0; - $cmf->newClassMetadataInstanceCallback = function ($className) use (&$metadataCallCount) { + $cmf->newClassMetadataInstanceCallback = static function ($className) use (&$metadataCallCount) { $metadataCallCount++; if ($metadataCallCount === 1) { self::assertEquals(SomeGrandParentEntity::class, $className); @@ -36,7 +36,7 @@ public function testItSkipsTransientClasses(): void self::assertEquals(SomeEntity::class, $className); } - return $this->createMock(ClassMetadata::class); + return self::createStub(ClassMetadata::class); }; $driverCallCount = 0; @@ -70,7 +70,7 @@ public function testItThrowsWhenAttemptingToGetMetadataForAnonymousClass(): void public function testAnonymousClassIsNotMistakenForShortAlias(): void { - $driver = $this->createMock(MappingDriver::class); + $driver = self::createStub(MappingDriver::class); $driver->method('isTransient')->willReturn(false); $cmf = $this->createTestFactory($driver); @@ -96,7 +96,7 @@ public function testItThrowsWhenAttemptingToCheckTransientForShortAlias(): void public function testItGetsTheSameMetadataForBackslashedClassName(): void { - $driver = $this->createMock(MappingDriver::class); + $driver = self::createStub(MappingDriver::class); $cmf = $this->createTestFactory($driver); $metadata = self::createStub(ClassMetadata::class); @@ -134,8 +134,8 @@ class TestAbstractClassMetadataFactory extends AbstractClassMetadataFactory /** @param ClassMetadata|null $defaultMetadata */ public function __construct( - private MappingDriver|null $driver = null, - private ClassMetadata|null $defaultMetadata = null, + private readonly MappingDriver|null $driver = null, + private readonly ClassMetadata|null $defaultMetadata = null, ) { } diff --git a/tests/Mapping/ClassMetadataFactoryTest.php b/tests/Mapping/ClassMetadataFactoryTest.php index 0e57fe55..34be5082 100644 --- a/tests/Mapping/ClassMetadataFactoryTest.php +++ b/tests/Mapping/ClassMetadataFactoryTest.php @@ -26,10 +26,10 @@ class ClassMetadataFactoryTest extends TestCase protected function setUp(): void { - $driver = $this->createMock(MappingDriver::class); + $driver = self::createStub(MappingDriver::class); /** @phpstan-var ClassMetadata */ - $metadata = $this->createMock(ClassMetadata::class); + $metadata = self::createStub(ClassMetadata::class); $this->cmf = new TestClassMetadataFactory($driver, $metadata); } @@ -65,7 +65,7 @@ public function testGetParentMetadata(): void public function testGetCachedMetadata(): void { - $metadata = $this->createMock(ClassMetadata::class); + $metadata = self::createStub(ClassMetadata::class); $cache = new ArrayAdapter(); $item = $cache->getItem($this->cmf->getCacheKey(ChildEntity::class)); $item->set($metadata); @@ -90,7 +90,7 @@ public function testCacheGetMetadataFor(): void public function testWillFallbackOnNotLoadedMetadata(): void { - $classMetadata = $this->createMock(ClassMetadata::class); + $classMetadata = self::createStub(ClassMetadata::class); $this->cmf->fallbackCallback = static fn () => $classMetadata; diff --git a/tests/Mapping/DriverChainTest.php b/tests/Mapping/DriverChainTest.php index 66ce22f2..141f6cea 100644 --- a/tests/Mapping/DriverChainTest.php +++ b/tests/Mapping/DriverChainTest.php @@ -22,7 +22,7 @@ class DriverChainTest extends TestCase public function testDelegateToMatchingNamespaceDriver(string $namespace1, string $namespace2): void { $className = DriverChainEntity::class; - $classMetadata = $this->createMock(ClassMetadata::class); + $classMetadata = self::createStub(ClassMetadata::class); $chain = new MappingDriverChain(); @@ -52,7 +52,7 @@ public function testDelegateToMatchingNamespaceDriver(string $namespace1, string public function testLoadMetadataShouldThrowMappingExceptionWhenNoDelegatorWasFound(): void { $className = DriverChainEntity::class; - $classMetadata = $this->createMock(ClassMetadata::class); + $classMetadata = self::createStub(ClassMetadata::class); $chain = new MappingDriverChain(); @@ -87,7 +87,7 @@ public function testGatherAllClassNames(): void #[Group('DDC-706')] public function testIsTransient(): void { - $driver1 = $this->createMock(MappingDriver::class); + $driver1 = self::createStub(MappingDriver::class); $chain = new MappingDriverChain(); $chain->addDriver($driver1, 'Doctrine\Tests\Models\CMS'); diff --git a/tests/Mapping/FileDriverTest.php b/tests/Mapping/FileDriverTest.php index a9955c48..9be65452 100644 --- a/tests/Mapping/FileDriverTest.php +++ b/tests/Mapping/FileDriverTest.php @@ -12,6 +12,7 @@ use Doctrine\Tests\Persistence\Mapping\Fixtures\NotLoadedClass; use Doctrine\Tests\Persistence\Mapping\Fixtures\TestClassMetadata; use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\MockObject\Stub; use PHPUnit\Framework\TestCase; use stdClass; @@ -43,7 +44,7 @@ public function testGetElementFromGlobalFile(): void public function testGetElementFromFile(): void { - $locator = $this->newLocator(); + $locator = $this->newLocator(true); $locator->expects(self::once()) ->method('findMappingFile') ->with(self::equalTo(stdClass::class)) @@ -56,7 +57,7 @@ public function testGetElementFromFile(): void public function testGetElementUpdatesClassCache(): void { - $locator = $this->newLocator(); + $locator = $this->newLocator(true); // findMappingFile should only be called once $locator->expects(self::once()) @@ -118,7 +119,7 @@ public function testGetAllClassNamesBothSources(): void public function testGetAllClassNamesBothSourcesNoDupes(): void { - $locator = $this->newLocator(); + $locator = $this->newLocator(true); $locator->expects(self::once()) ->method('getAllClassNames') ->with(self::equalTo('global')) @@ -138,7 +139,7 @@ public function testGetAllClassNamesBothSourcesNoDupes(): void public function testIsNotTransient(): void { - $locator = $this->newLocator(); + $locator = $this->newLocator(true); $locator->expects(self::once()) ->method('fileExists') ->with(self::equalTo(stdClass::class)) @@ -154,7 +155,7 @@ public function testIsNotTransient(): void public function testIsTransient(): void { - $locator = $this->newLocator(); + $locator = $this->newLocator(true); $locator->expects(self::once()) ->method('fileExists') ->with(self::equalTo(NotLoadedClass::class)) @@ -172,10 +173,10 @@ public function testNonLocatorFallback(): void self::assertFalse($driver->isTransient(stdClass::class)); } - /** @return FileLocator&MockObject */ - private function newLocator(): MockObject + /** @return ($mock is true ? (FileLocator&MockObject) : (FileLocator&Stub)) */ + private function newLocator(bool $mock = false): FileLocator { - $locator = $this->createMock(FileLocator::class); + $locator = $mock ? $this->createMock(FileLocator::class) : self::createStub(FileLocator::class); $locator->method('getFileExtension')->willReturn('.yml'); $locator->method('getPaths')->willReturn([__DIR__ . '/_files']); @@ -187,9 +188,9 @@ private function createTestFileDriver(string|array|FileLocator $locator, string| { $driver = new TestFileDriver($locator, $fileExtension); - $driver->stdClass = $this->createMock(ClassMetadata::class); - $driver->stdGlobal = $this->createMock(ClassMetadata::class); - $driver->stdGlobal2 = $this->createMock(ClassMetadata::class); + $driver->stdClass = self::createStub(ClassMetadata::class); + $driver->stdGlobal = self::createStub(ClassMetadata::class); + $driver->stdGlobal2 = self::createStub(ClassMetadata::class); return $driver; } diff --git a/tests/Mapping/RuntimeReflectionServiceTest.php b/tests/Mapping/RuntimeReflectionServiceTest.php index c6e54b7f..e8a7f3c2 100644 --- a/tests/Mapping/RuntimeReflectionServiceTest.php +++ b/tests/Mapping/RuntimeReflectionServiceTest.php @@ -21,9 +21,11 @@ class RuntimeReflectionServiceTest extends TestCase public mixed $unusedPublicProperty; - private string $typedNoDefaultProperty; + /** @phpstan-ignore property.uninitializedReadonly */ + private readonly string $typedNoDefaultProperty; private string $typedDefaultProperty = ''; - private string $nonTypedNoDefaultProperty; // phpcs:ignore SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedProperty + /** @phpstan-ignore property.uninitializedReadonly */ + private readonly string $nonTypedNoDefaultProperty; // phpcs:ignore SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedProperty private string $nonTypedDefaultProperty = ''; // phpcs:ignore SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedProperty public string $typedNoDefaultPublicProperty; diff --git a/tests/ObjectManagerDecoratorTest.php b/tests/ObjectManagerDecoratorTest.php index ba13da0d..5477b1ee 100644 --- a/tests/ObjectManagerDecoratorTest.php +++ b/tests/ObjectManagerDecoratorTest.php @@ -98,7 +98,7 @@ public function testFlush(): void public function testGetRepository(): void { - $repository = $this->createMock(ObjectRepository::class); + $repository = self::createStub(ObjectRepository::class); $this->wrapped->expects(self::once()) ->method('getRepository') @@ -110,7 +110,7 @@ public function testGetRepository(): void public function testGetClassMetadata(): void { - $classMetadata = $this->createMock(ClassMetadata::class); + $classMetadata = self::createStub(ClassMetadata::class); $this->wrapped->expects(self::once()) ->method('getClassMetadata') @@ -122,7 +122,7 @@ public function testGetClassMetadata(): void public function testGetClassMetadataFactory(): void { - $classMetadataFactory = $this->createMock(ClassMetadataFactory::class); + $classMetadataFactory = self::createStub(ClassMetadataFactory::class); $this->wrapped->expects(self::once()) ->method('getMetadataFactory') diff --git a/tests/Reflection/TypedNoDefaultReflectionPropertyTest.php b/tests/Reflection/TypedNoDefaultReflectionPropertyTest.php index 3b3f41ee..244adff7 100644 --- a/tests/Reflection/TypedNoDefaultReflectionPropertyTest.php +++ b/tests/Reflection/TypedNoDefaultReflectionPropertyTest.php @@ -72,7 +72,7 @@ public function setId(mixed $id): void class TypedNullableFoo { - private string|null $value; + private string|null $value = null; public function setValue(mixed $value): void { diff --git a/tests/RuntimeReflectionPropertyTest.php b/tests/RuntimeReflectionPropertyTest.php index f88b546b..18860e4e 100644 --- a/tests/RuntimeReflectionPropertyTest.php +++ b/tests/RuntimeReflectionPropertyTest.php @@ -39,8 +39,8 @@ public function testGetSetValue(string $name, string $value): void } /** @param class-string $proxyClass */ - #[TestWith(['Doctrine\\Tests\\Persistence\\RuntimeReflectionPropertyTestProxyMock'])] - #[TestWith(['\\Doctrine\\Tests\\Persistence\\RuntimeReflectionPropertyTestProxyMock'])] + #[TestWith([RuntimeReflectionPropertyTestProxyMock::class])] + #[TestWith([RuntimeReflectionPropertyTestProxyMock::class])] public function testGetValueOnProxyProperty(string $proxyClass): void { $getCheckMock = $this->createMock(DummyMock::class);