diff --git a/src/Console/ApplicationDecorator.php b/src/Console/ApplicationDecorator.php deleted file mode 100644 index 0799c00..0000000 --- a/src/Console/ApplicationDecorator.php +++ /dev/null @@ -1,35 +0,0 @@ -getName(), $application->getVersion()); - - $this->setCatchExceptions($application->areExceptionsCaught()); - $this->setAutoExit($application->isAutoExitEnabled()); - } - - protected function getDefaultInputDefinition(): InputDefinition - { - $definition = parent::getDefaultInputDefinition(); - - $definition->addOption(new InputOption( - 'correlation-id', - null, - InputOption::VALUE_REQUIRED, - 'Correlation ID for this command execution' - )); - - return $definition; - } -} diff --git a/src/DependencyInjection/Compiler/ConsoleCommandCompilerPass.php b/src/DependencyInjection/Compiler/ConsoleCommandCompilerPass.php deleted file mode 100644 index 5ddb872..0000000 --- a/src/DependencyInjection/Compiler/ConsoleCommandCompilerPass.php +++ /dev/null @@ -1,32 +0,0 @@ -hasParameter('correlation_id.cli.allow_option') || !$container->getParameter('correlation_id.cli.allow_option')) { - return; - } - - if (!$container->hasDefinition('console.application')) { - return; - } - - if (!$container->hasDefinition(ApplicationDecorator::class)) { - $container->register(ApplicationDecorator::class, ApplicationDecorator::class) - ->setDecoratedService('console.application') - ->setArguments([new Reference('.inner')]) - ->addMethodCall('setDispatcher', [new Reference('event_dispatcher')]) - ->setPublic(false); - } - } -} diff --git a/src/EventListener/ConsoleListener.php b/src/EventListener/ConsoleListener.php index a401138..8f6f33d 100644 --- a/src/EventListener/ConsoleListener.php +++ b/src/EventListener/ConsoleListener.php @@ -43,12 +43,12 @@ public function onConsoleCommand(ConsoleCommandEvent $event): void return; } - $correlationId = null; $input = $event->getInput(); + $correlationId = null; - if ($this->allowOption && $input->hasParameterOption('--' . self::OPTION_NAME)) { - $value = $input->getParameterOption('--' . self::OPTION_NAME); - if (is_string($value)) { + if ($this->allowOption) { + $value = $input->getParameterOption('--' . self::OPTION_NAME, null); + if (is_string($value) && $value !== '') { $correlationId = $this->validator->sanitize($value); } } diff --git a/src/SymfonyCorrelationIdBundle.php b/src/SymfonyCorrelationIdBundle.php index f8514f6..7d31e1c 100644 --- a/src/SymfonyCorrelationIdBundle.php +++ b/src/SymfonyCorrelationIdBundle.php @@ -4,7 +4,6 @@ namespace MdavidDev\SymfonyCorrelationIdBundle; -use MdavidDev\SymfonyCorrelationIdBundle\DependencyInjection\Compiler\ConsoleCommandCompilerPass; use MdavidDev\SymfonyCorrelationIdBundle\DependencyInjection\Compiler\MonologCompilerPass; use MdavidDev\SymfonyCorrelationIdBundle\DependencyInjection\CorrelationIdExtension; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -29,6 +28,5 @@ public function build(ContainerBuilder $container): void parent::build($container); $container->addCompilerPass(new MonologCompilerPass()); - $container->addCompilerPass(new ConsoleCommandCompilerPass()); } } diff --git a/tests/Functional/Integration/ConsoleIntegrationTest.php b/tests/Functional/Integration/ConsoleIntegrationTest.php index d8fa18a..00f8aeb 100644 --- a/tests/Functional/Integration/ConsoleIntegrationTest.php +++ b/tests/Functional/Integration/ConsoleIntegrationTest.php @@ -13,10 +13,10 @@ use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpKernel\Kernel; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; @@ -77,10 +77,20 @@ public function testConsoleCommandUsesProvidedOption(): void /** @var CorrelationIdStorage $storage */ $storage = $container->get(CorrelationIdStorage::class); - /** @var Application $application */ - $application = $container->get('console.application'); + $application = new Application(); + /** @var EventDispatcherInterface $dispatcher */ + $dispatcher = $container->get('event_dispatcher'); + $application->setDispatcher($dispatcher); $application->setAutoExit(false); + $definition = $application->getDefinition(); + $definition->addOption(new InputOption( + 'correlation-id', + null, + InputOption::VALUE_REQUIRED, + 'Correlation ID for this command execution' + )); + $capturedId = null; $command = new class($storage, $capturedId) extends Command { public ?string $capturedId = null; @@ -132,13 +142,13 @@ public function registerContainerConfiguration(LoaderInterface $loader): void 'test' => true, ]); - $container->register('console.application', Application::class) - ->setPublic(true) - ->addMethodCall('setDispatcher', [new Reference('event_dispatcher')]); - if ($container->hasAlias(CorrelationIdStorage::class)) { $container->getAlias(CorrelationIdStorage::class)->setPublic(true); } + + if ($container->hasAlias('event_dispatcher')) { + $container->getAlias('event_dispatcher')->setPublic(true); + } }); } diff --git a/tests/Unit/DependencyInjection/Compiler/ConsoleCommandCompilerPassTest.php b/tests/Unit/DependencyInjection/Compiler/ConsoleCommandCompilerPassTest.php deleted file mode 100644 index d0d1dd7..0000000 --- a/tests/Unit/DependencyInjection/Compiler/ConsoleCommandCompilerPassTest.php +++ /dev/null @@ -1,84 +0,0 @@ -compilerPass = new ConsoleCommandCompilerPass(); - $this->container = new ContainerBuilder(); - } - - public function testProcessDoesNothingIfCliOptionIsDisabled(): void - { - $this->container->setParameter('correlation_id.cli.allow_option', false); - $this->container->register('console.application', 'Symfony\Component\Console\Application'); - - $this->compilerPass->process($this->container); - - $this->assertFalse($this->container->hasDefinition(ApplicationDecorator::class)); - } - - public function testProcessDoesNothingIfCliOptionParameterIsMissing(): void - { - $this->container->register('console.application', 'Symfony\Component\Console\Application'); - - $this->compilerPass->process($this->container); - - $this->assertFalse($this->container->hasDefinition(ApplicationDecorator::class)); - } - - public function testProcessDoesNothingIfConsoleApplicationDefinitionIsMissing(): void - { - $this->container->setParameter('correlation_id.cli.allow_option', true); - - $this->compilerPass->process($this->container); - - $this->assertFalse($this->container->hasDefinition(ApplicationDecorator::class)); - } - - public function testProcessRegistersDecorator(): void - { - $this->container->setParameter('correlation_id.cli.allow_option', true); - $this->container->register('console.application', 'Symfony\Component\Console\Application'); - - $this->compilerPass->process($this->container); - - $this->assertTrue($this->container->hasDefinition(ApplicationDecorator::class)); - $definition = $this->container->getDefinition(ApplicationDecorator::class); - - $this->assertSame('console.application', $definition->getDecoratedService()[0]); - $this->assertSame('.inner', (string) $definition->getArgument(0)); - - $methodCalls = $definition->getMethodCalls(); - $this->assertCount(1, $methodCalls); - $this->assertSame('setDispatcher', $methodCalls[0][0]); - $this->assertSame('event_dispatcher', (string) $methodCalls[0][1][0]); - } - - public function testProcessDoesNotRegisterDecoratorIfAlreadyDefined(): void - { - $this->container->setParameter('correlation_id.cli.allow_option', true); - $this->container->register('console.application', 'Symfony\Component\Console\Application'); - - $manualDefinition = new Definition(ApplicationDecorator::class); - $this->container->setDefinition(ApplicationDecorator::class, $manualDefinition); - - $this->compilerPass->process($this->container); - - $this->assertSame($manualDefinition, $this->container->getDefinition(ApplicationDecorator::class)); - $this->assertEmpty($manualDefinition->getMethodCalls()); - } -} diff --git a/tests/Unit/EventListener/ConsoleListenerTest.php b/tests/Unit/EventListener/ConsoleListenerTest.php index c430eee..c25fb41 100644 --- a/tests/Unit/EventListener/ConsoleListenerTest.php +++ b/tests/Unit/EventListener/ConsoleListenerTest.php @@ -16,7 +16,6 @@ use Symfony\Component\Console\Event\ConsoleCommandEvent; use Symfony\Component\Console\Event\ConsoleErrorEvent; use Symfony\Component\Console\Event\ConsoleTerminateEvent; -use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\HttpFoundation\RequestStack; @@ -59,7 +58,7 @@ public function testGeneratesIdWithPrefixWhenNoOptionProvided(): void $output = $this->createMock(OutputInterface::class); $command = $this->createMock(Command::class); - $input->method('hasParameterOption')->with('--correlation-id')->willReturn(false); + $input->method('getParameterOption')->with('--correlation-id', null)->willReturn(null); $this->generator->expects($this->once())->method('generate')->willReturn('uuid-123'); @@ -75,8 +74,7 @@ public function testUsesOptionWhenProvidedAndValid(): void $output = $this->createMock(OutputInterface::class); $command = $this->createMock(Command::class); - $input->method('hasParameterOption')->with('--correlation-id')->willReturn(true); - $input->method('getParameterOption')->with('--correlation-id')->willReturn('custom-id'); + $input->method('getParameterOption')->with('--correlation-id', null)->willReturn('custom-id'); $this->generator->expects($this->never())->method('generate'); @@ -92,8 +90,7 @@ public function testGeneratesIdWhenOptionProvidedButInvalid(): void $output = $this->createMock(OutputInterface::class); $command = $this->createMock(Command::class); - $input->method('hasParameterOption')->with('--correlation-id')->willReturn(true); - $input->method('getParameterOption')->with('--correlation-id')->willReturn(' '); + $input->method('getParameterOption')->with('--correlation-id', null)->willReturn(' '); $this->generator->expects($this->once())->method('generate')->willReturn('uuid-456'); @@ -103,7 +100,7 @@ public function testGeneratesIdWhenOptionProvidedButInvalid(): void $this->assertSame('CLI-uuid-456', $this->storage->get()); } - public function testDoesNotAddOptionWhenAllowOptionIsFalse(): void + public function testDoesNotReadOptionWhenAllowOptionIsFalse(): void { $listener = new ConsoleListener($this->storage, $this->generator, $this->validator, 'CLI-', false); @@ -111,6 +108,8 @@ public function testDoesNotAddOptionWhenAllowOptionIsFalse(): void $output = $this->createMock(OutputInterface::class); $command = $this->createMock(Command::class); + $input->expects($this->never())->method('getParameterOption'); + $this->generator->expects($this->once())->method('generate')->willReturn('uuid-789'); $event = new ConsoleCommandEvent($command, $input, $output);