Permalink
Browse files

OXDEV-1284 Introduce test container factory

  • Loading branch information...
iegupov committed Jan 4, 2019
1 parent d71bb1f commit bbf2f192a4e818dc2cc1ef46d328a9097ad305e3
@@ -6,29 +6,20 @@
namespace OxidEsales\EshopCommunity\Tests\Integration\Internal;
use OxidEsales\EshopCommunity\Internal\Application\ContainerBuilderFactory;
use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyContainerBuilder;
/**
* @internal
*/
trait ContainerTrait
{
protected function get(string $serviceId)
{
$containerBuilder = (new ContainerBuilderFactory())->create();
$container = $containerBuilder->getContainer();
$this->setContainerDefinitionToPublic($container, $serviceId);
$container->compile();
return $container->get($serviceId);
}
private $container;
private function setContainerDefinitionToPublic(SymfonyContainerBuilder $container, string $definitionId): SymfonyContainerBuilder
protected function get(string $serviceId)
{
$definition = $container->getDefinition($definitionId);
$definition->setPublic(true);
$container->setDefinition($definitionId, $definition);
if ($this->container === null) {
$this->container = (new TestContainerFactory())->create();
$this->container->compile();
}
return $container;
return $this->container->get($serviceId);
}
}
@@ -6,15 +6,13 @@
namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Module\Configuration\Dao;
use OxidEsales\EshopCommunity\Internal\Application\ContainerBuilder;
use OxidEsales\EshopCommunity\Internal\Module\Configuration\Dao\ModuleConfigurationDaoInterface;
use OxidEsales\EshopCommunity\Internal\Module\Configuration\Dao\ProjectConfigurationDaoInterface;
use OxidEsales\EshopCommunity\Internal\Module\Configuration\DataObject\EnvironmentConfiguration;
use OxidEsales\EshopCommunity\Internal\Module\Configuration\DataObject\ModuleConfiguration;
use OxidEsales\EshopCommunity\Internal\Module\Configuration\DataObject\ProjectConfiguration;
use OxidEsales\EshopCommunity\Internal\Module\Configuration\DataObject\ShopConfiguration;
use OxidEsales\EshopCommunity\Tests\Integration\Internal\ContainerTrait;
use OxidEsales\Facts\Facts;
use PHPUnit\Framework\TestCase;
/**
@@ -24,15 +22,8 @@ class ModuleConfigurationDaoTest extends TestCase
{
use ContainerTrait;
/**
* @var \Symfony\Component\DependencyInjection\ContainerBuilder
*/
private $container;
protected function setUp()
{
$this->container = $this->getContainer();
$this->prepareProjectConfiguration();
parent::setUp();
@@ -43,7 +34,7 @@ public function testSaving()
$moduleConfiguration = new ModuleConfiguration();
$moduleConfiguration->setId('testId');
$dao = $this->container->get(ModuleConfigurationDaoInterface::class);
$dao = $this->get(ModuleConfigurationDaoInterface::class);
$dao->save($moduleConfiguration, 1);
$this->assertEquals(
@@ -62,36 +53,8 @@ private function prepareProjectConfiguration()
$projectConfiguration = new ProjectConfiguration();
$projectConfiguration->addEnvironmentConfiguration('prod', $environmentConfiguration);
$dao = $this->container->get(ProjectConfigurationDaoInterface::class);
$dao = $this->get(ProjectConfigurationDaoInterface::class);
$dao->persistConfiguration($projectConfiguration);
}
/**
* We need to replace services in the container with a mock
*
* @return \Symfony\Component\DependencyInjection\ContainerBuilder
*/
private function getContainer()
{
$containerBuilder = new ContainerBuilder(new Facts());
$container = $containerBuilder->getContainer();
$projectConfigurationYmlStorageDefinition = $container->getDefinition('oxid_esales.module.configuration.project_configuration_yaml_file_storage');
$projectConfigurationYmlStorageDefinition->setArgument(
'$filePath',
tempnam(sys_get_temp_dir() . '/test_project_configuration', 'test_')
);
$container->setDefinition(
'oxid_esales.module.configuration.project_configuration_yaml_file_storage',
$projectConfigurationYmlStorageDefinition
);
$this->setContainerDefinitionToPublic($container, ProjectConfigurationDaoInterface::class);
$this->setContainerDefinitionToPublic($container, ModuleConfigurationDaoInterface::class);
$container->compile();
return $container;
}
}
@@ -12,7 +12,6 @@
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;
use OxidEsales\EshopCommunity\Internal\Module\Configuration\Dao\ProjectConfigurationDaoInterface;
use OxidEsales\EshopCommunity\Internal\Module\Configuration\DataObject\Chain;
@@ -23,10 +22,9 @@
use OxidEsales\EshopCommunity\Internal\Module\Configuration\DataObject\ShopConfiguration;
use OxidEsales\EshopCommunity\Internal\Module\Setup\Service\ModuleActivationServiceInterface;
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 OxidEsales\EshopCommunity\Tests\Integration\Internal\TestContainerFactory;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
@@ -35,8 +33,6 @@
*/
class ModuleActivationServiceTest extends TestCase
{
use ContainerTrait;
/**
* @var ContainerInterface
*/
@@ -315,25 +311,11 @@ private function persistModuleConfiguration(ModuleConfiguration $moduleConfigura
*/
private function setupAndConfigureContainer()
{
$containerBuilder = new ContainerBuilder(new Facts());
$container = $containerBuilder->getContainer();
$container = (new TestContainerFactory())->create();
$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(
'$filePath',
tempnam(sys_get_temp_dir() . '/test_project_configuration', 'test_')
);
$container->setDefinition(
'oxid_esales.module.configuration.project_configuration_yaml_file_storage',
$projectConfigurationYmlStorageDefinition
);
$this->setContainerDefinitionToPublic($container, ProjectConfigurationDaoInterface::class);
$this->setContainerDefinitionToPublic($container, ModuleActivationServiceInterface::class);
$container->compile();
return $container;
@@ -0,0 +1,52 @@
<?php declare(strict_types=1);
/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/
namespace OxidEsales\EshopCommunity\Tests\Integration\Internal;
use OxidEsales\EshopCommunity\Internal\Application\ContainerBuilder;
use OxidEsales\Facts\Facts;
use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyContainerBuilder;
/**
* @internal
*/
class TestContainerFactory
{
public function create(): SymfonyContainerBuilder
{
$containerBuilder = new ContainerBuilder(new Facts());
$container = $containerBuilder->getContainer();
$container = $this->setAllServicesAsPublic($container);
$container = $this->setTestProjectConfigurationFile($container);
return $container;
}
private function setAllServicesAsPublic(SymfonyContainerBuilder $container): SymfonyContainerBuilder
{
foreach ($container->getDefinitions() as $definition) {
$definition->setPublic(true);
}
return $container;
}
private function setTestProjectConfigurationFile(SymfonyContainerBuilder $container): SymfonyContainerBuilder
{
$projectConfigurationYmlStorageDefinition = $container->getDefinition('oxid_esales.module.configuration.project_configuration_yaml_file_storage');
$projectConfigurationYmlStorageDefinition->setArgument(
'$filePath',
tempnam(sys_get_temp_dir() . '/test_project_configuration', 'test_')
);
$container->setDefinition(
'oxid_esales.module.configuration.project_configuration_yaml_file_storage',
$projectConfigurationYmlStorageDefinition
);
return $container;
}
}

0 comments on commit bbf2f19

Please sign in to comment.