Skip to content

Commit

Permalink
[TASK] Replace usages of getMockForAbstractClass in EXT:core
Browse files Browse the repository at this point in the history
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

Resolves: #101630
Related: #101601
Releases: main, 12.4
Change-Id: I27f526cb7ee4e1f2081c05befd3d70549dd0e2fd
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80945
Tested-by: core-ci <[email protected]>
Tested-by: Benjamin Franzke <[email protected]>
Reviewed-by: Benjamin Franzke <[email protected]>
  • Loading branch information
oliverklee authored and bnf committed Sep 11, 2023
1 parent d50ac42 commit 70fb8db
Show file tree
Hide file tree
Showing 12 changed files with 444 additions and 50 deletions.
6 changes: 3 additions & 3 deletions Tests/Unit/Log/Processor/AbstractMemoryProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace TYPO3\CMS\Core\Tests\Unit\Log\Processor;

use TYPO3\CMS\Core\Log\Processor\AbstractMemoryProcessor;
use TYPO3\CMS\Core\Tests\Unit\Log\Processor\Fixtures\TestingMemoryProcessor;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

final class AbstractMemoryProcessorTest extends UnitTestCase
Expand All @@ -27,7 +27,7 @@ final class AbstractMemoryProcessorTest extends UnitTestCase
*/
public function setRealMemoryUsageSetsRealMemoryUsage(): void
{
$processor = $this->getMockForAbstractClass(AbstractMemoryProcessor::class);
$processor = new TestingMemoryProcessor();
$processor->setRealMemoryUsage(false);
self::assertFalse($processor->getRealMemoryUsage());
}
Expand All @@ -37,7 +37,7 @@ public function setRealMemoryUsageSetsRealMemoryUsage(): void
*/
public function setFormatSizeSetsFormatSize(): void
{
$processor = $this->getMockForAbstractClass(AbstractMemoryProcessor::class);
$processor = new TestingMemoryProcessor();
$processor->setFormatSize(false);
self::assertFalse($processor->getFormatSize());
}
Expand Down
32 changes: 32 additions & 0 deletions Tests/Unit/Log/Processor/Fixtures/TestingMemoryProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace TYPO3\CMS\Core\Tests\Unit\Log\Processor\Fixtures;

use TYPO3\CMS\Core\Log\LogRecord;
use TYPO3\CMS\Core\Log\Processor\AbstractMemoryProcessor;

/**
* Testing subclass of `AbstractMemoryProcessor`.
*/
final class TestingMemoryProcessor extends AbstractMemoryProcessor
{
public function processLogRecord(LogRecord $logRecord): LogRecord
{
throw new \BadMethodCallException('Not implemented', 1691578434);
}
}
9 changes: 5 additions & 4 deletions Tests/Unit/Resource/AbstractFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

namespace TYPO3\CMS\Core\Tests\Unit\Resource;

use TYPO3\CMS\Core\Resource\AbstractFile;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\FolderInterface;
use TYPO3\CMS\Core\Resource\ResourceStorage;
use TYPO3\CMS\Core\Tests\Unit\Resource\Fixtures\TestingFile;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

/**
Expand All @@ -37,8 +38,8 @@ public function getParentFolderGetsParentFolderFromStorage(): void

$mockedStorageForParent = $this->createMock(ResourceStorage::class);

$parentFolderFixture = $this->getMockForAbstractClass(AbstractFile::class);
$parentFolderFixture->setIdentifier($parentIdentifier)->setStorage($mockedStorageForParent);
$parentFolderFixture = $this->createMock(FolderInterface::class);
$parentFolderFixture->method('getStorage')->willReturn($mockedStorageForParent);

$mockedStorage = $this->getMockBuilder(ResourceStorage::class)
->onlyMethods(['getFolderIdentifierFromFileIdentifier', 'getFolder'])
Expand All @@ -47,7 +48,7 @@ public function getParentFolderGetsParentFolderFromStorage(): void
$mockedStorage->expects(self::once())->method('getFolderIdentifierFromFileIdentifier')->with($currentIdentifier)->willReturn($parentIdentifier);
$mockedStorage->expects(self::once())->method('getFolder')->with($parentIdentifier)->willReturn($parentFolderFixture);

$currentFolderFixture = $this->getMockForAbstractClass(AbstractFile::class);
$currentFolderFixture = new TestingFile();
$currentFolderFixture->setIdentifier($currentIdentifier)->setStorage($mockedStorage);

self::assertSame($parentFolderFixture, $currentFolderFixture->getParentFolder());
Expand Down
16 changes: 8 additions & 8 deletions Tests/Unit/Resource/Collection/FileCollectionRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

namespace TYPO3\CMS\Core\Tests\Unit\Resource\Collection;

use TYPO3\CMS\Core\Resource\Collection\AbstractFileCollection;
use TYPO3\CMS\Core\Resource\Collection\FileCollectionRegistry;
use TYPO3\CMS\Core\Resource\Collection\StaticFileCollection;
use TYPO3\CMS\Core\Tests\Unit\Resource\Collection\Fixtures\TestingFileCollection;
use TYPO3\CMS\Core\Utility\StringUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

Expand All @@ -30,7 +30,7 @@ final class FileCollectionRegistryTest extends UnitTestCase
*/
public function registeredFileCollectionClassesCanBeRetrieved(): void
{
$className = get_class($this->getMockForAbstractClass(AbstractFileCollection::class));
$className = TestingFileCollection::class;
$subject = new FileCollectionRegistry();
$subject->registerFileCollectionClass($className, 'foobar');
$returnedClassName = $subject->getFileCollectionClass('foobar');
Expand All @@ -56,7 +56,7 @@ public function registerFileCollectionClassThrowsExceptionIfTypeIsTooLong(): voi
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1391295611);
$subject = new FileCollectionRegistry();
$className = get_class($this->getMockForAbstractClass(AbstractFileCollection::class));
$className = TestingFileCollection::class;
$type = str_pad('', 40);
$subject->registerFileCollectionClass($className, $type);
}
Expand All @@ -69,7 +69,7 @@ public function registerFileCollectionClassThrowsExceptionIfTypeIsAlreadyRegiste
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1391295643);
$subject = new FileCollectionRegistry();
$className = get_class($this->getMockForAbstractClass(AbstractFileCollection::class));
$className = TestingFileCollection::class;
$className2 = get_class($this->getMockForAbstractClass(StaticFileCollection::class));
$subject->registerFileCollectionClass($className, 'foobar');
$subject->registerFileCollectionClass($className2, 'foobar');
Expand All @@ -80,7 +80,7 @@ public function registerFileCollectionClassThrowsExceptionIfTypeIsAlreadyRegiste
*/
public function registerFileCollectionClassOverridesExistingRegisteredFileCollectionClass(): void
{
$className = get_class($this->getMockForAbstractClass(AbstractFileCollection::class));
$className = TestingFileCollection::class;
$className2 = get_class($this->getMockForAbstractClass(StaticFileCollection::class));
$subject = new FileCollectionRegistry();
$subject->registerFileCollectionClass($className, 'foobar');
Expand All @@ -103,7 +103,7 @@ public function getFileCollectionClassThrowsExceptionIfClassIsNotRegistered(): v
*/
public function getFileCollectionClassAcceptsClassNameIfClassIsRegistered(): void
{
$className = get_class($this->getMockForAbstractClass(AbstractFileCollection::class));
$className = TestingFileCollection::class;
$subject = new FileCollectionRegistry();
$subject->registerFileCollectionClass($className, 'foobar');
self::assertEquals($className, $subject->getFileCollectionClass('foobar'));
Expand All @@ -114,7 +114,7 @@ public function getFileCollectionClassAcceptsClassNameIfClassIsRegistered(): voi
*/
public function fileCollectionRegistryIsInitializedWithPreconfiguredFileCollections(): void
{
$className = get_class($this->getMockForAbstractClass(AbstractFileCollection::class));
$className = TestingFileCollection::class;
$type = substr(StringUtility::getUniqueId('type_'), 0, 30);
$GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredCollections'] = [
$type => $className,
Expand All @@ -128,7 +128,7 @@ public function fileCollectionRegistryIsInitializedWithPreconfiguredFileCollecti
*/
public function fileCollectionExistsReturnsTrueForAllExistingFileCollections(): void
{
$className = get_class($this->getMockForAbstractClass(AbstractFileCollection::class));
$className = TestingFileCollection::class;
$type = 'foo';
$GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredCollections'] = [
$type => $className,
Expand Down
31 changes: 31 additions & 0 deletions Tests/Unit/Resource/Collection/Fixtures/TestingFileCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace TYPO3\CMS\Core\Tests\Unit\Resource\Collection\Fixtures;

use TYPO3\CMS\Core\Resource\Collection\AbstractFileCollection;

/**
* Testing subclass of `AbstractFileCollection`.
*/
final class TestingFileCollection extends AbstractFileCollection
{
public function loadContents(): void
{
// stub
}
}
4 changes: 2 additions & 2 deletions Tests/Unit/Resource/Driver/AbstractDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace TYPO3\CMS\Core\Tests\Unit\Resource\Driver;

use TYPO3\CMS\Core\Resource\Driver\AbstractDriver;
use TYPO3\CMS\Core\Tests\Unit\Resource\Driver\Fixtures\TestingDriver;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

final class AbstractDriverTest extends UnitTestCase
Expand All @@ -27,7 +27,7 @@ final class AbstractDriverTest extends UnitTestCase
*/
public function isCaseSensitiveFileSystemReturnsTrueIfNothingIsConfigured(): void
{
$subject = $this->getMockForAbstractClass(AbstractDriver::class, [], '', false);
$subject = new TestingDriver();
self::assertTrue($subject->isCaseSensitiveFileSystem());
}
}
3 changes: 2 additions & 1 deletion Tests/Unit/Resource/Driver/DriverRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use TYPO3\CMS\Core\Resource\Driver\DriverInterface;
use TYPO3\CMS\Core\Resource\Driver\DriverRegistry;
use TYPO3\CMS\Core\Tests\Unit\Resource\Driver\Fixtures\TestingDriver;
use TYPO3\CMS\Core\Utility\StringUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

Expand Down Expand Up @@ -55,7 +56,7 @@ public function registerDriverClassThrowsExceptionIfShortnameIsAlreadyTakenByAno
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1314979451);
$className = get_class($this->createMock(DriverInterface::class));
$className2 = get_class($this->getMockForAbstractClass(DriverInterface::class));
$className2 = TestingDriver::class;
$subject = new DriverRegistry();
$subject->registerDriverClass($className, 'foobar');
$subject->registerDriverClass($className2, 'foobar');
Expand Down
Loading

0 comments on commit 70fb8db

Please sign in to comment.