Permalink
Browse files

OXDEV-1284 Activate/deactivate module services on module activation/d…

…eactivation
  • Loading branch information...
iegupov committed Jan 2, 2019
1 parent 149fa60 commit 0a5299ee7fbf9725db3a4874cfd06aa3f5684068
@@ -38,9 +38,9 @@ public function __construct(FactsContextInterface $context)
}
/**
* @return Container
* @return SymfonyContainerBuilder
*/
public function getContainer(): Container
public function getContainer(): SymfonyContainerBuilder
{
$symfonyContainer = new SymfonyContainerBuilder();
$symfonyContainer->addCompilerPass(new RegisterListenersPass());
@@ -45,27 +45,35 @@ class ModuleActivationService implements ModuleActivationServiceInterface
*/
private $classExtensionChainService;
/**
* @var ModuleServicesActivationServiceInterface
*/
private $moduleServicesActivationService;
/**
* ModuleActivationService constructor.
*
* @param ModuleConfigurationDaoInterface $ModuleConfigurationDao
* @param EventDispatcherInterface $eventDispatcher
* @param ModuleSettingsHandlingServiceInterface $moduleSettingsHandlingService
* @param ModuleStateServiceInterface $stateService
* @param ExtensionChainServiceInterface $classExtensionChainService
* @param ModuleConfigurationDaoInterface $ModuleConfigurationDao
* @param EventDispatcherInterface $eventDispatcher
* @param ModuleSettingsHandlingServiceInterface $moduleSettingsHandlingService
* @param ModuleStateServiceInterface $stateService
* @param ExtensionChainServiceInterface $classExtensionChainService
* @param ModuleServicesActivationServiceInterface $moduleServicesActivationService
*/
public function __construct(
ModuleConfigurationDaoInterface $ModuleConfigurationDao,
EventDispatcherInterface $eventDispatcher,
ModuleSettingsHandlingServiceInterface $moduleSettingsHandlingService,
ModuleStateServiceInterface $stateService,
ExtensionChainServiceInterface $classExtensionChainService
ModuleConfigurationDaoInterface $ModuleConfigurationDao,
EventDispatcherInterface $eventDispatcher,
ModuleSettingsHandlingServiceInterface $moduleSettingsHandlingService,
ModuleStateServiceInterface $stateService,
ExtensionChainServiceInterface $classExtensionChainService,
ModuleServicesActivationServiceInterface $moduleServicesActivationService
) {
$this->moduleConfigurationDao = $ModuleConfigurationDao;
$this->eventDispatcher = $eventDispatcher;
$this->moduleSettingsHandlingService = $moduleSettingsHandlingService;
$this->stateService = $stateService;
$this->classExtensionChainService = $classExtensionChainService;
$this->moduleServicesActivationService = $moduleServicesActivationService;
}
@@ -85,6 +93,8 @@ public function activate(string $moduleId, int $shopId)
$this->moduleSettingsHandlingService->handleOnActivation($moduleConfiguration, $shopId);
$this->moduleServicesActivationService->activateModuleServices($moduleId, $shopId);
$this->stateService->setActive($moduleId, $shopId);
$moduleConfiguration->setAutoActive(true);
@@ -119,6 +129,8 @@ public function deactivate(string $moduleId, int $shopId)
$this->moduleSettingsHandlingService->handleOnDeactivation($moduleConfiguration, $shopId);
$this->moduleServicesActivationService->deactivateModuleServices($moduleId, $shopId);
$this->stateService->setDeactivated($moduleId, $shopId);
$moduleConfiguration->setAutoActive(false);
@@ -101,6 +101,7 @@ services:
class: OxidEsales\EshopCommunity\Internal\Module\Setup\Service\ModuleServicesActivationService
autowire: true
public: true

oxid_esales.module.setup.class_extension_chain_service:
class: OxidEsales\EshopCommunity\Internal\Module\Setup\Service\ClassExtensionChainService
autowire: true
@@ -10,6 +10,7 @@
use OxidEsales\EshopCommunity\Internal\Adapter\Configuration\Dao\ShopConfigurationSettingDaoInterface;
use OxidEsales\EshopCommunity\Internal\Adapter\Configuration\DataObject\ShopConfigurationSetting;
use OxidEsales\EshopCommunity\Internal\Adapter\ShopAdapter;
use OxidEsales\EshopCommunity\Internal\Adapter\ShopAdapterInterface;
use OxidEsales\EshopCommunity\Internal\Application\ContainerBuilder;
use OxidEsales\EshopCommunity\Internal\Module\Configuration\Dao\ModuleConfigurationDaoInterface;
@@ -24,6 +25,7 @@
use OxidEsales\EshopCommunity\Internal\Module\State\ModuleStateServiceInterface;
use OxidEsales\EshopCommunity\Tests\Integration\Internal\ContainerTrait;
use OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\TestData\TestModule\ModuleEvents;
use OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\TestData\TestModule\SomeModuleService;
use OxidEsales\Facts\Facts;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
@@ -172,18 +174,33 @@ public function testClassExtensionChainUpdate()
);
}
public function testActivationOfModuleServices()
{
$moduleConfiguration = $this->getTestModuleConfiguration();
$this->persistModuleConfiguration($moduleConfiguration);
$moduleActivationService = $this->container->get(ModuleActivationServiceInterface::class);
$moduleActivationService->activate($this->testModuleId, $this->shopId);
$this->assertInstanceOf(
SomeModuleService::class,
$this->setupAndConfigureContainer()->get(SomeModuleService::class)
);
}
/**
* @return ShopAdapterInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private function getShopAdapterMock()
{
$shopAdapter = $this
->getMockBuilder(ShopAdapterInterface::class)
->getMockBuilder(ShopAdapter::class)
->setMethods(['getModuleFullPath'])
->getMock();
$shopAdapter
->method('getModuleFullPath')
->willReturn(__DIR__ . '/../TestData/TestModule');
->willReturn(__DIR__ . '/../../TestData/TestModule');
return $shopAdapter;
}
@@ -192,6 +209,10 @@ private function getTestModuleConfiguration(): ModuleConfiguration
{
$moduleConfiguration = new ModuleConfiguration();
$moduleConfiguration->setId($this->testModuleId);
$moduleConfiguration->addSetting(
new ModuleSetting(ModuleSetting::PATH, 'TestModule')
);
/**
$moduleConfiguration->addSetting(
new ModuleSetting(ModuleSetting::PATH, 'somePath')
@@ -297,16 +318,8 @@ private function setupAndConfigureContainer()
$containerBuilder = new ContainerBuilder(new Facts());
$container = $containerBuilder->getContainer();
$smartyPluginDirectoriesValidatordefinition = $container->getDefinition(
'oxid_esales.module.setup.validator.smarty_plugin_directories_module_setting_validator'
);
$shopAdapter = $this->getShopAdapterMock();
$smartyPluginDirectoriesValidatordefinition->setArguments([$shopAdapter]);
$container->setDefinition(
'oxid_esales.module.setup.validator.smarty_plugin_directories_module_setting_validator',
$smartyPluginDirectoriesValidatordefinition
);
$container->set(ShopAdapterInterface::class, $this->getShopAdapterMock());
$container->autowire(ShopAdapterInterface::class, ShopAdapter::class);
$projectConfigurationYmlStorageDefinition = $container->getDefinition('oxid_esales.module.configuration.project_configuration_yaml_file_storage');
$projectConfigurationYmlStorageDefinition->setArgument(
@@ -12,16 +12,20 @@
use OxidEsales\EshopCommunity\Internal\Application\DataObject\DIServiceWrapper;
use OxidEsales\EshopCommunity\Internal\Module\Setup\Service\ModuleServicesActivationService;
use OxidEsales\EshopCommunity\Internal\Module\Setup\Service\ModuleServicesActivationServiceInterface;
use OxidEsales\EshopCommunity\Tests\Unit\Internal\ProjectDIConfig\TestModule\OtherService;
use OxidEsales\EshopCommunity\Tests\Unit\Internal\ProjectDIConfig\TestModule\TestEventSubscriber;
use OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\TestData\TestModule\SomeModuleService;
use OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\TestData\TestModule\TestEventSubscriber;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class ModuleServicesActivationServiceTest extends TestCase
{
private $testModuleId = 'testModuleId';
private $testModuleDirectory = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'TestData' . DIRECTORY_SEPARATOR . 'TestModule';
private $testModuleDirectory = __DIR__ . DIRECTORY_SEPARATOR
. '..' . DIRECTORY_SEPARATOR
. '..' . DIRECTORY_SEPARATOR
. 'TestData' . DIRECTORY_SEPARATOR
. 'TestModule';
/**
* @var ProjectYamlDaoInterface | MockObject
@@ -62,7 +66,7 @@ public function testActivateServicesForShops()
$moduleConfig = new DIConfigWrapper([
'services' => [
'testEventSubscriber' => ['class' => TestEventSubscriber::class],
'otherService' => ['class' => OtherService::class],
'otherService' => ['class' => SomeModuleService::class],
],
]);
@@ -105,7 +109,7 @@ public function testDeactivateServicesForShops()
$moduleConfig = new DIConfigWrapper([
'services' => [
'testEventSubscriber' => ['class' => $shopAwareService],
'otherService' => ['class' => OtherService::class],
'otherService' => ['class' => SomeModuleService::class],
],
]);
@@ -146,7 +150,7 @@ public function testDeactivateServicesForAllShops()
$moduleConfig = new DIConfigWrapper([
'services' => [
'testEventSubscriber' => ['class' => $shopAwareService],
'otherService' => ['class' => OtherService::class],
'otherService' => ['class' => SomeModuleService::class],
]
]);
@@ -5,12 +5,12 @@
* See LICENSE file for license details.
*/
namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\TestModule;
namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\TestData\TestModule;
/**
* @internal
*/
class OtherService
class SomeModuleService
{
public function doSomething()
{
@@ -5,7 +5,7 @@
* See LICENSE file for license details.
*/
namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\TestModule;
namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\TestData\TestModule;
use OxidEsales\EshopCommunity\Internal\Application\Events\AbstractShopAwareEventSubscriber;
use Symfony\Component\EventDispatcher\Event;
@@ -1,9 +1,9 @@
services:

OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\TestModule\TestEventSubscriber:
class: OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\TestModule\TestEventSubscriber
OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\TestData\TestModule\TestEventSubscriber:
class: OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\TestData\TestModule\TestEventSubscriber
tags:
- { name: kernel.event_subscriber }

OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\TestModule\OtherService:
class: OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\TestModule\OtherService
OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\TestData\TestModule\SomeModuleService:
class: OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\TestData\TestModule\SomeModuleService
@@ -10,6 +10,7 @@
use OxidEsales\EshopCommunity\Internal\Module\Configuration\Dao\ModuleConfigurationDaoInterface;
use OxidEsales\EshopCommunity\Internal\Module\Setup\Service\ExtensionChainServiceInterface;
use OxidEsales\EshopCommunity\Internal\Module\Setup\Service\ModuleActivationService;
use OxidEsales\EshopCommunity\Internal\Module\Setup\Service\ModuleServicesActivationServiceInterface;
use OxidEsales\EshopCommunity\Internal\Module\Setup\Service\ModuleSettingsHandlingServiceInterface;
use OxidEsales\EshopCommunity\Internal\Module\State\ModuleStateServiceInterface;
use PHPUnit\Framework\TestCase;
@@ -52,7 +53,8 @@ private function getTestModuleActivationService(): ModuleActivationService
$this->getMockBuilder(EventDispatcherInterface::class)->getMock(),
$this->getMockBuilder(ModuleSettingsHandlingServiceInterface::class)->getMock(),
$moduleStateService,
$this->getMockBuilder(ExtensionChainServiceInterface::class)->getMock()
$this->getMockBuilder(ExtensionChainServiceInterface::class)->getMock(),
$this->getMockBuilder(ModuleServicesActivationServiceInterface::class)->getMock()
);
return $moduleActivationService;

0 comments on commit 0a5299e

Please sign in to comment.