From 09789e678c76bf1d8ca66f7f920e4f058fae9bb6 Mon Sep 17 00:00:00 2001 From: Christian Kuhn Date: Tue, 27 Feb 2024 16:53:10 +0100 Subject: [PATCH] [TASK] Avoid phpunit addMethods() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit phpunit 11 deprecates addMethods() on mocks. There are various solutions: * Use onlyMethods([]) instead of addMethods(['dummy']) to "mock nothing" * Remove a couple of calls that were bogus in the first place * Turn calls into mocks of interfaces * Turn calls into mocks of fixture classes * Instantiate a subject with new() Also have the one or the other minor cleanup as drive-by. Change-Id: I5dad1cc65c14ed50b3941beaa7132040bc415a11 Resolves: #103218 Releases: main Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83150 Tested-by: Anja Leichsenring Reviewed-by: Anja Leichsenring Reviewed-by: Oliver Klee Tested-by: Oliver Klee Tested-by: core-ci Reviewed-by: Stefan Bürk Tested-by: Stefan Bürk --- Build/phpstan/phpstan-baseline.neon | 15 -- .../Cache/Backend/FileBackendTest.php | 50 +++--- .../BackendUserAuthenticationTest.php | 2 +- .../Error/ProductionExceptionHandlerTest.php | 14 +- .../Unit/Log/Writer/DatabaseWriterTest.php | 5 +- .../Tests/Unit/Log/Writer/FileWriterTest.php | 2 +- .../Tests/Unit/Package/PackageManagerTest.php | 71 +------- .../core/Tests/Unit/Resource/FileTest.php | 14 +- .../Unit/Resource/MetaDataAspectTest.php | 1 - .../Processing/LocalPreviewHelperTest.php | 2 +- .../Unit/Service/FlexFormServiceTest.php | 5 +- .../Tests/Unit/Utility/GeneralUtilityTest.php | 10 +- .../Tests/Unit/Mvc/View/JsonViewTest.php | 153 ++++++++---------- .../Unit/Persistence/Generic/BackendTest.php | 22 +-- .../Fixtures/TearDownableBackendInterface.php | 23 +++ .../Generic/PersistenceManagerTest.php | 8 +- .../Persistence/Generic/QueryFactoryTest.php | 64 +++----- .../Unit/Utility/DebuggerUtilityTest.php | 4 +- .../Core/Rendering/RenderingContextTest.php | 59 +++---- .../ContentObject/CaseContentObjectTest.php | 2 +- .../ContentObjectRendererTest.php | 2 +- .../ContentObject/ImageContentObjectTest.php | 4 +- .../Unit/Processor/GalleryProcessorTest.php | 2 +- .../CachingFrameworkGarbageCollectionTest.php | 4 +- 24 files changed, 181 insertions(+), 357 deletions(-) create mode 100644 typo3/sysext/extbase/Tests/Unit/Persistence/Generic/Fixtures/TearDownableBackendInterface.php diff --git a/Build/phpstan/phpstan-baseline.neon b/Build/phpstan/phpstan-baseline.neon index 47156686f2a0..7b95c57d83b5 100644 --- a/Build/phpstan/phpstan-baseline.neon +++ b/Build/phpstan/phpstan-baseline.neon @@ -1175,26 +1175,11 @@ parameters: count: 8 path: ../../typo3/sysext/extbase/Tests/Unit/Mvc/Controller/MvcPropertyMappingConfigurationServiceTest.php - - - message: "#^Property class@anonymous/extbase/Tests/Unit/Mvc/View/JsonViewTest\\.php\\:108\\:\\:\\$prohibited is unused\\.$#" - count: 1 - path: ../../typo3/sysext/extbase/Tests/Unit/Mvc/View/JsonViewTest.php - - message: "#^Parameter \\#1 \\$object of method TYPO3\\\\CMS\\\\Extbase\\\\Persistence\\\\Generic\\\\Backend\\:\\:getIdentifierByObject\\(\\) expects object, string given\\.$#" count: 1 path: ../../typo3/sysext/extbase/Tests/Unit/Persistence/Generic/BackendTest.php - - - message: "#^Call to an undefined method Psr\\\\Container\\\\ContainerInterface\\:\\:expects\\(\\)\\.$#" - count: 1 - path: ../../typo3/sysext/extbase/Tests/Unit/Persistence/Generic/QueryFactoryTest.php - - - - message: "#^Call to an undefined method Psr\\\\Container\\\\ContainerInterface\\:\\:method\\(\\)\\.$#" - count: 1 - path: ../../typo3/sysext/extbase/Tests/Unit/Persistence/Generic/QueryFactoryTest.php - - message: "#^Call to an undefined method TYPO3\\\\CMS\\\\Extbase\\\\Persistence\\\\PersistenceManagerInterface\\:\\:expects\\(\\)\\.$#" count: 4 diff --git a/typo3/sysext/core/Tests/Functional/Cache/Backend/FileBackendTest.php b/typo3/sysext/core/Tests/Functional/Cache/Backend/FileBackendTest.php index 8b23e1899e49..e429a0568a2f 100644 --- a/typo3/sysext/core/Tests/Functional/Cache/Backend/FileBackendTest.php +++ b/typo3/sysext/core/Tests/Functional/Cache/Backend/FileBackendTest.php @@ -44,7 +44,7 @@ public function setCacheDirectoryThrowsExceptionOnNonWritableDirectory(): void { $this->expectException(Exception::class); $this->expectExceptionCode(1303669848); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory('http://localhost/'); $subject->setCache(new NullFrontend('foo')); } @@ -120,7 +120,7 @@ public function setCacheDirectoryAllowsAbsoluteDottedPathWithTrailingSlash(): vo #[Test] public function getCacheDirectoryReturnsTheCurrentCacheDirectory(): void { - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache(new NullFrontend('SomeCache')); self::assertEquals($this->instancePath . '/Foo/cache/code/SomeCache/', $subject->getCacheDirectory()); @@ -131,7 +131,7 @@ public function aDedicatedCacheDirectoryIsUsedForCodeCaches(): void { $mockCache = $this->createMock(PhpFrontend::class); $mockCache->method('getIdentifier')->willReturn('SomeCache'); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); self::assertEquals($this->instancePath . '/Foo/cache/code/SomeCache/', $subject->getCacheDirectory()); @@ -142,7 +142,7 @@ public function setThrowsExceptionIfDataIsNotAString(): void { $this->expectException(InvalidDataException::class); $this->expectExceptionCode(1204481674); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache(new NullFrontend('SomeCache')); $subject->set('some identifier', ['not a string']); @@ -156,7 +156,7 @@ public function setReallySavesToTheSpecifiedDirectory(): void $data = 'some data' . microtime(); $entryIdentifier = 'BackendFileTest'; $pathAndFilename = $this->instancePath . '/Foo/cache/data/UnitTestCache/' . $entryIdentifier; - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $subject->set($entryIdentifier, $data); @@ -173,7 +173,7 @@ public function setOverwritesAnAlreadyExistingCacheEntryForTheSameIdentifier(): $data1 = 'some data' . microtime(); $data2 = 'some data' . microtime(); $entryIdentifier = 'BackendFileRemoveBeforeSetTest'; - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $subject->set($entryIdentifier, $data1, [], 500); @@ -191,7 +191,7 @@ public function setAlsoSavesSpecifiedTags(): void $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache'); $data = 'some data' . microtime(); $entryIdentifier = 'BackendFileRemoveBeforeSetTest'; - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $subject->set($entryIdentifier, $data, ['Tag1', 'Tag2']); @@ -208,12 +208,12 @@ public function setCacheDetectsAndLoadsAFrozenCache(): void $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache'); $data = 'some data' . microtime(); $entryIdentifier = 'BackendFileTest'; - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $subject->set($entryIdentifier, $data, ['Tag1', 'Tag2']); $subject->freeze(); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); self::assertTrue($subject->isFrozen()); @@ -225,7 +225,7 @@ public function getReturnsContentOfTheCorrectCacheFile(): void { $mockCache = $this->createMock(AbstractFrontend::class); $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache'); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['setTag'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $entryIdentifier = 'BackendFileTest'; @@ -269,7 +269,7 @@ public function hasReturnsTrueIfAnEntryExists(): void { $mockCache = $this->createMock(AbstractFrontend::class); $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache'); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $entryIdentifier = 'BackendFileTest'; @@ -311,7 +311,7 @@ public function removeReallyRemovesACacheEntry(): void $data = 'some data' . microtime(); $entryIdentifier = 'BackendFileTest'; $pathAndFilename = $this->instancePath . '/Foo/cache/data/UnitTestCache/' . $entryIdentifier; - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $subject->set($entryIdentifier, $data); @@ -346,7 +346,7 @@ public function setThrowsExceptionForInvalidIdentifier(string $identifier): void $this->expectExceptionCode(1282073032); $mockCache = $this->createMock(AbstractFrontend::class); $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache'); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->setConstructorArgs(['test'])->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $subject->set($identifier, 'cache data', []); @@ -360,7 +360,7 @@ public function getThrowsExceptionForInvalidIdentifier(string $identifier): void $this->expectExceptionCode(1282073033); $mockCache = $this->createMock(AbstractFrontend::class); $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache'); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $subject->get($identifier); @@ -372,7 +372,7 @@ public function hasThrowsExceptionForInvalidIdentifier(string $identifier): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionCode(1282073034); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->has($identifier); } @@ -384,7 +384,7 @@ public function removeThrowsExceptionForInvalidIdentifier(string $identifier): v $this->expectExceptionCode(1334756960); $mockCache = $this->createMock(AbstractFrontend::class); $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache'); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $subject->remove($identifier); @@ -398,7 +398,7 @@ public function requireOnceThrowsExceptionForInvalidIdentifier(string $identifie $this->expectExceptionCode(1282073036); $mockCache = $this->createMock(AbstractFrontend::class); $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache'); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $subject->requireOnce($identifier); @@ -409,7 +409,7 @@ public function requireOnceIncludesAndReturnsResultOfIncludedPhpFile(): void { $mockCache = $this->createMock(AbstractFrontend::class); $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache'); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $entryIdentifier = 'SomePhpEntry'; @@ -443,7 +443,7 @@ public function requireThrowsExceptionForInvalidIdentifier(string $identifier): $this->expectExceptionCode(1532528246); $mockCache = $this->createMock(AbstractFrontend::class); $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache'); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $subject->require($identifier); @@ -454,7 +454,7 @@ public function requireIncludesAndReturnsResultOfIncludedPhpFile(): void { $mockCache = $this->createMock(AbstractFrontend::class); $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache'); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $entryIdentifier = 'SomePhpEntry2'; @@ -500,7 +500,7 @@ public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag(): void { $mockCache = $this->createMock(AbstractFrontend::class); $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache'); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $data = 'some data' . microtime(); @@ -518,7 +518,7 @@ public function findIdentifiersByTagDoesNotReturnExpiredEntries(): void { $mockCache = $this->createMock(AbstractFrontend::class); $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache'); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $data = 'some data'; @@ -536,7 +536,7 @@ public function flushRemovesAllCacheEntries(): void { $mockCache = $this->createMock(AbstractFrontend::class); $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache'); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $data = 'some data'; @@ -554,7 +554,7 @@ public function flushCreatesCacheDirectoryAgain(): void { $mockCache = $this->createMock(AbstractFrontend::class); $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache'); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $subject->flush(); @@ -610,7 +610,7 @@ public function flushUnfreezesTheCache(): void { $mockCache = $this->createMock(AbstractFrontend::class); $mockCache->expects(self::atLeastOnce())->method('getIdentifier')->willReturn('UnitTestCache'); - $subject = $this->getMockBuilder(FileBackend::class)->addMethods(['dummy'])->disableOriginalConstructor()->getMock(); + $subject = new FileBackend(''); $subject->setCacheDirectory($this->instancePath . '/Foo/'); $subject->setCache($mockCache); $subject->freeze(); diff --git a/typo3/sysext/core/Tests/Unit/Authentication/BackendUserAuthenticationTest.php b/typo3/sysext/core/Tests/Unit/Authentication/BackendUserAuthenticationTest.php index d865eb8f1727..2cc9824759cf 100644 --- a/typo3/sysext/core/Tests/Unit/Authentication/BackendUserAuthenticationTest.php +++ b/typo3/sysext/core/Tests/Unit/Authentication/BackendUserAuthenticationTest.php @@ -91,7 +91,7 @@ public function logoffCleansFormProtectionIfBackendUserIsLoggedIn(): void $GLOBALS['BE_USER']->initializeUserSessionManager($userSessionManager); $subject = $this->getMockBuilder(BackendUserAuthentication::class) - ->addMethods(['dummy']) + ->onlyMethods([]) ->disableOriginalConstructor() ->getMock(); diff --git a/typo3/sysext/core/Tests/Unit/Error/ProductionExceptionHandlerTest.php b/typo3/sysext/core/Tests/Unit/Error/ProductionExceptionHandlerTest.php index 03eff59b7813..1a21ebc2c3b5 100644 --- a/typo3/sysext/core/Tests/Unit/Error/ProductionExceptionHandlerTest.php +++ b/typo3/sysext/core/Tests/Unit/Error/ProductionExceptionHandlerTest.php @@ -22,6 +22,7 @@ use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Psr\Log\LoggerTrait; +use TYPO3\CMS\Core\Error\Http\StatusException; use TYPO3\CMS\Core\Error\ProductionExceptionHandler; use TYPO3\CMS\Core\Information\Typo3Information; use TYPO3\CMS\Core\Utility\GeneralUtility; @@ -32,9 +33,6 @@ final class ProductionExceptionHandlerTest extends UnitTestCase protected bool $resetSingletonInstances = true; protected ProductionExceptionHandler&MockObject $subject; - /** - * Sets up this test case. - */ protected function setUp(): void { parent::setUp(); @@ -79,10 +77,7 @@ public function echoExceptionWebEscapesExceptionTitle(): void $typo3InformationMock->method('getCopyrightYear')->willReturn('1999-20XX'); GeneralUtility::addInstance(Typo3Information::class, $typo3InformationMock); $title = 'b'; - $exception = $this->getMockBuilder(\Exception::class) - ->addMethods(['getTitle']) - ->setConstructorArgs(['some message']) - ->getMock(); + $exception = $this->createMock(StatusException::class); $exception->method('getTitle')->willReturn($title); ob_start(); $this->subject->echoExceptionWeb($exception); @@ -92,11 +87,6 @@ public function echoExceptionWebEscapesExceptionTitle(): void self::assertStringNotContainsString($title, $output); } - /** - * Data provider with allowed contexts. - * - * @return string[][] - */ public static function exampleUrlsForTokenAnonymization(): array { return [ diff --git a/typo3/sysext/core/Tests/Unit/Log/Writer/DatabaseWriterTest.php b/typo3/sysext/core/Tests/Unit/Log/Writer/DatabaseWriterTest.php index 3d5c869d5092..4a7f34d83a96 100644 --- a/typo3/sysext/core/Tests/Unit/Log/Writer/DatabaseWriterTest.php +++ b/typo3/sysext/core/Tests/Unit/Log/Writer/DatabaseWriterTest.php @@ -28,10 +28,7 @@ final class DatabaseWriterTest extends UnitTestCase public function getTableReturnsPreviouslySetTable(): void { $logTable = StringUtility::getUniqueId('logtable_'); - $subject = $this->getMockBuilder(DatabaseWriter::class) - ->addMethods(['dummy']) - ->disableOriginalConstructor() - ->getMock(); + $subject = new DatabaseWriter(); $subject->setLogTable($logTable); self::assertSame($logTable, $subject->getLogTable()); } diff --git a/typo3/sysext/core/Tests/Unit/Log/Writer/FileWriterTest.php b/typo3/sysext/core/Tests/Unit/Log/Writer/FileWriterTest.php index a8293cb7192e..08a692eefb63 100644 --- a/typo3/sysext/core/Tests/Unit/Log/Writer/FileWriterTest.php +++ b/typo3/sysext/core/Tests/Unit/Log/Writer/FileWriterTest.php @@ -160,7 +160,7 @@ public function logsToFileWithUnescapedCharacters(): void public function aSecondLogWriterToTheSameFileDoesNotOpenTheFileTwice(): void { $firstWriter = $this->getMockBuilder(FileWriter::class) - ->addMethods(['dummy']) + ->onlyMethods([]) ->getMock(); $secondWriter = $this->getMockBuilder(FileWriter::class) ->onlyMethods(['createLogFile']) diff --git a/typo3/sysext/core/Tests/Unit/Package/PackageManagerTest.php b/typo3/sysext/core/Tests/Unit/Package/PackageManagerTest.php index 47c2af3a894d..01d99c472ab4 100644 --- a/typo3/sysext/core/Tests/Unit/Package/PackageManagerTest.php +++ b/typo3/sysext/core/Tests/Unit/Package/PackageManagerTest.php @@ -24,9 +24,6 @@ use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend; use TYPO3\CMS\Core\Core\Environment; use TYPO3\CMS\Core\Package\Cache\PackageStatesPackageCache; -use TYPO3\CMS\Core\Package\Exception; -use TYPO3\CMS\Core\Package\Exception\InvalidPackageStateException; -use TYPO3\CMS\Core\Package\Exception\PackageStatesFileNotWritableException; use TYPO3\CMS\Core\Package\Exception\ProtectedPackageKeyException; use TYPO3\CMS\Core\Package\Exception\UnknownPackageException; use TYPO3\CMS\Core\Package\Exception\UnknownPackagePathException; @@ -37,9 +34,6 @@ use TYPO3\TestingFramework\Core\AccessibleObjectInterface; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; -/** - * Testcase for the default package manager - */ final class PackageManagerTest extends UnitTestCase { protected PackageManager&MockObject&AccessibleObjectInterface $packageManager; @@ -56,15 +50,8 @@ protected function setUp(): void $this->testRoot = Environment::getVarPath() . '/tests/PackageManager/'; $this->testFilesToDelete[] = $this->testRoot; - $mockCache = $this->getMockBuilder(PhpFrontend::class) - ->onlyMethods(['has', 'set', 'getBackend']) - ->disableOriginalConstructor() - ->getMock(); - $mockCacheBackend = $this->getMockBuilder(SimpleFileBackend::class) - ->onlyMethods(['has', 'set', 'getCacheDirectory']) - ->addMethods(['getBackend']) - ->disableOriginalConstructor() - ->getMock(); + $mockCache = $this->createMock(PhpFrontend::class); + $mockCacheBackend = $this->createMock(SimpleFileBackend::class); $mockCache->method('has')->willReturn(false); $mockCache->method('set')->willReturn(true); $mockCache->method('getBackend')->willReturn($mockCacheBackend); @@ -108,9 +95,6 @@ protected function createPackage(string $packageKey): Package return $package; } - /** - * @throws UnknownPackageException - */ #[Test] public function getPackageReturnsTheSpecifiedPackage(): void { @@ -120,9 +104,6 @@ public function getPackageReturnsTheSpecifiedPackage(): void self::assertInstanceOf(Package::class, $package, 'The result of getPackage() was no valid package object.'); } - /** - * @throws UnknownPackageException - */ #[Test] public function getPackageThrowsExceptionOnUnknownPackage(): void { @@ -207,16 +188,11 @@ public function scanAvailablePackagesKeepsExistingPackageConfiguration(): void #[Test] public function extractPackageKeyFromPackagePathFindsPackageKey(): void { - $package = $this->createPackage('typo3/my-package'); - + $this->createPackage('typo3/my-package'); $resolvedPackage = $this->packageManager->extractPackageKeyFromPackagePath('EXT:typo3/my-package/path/to/file'); - self::assertSame('typo3/my-package', $resolvedPackage); } - /** - * @throws UnknownPackagePathException - */ #[Test] public function extractPackageKeyFromPackagePathThrowsExceptionOnNonPackagePaths(): void { @@ -226,9 +202,6 @@ public function extractPackageKeyFromPackagePathThrowsExceptionOnNonPackagePaths $this->packageManager->extractPackageKeyFromPackagePath($this->testRoot . 'Packages/Application/InvalidPackage/'); } - /** - * @throws UnknownPackageException - */ #[Test] public function extractPackageKeyFromPackagePathThrowsExceptionOnInvalidPackagePaths(): void { @@ -277,10 +250,7 @@ public function packageStatesConfigurationContainsRelativePaths(): void self::assertEquals($expectedPackageStatesConfiguration, $actualPackageStatesConfiguration['packages']); } - /** - * Data Provider returning valid package keys and the corresponding path - */ - public static function packageKeysAndPaths(): array + public static function createPackageCreatesPackageFolderAndReturnsPackageDataProvider(): array { return [ ['TYPO3.YetAnotherTestPackage', 'Packages/Application/TYPO3.YetAnotherTestPackage/'], @@ -288,7 +258,7 @@ public static function packageKeysAndPaths(): array ]; } - #[DataProvider('packageKeysAndPaths')] + #[DataProvider('createPackageCreatesPackageFolderAndReturnsPackageDataProvider')] #[Test] public function createPackageCreatesPackageFolderAndReturnsPackage($packageKey, $expectedPackagePath): void { @@ -301,32 +271,18 @@ public function createPackageCreatesPackageFolderAndReturnsPackage($packageKey, self::assertTrue($this->packageManager->isPackageAvailable($packageKey)); } - /** - * @throws ProtectedPackageKeyException - * @throws UnknownPackageException - * @throws PackageStatesFileNotWritableException - */ #[Test] public function activatePackageAndDeactivatePackageActivateAndDeactivateTheGivenPackage(): void { $packageKey = 'Acme.YetAnotherTestPackage'; - $this->createPackage($packageKey); - $this->packageManager->method('sortActivePackagesByDependencies')->willReturn([]); - $this->packageManager->deactivatePackage($packageKey); self::assertFalse($this->packageManager->isPackageActive($packageKey)); - $this->packageManager->activatePackage($packageKey); self::assertTrue($this->packageManager->isPackageActive($packageKey)); } - /** - * @throws PackageStatesFileNotWritableException - * @throws ProtectedPackageKeyException - * @throws UnknownPackageException - */ #[Test] public function deactivatePackageThrowsAnExceptionIfPackageIsProtected(): void { @@ -339,11 +295,6 @@ public function deactivatePackageThrowsAnExceptionIfPackageIsProtected(): void $this->packageManager->deactivatePackage('Acme.YetAnotherTestPackage'); } - /** - * @throws ProtectedPackageKeyException - * @throws UnknownPackageException - * @throws Exception - */ #[Test] public function deletePackageThrowsErrorIfPackageIsNotAvailable(): void { @@ -354,12 +305,6 @@ public function deletePackageThrowsErrorIfPackageIsNotAvailable(): void $this->packageManager->deletePackage('PrettyUnlikelyThatThisPackageExists'); } - /** - * @throws InvalidPackageStateException - * @throws ProtectedPackageKeyException - * @throws UnknownPackageException - * @throws Exception - */ #[Test] public function deletePackageThrowsAnExceptionIfPackageIsProtected(): void { @@ -371,12 +316,6 @@ public function deletePackageThrowsAnExceptionIfPackageIsProtected(): void $this->packageManager->deletePackage('Acme.YetAnotherTestPackage'); } - /** - * @throws InvalidPackageStateException - * @throws ProtectedPackageKeyException - * @throws UnknownPackageException - * @throws Exception - */ #[Test] public function deletePackageRemovesPackageFromAvailableAndActivePackagesAndDeletesThePackageDirectory(): void { diff --git a/typo3/sysext/core/Tests/Unit/Resource/FileTest.php b/typo3/sysext/core/Tests/Unit/Resource/FileTest.php index 63ac0e335594..6ca077c2ef61 100644 --- a/typo3/sysext/core/Tests/Unit/Resource/FileTest.php +++ b/typo3/sysext/core/Tests/Unit/Resource/FileTest.php @@ -19,6 +19,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\MockObject\MockObject; use TYPO3\CMS\Core\Resource\File; use TYPO3\CMS\Core\Resource\Folder; use TYPO3\CMS\Core\Resource\Index\MetaDataRepository; @@ -29,17 +30,11 @@ use TYPO3\CMS\Core\Utility\StringUtility; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; -/** - * Testcase for the file class of the TYPO3 FAL - */ final class FileTest extends UnitTestCase { protected bool $resetSingletonInstances = true; - /** - * @var ResourceStorage - */ - protected $storageMock; + protected ResourceStorage&MockObject $storageMock; protected function setUp(): void { @@ -72,9 +67,6 @@ public function commonPropertiesAreAvailableWithOwnGetters(): void } } - /** - * Tests if a file is seen as indexed if the record has a uid - */ #[Test] public function fileIndexStatusIsTrueIfUidIsSet(): void { @@ -141,7 +133,7 @@ public function updatePropertiesReloadsStorageObjectIfStorageChanges(): void 'storage' => 'first', ]; $subject = $this->getMockBuilder(File::class) - ->addMethods(['loadStorage']) + ->onlyMethods([]) ->setConstructorArgs([$fileProperties, $this->storageMock]) ->getMock(); $mockedNewStorage = $this->createMock(ResourceStorage::class); diff --git a/typo3/sysext/core/Tests/Unit/Resource/MetaDataAspectTest.php b/typo3/sysext/core/Tests/Unit/Resource/MetaDataAspectTest.php index 720aa5e71e9d..6bce4a89e939 100644 --- a/typo3/sysext/core/Tests/Unit/Resource/MetaDataAspectTest.php +++ b/typo3/sysext/core/Tests/Unit/Resource/MetaDataAspectTest.php @@ -171,7 +171,6 @@ public function existingMetaDataGetsUpdated(): void $metaDataRepositoryMock = $this->getMockBuilder(MetaDataRepository::class) ->onlyMethods(['createMetaDataRecord', 'getTableFields']) - ->addMethods(['loadFromRepository']) ->setConstructorArgs([$eventDispatcherMock]) ->getMock(); diff --git a/typo3/sysext/core/Tests/Unit/Resource/Processing/LocalPreviewHelperTest.php b/typo3/sysext/core/Tests/Unit/Resource/Processing/LocalPreviewHelperTest.php index 681c67bc13c7..709f22e7f850 100644 --- a/typo3/sysext/core/Tests/Unit/Resource/Processing/LocalPreviewHelperTest.php +++ b/typo3/sysext/core/Tests/Unit/Resource/Processing/LocalPreviewHelperTest.php @@ -66,7 +66,7 @@ public function processDoesNotScaleUpImages(): void $localPreviewHelper = $this->getMockBuilder(LocalPreviewHelper::class) ->disableOriginalConstructor() - ->addMethods(['dummy']) + ->onlyMethods([]) ->getMock(); $task = $this->createMock(TaskInterface::class); diff --git a/typo3/sysext/core/Tests/Unit/Service/FlexFormServiceTest.php b/typo3/sysext/core/Tests/Unit/Service/FlexFormServiceTest.php index 6011a2d01f5c..cd76fdca7809 100644 --- a/typo3/sysext/core/Tests/Unit/Service/FlexFormServiceTest.php +++ b/typo3/sysext/core/Tests/Unit/Service/FlexFormServiceTest.php @@ -104,10 +104,7 @@ public function convertFlexFormContentToArrayResolvesComplexArrayStructure(): vo $cacheManagerMock->method('getCache')->willReturn($cacheMock); GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerMock); - $flexFormService = $this->getMockBuilder(FlexFormService::class) - ->addMethods(['dummy']) - ->disableOriginalConstructor() - ->getMock(); + $flexFormService = new FlexFormService(); $convertedFlexFormArray = $flexFormService->convertFlexFormContentToArray($input); self::assertSame($expected, $convertedFlexFormArray); } diff --git a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php index 8832a4ca2c97..54767c892d94 100644 --- a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php +++ b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php @@ -2762,10 +2762,7 @@ public function setSingletonInstanceForClassThatIsNoSubclassOfProvidedClassThrow { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionCode(1288967686); - - $instance = $this->getMockBuilder(SingletonInterface::class) - ->addMethods(['foo']) - ->getMock(); + $instance = $this->getMockBuilder(SingletonInterface::class)->getMock(); $singletonClassName = get_class($this->createMock(SingletonInterface::class)); GeneralUtility::setSingletonInstance($singletonClassName, $instance); } @@ -2852,10 +2849,7 @@ public function addInstanceForClassThatIsNoSubclassOfProvidedClassThrowsExceptio { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionCode(1288967686); - - $instance = $this->getMockBuilder(\stdClass::class) - ->addMethods(['bar']) - ->getMock(); + $instance = $this->getMockBuilder(\stdClass::class)->getMock(); $singletonClassName = get_class($this->createMock(\stdClass::class)); GeneralUtility::addInstance($singletonClassName, $instance); } diff --git a/typo3/sysext/extbase/Tests/Unit/Mvc/View/JsonViewTest.php b/typo3/sysext/extbase/Tests/Unit/Mvc/View/JsonViewTest.php index 2ba2fa066c5f..a3201b10a799 100644 --- a/typo3/sysext/extbase/Tests/Unit/Mvc/View/JsonViewTest.php +++ b/typo3/sysext/extbase/Tests/Unit/Mvc/View/JsonViewTest.php @@ -27,33 +27,11 @@ use TYPO3\CMS\Extbase\Reflection\ReflectionService; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; -/** - * Testcase for the JSON view - */ final class JsonViewTest extends UnitTestCase { protected bool $resetSingletonInstances = true; - /** - * @var JsonView - */ - protected $view; - - /** - * Sets up this test case - */ - protected function setUp(): void - { - parent::setUp(); - $this->view = $this->getMockBuilder(JsonView::class) - ->addMethods(['loadConfigurationFromYamlFile']) - ->getMock(); - } - - /** - * data provider for testTransformValue() - */ - public static function jsonViewTestData(): array + public static function transformValueDataProvider(): array { $output = []; @@ -106,9 +84,9 @@ public static function jsonViewTestData(): array $properties = ['foo' => 'bar', 'prohibited' => 'xxx']; $nestedObject = new class ($properties) { - private $properties; - private $prohibited; - public function __construct($properties) + private array $properties; + private string $prohibited = 'foo'; + public function __construct(array $properties) { $this->properties = $properties; } @@ -122,10 +100,15 @@ public function getPath(): string return 'path'; } - public function getProperties() + public function getProperties(): array { return $this->properties; } + + public function getProhibited(): string + { + return $this->prohibited; + } }; $object = $nestedObject; $configuration = [ @@ -169,13 +152,9 @@ public function getProperties() return $output; } - /** - * @param object|array $object - * @param array|string $expected - */ - #[DataProvider('jsonViewTestData')] + #[DataProvider('transformValueDataProvider')] #[Test] - public function transformValue($object, array $configuration, $expected, string $description): void + public function transformValue(object|array $object, array $configuration, array|string $expected, string $description): void { GeneralUtility::setSingletonInstance(ReflectionService::class, new ReflectionService(new NullFrontend('extbase'), 'ClassSchemata')); @@ -185,10 +164,8 @@ public function transformValue($object, array $configuration, $expected, string self::assertSame($expected, $actual, $description); } - /** - * data provider for testRecursive() - */ - public static function jsonViewTestDataRecursive(): array + + public static function recursiveDataProvider(): array { $object = new class ('foo') { private $value1 = ''; @@ -341,13 +318,9 @@ public function getSecret(): string return $output; } - /** - * @param object|array $object - * @param array|string $expected - */ - #[DataProvider('jsonViewTestDataRecursive')] + #[DataProvider('recursiveDataProvider')] #[Test] - public function recursive($object, array $configuration, $expected, string $variableToRender, string $description): void + public function recursive(object|array $object, array $configuration, object|array $expected, string $variableToRender, string $description): void { GeneralUtility::setSingletonInstance(ReflectionService::class, new ReflectionService(new NullFrontend('extbase'), 'ClassSchemata')); @@ -360,10 +333,7 @@ public function recursive($object, array $configuration, $expected, string $vari self::assertSame($expected, $actual, $description); } - /** - * data provider for testTransformValueWithObjectIdentifierExposure() - */ - public static function objectIdentifierExposureTestData(): array + public static function transformValueWithObjectIdentifierExposureDataProvider(): array { $output = []; @@ -389,7 +359,7 @@ public static function objectIdentifierExposureTestData(): array return $output; } - #[DataProvider('objectIdentifierExposureTestData')] + #[DataProvider('transformValueWithObjectIdentifierExposureDataProvider')] #[Test] public function transformValueWithObjectIdentifierExposure( object $object, @@ -412,10 +382,7 @@ public function transformValueWithObjectIdentifierExposure( self::assertSame($expected, $actual, $description); } - /** - * A data provider - */ - public static function exposeClassNameSettingsAndResults(): array + public static function viewExposesClassNameFullyIfConfiguredSoDataProvider(): array { $className = StringUtility::getUniqueId('DummyClass'); $namespace = 'TYPO3\CMS\Extbase\Tests\Unit\Mvc\View\\' . $className; @@ -441,7 +408,7 @@ public static function exposeClassNameSettingsAndResults(): array ]; } - #[DataProvider('exposeClassNameSettingsAndResults')] + #[DataProvider('viewExposesClassNameFullyIfConfiguredSoDataProvider')] #[Test] public function viewExposesClassNameFullyIfConfiguredSo( ?int $exposeClassNameSetting, @@ -476,10 +443,11 @@ public function renderReturnsJsonRepresentationOfAssignedObject(): void { $object = new \stdClass(); $object->foo = 'Foo'; - $this->view->assign('value', $object); + $subject = new JsonView(); + $subject->assign('value', $object); $expectedResult = '{"foo":"Foo"}'; - $actualResult = $this->view->render(); + $actualResult = $subject->render(); self::assertSame($expectedResult, $actualResult); } @@ -487,10 +455,11 @@ public function renderReturnsJsonRepresentationOfAssignedObject(): void public function renderReturnsJsonRepresentationOfAssignedArray(): void { $array = ['foo' => 'Foo', 'bar' => 'Bar']; - $this->view->assign('value', $array); + $subject = new JsonView(); + $subject->assign('value', $array); $expectedResult = '{"foo":"Foo","bar":"Bar"}'; - $actualResult = $this->view->render(); + $actualResult = $subject->render(); self::assertSame($expectedResult, $actualResult); } @@ -498,10 +467,11 @@ public function renderReturnsJsonRepresentationOfAssignedArray(): void public function renderReturnsJsonRepresentationOfAssignedSimpleValue(): void { $value = 'Foo'; - $this->view->assign('value', $value); + $subject = new JsonView(); + $subject->assign('value', $value); $expectedResult = '"Foo"'; - $actualResult = $this->view->render(); + $actualResult = $subject->render(); self::assertSame($expectedResult, $actualResult); } @@ -509,17 +479,15 @@ public function renderReturnsJsonRepresentationOfAssignedSimpleValue(): void public function renderKeepsUtf8CharactersUnescaped(): void { $value = 'Gürkchen'; - $this->view->assign('value', $value); + $subject = new JsonView(); + $subject->assign('value', $value); - $actualResult = $this->view->render(); + $actualResult = $subject->render(); $expectedResult = '"' . $value . '"'; self::assertSame($expectedResult, $actualResult); } - /** - * @return string[][] - */ public static function escapeCharacterDataProvider(): array { return [ @@ -532,9 +500,10 @@ public static function escapeCharacterDataProvider(): array #[Test] public function renderEscapesEscapeCharacters(string $character): void { - $this->view->assign('value', $character); + $subject = new JsonView(); + $subject->assign('value', $character); - $actualResult = $this->view->render(); + $actualResult = $subject->render(); $expectedResult = '"\\' . $character . '"'; self::assertSame($expectedResult, $actualResult); @@ -544,22 +513,24 @@ public function renderEscapesEscapeCharacters(string $character): void public function renderReturnsNullIfNameOfAssignedVariableIsNotEqualToValue(): void { $value = 'Foo'; - $this->view->assign('foo', $value); + $subject = new JsonView(); + $subject->assign('foo', $value); $expectedResult = 'null'; - $actualResult = $this->view->render(); + $actualResult = $subject->render(); self::assertSame($expectedResult, $actualResult); } #[Test] public function renderOnlyRendersVariableWithTheNameValue(): void { - $this->view + $subject = new JsonView(); + $subject ->assign('value', 'Value') ->assign('someOtherVariable', 'Foo'); $expectedResult = '"Value"'; - $actualResult = $this->view->render(); + $actualResult = $subject->render(); self::assertSame($expectedResult, $actualResult); } @@ -567,25 +538,27 @@ public function renderOnlyRendersVariableWithTheNameValue(): void public function setVariablesToRenderOverridesValueToRender(): void { $value = 'Foo'; - $this->view->assign('foo', $value); - $this->view->setVariablesToRender(['foo']); + $subject = new JsonView(); + $subject->assign('foo', $value); + $subject->setVariablesToRender(['foo']); $expectedResult = '"Foo"'; - $actualResult = $this->view->render(); + $actualResult = $subject->render(); self::assertSame($expectedResult, $actualResult); } #[Test] public function renderRendersMultipleValuesIfTheyAreSpecifiedAsVariablesToRender(): void { - $this->view + $subject = new JsonView(); + $subject ->assign('value', 'Value1') ->assign('secondValue', 'Value2') ->assign('someOtherVariable', 'Value3'); - $this->view->setVariablesToRender(['value', 'secondValue']); + $subject->setVariablesToRender(['value', 'secondValue']); $expectedResult = '{"value":"Value1","secondValue":"Value2"}'; - $actualResult = $this->view->render(); + $actualResult = $subject->render(); self::assertSame($expectedResult, $actualResult); } @@ -596,14 +569,15 @@ public function renderCanRenderMultipleComplexObjects(): void $object = new \stdClass(); $object->foo = 'Foo'; - $this->view + $subject = new JsonView(); + $subject ->assign('array', $array) ->assign('object', $object) ->assign('someOtherVariable', 'Value3'); - $this->view->setVariablesToRender(['array', 'object']); + $subject->setVariablesToRender(['array', 'object']); $expectedResult = '{"array":{"foo":{"bar":"Baz"}},"object":{"foo":"Foo"}}'; - $actualResult = $this->view->render(); + $actualResult = $subject->render(); self::assertSame($expectedResult, $actualResult); } @@ -612,8 +586,9 @@ public function renderCanRenderPlainArray(): void { $array = [['name' => 'Foo', 'secret' => true], ['name' => 'Bar', 'secret' => true]]; - $this->view->assign('value', $array); - $this->view->setConfiguration([ + $subject = new JsonView(); + $subject->assign('value', $array); + $subject->setConfiguration([ 'value' => [ '_descendAll' => [ '_only' => ['name'], @@ -622,7 +597,7 @@ public function renderCanRenderPlainArray(): void ]); $expectedResult = '[{"name":"Foo"},{"name":"Bar"}]'; - $actualResult = $this->view->render(); + $actualResult = $subject->render(); self::assertSame($expectedResult, $actualResult); } @@ -636,8 +611,9 @@ public function renderCanRenderPlainArrayWithNumericKeys(): void ], ]; - $this->view->assign('value', $array); - $this->view->setConfiguration([ + $subject = new JsonView(); + $subject->assign('value', $array); + $subject->setConfiguration([ 'value' => [ 'items' => [ // note: this exclude is just here, and should have no effect as the items have numeric keys @@ -647,7 +623,7 @@ public function renderCanRenderPlainArrayWithNumericKeys(): void ]); $expectedResult = '{"items":[{"name":"Foo"},{"name":"Bar"}]}'; - $actualResult = $this->view->render(); + $actualResult = $subject->render(); self::assertSame($expectedResult, $actualResult); } @@ -656,8 +632,9 @@ public function descendAllKeepsArrayIndexes(): void { $array = [['name' => 'Foo', 'secret' => true], ['name' => 'Bar', 'secret' => true]]; - $this->view->assign('value', $array); - $this->view->setConfiguration([ + $subject = new JsonView(); + $subject->assign('value', $array); + $subject->setConfiguration([ 'value' => [ '_descendAll' => [ '_descendAll' => [], @@ -666,7 +643,7 @@ public function descendAllKeepsArrayIndexes(): void ]); $expectedResult = '[{"name":"Foo","secret":true},{"name":"Bar","secret":true}]'; - $actualResult = $this->view->render(); + $actualResult = $subject->render(); self::assertSame($expectedResult, $actualResult); } } diff --git a/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/BackendTest.php b/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/BackendTest.php index 651f04c923c6..4d58bfa5b779 100644 --- a/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/BackendTest.php +++ b/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/BackendTest.php @@ -101,17 +101,11 @@ public function getIdentifierByObjectReturnsIdentifierForNonLazyObject(): void { $fakeUuid = 'fakeUuid'; $configurationManager = $this->createMock(ConfigurationManagerInterface::class); - $session = $this->getMockBuilder(\stdClass::class) - ->addMethods(['getIdentifierByObject']) - ->disableOriginalConstructor() - ->getMock(); + $session = $this->createMock(Session::class); $object = new \stdClass(); - $session->expects(self::once())->method('getIdentifierByObject')->with($object)->willReturn($fakeUuid); - $backend = $this->getAccessibleMock(Backend::class, null, [$configurationManager], '', false); $backend->_set('session', $session); - self::assertEquals($backend->getIdentifierByObject($object), $fakeUuid); } @@ -120,23 +114,13 @@ public function getIdentifierByObjectReturnsIdentifierForLazyObject(): void { $fakeUuid = 'fakeUuid'; $configurationManager = $this->createMock(ConfigurationManagerInterface::class); - $proxy = $this->getMockBuilder(LazyLoadingProxy::class) - ->onlyMethods(['_loadRealInstance']) - ->disableOriginalConstructor() - ->disableProxyingToOriginalMethods() - ->getMock(); - $session = $this->getMockBuilder(\stdClass::class) - ->addMethods(['getIdentifierByObject']) - ->disableOriginalConstructor() - ->getMock(); + $proxy = $this->createMock(LazyLoadingProxy::class); + $session = $this->createMock(Session::class); $object = new \stdClass(); - $proxy->expects(self::once())->method('_loadRealInstance')->willReturn($object); $session->expects(self::once())->method('getIdentifierByObject')->with($object)->willReturn($fakeUuid); - $backend = $this->getAccessibleMock(Backend::class, null, [$configurationManager], '', false); $backend->_set('session', $session); - self::assertEquals($backend->getIdentifierByObject($proxy), $fakeUuid); } } diff --git a/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/Fixtures/TearDownableBackendInterface.php b/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/Fixtures/TearDownableBackendInterface.php new file mode 100644 index 000000000000..f10a32c3e5db --- /dev/null +++ b/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/Fixtures/TearDownableBackendInterface.php @@ -0,0 +1,23 @@ +getMockBuilder(BackendInterface::class) - ->onlyMethods(get_class_methods(BackendInterface::class)) - ->addMethods(['tearDown']) - ->getMock(); + $mockBackend = $this->createMock(TearDownableBackendInterface::class); $mockBackend->expects(self::once())->method('tearDown'); - $persistenceManager = new PersistenceManager( $this->createMock(QueryFactoryInterface::class), $mockBackend, $this->createMock(Session::class) ); - $persistenceManager->tearDown(); } diff --git a/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/QueryFactoryTest.php b/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/QueryFactoryTest.php index c24923b5ff7a..10bbb134d591 100644 --- a/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/QueryFactoryTest.php +++ b/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/QueryFactoryTest.php @@ -19,7 +19,6 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; -use PHPUnit\Framework\MockObject\MockObject; use Psr\Container\ContainerInterface; use TYPO3\CMS\Core\Context\Context; use TYPO3\CMS\Core\Utility\GeneralUtility; @@ -34,36 +33,7 @@ final class QueryFactoryTest extends UnitTestCase { - protected string $className = 'Vendor\\Ext\\Domain\\Model\\ClubMate'; - protected QueryFactory $queryFactory; - protected ContainerInterface $container; - protected DataMapFactory&MockObject $dataMapFactory; - protected DataMap&MockObject $dataMap; - - protected function setUp(): void - { - parent::setUp(); - - $this->container = $this->createMock(ContainerInterface::class); - - $this->dataMap = $this->getMockBuilder(DataMap::class) - ->onlyMethods(['getIsStatic', 'getRootLevel']) - ->setConstructorArgs(['Vendor\\Ext\\Domain\\Model\\ClubMate', 'tx_ext_domain_model_clubmate']) - ->getMock(); - - $this->dataMapFactory = $this->getMockBuilder(DataMapFactory::class) - ->disableOriginalConstructor() - ->onlyMethods(['buildDataMap']) - ->addMethods(['convertClassNameToTableName']) - ->getMock(); - $this->dataMapFactory->method('buildDataMap')->willReturn($this->dataMap); - - $this->queryFactory = new QueryFactory( - $this->createMock(ConfigurationManagerInterface::class), - $this->dataMapFactory, - $this->container - ); - } + private string $className = 'Vendor\\Ext\\Domain\\Model\\ClubMate'; public static function getStaticAndRootLevelAndExpectedResult(): array { @@ -75,30 +45,34 @@ public static function getStaticAndRootLevelAndExpectedResult(): array ]; } - /** - * @param bool $static - * @param bool $rootLevel - * @param bool $expectedResult - */ #[DataProvider('getStaticAndRootLevelAndExpectedResult')] #[Test] - public function createDoesNotRespectStoragePageIfStaticOrRootLevelIsTrue($static, $rootLevel, $expectedResult): void + public function createDoesNotRespectStoragePageIfStaticOrRootLevelIsTrue(bool $static, bool $rootLevel, bool $expectedResult): void { - $this->dataMap->method('getIsStatic')->willReturn($static); - $this->dataMap->method('getRootLevel')->willReturn($rootLevel); - + $container = $this->createMock(ContainerInterface::class); + $dataMap = $this->getMockBuilder(DataMap::class) + ->onlyMethods(['getIsStatic', 'getRootLevel']) + ->setConstructorArgs(['Vendor\\Ext\\Domain\\Model\\ClubMate', 'tx_ext_domain_model_clubmate']) + ->getMock(); + $dataMapFactoryMock = $this->createMock(DataMapFactory::class); + $dataMapFactoryMock->method('buildDataMap')->willReturn($dataMap); + $queryFactory = new QueryFactory( + $this->createMock(ConfigurationManagerInterface::class), + $dataMapFactoryMock, + $container + ); + $dataMap->method('getIsStatic')->willReturn($static); + $dataMap->method('getRootLevel')->willReturn($rootLevel); $query = $this->createMock(QueryInterface::class); $querySettings = new Typo3QuerySettings( new Context(), $this->createMock(ConfigurationManagerInterface::class) ); GeneralUtility::addInstance(QuerySettingsInterface::class, $querySettings); - $this->container->method('has')->willReturn(true); - $this->container->expects(self::once())->method('get')->with(QueryInterface::class)->willReturn($query); - + $container->method('has')->willReturn(true); + $container->expects(self::once())->method('get')->with(QueryInterface::class)->willReturn($query); $query->expects(self::once())->method('setQuerySettings')->with($querySettings); - $this->queryFactory->create($this->className); - + $queryFactory->create($this->className); self::assertSame( $expectedResult, $querySettings->getRespectStoragePage() diff --git a/typo3/sysext/extbase/Tests/Unit/Utility/DebuggerUtilityTest.php b/typo3/sysext/extbase/Tests/Unit/Utility/DebuggerUtilityTest.php index 5b47a4b94c9a..c7e1759e00d4 100644 --- a/typo3/sysext/extbase/Tests/Unit/Utility/DebuggerUtilityTest.php +++ b/typo3/sysext/extbase/Tests/Unit/Utility/DebuggerUtilityTest.php @@ -29,9 +29,7 @@ final class DebuggerUtilityTest extends UnitTestCase #[Test] public function debuggerRewindsInstancesOfIterator(): void { - $objectStorage = $this->getMockBuilder(ObjectStorage::class) - ->addMethods(['dummy']) - ->getMock(); + $objectStorage = $this->getMockBuilder(ObjectStorage::class)->onlyMethods([])->getMock(); for ($i = 0; $i < 5; $i++) { $obj = new \stdClass(); $obj->property = $i; diff --git a/typo3/sysext/fluid/Tests/Unit/Core/Rendering/RenderingContextTest.php b/typo3/sysext/fluid/Tests/Unit/Core/Rendering/RenderingContextTest.php index 064587d73a6c..55eb9a4b45bb 100644 --- a/typo3/sysext/fluid/Tests/Unit/Core/Rendering/RenderingContextTest.php +++ b/typo3/sysext/fluid/Tests/Unit/Core/Rendering/RenderingContextTest.php @@ -29,58 +29,25 @@ final class RenderingContextTest extends UnitTestCase { - /** - * Parsing state - * - * @var RenderingContext - */ - protected $renderingContext; - - protected function setUp(): void - { - parent::setUp(); - $this->renderingContext = $this->getMockBuilder(RenderingContext::class) - ->addMethods(['dummy']) - ->disableOriginalConstructor() - ->getMock(); - } - #[Test] public function templateVariableContainerCanBeReadCorrectly(): void { $templateVariableContainer = $this->createMock(StandardVariableProvider::class); - $this->renderingContext->setVariableProvider($templateVariableContainer); - self::assertSame($this->renderingContext->getVariableProvider(), $templateVariableContainer, 'Template Variable Container could not be read out again.'); + $subject = $this->getMockBuilder(RenderingContext::class)->onlyMethods([])->disableOriginalConstructor()->getMock(); + $subject->setVariableProvider($templateVariableContainer); + self::assertSame($subject->getVariableProvider(), $templateVariableContainer, 'Template Variable Container could not be read out again.'); } #[Test] public function viewHelperVariableContainerCanBeReadCorrectly(): void { $viewHelperVariableContainer = $this->createMock(ViewHelperVariableContainer::class); - $this->renderingContext->setViewHelperVariableContainer($viewHelperVariableContainer); - self::assertSame($viewHelperVariableContainer, $this->renderingContext->getViewHelperVariableContainer()); - } - - /** - * @param string $input - * @param string $expected - */ - #[DataProvider('getControllerActionTestValues')] - #[Test] - public function setControllerActionProcessesInputCorrectly($input, $expected): void - { - $subject = $this->getMockBuilder(RenderingContext::class) - ->addMethods(['dummy']) - ->disableOriginalConstructor() - ->getMock(); - $serverRequest = (new ServerRequest())->withAttribute('extbase', new ExtbaseRequestParameters()); - $request = new Request($serverRequest); - $subject->setRequest($request); - $subject->setControllerAction($input); - self::assertSame(lcfirst($expected), $subject->getControllerAction()); + $subject = $this->getMockBuilder(RenderingContext::class)->onlyMethods([])->disableOriginalConstructor()->getMock(); + $subject->setViewHelperVariableContainer($viewHelperVariableContainer); + self::assertSame($viewHelperVariableContainer, $subject->getViewHelperVariableContainer()); } - public static function getControllerActionTestValues(): array + public static function setControllerActionProcessesInputCorrectlyDataProvider(): array { return [ ['default', 'default'], @@ -91,4 +58,16 @@ public static function getControllerActionTestValues(): array ['Sub/Default.sub.html', 'Sub/Default'], ]; } + + #[DataProvider('setControllerActionProcessesInputCorrectlyDataProvider')] + #[Test] + public function setControllerActionProcessesInputCorrectly(string $input, string $expected): void + { + $subject = $this->getMockBuilder(RenderingContext::class)->onlyMethods([])->disableOriginalConstructor()->getMock(); + $serverRequest = (new ServerRequest())->withAttribute('extbase', new ExtbaseRequestParameters()); + $request = new Request($serverRequest); + $subject->setRequest($request); + $subject->setControllerAction($input); + self::assertSame(lcfirst($expected), $subject->getControllerAction()); + } } diff --git a/typo3/sysext/frontend/Tests/Unit/ContentObject/CaseContentObjectTest.php b/typo3/sysext/frontend/Tests/Unit/ContentObject/CaseContentObjectTest.php index aa1d575ac350..b16e24348c99 100644 --- a/typo3/sysext/frontend/Tests/Unit/ContentObject/CaseContentObjectTest.php +++ b/typo3/sysext/frontend/Tests/Unit/ContentObject/CaseContentObjectTest.php @@ -38,7 +38,7 @@ protected function setUp(): void { parent::setUp(); $tsfe = $this->getMockBuilder(TypoScriptFrontendController::class) - ->addMethods(['dummy']) + ->onlyMethods([]) ->disableOriginalConstructor() ->getMock(); diff --git a/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php b/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php index f80b9f57d703..c6a4e32e7518 100644 --- a/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php +++ b/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php @@ -3199,7 +3199,7 @@ public static function stdWrapBrDataProvider(): array #[Test] public function stdWrap_br(string $expected, string $input, ?string $doctype): void { - $pageRenderer = $this->getMockBuilder(PageRenderer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock(); + $pageRenderer = $this->getMockBuilder(PageRenderer::class)->disableOriginalConstructor()->onlyMethods([])->getMock(); $pageRenderer->setLanguage(new Locale()); $pageRenderer->setDocType(DocType::createFromConfigurationKey($doctype)); GeneralUtility::setSingletonInstance(PageRenderer::class, $pageRenderer); diff --git a/typo3/sysext/frontend/Tests/Unit/ContentObject/ImageContentObjectTest.php b/typo3/sysext/frontend/Tests/Unit/ContentObject/ImageContentObjectTest.php index 64f33556c1ae..07871f2410a0 100644 --- a/typo3/sysext/frontend/Tests/Unit/ContentObject/ImageContentObjectTest.php +++ b/typo3/sysext/frontend/Tests/Unit/ContentObject/ImageContentObjectTest.php @@ -67,7 +67,7 @@ protected function setUp(): void ]); $this->subject->setRequest(new ServerRequest()); $this->subject->setContentObjectRenderer($contentObjectRenderer); - $pageRenderer = $this->getMockBuilder(PageRenderer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock(); + $pageRenderer = $this->getMockBuilder(PageRenderer::class)->disableOriginalConstructor()->onlyMethods([])->getMock(); $this->subject->_set('pageRenderer', $pageRenderer); } @@ -366,7 +366,7 @@ public function getImageSourceCollectionRendersDefinedLayoutKeyData( $cObj->start([], 'tt_content'); $file = 'testImageName'; - $pageRenderer = $this->getMockBuilder(PageRenderer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock(); + $pageRenderer = $this->getMockBuilder(PageRenderer::class)->disableOriginalConstructor()->onlyMethods([])->getMock(); $pageRenderer->setLanguage(new Locale()); $pageRenderer->setDocType(DocType::createFromConfigurationKey($doctype)); GeneralUtility::setSingletonInstance(PageRenderer::class, $pageRenderer); diff --git a/typo3/sysext/frontend/Tests/Unit/Processor/GalleryProcessorTest.php b/typo3/sysext/frontend/Tests/Unit/Processor/GalleryProcessorTest.php index 65d394baa531..cc60dc47100f 100644 --- a/typo3/sysext/frontend/Tests/Unit/Processor/GalleryProcessorTest.php +++ b/typo3/sysext/frontend/Tests/Unit/Processor/GalleryProcessorTest.php @@ -38,7 +38,7 @@ protected function setUp(): void { parent::setUp(); $this->contentObjectRenderer = $this->getMockBuilder(ContentObjectRenderer::class) - ->addMethods(['dummy']) + ->onlyMethods([]) ->getMock(); } diff --git a/typo3/sysext/scheduler/Tests/Unit/Task/CachingFrameworkGarbageCollectionTest.php b/typo3/sysext/scheduler/Tests/Unit/Task/CachingFrameworkGarbageCollectionTest.php index 14dbcef87510..02e66b10db08 100644 --- a/typo3/sysext/scheduler/Tests/Unit/Task/CachingFrameworkGarbageCollectionTest.php +++ b/typo3/sysext/scheduler/Tests/Unit/Task/CachingFrameworkGarbageCollectionTest.php @@ -46,7 +46,7 @@ public function executeCallsCollectGarbageOfConfiguredBackend(): void ], ]; $subject = $this->getMockBuilder(CachingFrameworkGarbageCollectionTask::class) - ->addMethods(['dummy']) + ->onlyMethods([]) ->disableOriginalConstructor() ->getMock(); $subject->selectedBackends = [AbstractBackend::class]; @@ -72,7 +72,7 @@ public function executeDoesNotCallCollectGarbageOfNotConfiguredBackend(): void ], ]; $subject = $this->getMockBuilder(CachingFrameworkGarbageCollectionTask::class) - ->addMethods(['dummy']) + ->onlyMethods([]) ->disableOriginalConstructor() ->getMock(); $subject->selectedBackends = [NullBackend::class];