Skip to content

Commit

Permalink
[TASK] Mitigate PHPUnit NameAlreadyInUseException
Browse files Browse the repository at this point in the history
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] sebastianbergmann/phpunit#5804

Resolves: #104005
Releases: main, 12.4
Change-Id: Icc558844275c9ae9f67a0e7c20daa318f5ad6b41
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/84530
Reviewed-by: Christian Kuhn <[email protected]>
Tested-by: core-ci <[email protected]>
Tested-by: Christian Kuhn <[email protected]>
Tested-by: Andreas Wolf <[email protected]>
Reviewed-by: Andreas Wolf <[email protected]>
  • Loading branch information
sbuerk authored and lolli42 committed Jun 10, 2024
1 parent 1cbcf82 commit e2aff12
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 34 deletions.
20 changes: 0 additions & 20 deletions Build/phpstan/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -825,26 +825,6 @@ parameters:
count: 1
path: ../../typo3/sysext/core/Tests/Unit/Pagination/SimplePaginationTest.php

-
message: "#^Parameter \\#1 \\$expected of static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\) expects class\\-string\\<ad9195e2487eea33c8a2abd5cf33cba4\\>, string given\\.$#"
count: 1
path: ../../typo3/sysext/core/Tests/Unit/Resource/Index/ExtractorRegistryTest.php

-
message: "#^Parameter \\#1 \\$expected of static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\) expects class\\-string\\<cef9aa4e1cd3aa7ff05dcdccb117156a\\>, string given\\.$#"
count: 1
path: ../../typo3/sysext/core/Tests/Unit/Resource/Index/ExtractorRegistryTest.php

-
message: "#^Parameter \\#1 \\$expected of static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\) expects class\\-string\\<db76010e5c24658c35ea1605cce2391d\\>, string given\\.$#"
count: 1
path: ../../typo3/sysext/core/Tests/Unit/Resource/Index/ExtractorRegistryTest.php

-
message: "#^Parameter \\#1 \\$name of method PHPUnit\\\\Framework\\\\MockObject\\\\MockBuilder\\<TYPO3\\\\CMS\\\\Core\\\\Resource\\\\Index\\\\ExtractorInterface\\>\\:\\:setMockClassName\\(\\) expects class\\-string, string given\\.$#"
count: 10
path: ../../typo3/sysext/core/Tests/Unit/Resource/Index/ExtractorRegistryTest.php

-
message: "#^Constructor of class TYPO3\\\\CMS\\\\Core\\\\Tests\\\\Unit\\\\Tree\\\\TableConfiguration\\\\Fixtures\\\\TreeDataProviderFixture has an unused parameter \\$configuration\\.$#"
count: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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
Expand Down Expand Up @@ -105,7 +106,7 @@ public function extractMetaDataComposesDataByAvailableExtractors(): void
$fileMock->method('getType')->willReturn(FileType::IMAGE->value);
$fileMock->method('getStorage')->willReturn($storageMock);

$extractorClass1 = md5('1');
$extractorClass1 = StringUtility::getUniqueId('extractor');
$extractorObject1 = $this->getMockBuilder(ExtractorInterface::class)
->setMockClassName($extractorClass1)
->getMock();
Expand All @@ -120,7 +121,7 @@ public function extractMetaDataComposesDataByAvailableExtractors(): void
'height' => 600,
]);

$extractorClass2 = md5('2');
$extractorClass2 = StringUtility::getUniqueId('extractor');
$extractorObject2 = $this->getMockBuilder(ExtractorInterface::class)
->setMockClassName($extractorClass2)
->getMock();
Expand Down Expand Up @@ -224,7 +225,7 @@ public function extractMetaDataComposesDataByAvailableExtractorsWithDifferentPri
$fileMock->expects(self::any())->method('getType')->willReturn(FileType::IMAGE->value);
$fileMock->expects(self::any())->method('getStorage')->willReturn($storageMock);

$extractorClass1 = md5('1');
$extractorClass1 = StringUtility::getUniqueId('extractor');
$extractorObject1 = $this->getMockBuilder(ExtractorInterface::class)
->setMockClassName($extractorClass1)
->getMock();
Expand All @@ -239,7 +240,7 @@ public function extractMetaDataComposesDataByAvailableExtractorsWithDifferentPri
'bar' => 'first',
]);

$extractorClass2 = md5('2');
$extractorClass2 = StringUtility::getUniqueId('extractor');
$extractorObject2 = $this->getMockBuilder(ExtractorInterface::class)
->setMockClassName($extractorClass2)
->getMock();
Expand Down

0 comments on commit e2aff12

Please sign in to comment.