Commit 6bfaefaf authored by Christian Kuhn's avatar Christian Kuhn Committed by Anja Leichsenring
Browse files

[TASK] Properly reset unit test singletons

typo3/testing-framework since version 3.8.0 can reset
singletons created by makeInstance automatically
if $this->$resetSingletonInstances is set to true.

This version additionally checks for left over singletons
in case that property has not been set and lets tests
fail in this case.

composer require --dev typo3/testing-framework:^3.8

As advantage, a manual backup of singleton instances
within tests is not needed anymore.

The patch comes with a set of test case adaptions to
cope with this new situation.

Change-Id: Ib5f278145e385e32d543541872cf5e1f208fad47
Resolves: #85209
Releases: master
Reviewed-on: https://review.typo3.org/57169


Tested-by: default avatarTYPO3com <no-reply@typo3.com>
Reviewed-by: Andreas Fernandez's avatarAndreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Andreas Fernandez's avatarAndreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: default avatarJoerg Boesche <typo3@joergboesche.de>
Reviewed-by: Anja Leichsenring's avatarAnja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring's avatarAnja Leichsenring <aleichsenring@ab-softlab.de>
parent 330a3d39
......@@ -64,7 +64,7 @@
"fiunchinho/phpunit-randomizer": "^4.0",
"friendsofphp/php-cs-fixer": "^2.0",
"typo3/cms-styleguide": "^9.1",
"typo3/testing-framework": "^3.2"
"typo3/testing-framework": "^3.8"
},
"suggest": {
"ext-gd": "GDlib/Freetype is required for building images with text (GIFBUILDER) and can also be used to scale images",
......
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "80a09c87266d7b40d69d7963c0d78d2f",
"content-hash": "8527aa1a371d8a4062ec28fa1c74c75b",
"packages": [
{
"name": "cogpowered/finediff",
......@@ -4404,16 +4404,16 @@
},
{
"name": "typo3/testing-framework",
"version": "3.7.0",
"version": "3.8.0",
"source": {
"type": "git",
"url": "https://github.com/TYPO3/testing-framework.git",
"reference": "a00307acc88ad2ad365932aa67ea73dc90a078f5"
"reference": "987bba68fdd93a9c1fcb397c7f0711e4f2cd9d1a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/TYPO3/testing-framework/zipball/a00307acc88ad2ad365932aa67ea73dc90a078f5",
"reference": "a00307acc88ad2ad365932aa67ea73dc90a078f5",
"url": "https://api.github.com/repos/TYPO3/testing-framework/zipball/987bba68fdd93a9c1fcb397c7f0711e4f2cd9d1a",
"reference": "987bba68fdd93a9c1fcb397c7f0711e4f2cd9d1a",
"shasum": ""
},
"require": {
......@@ -4462,7 +4462,7 @@
"tests",
"typo3"
],
"time": "2018-06-08T10:33:39+00:00"
"time": "2018-06-10T17:26:48+00:00"
},
{
"name": "webmozart/assert",
......
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Adminpanel\Tests\Unit\Middleware;
/*
* 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!
*/
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Adminpanel\Controller\MainController;
......@@ -15,8 +28,15 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
/**
* Test case
*/
class AdminPanelInitiatorTest extends UnitTestCase
{
/**
* @var bool Reset singletons created by subject
*/
protected $resetSingletonInstances = true;
/**
* @test
......@@ -124,7 +144,7 @@ class AdminPanelInitiatorTest extends UnitTestCase
}
/**
* @return \Prophecy\Prophecy\ObjectProphecy|\Psr\Http\Server\RequestHandlerInterface
* @return ObjectProphecy|RequestHandlerInterface
*/
protected function prophesizeHandler()
{
......
......@@ -26,10 +26,15 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
/**
* Class LoginControllerTest
* Test case
*/
class LoginControllerTest extends UnitTestCase
{
/**
* @var bool Reset singletons created by subject
*/
protected $resetSingletonInstances = true;
/**
* @var LoginController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
*/
......
......@@ -39,14 +39,8 @@ class DatabaseSystemLanguageRowsTest extends UnitTestCase
*/
protected $subject;
/**
* @var array A backup of registered singleton instances
*/
protected $singletonInstances = [];
protected function setUp()
{
$this->singletonInstances = GeneralUtility::getSingletonInstances();
$languageService = $this->prophesize(LanguageService::class);
$GLOBALS['LANG'] = $languageService->reveal();
$languageService->sL(Argument::cetera())->willReturnArgument(0);
......@@ -56,7 +50,6 @@ class DatabaseSystemLanguageRowsTest extends UnitTestCase
protected function tearDown()
{
GeneralUtility::purgeInstances();
GeneralUtility::resetSingletonInstances($this->singletonInstances);
parent::tearDown();
}
......
......@@ -30,23 +30,18 @@ use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
class TcaCheckboxItemsTest extends UnitTestCase
{
/**
* @var array A backup of registered singleton instances
* Tear down
*/
protected $singletonInstances = [];
protected function setUp()
{
$this->singletonInstances = GeneralUtility::getSingletonInstances();
}
protected function tearDown()
{
GeneralUtility::purgeInstances();
GeneralUtility::resetSingletonInstances($this->singletonInstances);
parent::tearDown();
}
public function checkboxConfigurationDataProvider()
/**
* @return array
*/
public function checkboxConfigurationDataProvider(): array
{
return [
'simpleCheckboxConfig' => [
......
......@@ -27,15 +27,8 @@ use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
*/
class TcaFlexPrepareTest extends UnitTestCase
{
/**
* @var array A backup of registered singleton instances
*/
protected $singletonInstances = [];
protected function setUp()
{
$this->singletonInstances = GeneralUtility::getSingletonInstances();
// Suppress cache foo in xml helpers of GeneralUtility
/** @var CacheManager|ObjectProphecy $cacheManagerProphecy */
$cacheManagerProphecy = $this->prophesize(CacheManager::class);
......@@ -47,7 +40,6 @@ class TcaFlexPrepareTest extends UnitTestCase
protected function tearDown()
{
GeneralUtility::purgeInstances();
GeneralUtility::resetSingletonInstances($this->singletonInstances);
parent::tearDown();
}
......
......@@ -30,20 +30,9 @@ use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
class TcaGroupTest extends UnitTestCase
{
/**
* @var array
* @var bool Reset singletons created by subject
*/
protected $singletonInstances;
protected function setUp()
{
$this->singletonInstances = GeneralUtility::getSingletonInstances();
}
protected function tearDown()
{
GeneralUtility::resetSingletonInstances($this->singletonInstances);
parent::tearDown();
}
protected $resetSingletonInstances = true;
/**
* @test
......
......@@ -30,19 +30,11 @@ use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
class TcaRadioItemsTest extends UnitTestCase
{
/**
* @var array A backup of registered singleton instances
* Tear down
*/
protected $singletonInstances = [];
protected function setUp()
{
$this->singletonInstances = GeneralUtility::getSingletonInstances();
}
protected function tearDown()
{
GeneralUtility::purgeInstances();
GeneralUtility::resetSingletonInstances($this->singletonInstances);
parent::tearDown();
}
......
......@@ -45,14 +45,10 @@ use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
class TcaSelectItemsTest extends UnitTestCase
{
/**
* @var array A backup of registered singleton instances
* Set up
*/
protected $singletonInstances = [];
protected function setUp()
{
$this->singletonInstances = GeneralUtility::getSingletonInstances();
// Default LANG prophecy just returns incoming value as label if calling ->sL()
$languageServiceProphecy = $this->prophesize(LanguageService::class);
$languageServiceProphecy->loadSingleTableDescription(Argument::cetera())->willReturn(null);
......@@ -70,10 +66,12 @@ class TcaSelectItemsTest extends UnitTestCase
GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
}
/**
* Tear down
*/
protected function tearDown()
{
GeneralUtility::purgeInstances();
GeneralUtility::resetSingletonInstances($this->singletonInstances);
parent::tearDown();
}
......
......@@ -41,24 +41,9 @@ use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
class TcaSelectTreeItemsTest extends UnitTestCase
{
/**
* @var array A backup of registered singleton instances
* @var bool Reset singletons created by subject
*/
protected $singletonInstances = [];
/**
* Initializes the mock object.
*/
public function setUp()
{
$this->singletonInstances = GeneralUtility::getSingletonInstances();
}
protected function tearDown()
{
GeneralUtility::purgeInstances();
GeneralUtility::resetSingletonInstances($this->singletonInstances);
parent::tearDown();
}
protected $resetSingletonInstances = true;
/**
* Setup a mock database connection with expectations for
......
......@@ -28,9 +28,9 @@ use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
class InlineStackProcessorTest extends UnitTestCase
{
/**
* @var array A backup of registered singleton instances
* @var bool Reset singletons created by subject
*/
protected $singletonInstances = [];
protected $resetSingletonInstances = true;
/**
* Set up
......@@ -38,7 +38,6 @@ class InlineStackProcessorTest extends UnitTestCase
public function setUp()
{
parent::setUp();
$this->singletonInstances = GeneralUtility::getSingletonInstances();
$cacheManagerProphecy = $this->prophesize(CacheManager::class);
$cacheProphecy = $this->prophesize(FrontendInterface::class);
$cacheManagerProphecy->getCache('cache_runtime')->willReturn($cacheProphecy->reveal());
......@@ -47,15 +46,6 @@ class InlineStackProcessorTest extends UnitTestCase
GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
}
/**
* Tear down
*/
protected function tearDown()
{
GeneralUtility::resetSingletonInstances($this->singletonInstances);
parent::tearDown();
}
/**
* @return array
*/
......
......@@ -22,11 +22,12 @@ use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
/**
* Test case for TcaFlexPrepare to render the functionality when a TCA migration happened
* Test case
*/
class TcaFlexPrepareTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
class TcaFlexPrepareTest extends UnitTestCase
{
/**
* @var TcaFlexPrepare
......@@ -39,14 +40,10 @@ class TcaFlexPrepareTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
protected $backendUserProphecy;
/**
* @var array A backup of registered singleton instances
* Set up
*/
protected $singletonInstances = [];
protected function setUp()
{
$this->singletonInstances = GeneralUtility::getSingletonInstances();
// Suppress cache foo in xml helpers of GeneralUtility
/** @var CacheManager|ObjectProphecy $cacheManagerProphecy */
$cacheManagerProphecy = $this->prophesize(CacheManager::class);
......@@ -57,10 +54,12 @@ class TcaFlexPrepareTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
$this->subject = new TcaFlexPrepare();
}
/**
* Tear down
*/
protected function tearDown()
{
GeneralUtility::purgeInstances();
GeneralUtility::resetSingletonInstances($this->singletonInstances);
parent::tearDown();
}
......
......@@ -14,12 +14,14 @@ namespace TYPO3\CMS\Core\Log;
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\SingletonInterface;
/**
* Global LogManager that keeps track of global logging information.
*
* Inspired by java.util.logging
*/
class LogManager implements \TYPO3\CMS\Core\SingletonInterface, LogManagerInterface
class LogManager implements SingletonInterface, LogManagerInterface
{
/**
* @var string
......
<?php
namespace TYPO3\CMS\Core\Tests\Integrity;
/*
* 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!
*/
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
/**
* This test case is used in test suites to check for healthy
* environments after other tests were run.
*
* This test is usually executed as the very last file in a suite and
* should fail if some other test before destroys the environment with
* invalid mocking or backups.
*/
class IntegrityTest extends UnitTestCase
{
/**
* This test fails if any test case manipulates the configurationManager
* property in LocalizationUtility due to mocking and fails to restore it
* properly.
*
* @test
*/
public function ensureLocalisationUtilityConfigurationManagerIsNull()
{
$reflectionClass = new \ReflectionClass(LocalizationUtility::class);
$property = $reflectionClass->getProperty('configurationManager');
$property->setAccessible(true);
$this->assertNull($property->getValue());
}
}
......@@ -14,15 +14,21 @@ namespace TYPO3\CMS\Core\Tests\Unit\Authentication;
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Authentication\AuthenticationService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
/**
* Testcase for class \TYPO3\CMS\Core\Authentication\AuthenticationService
* Test case
*/
class AuthenticationServiceTest extends UnitTestCase
{
/**
* @var bool Reset singletons created by subject
*/
protected $resetSingletonInstances = true;
/**
* Date provider for processLoginReturnsCorrectData
*
......
......@@ -26,6 +26,11 @@ use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
*/
class PdoBackendTest extends UnitTestCase
{
/**
* @var bool Reset singletons created by subject
*/
protected $resetSingletonInstances = true;
/**
* Sets up this testcase
*/
......@@ -34,6 +39,7 @@ class PdoBackendTest extends UnitTestCase
if (!extension_loaded('pdo_sqlite')) {
$this->markTestSkipped('pdo_sqlite extension was not available');
}
parent::setUp();
}
/**
......
......@@ -57,9 +57,9 @@ use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
class FlexFormToolsTest extends UnitTestCase
{
/**
* @var array A backup of registered singleton instances
* @var bool Reset singletons created by subject
*/
protected $singletonInstances = [];
protected $resetSingletonInstances = true;
/**
* Set up
......@@ -67,7 +67,6 @@ class FlexFormToolsTest extends UnitTestCase
public function setUp()
{
parent::setUp();
$this->singletonInstances = GeneralUtility::getSingletonInstances();
// Underlying static GeneralUtility::xml2array() uses caches that have to be mocked here
$cacheManagerProphecy = $this->prophesize(CacheManager::class);
$cacheProphecy = $this->prophesize(FrontendInterface::class);
......@@ -77,15 +76,6 @@ class FlexFormToolsTest extends UnitTestCase
GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
}
/**
* Tear down
*/
protected function tearDown()
{
GeneralUtility::resetSingletonInstances($this->singletonInstances);
parent::tearDown();
}
/**
* @test
*/
......
......@@ -36,6 +36,11 @@ class DataHandlerTest extends UnitTestCase
*/
protected static $suppressNotices = true;
/**
* @var bool Reset singletons created by subject
*/
protected $resetSingletonInstances = true;
/**
* @var array A backup of registered singleton instances
*/
......@@ -57,7 +62,6 @@ class DataHandlerTest extends UnitTestCase
protected function setUp()
{
$GLOBALS['TCA'] = [];
$this->singletonInstances = GeneralUtility::getSingletonInstances();
$cacheManagerProphecy = $this->prophesize(CacheManager::class);
GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
$cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
......@@ -67,18 +71,6 @@ class DataHandlerTest extends UnitTestCase
$this->subject->start([], '', $this->backEndUser);
}
/**
* Tear down the tests
*/
protected function tearDown()
{
GeneralUtility::resetSingletonInstances($this->singletonInstances);
parent::tearDown();
}
//////////////////////////////////////
// Tests for the basic functionality
//////////////////////////////////////
/**
* @test
*/
......
......@@ -24,7 +24,6 @@ use Doctrine\DBAL\Types\SmallIntType;
use Doctrine\DBAL\Types\TextType;
use TYPO3\CMS\Core\Database\Schema\Parser\Parser;
use TYPO3\CMS\Core\Database\Schema\SqlReader;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
/**
......@@ -32,6 +31,11 @@ use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
*/
class TableBuilderTest extends UnitTestCase
{
/**
* @var bool Reset singletons created by subject
*/
protected $resetSingletonInstances = true;
/**
* @var Table
*/
......@@ -44,10 +48,10 @@ class TableBuilderTest extends UnitTestCase
{
parent::setUp();
$sqlFile = file_get_contents(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'Fixtures', 'tablebuilder.sql']));
$sqlReader = GeneralUtility::makeInstance(SqlReader::class);
$sqlReader = new SqlReader();
$statements = $sqlReader->getCreateTableStatementArray($sqlFile);
$parser = GeneralUtility::makeInstance(Parser::class, $statements[0]);
$parser = new Parser($statements[0]);
$this->table = $parser->parse()[0];
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment