[TASK] Mock SignalSlot\Dispatcher Singletons in unit tests 56/59756/3
authorBenjamin Franzke <bfr@qbus.de>
Mon, 18 Feb 2019 07:59:10 +0000 (08:59 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Sat, 23 Feb 2019 16:33:53 +0000 (17:33 +0100)
Prevent indirect ObjectManager invocation by mocking FileRepository.

Releases: master, 9.5
Resolves: #87741
Change-Id: I4a0c4f322f47223406a313f222f1f5b5e53c6ed7
Reviewed-on: https://review.typo3.org/c/59756
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/backend/Tests/Unit/Utility/BackendUtilityTest.php
typo3/sysext/core/Tests/Unit/Database/Schema/Parser/TableBuilderTest.php
typo3/sysext/core/Tests/Unit/Database/Schema/SqlReaderTest.php
typo3/sysext/core/Tests/Unit/Imaging/IconFactoryTest.php
typo3/sysext/core/Tests/Unit/Resource/MetaDataAspectTest.php
typo3/sysext/core/Tests/Unit/Utility/AccessibleProxies/ExtensionManagementUtilityAccessibleProxy.php
typo3/sysext/core/Tests/Unit/Utility/ExtensionManagementUtilityTest.php

index 5fd971a..8339a4d 100644 (file)
@@ -32,6 +32,7 @@ use TYPO3\CMS\Core\Database\Query\Restriction\DefaultRestrictionContainer;
 use TYPO3\CMS\Core\Database\RelationHandler;
 use TYPO3\CMS\Core\Localization\LanguageService;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\SignalSlot\Dispatcher as SignalSlotDispatcher;
 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
 /**
@@ -1049,6 +1050,10 @@ class BackendUtilityTest extends UnitTestCase
         $cacheProphecy->set(Argument::cetera())->willReturn(false);
         $cacheProphecy->get('backendUtilityBeGetRootLine')->willReturn(['13--1' => []]);
         GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
+        $signalSlotDispatcherProphecy = $this->prophesize(SignalSlotDispatcher::class);
+        $signalSlotDispatcherProphecy->dispatch(Argument::any(), Argument::any(), Argument::type('array'))->willReturnArgument(2);
+        GeneralUtility::setSingletonInstance(SignalSlotDispatcher::class, $signalSlotDispatcherProphecy->reveal());
+
         $result = BackendUtility::getPagesTSconfig($pageId);
         $this->assertEquals($expected, $result);
     }
index 37a940d..a911bea 100644 (file)
@@ -25,6 +25,7 @@ use Doctrine\DBAL\Types\TextType;
 use TYPO3\CMS\Core\Database\Schema\Parser\Parser;
 use TYPO3\CMS\Core\Database\Schema\SqlReader;
 use TYPO3\CMS\Core\Package\PackageManager;
+use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
 /**
@@ -49,8 +50,9 @@ class TableBuilderTest extends UnitTestCase
     {
         parent::setUp();
         $sqlFile = file_get_contents(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'Fixtures', 'tablebuilder.sql']));
+        $signalSlotDispatcherProphecy = $this->prophesize(Dispatcher::class);
         $packageManagerProphecy = $this->prophesize(PackageManager::class);
-        $sqlReader = new SqlReader(null, $packageManagerProphecy->reveal());
+        $sqlReader = new SqlReader($signalSlotDispatcherProphecy->reveal(), $packageManagerProphecy->reveal());
         $statements = $sqlReader->getCreateTableStatementArray($sqlFile);
 
         $parser = new Parser($statements[0]);
index b15bcb6..6c476d4 100644 (file)
@@ -18,6 +18,7 @@ namespace TYPO3\CMS\Core\Tests\Unit\Database\Schema;
 
 use TYPO3\CMS\Core\Database\Schema\SqlReader;
 use TYPO3\CMS\Core\Package\PackageManager;
+use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
 /**
@@ -35,7 +36,7 @@ class SqlReaderTest extends UnitTestCase
      */
     public function getStatementArraySplitsStatements()
     {
-        $subject = new SqlReader(null, $this->prophesize(PackageManager::class)->reveal());
+        $subject = new SqlReader($this->prophesize(Dispatcher::class)->reveal(), $this->prophesize(PackageManager::class)->reveal());
         $result = $subject->getStatementArray(
             'CREATE TABLE aTestTable(' . LF . '  aTestField INT(11)' . LF . ');' .
             LF .
@@ -51,7 +52,7 @@ class SqlReaderTest extends UnitTestCase
      */
     public function getStatementArrayFiltersStatements()
     {
-        $subject = new SqlReader(null, $this->prophesize(PackageManager::class)->reveal());
+        $subject = new SqlReader($this->prophesize(Dispatcher::class)->reveal(), $this->prophesize(PackageManager::class)->reveal());
         $result = $subject->getStatementArray(
             'CREATE TABLE aTestTable(' . LF . '  aTestField INT(11)' . LF . ');' .
             LF .
@@ -67,7 +68,7 @@ class SqlReaderTest extends UnitTestCase
      */
     public function getInsertStatementArrayResult()
     {
-        $subject = new SqlReader(null, $this->prophesize(PackageManager::class)->reveal());
+        $subject = new SqlReader($this->prophesize(Dispatcher::class)->reveal(), $this->prophesize(PackageManager::class)->reveal());
         $result = $subject->getInsertStatementArray(
             'CREATE TABLE aTestTable(' . LF . '  aTestField INT(11)' . LF . ');' .
             LF .
@@ -83,7 +84,7 @@ class SqlReaderTest extends UnitTestCase
      */
     public function getInsertStatementArrayResultWithNewline()
     {
-        $subject = new SqlReader(null, $this->prophesize(PackageManager::class)->reveal());
+        $subject = new SqlReader($this->prophesize(Dispatcher::class)->reveal(), $this->prophesize(PackageManager::class)->reveal());
         $result = $subject->getInsertStatementArray(
             'CREATE TABLE aTestTable(' . LF . '  aTestField INT(11)' . LF . ');' .
             LF .
@@ -101,7 +102,7 @@ class SqlReaderTest extends UnitTestCase
      */
     public function getCreateTableStatementArrayResult()
     {
-        $subject = new SqlReader(null, $this->prophesize(PackageManager::class)->reveal());
+        $subject = new SqlReader($this->prophesize(Dispatcher::class)->reveal(), $this->prophesize(PackageManager::class)->reveal());
         $result = $subject->getCreateTableStatementArray(
             'CREATE TABLE aTestTable(' . LF . '  aTestField INT(11)' . LF . ');' .
             LF .
index 473704c..c66ad5d 100644 (file)
@@ -23,6 +23,8 @@ use TYPO3\CMS\Core\Imaging\IconRegistry;
 use TYPO3\CMS\Core\Resource\File;
 use TYPO3\CMS\Core\Resource\Folder;
 use TYPO3\CMS\Core\Resource\ResourceStorage;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\SignalSlot\Dispatcher as SignalSlotDispatcher;
 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
 /**
@@ -88,6 +90,10 @@ class IconFactoryTest extends UnitTestCase
     protected function setUp()
     {
         $this->iconRegistryMock = $this->prophesize(IconRegistry::class);
+        $signalSlotDispatcherMock = $this->prophesize(SignalSlotDispatcher::class);
+        $signalSlotDispatcherMock->dispatch(Argument::any(), Argument::any(), Argument::type('array'))->willReturnArgument(2);
+        GeneralUtility::setSingletonInstance(SignalSlotDispatcher::class, $signalSlotDispatcherMock->reveal());
+
         $this->subject = new IconFactory($this->iconRegistryMock->reveal());
 
         $this->iconRegistryMock->isRegistered('tcarecords--default')->willReturn(false);
index db1ff32..0c0ac6e 100644 (file)
@@ -25,6 +25,7 @@ use TYPO3\CMS\Core\Resource\Index\MetaDataRepository;
 use TYPO3\CMS\Core\Resource\MetaDataAspect;
 use TYPO3\CMS\Core\Resource\ResourceStorage;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\SignalSlot\Dispatcher as SignalSlotDispatcher;
 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
 /**
@@ -149,13 +150,15 @@ class MetaDataAspectTest extends UnitTestCase
         $connectionPoolProphecy = $this->prophesize(ConnectionPool::class);
         $connectionPoolProphecy->getConnectionForTable(Argument::cetera())->willReturn($connectionProphecy->reveal());
         GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphecy->reveal());
+        $dispatcherProphecy = $this->prophesize(SignalSlotDispatcher::class);
 
         $metaDataRepositoryMock = $this->getMockBuilder(MetaDataRepository::class)
-            ->setMethods(['findByFileUid', 'getTableFields', 'update'])
+            ->setMethods(['findByFileUid', 'getTableFields', 'update', 'getSignalSlotDispatcher'])
             ->getMock();
         $metaDataRepositoryMock->expects($this->any())->method('findByFileUid')->willReturn([]);
         $metaDataRepositoryMock->expects($this->any())->method('getTableFields')->willReturn(['title' => 'sometype']);
         $metaDataRepositoryMock->expects($this->never())->method('update');
+        $metaDataRepositoryMock->expects($this->any())->method('getSignalSlotDispatcher')->willReturn($dispatcherProphecy->reveal());
         GeneralUtility::setSingletonInstance(MetaDataRepository::class, $metaDataRepositoryMock);
 
         $file->getMetaData()->add($metaData)->save();
index 926fe07..1095a46 100644 (file)
@@ -17,6 +17,7 @@ namespace TYPO3\CMS\Core\Tests\Unit\Utility\AccessibleProxies;
 use TYPO3\CMS\Core\Cache\CacheManager;
 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
+use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
 
 /**
  * Accessible proxy with protected methods made public
@@ -28,6 +29,11 @@ class ExtensionManagementUtilityAccessibleProxy extends ExtensionManagementUtili
         static::$cacheManager = $cacheManager;
     }
 
+    public static function setSignalSlotDispatcher(Dispatcher $dispatcher = null)
+    {
+        static::$signalSlotDispatcher = $dispatcher;
+    }
+
     public static function getPackageManager()
     {
         return static::$packageManager;
index c8b4c47..c022f94 100644 (file)
@@ -24,6 +24,7 @@ use TYPO3\CMS\Core\Package\PackageManager;
 use TYPO3\CMS\Core\Tests\Unit\Utility\AccessibleProxies\ExtensionManagementUtilityAccessibleProxy;
 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\SignalSlot\Dispatcher as SignalSlotDispatcher;
 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
 /**
@@ -56,6 +57,7 @@ class ExtensionManagementUtilityTest extends UnitTestCase
     {
         ExtensionManagementUtilityAccessibleProxy::setPackageManager($this->backUpPackageManager);
         ExtensionManagementUtilityAccessibleProxy::setCacheManager(null);
+        ExtensionManagementUtilityAccessibleProxy::setSignalSlotDispatcher(null);
         parent::tearDown();
     }
 
@@ -1451,6 +1453,11 @@ class ExtensionManagementUtilityTest extends UnitTestCase
         ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
         $mockCache->expects($this->once())->method('require')->will($this->returnValue(false));
         $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($uniqueStringInTableConfiguration), $this->anything());
+
+        $mockSignalSlotDispatcher = $this->createMock(SignalSlotDispatcher::class);
+        $mockSignalSlotDispatcher->expects($this->once())->method('dispatch')->with($this->anything(), $this->anything(), $this->isType('array'))->will($this->returnArgument(2));
+        ExtensionManagementUtilityAccessibleProxy::setSignalSlotDispatcher($mockSignalSlotDispatcher);
+
         ExtensionManagementUtility::loadBaseTca(true);
     }