Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions src/Console/ApplicationDecorator.php

This file was deleted.

32 changes: 0 additions & 32 deletions src/DependencyInjection/Compiler/ConsoleCommandCompilerPass.php

This file was deleted.

8 changes: 4 additions & 4 deletions src/EventListener/ConsoleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/SymfonyCorrelationIdBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +28,5 @@ public function build(ContainerBuilder $container): void
parent::build($container);

$container->addCompilerPass(new MonologCompilerPass());
$container->addCompilerPass(new ConsoleCommandCompilerPass());
}
}
24 changes: 17 additions & 7 deletions tests/Functional/Integration/ConsoleIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
});
}

Expand Down

This file was deleted.

13 changes: 6 additions & 7 deletions tests/Unit/EventListener/ConsoleListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -103,14 +100,16 @@ 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);

$input = $this->createMock(InputInterface::class);
$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);
Expand Down