Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
[TASK] Fix unit tests for external API
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Mar 3, 2017
1 parent a9b633c commit af896f2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
13 changes: 10 additions & 3 deletions Classes/Core/Service/CacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class CacheService implements SingletonInterface
'groups' => ['all', 'system']
];

/**
* @var CacheManager
*/
protected $cacheManager;

/**
* Function called from `ext_localconf` file which will register the
* internal cache earlier.
Expand Down Expand Up @@ -119,9 +124,11 @@ protected function getCache()
*/
protected function getCacheManager()
{
/** @var CacheManager $cacheManager */
$cacheManager = GeneralUtility::makeInstance(CacheManager::class);
if (null === $this->cacheManager) {
/** @var CacheManager $cacheManager */
$this->cacheManager = GeneralUtility::makeInstance(CacheManager::class);
}

return $cacheManager;
return $this->cacheManager;
}
}
33 changes: 24 additions & 9 deletions Tests/Unit/ConfigurationObjectUnitTestUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,20 @@ protected function initializeConfigurationObjectTestServices()
*/
private function setUpConfigurationObjectCore()
{
$this->configurationObjectCoreMock = $this->getMockBuilder(Core::class)
$this->configurationObjectCoreMock = $this->getConfigurationObjectMockBuilder(Core::class)
->setMethods(['getServiceFactoryInstance'])
->getMock();
$this->configurationObjectCoreMock->injectObjectManager($this->getConfigurationObjectObjectManagerMock());
$this->configurationObjectCoreMock->injectReflectionService(new ReflectionService);
$this->configurationObjectCoreMock->injectValidatorResolver(new ValidatorResolver);
$this->configurationObjectCoreMock->injectParentsUtility(new ParentsUtility);
$this->configurationObjectCoreMock->injectCacheService(new InternalCacheService);

$this->configurationObjectCoreMock->method('getServiceFactoryInstance')
->will(
$this->returnCallback(
function () {
/** @var ServiceFactory|\PHPUnit_Framework_MockObject_MockObject $serviceFactoryMock */
$serviceFactoryMock = $this->getMockBuilder(ServiceFactory::class)
$serviceFactoryMock = $this->getConfigurationObjectMockBuilder(ServiceFactory::class)
->setMethods(['manageServiceData'])
->getMock();
$serviceFactoryMock->method('manageServiceData')
Expand Down Expand Up @@ -127,6 +126,11 @@ function (array $service) {
]);

$this->configurationObjectCoreMock->injectCacheManager($cacheManager);
$cacheService = new InternalCacheService;
$cacheService->registerInternalCache();
$this->inject($cacheService, 'cacheManager', $cacheManager);

$this->configurationObjectCoreMock->injectCacheService($cacheService);

$reflectedCore = new \ReflectionClass(Core::class);
$objectManagerProperty = $reflectedCore->getProperty('instance');
Expand Down Expand Up @@ -167,7 +171,7 @@ protected function injectMockedValidatorResolverInCore()
protected function injectMockedConfigurationObjectFactory()
{
/** @var ConfigurationObjectMapper|\PHPUnit_Framework_MockObject_MockObject $mockedConfigurationObjectMapper */
$mockedConfigurationObjectMapper = $this->getMockBuilder(ConfigurationObjectMapper::class)
$mockedConfigurationObjectMapper = $this->getConfigurationObjectMockBuilder(ConfigurationObjectMapper::class)
->setMethods(['getObjectConverter'])
->getMock();

Expand Down Expand Up @@ -227,7 +231,7 @@ protected function injectMockedConfigurationObjectFactory()
$mockedConfigurationObjectMapper->initializeObject();

/** @var ConfigurationObjectFactory|\PHPUnit_Framework_MockObject_MockObject $mockedConfigurationObjectFactory */
$mockedConfigurationObjectFactory = $this->getMockBuilder(ConfigurationObjectFactory::class)
$mockedConfigurationObjectFactory = $this->getConfigurationObjectMockBuilder(ConfigurationObjectFactory::class)
->setMethods(['getConfigurationObjectMapper'])
->getMock();

Expand All @@ -250,7 +254,7 @@ protected function injectMockedConfigurationObjectFactory()
private function getConfigurationObjectObjectManagerMock()
{
/** @var \PHPUnit_Framework_MockObject_MockObject $mockObjectManager */
$mockObjectManager = $this->getMockBuilder(ObjectManagerInterface::class)
$mockObjectManager = $this->getConfigurationObjectMockBuilder(ObjectManagerInterface::class)
->getMock();
$mockObjectManager->expects($this->any())
->method('get')
Expand All @@ -262,7 +266,7 @@ function () {

if (in_array(AbstractValidator::class, class_parents($className))) {
/** @var AbstractValidator|\PHPUnit_Framework_MockObject_MockObject $instance */
$instance = $this->getMockBuilder($className)
$instance = $this->getConfigurationObjectMockBuilder($className)
->setMethods(['translateErrorMessage'])
->setConstructorArgs($arguments)
->getMock();
Expand All @@ -283,7 +287,7 @@ function ($key, $extension) {
)
) {
/** @var ConfigurationManager|\PHPUnit_Framework_MockObject_MockObject $configurationManager */
$configurationManager = $this->getMockBuilder(ConfigurationManager::class)
$configurationManager = $this->getConfigurationObjectMockBuilder(ConfigurationManager::class)
->setMethods(['isFeatureEnabled'])
->getMock();
$configurationManager->method('isFeatureEnabled')
Expand All @@ -294,7 +298,7 @@ function ($key, $extension) {
$reflectedProperty->setValue($configurationManager, Core::get()->getObjectManager());

/** @var EnvironmentService|\PHPUnit_Framework_MockObject_MockObject $environmentServiceMock */
$environmentServiceMock = $this->getMockBuilder(EnvironmentService::class)
$environmentServiceMock = $this->getConfigurationObjectMockBuilder(EnvironmentService::class)
->setMethods(['isEnvironmentInFrontendMode', 'isEnvironmentInBackendMode'])
->getMock();
$environmentServiceMock
Expand Down Expand Up @@ -337,4 +341,15 @@ function ($key, $extension) {

return $mockObjectManager;
}

/**
* Just a wrapper to have auto-completion.
*
* @param string $className
* @return \PHPUnit_Framework_MockObject_MockBuilder
*/
private function getConfigurationObjectMockBuilder($className)
{
return $this->getMockBuilder($className);
}
}

0 comments on commit af896f2

Please sign in to comment.