[TASK] Mock ResourceFactory Singletons in unit tests 54/59754/4
authorBenjamin Franzke <bfr@qbus.de>
Mon, 18 Feb 2019 13:54:49 +0000 (14:54 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Sat, 23 Feb 2019 16:38:01 +0000 (17:38 +0100)
This prevents indirect SignalSlotDispatcher and ObjectManager
invocation by stubbing the ResourceFactory (or removing unneeded
mocks by preventing constructor invocation as in ImageServiceTest).

Releases: master, 9.5
Resolves: #87739
Change-Id: I70c5413fbdf66f3deb8ae0113e62f17ad8168f19
Reviewed-on: https://review.typo3.org/c/59754
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/core/Tests/Unit/Resource/ResourceStorageTest.php
typo3/sysext/extbase/Tests/Unit/Service/ImageScriptServiceTest.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/ImageViewHelperTest.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php
typo3/sysext/frontend/Tests/Unit/ContentObject/FilesContentObjectTest.php

index c1ebb56..0a0c4d1 100644 (file)
@@ -75,6 +75,7 @@ class ResourceStorageTest extends BaseTestCase
      * @param array $configuration
      * @param bool $mockPermissionChecks
      * @param AbstractDriver|\PHPUnit_Framework_MockObject_MockObject $driverObject
+     * @param ResourceFactory $resourceFactory
      * @param array $storageRecord
      * @param array $mockedMethods
      */
@@ -82,6 +83,7 @@ class ResourceStorageTest extends BaseTestCase
         array $configuration,
         bool $mockPermissionChecks = false,
         AbstractDriver $driverObject = null,
+        ResourceFactory $resourceFactory = null,
         array $storageRecord = [],
         array $mockedMethods = []
     ): void {
@@ -100,6 +102,10 @@ class ResourceStorageTest extends BaseTestCase
         if ($driverObject === null) {
             $driverObject = $this->getMockForAbstractClass(AbstractDriver::class, [], '', false);
         }
+        if ($resourceFactory === null) {
+            $resourceFactory = $this->createMock(ResourceFactory::class);
+        }
+        $mockedMethods[] = 'getResourceFactoryInstance';
         if ($mockPermissionChecks) {
             $mockedMethods = array_merge($mockedMethods, $permissionMethods);
         }
@@ -109,6 +115,7 @@ class ResourceStorageTest extends BaseTestCase
             ->setMethods(array_unique($mockedMethods))
             ->setConstructorArgs([$driverObject, $storageRecord])
             ->getMock();
+        $this->subject->expects($this->any())->method('getResourceFactoryInstance')->will($this->returnValue($resourceFactory));
         $this->subject->expects($this->any())->method('getIndexer')->will($this->returnValue($this->createMock(Indexer::class)));
         if ($mockPermissionChecks) {
             foreach ($permissionMethods as $method) {
@@ -241,7 +248,7 @@ class ResourceStorageTest extends BaseTestCase
             $this->subject,
             null
         );
-        $this->prepareSubject([], false, $mockedDriver, $storageRecord);
+        $this->prepareSubject([], false, $mockedDriver, null, $storageRecord);
         $this->assertEquals(
             $capabilities['public'],
             $this->subject->isPublic(),
@@ -295,12 +302,14 @@ class ResourceStorageTest extends BaseTestCase
         $driver = $this->getMockBuilder(LocalDriver::class)
             ->setConstructorArgs([['basePath' => $this->getMountRootUrl()]])
             ->getMock();
+        $mockedResourceFactory = $this->createMock(ResourceFactory::class);
         /** @var $subject ResourceStorage|\PHPUnit_Framework_MockObject_MockObject */
         $subject = $this->getMockBuilder(ResourceStorage::class)
-            ->setMethods(['isOnline'])
+            ->setMethods(['isOnline', 'getResourceFactoryInstance'])
             ->setConstructorArgs([$driver, ['configuration' => []]])
             ->getMock();
         $subject->expects($this->once())->method('isOnline')->will($this->returnValue(false));
+        $subject->expects($this->any())->method('getResourceFactoryInstance')->will($this->returnValue($mockedResourceFactory));
 
         $sourceFileIdentifier = '/sourceFile.ext';
         $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
@@ -349,17 +358,19 @@ class ResourceStorageTest extends BaseTestCase
         /** @var $mockedDriver LocalDriver|\PHPUnit_Framework_MockObject_MockObject */
         $mockedDriver = $this->createMock(LocalDriver::class);
         $mockedDriver->expects($this->any())->method('getPermissions')->will($this->returnValue($permissionsFromDriver));
+        $mockedResourceFactory = $this->createMock(ResourceFactory::class);
         /** @var $mockedFolder Folder|\PHPUnit_Framework_MockObject_MockObject */
         $mockedFolder = $this->createMock(Folder::class);
         // Let all other checks pass
         /** @var $subject ResourceStorage|\PHPUnit_Framework_MockObject_MockObject */
         $subject = $this->getMockBuilder(ResourceStorage::class)
-            ->setMethods(['isWritable', 'isBrowsable', 'checkUserActionPermission'])
+            ->setMethods(['isWritable', 'isBrowsable', 'checkUserActionPermission', 'getResourceFactoryInstance'])
             ->setConstructorArgs([$mockedDriver, []])
             ->getMock();
         $subject->expects($this->any())->method('isWritable')->will($this->returnValue(true));
         $subject->expects($this->any())->method('isBrowsable')->will($this->returnValue(true));
         $subject->expects($this->any())->method('checkUserActionPermission')->will($this->returnValue(true));
+        $subject->expects($this->any())->method('getResourceFactoryInstance')->will($this->returnValue($mockedResourceFactory));
         $subject->setDriver($mockedDriver);
 
         $this->assertSame($expectedResult, $subject->checkFolderActionPermission($action, $mockedFolder));
@@ -449,7 +460,7 @@ class ResourceStorageTest extends BaseTestCase
      */
     public function metaDataEditIsNotAllowedWhenWhenNoFileMountsAreSet(): void
     {
-        $this->prepareSubject([], false, null, [], ['isWithinProcessingFolder']);
+        $this->prepareSubject([], false, null, null, [], ['isWithinProcessingFolder']);
         $this->subject->setEvaluatePermissions(true);
         $this->assertFalse($this->subject->checkFileActionPermission('editMeta', new File(['identifier' => '/foo/bar.jpg'], $this->subject)));
     }
@@ -460,7 +471,7 @@ class ResourceStorageTest extends BaseTestCase
     public function metaDataEditIsAllowedWhenWhenInFileMount(): void
     {
         $driverMock = $this->getMockForAbstractClass(AbstractDriver::class, [], '', false);
-        $this->prepareSubject([], false, $driverMock, [], ['isWithinProcessingFolder']);
+        $this->prepareSubject([], false, $driverMock, null, [], ['isWithinProcessingFolder']);
 
         $fileStub = new File(['identifier' => '/foo/bar.jpg'], $this->subject);
         $folderStub = new Folder($this->subject, '/foo/', 'foo');
@@ -487,7 +498,7 @@ class ResourceStorageTest extends BaseTestCase
     public function metaDataEditIsNotAllowedWhenWhenInReadOnlyFileMount(): void
     {
         $driverMock = $this->getMockForAbstractClass(AbstractDriver::class, [], '', false);
-        $this->prepareSubject([], false, $driverMock, [], ['isWithinProcessingFolder']);
+        $this->prepareSubject([], false, $driverMock, null, [], ['isWithinProcessingFolder']);
 
         $fileStub = new File(['identifier' => '/foo/bar.jpg'], $this->subject);
         $folderStub = new Folder($this->subject, '/foo/', 'foo');
@@ -784,7 +795,7 @@ class ResourceStorageTest extends BaseTestCase
     {
         $mockedDriver = $this->createDriverMock([], $this->subject);
         $mockedDriver->expects($this->once())->method('renameFile')->will($this->returnValue('bar'));
-        $this->prepareSubject([], true, $mockedDriver, [], ['emitPreFileRenameSignal', 'emitPostFileRenameSignal']);
+        $this->prepareSubject([], true, $mockedDriver, null, [], ['emitPreFileRenameSignal', 'emitPostFileRenameSignal']);
         /** @var File $file */
         $file = new File(['identifier' => 'foo', 'name' => 'foo'], $this->subject);
         $result = $this->subject->renameFile($file, 'bar');
@@ -808,16 +819,19 @@ class ResourceStorageTest extends BaseTestCase
             'bar',
             'bar_01'
         ));
+        $resourceFactory = $this->createMock(ResourceFactory::class);
         $this->prepareSubject(
             [],
             true,
             $mockedDriver,
+            $resourceFactory,
             [],
             ['emitPreFileRenameSignal', 'emitPostFileRenameSignal', 'getUniqueName']
         );
+        $resourceFactory->expects($this->once())->method('createFolderObject')->will($this->returnValue(new Folder($this->subject, '', '')));
         /** @var File $file */
         $file = new File(['identifier' => 'foo', 'name' => 'foo'], $this->subject);
-        $this->subject->expects($this->once())->method('getUniqueName')->will($this->returnValue('bar_01'));
+        $this->subject->expects($this->any())->method('getUniqueName')->will($this->returnValue('bar_01'));
         $result = $this->subject->renameFile($file, 'bar');
         // fake what the indexer does in updateIndexEntry
         $result->updateProperties(['name' => $result->getIdentifier()]);
@@ -834,7 +848,7 @@ class ResourceStorageTest extends BaseTestCase
             'foo',
             1489593099
         )));
-        $this->prepareSubject([], true, $mockedDriver, [], ['emitPreFileRenameSignal', 'emitPostFileRenameSignal']);
+        $this->prepareSubject([], true, $mockedDriver, null, [], ['emitPreFileRenameSignal', 'emitPostFileRenameSignal']);
         /** @var File $file */
         $file = new File(['identifier' => 'foo', 'name' => 'foo'], $this->subject);
         $this->expectException(ExistingTargetFileNameException::class);
@@ -852,19 +866,17 @@ class ResourceStorageTest extends BaseTestCase
             1489593098
         )));
         $mockedDriver->expects($this->any())->method('sanitizeFileName')->will($this->returnValue('bar'));
