From 020b444db1227bc43179a733fc51d5b8e4abbdd8 Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Mon, 18 Feb 2019 08:59:10 +0100 Subject: [PATCH] [TASK] Mock SignalSlot\Dispatcher Singletons in unit tests 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 Tested-by: Susanne Moog Tested-by: Anja Leichsenring Reviewed-by: Benni Mack Reviewed-by: Susanne Moog Reviewed-by: Anja Leichsenring --- .../backend/Tests/Unit/Utility/BackendUtilityTest.php | 5 +++++ .../Unit/Database/Schema/Parser/TableBuilderTest.php | 4 +++- .../core/Tests/Unit/Database/Schema/SqlReaderTest.php | 11 ++++++----- .../core/Tests/Unit/Imaging/IconFactoryTest.php | 6 ++++++ .../core/Tests/Unit/Resource/MetaDataAspectTest.php | 5 ++++- .../ExtensionManagementUtilityAccessibleProxy.php | 6 ++++++ .../Unit/Utility/ExtensionManagementUtilityTest.php | 7 +++++++ 7 files changed, 37 insertions(+), 7 deletions(-) diff --git a/typo3/sysext/backend/Tests/Unit/Utility/BackendUtilityTest.php b/typo3/sysext/backend/Tests/Unit/Utility/BackendUtilityTest.php index 5fd971aceec5..8339a4d78a57 100644 --- a/typo3/sysext/backend/Tests/Unit/Utility/BackendUtilityTest.php +++ b/typo3/sysext/backend/Tests/Unit/Utility/BackendUtilityTest.php @@ -32,6 +32,7 @@ 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 @@ public function getPagesTSconfigWorksWithoutInitializedBackendUser() $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); } diff --git a/typo3/sysext/core/Tests/Unit/Database/Schema/Parser/TableBuilderTest.php b/typo3/sysext/core/Tests/Unit/Database/Schema/Parser/TableBuilderTest.php index 37a940d3a822..a911bea3f998 100644 --- a/typo3/sysext/core/Tests/Unit/Database/Schema/Parser/TableBuilderTest.php +++ b/typo3/sysext/core/Tests/Unit/Database/Schema/Parser/TableBuilderTest.php @@ -25,6 +25,7 @@ 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 @@ protected function setUp() { 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]); diff --git a/typo3/sysext/core/Tests/Unit/Database/Schema/SqlReaderTest.php b/typo3/sysext/core/Tests/Unit/Database/Schema/SqlReaderTest.php index b15bcb6f5da1..6c476d4d3e53 100644 --- a/typo3/sysext/core/Tests/Unit/Database/Schema/SqlReaderTest.php +++ b/typo3/sysext/core/Tests/Unit/Database/Schema/SqlReaderTest.php @@ -18,6 +18,7 @@ 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 @@ public function getStatementArraySplitsStatements() */ 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 @@ public function getStatementArrayFiltersStatements() */ 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 @@ public function getInsertStatementArrayResult() */ 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 @@ public function getInsertStatementArrayResultWithNewline() */ 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 . diff --git a/typo3/sysext/core/Tests/Unit/Imaging/IconFactoryTest.php b/typo3/sysext/core/Tests/Unit/Imaging/IconFactoryTest.php index 473704ca5baf..c66ad5d17542 100644 --- a/typo3/sysext/core/Tests/Unit/Imaging/IconFactoryTest.php +++ b/typo3/sysext/core/Tests/Unit/Imaging/IconFactoryTest.php @@ -23,6 +23,8 @@ 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); diff --git a/typo3/sysext/core/Tests/Unit/Resource/MetaDataAspectTest.php b/typo3/sysext/core/Tests/Unit/Resource/MetaDataAspectTest.php index db1ff3276abb..0c0ac6ed1895 100644 --- a/typo3/sysext/core/Tests/Unit/Resource/MetaDataAspectTest.php +++ b/typo3/sysext/core/Tests/Unit/Resource/MetaDataAspectTest.php @@ -25,6 +25,7 @@ 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 @@ public function newMetaDataIsCreated(): void $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(); diff --git a/typo3/sysext/core/Tests/Unit/Utility/AccessibleProxies/ExtensionManagementUtilityAccessibleProxy.php b/typo3/sysext/core/Tests/Unit/Utility/AccessibleProxies/ExtensionManagementUtilityAccessibleProxy.php index 926fe074e8b0..1095a4604605 100644 --- a/typo3/sysext/core/Tests/Unit/Utility/AccessibleProxies/ExtensionManagementUtilityAccessibleProxy.php +++ b/typo3/sysext/core/Tests/Unit/Utility/AccessibleProxies/ExtensionManagementUtilityAccessibleProxy.php @@ -17,6 +17,7 @@ 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 @@ public static function setCacheManager(CacheManager $cacheManager = null) static::$cacheManager = $cacheManager; } + public static function setSignalSlotDispatcher(Dispatcher $dispatcher = null) + { + static::$signalSlotDispatcher = $dispatcher; + } + public static function getPackageManager() { return static::$packageManager; diff --git a/typo3/sysext/core/Tests/Unit/Utility/ExtensionManagementUtilityTest.php b/typo3/sysext/core/Tests/Unit/Utility/ExtensionManagementUtilityTest.php index c8b4c4755e2b..c022f948b656 100644 --- a/typo3/sysext/core/Tests/Unit/Utility/ExtensionManagementUtilityTest.php +++ b/typo3/sysext/core/Tests/Unit/Utility/ExtensionManagementUtilityTest.php @@ -24,6 +24,7 @@ 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 @@ protected function tearDown() { ExtensionManagementUtilityAccessibleProxy::setPackageManager($this->backUpPackageManager); ExtensionManagementUtilityAccessibleProxy::setCacheManager(null); + ExtensionManagementUtilityAccessibleProxy::setSignalSlotDispatcher(null); parent::tearDown(); } @@ -1451,6 +1453,11 @@ public function loadBaseTcaCreatesCacheFileWithContentOfAnExtensionsConfiguratio 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); }