From c104380397080b37f39761234479ca7deb2180ae Mon Sep 17 00:00:00 2001 From: jlabedo Date: Fri, 1 Sep 2023 15:31:12 +0200 Subject: [PATCH] add config option to enable test module in Symfony and Laravel --- packages/Laravel/config/ecotone.php | 10 +++++++ packages/Laravel/src/EcotoneProvider.php | 7 ++++- .../Application/Execution/ApplicationTest.php | 15 +++++++++- .../tests/Application/config/ecotone.php | 1 + .../Compiler/EcotoneCompilerPass.php | 8 ++++- .../DepedencyInjection/Configuration.php | 4 +++ .../DepedencyInjection/EcotoneExtension.php | 1 + .../config/packages/test/framework.yaml | 5 +++- .../tests/phpunit/SymfonyApplicationTest.php | 30 +++++++++++++++++++ 9 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 packages/Symfony/tests/phpunit/SymfonyApplicationTest.php diff --git a/packages/Laravel/config/ecotone.php b/packages/Laravel/config/ecotone.php index f9e93c68c..a4a300de4 100644 --- a/packages/Laravel/config/ecotone.php +++ b/packages/Laravel/config/ecotone.php @@ -101,4 +101,14 @@ | */ 'skippedModulePackageNames' => [], + + /* + |-------------------------------------------------------------------------- + | test + |-------------------------------------------------------------------------- + | + | Whether ecotone test module should be loaded + | + */ + 'test' => false, ]; diff --git a/packages/Laravel/src/EcotoneProvider.php b/packages/Laravel/src/EcotoneProvider.php index c704f1738..16d3dff7d 100644 --- a/packages/Laravel/src/EcotoneProvider.php +++ b/packages/Laravel/src/EcotoneProvider.php @@ -54,13 +54,18 @@ public function register() $errorChannel = Config::get('ecotone.defaultErrorChannel'); + $skippedModules = Config::get('ecotone.skippedModulePackageNames'); + if (! Config::get('ecotone.test')) { + $skippedModules[] = ModulePackageList::TEST_PACKAGE; + } + /** @TODO Ecotone 2.0 use ServiceContext to configure Laravel */ $applicationConfiguration = ServiceConfiguration::createWithDefaults() ->withEnvironment($environment) ->withLoadCatalog(Config::get('ecotone.loadAppNamespaces') ? 'app' : '') ->withFailFast(false) ->withNamespaces(Config::get('ecotone.namespaces')) - ->withSkippedModulePackageNames(array_merge(Config::get('ecotone.skippedModulePackageNames'), [ModulePackageList::TEST_PACKAGE])); + ->withSkippedModulePackageNames($skippedModules); if ($isCachingConfiguration) { $applicationConfiguration = $applicationConfiguration diff --git a/packages/Laravel/tests/Application/Execution/ApplicationTest.php b/packages/Laravel/tests/Application/Execution/ApplicationTest.php index 0be27af6a..903596898 100644 --- a/packages/Laravel/tests/Application/Execution/ApplicationTest.php +++ b/packages/Laravel/tests/Application/Execution/ApplicationTest.php @@ -4,10 +4,13 @@ namespace Test\Ecotone\Laravel\Application\Execution; +use Ecotone\Lite\Test\MessagingTestSupport; +use Ecotone\Messaging\Config\ConfiguredMessagingSystem; use Ecotone\Modelling\CommandBus; use Ecotone\Modelling\QueryBus; use Illuminate\Foundation\Http\Kernel; use Illuminate\Foundation\Testing\TestCase; +use Illuminate\Support\Facades\Config; use Test\Ecotone\Laravel\Fixture\User\User; use Test\Ecotone\Laravel\Fixture\User\UserRepository; @@ -67,6 +70,16 @@ public function test_sending_command_using_expression_language() $amount, $queryBus->sendWithRouting('getAmount') ); - ; + } + + public function test_it_boots_messaging_system_with_test_support(): void + { + $app = $this->createApplication(); + + $messagingSystem = $app->get(ConfiguredMessagingSystem::class); + + self::assertInstanceOf( + MessagingTestSupport::class, + $messagingSystem->getGatewayByName(MessagingTestSupport::class)); } } diff --git a/packages/Laravel/tests/Application/config/ecotone.php b/packages/Laravel/tests/Application/config/ecotone.php index 63ff10f15..119471869 100644 --- a/packages/Laravel/tests/Application/config/ecotone.php +++ b/packages/Laravel/tests/Application/config/ecotone.php @@ -7,4 +7,5 @@ 'Test\Ecotone\Laravel\Fixture', ], 'skippedModulePackageNames' => ModulePackageList::allPackages(), + 'test' => true, ]; diff --git a/packages/Symfony/DepedencyInjection/Compiler/EcotoneCompilerPass.php b/packages/Symfony/DepedencyInjection/Compiler/EcotoneCompilerPass.php index d97973682..8a0fd48b6 100644 --- a/packages/Symfony/DepedencyInjection/Compiler/EcotoneCompilerPass.php +++ b/packages/Symfony/DepedencyInjection/Compiler/EcotoneCompilerPass.php @@ -30,6 +30,7 @@ class EcotoneCompilerPass implements CompilerPassInterface public const SERVICE_NAME = 'ecotone.service_name'; public const WORKING_NAMESPACES_CONFIG = 'ecotone.namespaces'; public const FAIL_FAST_CONFIG = 'ecotone.fail_fast'; + public const TEST = 'ecotone.test'; public const LOAD_SRC = 'ecotone.load_src'; public const DEFAULT_SERIALIZATION_MEDIA_TYPE = 'ecotone.serializationMediaType'; public const ERROR_CHANNEL = 'ecotone.errorChannel'; @@ -52,13 +53,18 @@ public static function getRootProjectPath(ContainerInterface $container) public static function getMessagingConfiguration(ContainerInterface $container, bool $useCachedVersion = false): Configuration { $ecotoneCacheDirectory = $container->getParameter('kernel.cache_dir') . self::CACHE_DIRECTORY_SUFFIX; + $skippedModules = $container->getParameter(self::SKIPPED_MODULE_PACKAGES); + if (! $container->getParameter(self::TEST)) { + $skippedModules[] = ModulePackageList::TEST_PACKAGE; + } + /** @TODO Ecotone 2.0 use ServiceContext to configure Symfony */ $serviceConfiguration = ServiceConfiguration::createWithDefaults() ->withEnvironment($container->getParameter('kernel.environment')) ->withFailFast($container->getParameter('kernel.environment') === 'prod' ? false : $container->getParameter(self::FAIL_FAST_CONFIG)) ->withLoadCatalog($container->getParameter(self::LOAD_SRC) ? 'src' : '') ->withNamespaces($container->getParameter(self::WORKING_NAMESPACES_CONFIG)) - ->withSkippedModulePackageNames(array_merge($container->getParameter(self::SKIPPED_MODULE_PACKAGES), [ModulePackageList::TEST_PACKAGE])) + ->withSkippedModulePackageNames($skippedModules) ->withCacheDirectoryPath($ecotoneCacheDirectory); if ($container->getParameter(self::SERVICE_NAME)) { diff --git a/packages/Symfony/DepedencyInjection/Configuration.php b/packages/Symfony/DepedencyInjection/Configuration.php index 40c19a38f..b647bfe08 100644 --- a/packages/Symfony/DepedencyInjection/Configuration.php +++ b/packages/Symfony/DepedencyInjection/Configuration.php @@ -26,6 +26,10 @@ public function getConfigTreeBuilder() ->defaultTrue() ->end() + ->booleanNode('test') + ->defaultFalse() + ->end() + ->booleanNode('loadSrcNamespaces') ->defaultTrue() ->end() diff --git a/packages/Symfony/DepedencyInjection/EcotoneExtension.php b/packages/Symfony/DepedencyInjection/EcotoneExtension.php index 45b26fc96..e775c2622 100644 --- a/packages/Symfony/DepedencyInjection/EcotoneExtension.php +++ b/packages/Symfony/DepedencyInjection/EcotoneExtension.php @@ -16,6 +16,7 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter(EcotoneCompilerPass::WORKING_NAMESPACES_CONFIG, $config['namespaces']); $container->setParameter(EcotoneCompilerPass::SERVICE_NAME, $config['serviceName']); $container->setParameter(EcotoneCompilerPass::FAIL_FAST_CONFIG, $config['failFast']); + $container->setParameter(EcotoneCompilerPass::TEST, $config['test']); $container->setParameter(EcotoneCompilerPass::LOAD_SRC, $config['loadSrcNamespaces']); $container->setParameter(EcotoneCompilerPass::DEFAULT_SERIALIZATION_MEDIA_TYPE, $config['defaultSerializationMediaType']); $container->setParameter(EcotoneCompilerPass::SKIPPED_MODULE_PACKAGES, $config['skippedModulePackageNames']); diff --git a/packages/Symfony/config/packages/test/framework.yaml b/packages/Symfony/config/packages/test/framework.yaml index b89dcfeae..102375ae2 100644 --- a/packages/Symfony/config/packages/test/framework.yaml +++ b/packages/Symfony/config/packages/test/framework.yaml @@ -1,2 +1,5 @@ framework: - test: ~ \ No newline at end of file + test: ~ + +ecotone: + test: true \ No newline at end of file diff --git a/packages/Symfony/tests/phpunit/SymfonyApplicationTest.php b/packages/Symfony/tests/phpunit/SymfonyApplicationTest.php new file mode 100644 index 000000000..0b622a72e --- /dev/null +++ b/packages/Symfony/tests/phpunit/SymfonyApplicationTest.php @@ -0,0 +1,30 @@ + 'test' + ]); + self::assertInstanceOf( + MessagingTestSupport::class, + self::getMessagingTestSupport()); + } + + protected static function getMessagingSystem(): ConfiguredMessagingSystem + { + return static::getContainer()->get(ConfiguredMessagingSystem::class); + } + + protected static function getMessagingTestSupport(): MessagingTestSupport + { + return static::getMessagingSystem()->getGatewayByName(MessagingTestSupport::class); + } +} \ No newline at end of file