-        $this->prepareSubject([], true, $mockedDriver, [], [
+        $resourceFactory = $this->prophesize(ResourceFactory::class);
+        $this->prepareSubject([], true, $mockedDriver, $resourceFactory->reveal(), [], [
             'emitPreFileRenameSignal',
             'emitPostFileRenameSignal',
             'replaceFile',
             'getPublicUrl',
-            'getResourceFactoryInstance'
         ]);
         $this->subject->expects($this->once())->method('getPublicUrl')->will($this->returnValue('somePath'));
-        $resourceFactory = $this->prophesize(ResourceFactory::class);
         $file = $this->prophesize(FileInterface::class);
         $resourceFactory->getFileObjectFromCombinedIdentifier(Argument::any())->willReturn($file->reveal());
         $this->subject->expects($this->once())->method('replaceFile')->will($this->returnValue($file->reveal()));
-        $this->subject->expects($this->any())->method('getResourceFactoryInstance')->will(self::returnValue($resourceFactory->reveal()));
         /** @var File $file */
         $file = new File(['identifier' => 'foo', 'name' => 'foo', 'missing' => false], $this->subject);
         $this->subject->renameFile($file, 'bar', DuplicationBehavior::REPLACE);
index a5ac9b8..d0f7e24 100644 (file)
@@ -18,6 +18,7 @@ namespace TYPO3\CMS\Extbase\Tests\Unit\Service;
 use TYPO3\CMS\Core\Resource\File;
 use TYPO3\CMS\Core\Resource\FileReference;
 use TYPO3\CMS\Core\Resource\ProcessedFile;
+use TYPO3\CMS\Core\Resource\ResourceFactory;
 use TYPO3\CMS\Extbase\Service\EnvironmentService;
 use TYPO3\CMS\Extbase\Service\ImageService;
 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
@@ -48,7 +49,8 @@ class ImageScriptServiceTest extends UnitTestCase
     protected function setUp(): void
     {
         $this->environmentService = $this->createMock(EnvironmentService::class);
-        $this->subject = new ImageService($this->environmentService);
+        $resourceFactory = $this->createMock(ResourceFactory::class);
+        $this->subject = new ImageService($this->environmentService, $resourceFactory);
         $_SERVER['HTTP_HOST'] = 'foo.bar';
     }
 
index 0e12c97..08a03fb 100644 (file)
@@ -156,9 +156,7 @@ class ImageViewHelperTest extends ViewHelperBaseTestcase
 
         $this->inject($image, 'originalFile', $originalFile);
         $this->inject($image, 'propertiesOfFileReference', []);
-        $imageService = $this->getMockBuilder(ImageService::class)
-            ->setMethods(['getImage', 'applyProcessingInstructions', 'getImageUri'])
-            ->getMock();
+        $imageService = $this->createMock(ImageService::class);
         $imageService->expects($this->once())->method('getImage')->willReturn($image);
         $imageService->expects($this->once())->method('applyProcessingInstructions')->with($image, $this->anything())->willReturn($processedFile);
         $imageService->expects($this->once())->method('getImageUri')->with($processedFile)->willReturn('test.png');
index e559250..becb7b1 100644 (file)
@@ -3064,6 +3064,9 @@ class ContentObjectRendererTest extends UnitTestCase
         $typoScriptFrontendControllerMockObject->tmpl = $templateServiceObjectMock;
         $GLOBALS['TSFE'] = $typoScriptFrontendControllerMockObject;
 
+        $resourceFactory = $this->prophesize(ResourceFactory::class);
+        GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactory->reveal());
+
         $this->subject->_set('typoScriptFrontendController', $typoScriptFrontendControllerMockObject);
 
         $this->assertEquals($expectedResult, $this->subject->typoLink($linkText, $configuration));
@@ -3215,6 +3218,9 @@ class ContentObjectRendererTest extends UnitTestCase
                 'parseFunc.' => $this->getLibParseFunc(),
             ],
         ];
+        $resourceFactory = $this->prophesize(ResourceFactory::class);
+        GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactory->reveal());
+
         $typoScriptFrontendControllerMockObject = $this->createMock(TypoScriptFrontendController::class);
         $typoScriptFrontendControllerMockObject->config = [
             'config' => [],
index 5d2faff..e379020 100644 (file)
@@ -424,7 +424,7 @@ class FilesContentObjectTest extends UnitTestCase
             $fileMap[] = [$i, [], $file];
         }
 
-        $resourceFactory = $this->getMockBuilder(ResourceFactory::class)->getMock();
+        $resourceFactory = $this->createMock(ResourceFactory::class);
         $resourceFactory->expects($this->any())
             ->method('getFileObject')
             ->will($this->returnValueMap($fileMap));
@@ -946,7 +946,7 @@ class FilesContentObjectTest extends UnitTestCase
             }
         }
 
-        $resourceFactory = $this->getMockBuilder(ResourceFactory::class)->getMock();
+        $resourceFactory = $this->createMock(ResourceFactory::class);
         $resourceFactory->expects($this->any())
             ->method('getFolderObjectFromCombinedIdentifier')
             ->will($this->returnValueMap($folderMap));