From 55da5d023fdb20d923a4d221ae713a9aefd3fa5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BCrk?= Date: Sat, 8 Jun 2024 17:17:50 +0200 Subject: [PATCH] [TASK] Mitigate PHPUnit `NameAlreadyInUseException` PHPUnit changed the way how double creation is handled internally and now emits an exception if a manually defined `mock class name` should be registered. In some places within the unit tests, manual mock class names have been used, because the names are needed for cross referencing at a later point. For this, different approaches have been used, for example hardcoded MD5 hashes as strings or creating hashes of static values like `md5('1')`, and also reused hashes in different places or in tests using data providers. This now leads to an exception: PHPUnit\Framework\MockObject\Generator\NameAlreadyInUseException The name "b70551b2b2db62b6b15a9bbfcbd50614" is already in use This change mitigates the issue by using `StringUtility::getUniqueId('somePrefix')` as is already done in other places. As a sideeffect, four phpstan baseline entries can be removed. Used command(s): Build/Scripts/runTests.sh -s phpstanGenerateBaseline [1] https://github.com/sebastianbergmann/phpunit/pull/5804 Resolves: #104005 Releases: main, 12.4 Change-Id: Icc558844275c9ae9f67a0e7c20daa318f5ad6b41 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/84531 Tested-by: core-ci Reviewed-by: Christian Kuhn Tested-by: Christian Kuhn --- .../Resource/Index/ExtractorRegistryTest.php | 21 ++++++++++--------- .../Resource/Service/ExtractorServiceTest.php | 9 ++++---- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/Tests/Unit/Resource/Index/ExtractorRegistryTest.php b/Tests/Unit/Resource/Index/ExtractorRegistryTest.php index ab26b5d8d2..5c8836e447 100644 --- a/Tests/Unit/Resource/Index/ExtractorRegistryTest.php +++ b/Tests/Unit/Resource/Index/ExtractorRegistryTest.php @@ -21,6 +21,7 @@ use PHPUnit\Framework\MockObject\MockObject; use TYPO3\CMS\Core\Resource\Index\ExtractorInterface; use TYPO3\CMS\Core\Resource\Index\ExtractorRegistry; +use TYPO3\CMS\Core\Utility\StringUtility; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; /** @@ -31,7 +32,7 @@ final class ExtractorRegistryTest extends UnitTestCase #[Test] public function registeredExtractorClassCanBeRetrieved(): void { - $extractorClass = 'a9f4d5e4ebb4b03547a2a6094e1170ac'; + $extractorClass = StringUtility::getUniqueId('extractor'); $extractorObject = $this->getMockBuilder(ExtractorInterface::class) ->setMockClassName($extractorClass) ->getMock(); @@ -67,19 +68,19 @@ public function registerExtractorClassThrowsExceptionIfClassDoesNotImplementRigh #[Test] public function registerExtractorClassWithHighestPriorityIsFirstInResult(): void { - $extractorClass1 = 'db76010e5c24658c35ea1605cce2391d'; + $extractorClass1 = StringUtility::getUniqueId('extractor'); $extractorObject1 = $this->getMockBuilder(ExtractorInterface::class) ->setMockClassName($extractorClass1) ->getMock(); $extractorObject1->method('getExecutionPriority')->willReturn(1); - $extractorClass2 = 'ad9195e2487eea33c8a2abd5cf33cba4'; + $extractorClass2 = StringUtility::getUniqueId('extractor'); $extractorObject2 = $this->getMockBuilder(ExtractorInterface::class) ->setMockClassName($extractorClass2) ->getMock(); $extractorObject2->method('getExecutionPriority')->willReturn(10); - $extractorClass3 = 'cef9aa4e1cd3aa7ff05dcdccb117156a'; + $extractorClass3 = StringUtility::getUniqueId('extractor'); $extractorObject3 = $this->getMockBuilder(ExtractorInterface::class) ->setMockClassName($extractorClass3) ->getMock(); @@ -106,13 +107,13 @@ public function registerExtractorClassWithHighestPriorityIsFirstInResult(): void #[Test] public function registeredExtractorClassWithSamePriorityAreAllReturned(): void { - $extractorClass1 = 'b70551b2b2db62b6b15a9bbfcbd50614'; + $extractorClass1 = StringUtility::getUniqueId('extractor'); $extractorObject1 = $this->getMockBuilder(ExtractorInterface::class) ->setMockClassName($extractorClass1) ->getMock(); $extractorObject1->method('getExecutionPriority')->willReturn(1); - $extractorClass2 = 'ac318f1659d278b79b38262f23a78d5d'; + $extractorClass2 = StringUtility::getUniqueId('extractor'); $extractorObject2 = $this->getMockBuilder(ExtractorInterface::class) ->setMockClassName($extractorClass2) ->getMock(); @@ -135,14 +136,14 @@ public function registeredExtractorClassWithSamePriorityAreAllReturned(): void #[Test] public function registeredExtractorsCanBeFilteredByDriverTypeButNoTyeREstrictionIsTreatedAsCompatible(): void { - $extractorClass1 = 'b70551b2b2db62b6b15a9bbfcbd50614'; + $extractorClass1 = StringUtility::getUniqueId('extractor'); $extractorObject1 = $this->getMockBuilder(ExtractorInterface::class) ->setMockClassName($extractorClass1) ->getMock(); $extractorObject1->method('getExecutionPriority')->willReturn(1); $extractorObject1->method('getDriverRestrictions')->willReturn([]); - $extractorClass2 = 'ac318f1659d278b79b38262f23a78d5d'; + $extractorClass2 = StringUtility::getUniqueId('extractor'); $extractorObject2 = $this->getMockBuilder(ExtractorInterface::class) ->setMockClassName($extractorClass2) ->getMock(); @@ -166,14 +167,14 @@ public function registeredExtractorsCanBeFilteredByDriverTypeButNoTyeREstriction #[Test] public function registeredExtractorsCanBeFilteredByDriverType(): void { - $extractorClass1 = 'b70551b2b2db62b6b15a9bbfcbd50614'; + $extractorClass1 = StringUtility::getUniqueId('extractor'); $extractorObject1 = $this->getMockBuilder(ExtractorInterface::class) ->setMockClassName($extractorClass1) ->getMock(); $extractorObject1->method('getExecutionPriority')->willReturn(1); $extractorObject1->method('getDriverRestrictions')->willReturn(['Foo']); - $extractorClass2 = 'ac318f1659d278b79b38262f23a78d5d'; + $extractorClass2 = StringUtility::getUniqueId('extractor'); $extractorObject2 = $this->getMockBuilder(ExtractorInterface::class) ->setMockClassName($extractorClass2) ->getMock(); diff --git a/Tests/Unit/Resource/Service/ExtractorServiceTest.php b/Tests/Unit/Resource/Service/ExtractorServiceTest.php index ebfadc1dd4..375bbe8595 100644 --- a/Tests/Unit/Resource/Service/ExtractorServiceTest.php +++ b/Tests/Unit/Resource/Service/ExtractorServiceTest.php @@ -24,6 +24,7 @@ use TYPO3\CMS\Core\Resource\Index\ExtractorRegistry; use TYPO3\CMS\Core\Resource\ResourceStorage; use TYPO3\CMS\Core\Resource\Service\ExtractorService; +use TYPO3\CMS\Core\Utility\StringUtility; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; final class ExtractorServiceTest extends UnitTestCase @@ -104,7 +105,7 @@ public function extractMetaDataComposesDataByAvailableExtractors(): void $fileMock->method('getType')->willReturn(File::FILETYPE_IMAGE); $fileMock->method('getStorage')->willReturn($storageMock); - $extractorClass1 = md5('1'); + $extractorClass1 = StringUtility::getUniqueId('extractor'); $extractorObject1 = $this->getMockBuilder(ExtractorInterface::class) ->setMockClassName($extractorClass1) ->getMock(); @@ -119,7 +120,7 @@ public function extractMetaDataComposesDataByAvailableExtractors(): void 'height' => 600, ]); - $extractorClass2 = md5('2'); + $extractorClass2 = StringUtility::getUniqueId('extractor'); $extractorObject2 = $this->getMockBuilder(ExtractorInterface::class) ->setMockClassName($extractorClass2) ->getMock(); @@ -223,7 +224,7 @@ public function extractMetaDataComposesDataByAvailableExtractorsWithDifferentPri $fileMock->expects(self::any())->method('getType')->willReturn(File::FILETYPE_IMAGE); $fileMock->expects(self::any())->method('getStorage')->willReturn($storageMock); - $extractorClass1 = md5('1'); + $extractorClass1 = StringUtility::getUniqueId('extractor'); $extractorObject1 = $this->getMockBuilder(ExtractorInterface::class) ->setMockClassName($extractorClass1) ->getMock(); @@ -238,7 +239,7 @@ public function extractMetaDataComposesDataByAvailableExtractorsWithDifferentPri 'bar' => 'first', ]); - $extractorClass2 = md5('2'); + $extractorClass2 = StringUtility::getUniqueId('extractor'); $extractorObject2 = $this->getMockBuilder(ExtractorInterface::class) ->setMockClassName($extractorClass2) ->getMock();