Skip to content

Commit

Permalink
[TASK] Avoid phpunit addMethods()
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Anja Leichsenring <[email protected]>
Reviewed-by: Oliver Klee <[email protected]>
Tested-by: Oliver Klee <[email protected]>
Tested-by: core-ci <[email protected]>
Reviewed-by: Stefan Bürk <[email protected]>
Tested-by: Stefan Bürk <[email protected]>
  • Loading branch information
lolli42 authored and maddy2101 committed Feb 27, 2024
1 parent ba4a911 commit 09789e6
Show file tree
Hide file tree
Showing 24 changed files with 181 additions and 357 deletions.
15 changes: 0 additions & 15 deletions Build/phpstan/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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']);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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']);
Expand All @@ -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());
Expand All @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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', []);
Expand All @@ -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);
Expand All @@ -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);
}

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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';
Expand Down Expand Up @@ -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();
Expand All @@ -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';
Expand All @@ -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';
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function logoffCleansFormProtectionIfBackendUserIsLoggedIn(): void
$GLOBALS['BE_USER']->initializeUserSessionManager($userSessionManager);

$subject = $this->getMockBuilder(BackendUserAuthentication::class)
->addMethods(['dummy'])
->onlyMethods([])
->disableOriginalConstructor()
->getMock();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -79,10 +77,7 @@ public function echoExceptionWebEscapesExceptionTitle(): void
$typo3InformationMock->method('getCopyrightYear')->willReturn('1999-20XX');
GeneralUtility::addInstance(Typo3Information::class, $typo3InformationMock);
$title = '<b>b</b><script>alert(1);</script>';
$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);
Expand All @@ -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 [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/core/Tests/Unit/Log/Writer/FileWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
Loading

0 comments on commit 09789e6

Please sign in to comment.