From eb24953b5a4c3e1312353601595ee38b364f2b52 Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Thu, 22 Oct 2026 18:53:40 +0200 Subject: [PATCH] #361 - Tests: ListProcessCommand, ConsoleProcessEvent, PhpFunctionProvider, log processors Co-Authored-By: Claude Opus 5.5 (1M context) --- tests/Command/ListProcessCommandTest.php | 104 +++++++++++++++ tests/Event/ConsoleProcessEventTest.php | 36 ++++++ .../PhpFunctionProviderTest.php | 33 +++++ tests/Logger/ProcessorsTest.php | 119 ++++++++++++++++++ 4 files changed, 292 insertions(+) create mode 100644 tests/Command/ListProcessCommandTest.php create mode 100644 tests/Event/ConsoleProcessEventTest.php create mode 100644 tests/ExpressionLanguage/PhpFunctionProviderTest.php create mode 100644 tests/Logger/ProcessorsTest.php diff --git a/tests/Command/ListProcessCommandTest.php b/tests/Command/ListProcessCommandTest.php new file mode 100644 index 00000000..29982424 --- /dev/null +++ b/tests/Command/ListProcessCommandTest.php @@ -0,0 +1,104 @@ +createTester(); + + self::assertSame(0, $tester->execute([])); + self::assertSame( + [ + 'There are 2 process configurations defined (and 1 private) :', + ' - test.a with 1 tasks First process', + ' - test.b with 0 tasks', + ], + $this->lines($tester->getDisplay()) + ); + } + + public function testAllTheProcessesAreListedWithTheAllOption(): void + { + $tester = $this->createTester(); + + self::assertSame(0, $tester->execute(['--all' => true])); + self::assertSame( + [ + 'There are 2 process configurations defined (and 1 private) :', + ' - test.a with 1 tasks First process', + ' - test.b with 0 tasks', + ' - test.private with 0 tasks (private) Private process', + ], + $this->lines($tester->getDisplay()) + ); + } + + /** + * @return list + */ + private function lines(string $display): array + { + return array_values(array_filter(array_map(rtrim(...), explode("\n", $display)))); + } + + private function createTester(): CommandTester + { + $process = static fn (string $description, bool $public, array $tasks = []): array => [ + 'options' => [], + 'entry_point' => null, + 'end_point' => null, + 'description' => $description, + 'help' => '', + 'public' => $public, + 'tasks' => $tasks, + ]; + $registry = new ProcessConfigurationRegistry( + [ + 'test.private' => $process('Private process', false), + 'test.b' => $process('', true), + 'test.a' => $process('First process', true, [ + 'task' => [ + 'service' => '@app.task', + 'options' => [], + 'description' => '', + 'help' => '', + 'outputs' => [], + 'errors' => [], + 'error_outputs' => [], + 'error_strategy' => null, + 'log_level' => null, + ], + ]), + ], + 'stop' + ); + + return new CommandTester(new ListProcessCommand($registry)); + } +} diff --git a/tests/Event/ConsoleProcessEventTest.php b/tests/Event/ConsoleProcessEventTest.php new file mode 100644 index 00000000..88bfe33f --- /dev/null +++ b/tests/Event/ConsoleProcessEventTest.php @@ -0,0 +1,36 @@ + 'value']); + + self::assertSame($input, $event->getConsoleInput()); + self::assertSame($output, $event->getConsoleOutput()); + self::assertSame('data', $event->getProcessInput()); + self::assertSame(['key' => 'value'], $event->getProcessContext()); + } +} diff --git a/tests/ExpressionLanguage/PhpFunctionProviderTest.php b/tests/ExpressionLanguage/PhpFunctionProviderTest.php new file mode 100644 index 00000000..e6803844 --- /dev/null +++ b/tests/ExpressionLanguage/PhpFunctionProviderTest.php @@ -0,0 +1,33 @@ + $function->getName(), $provider->getFunctions())); + self::assertSame('FOO', $expressionLanguage->evaluate('strtoupper(value)', ['value' => 'foo'])); + self::assertTrue($expressionLanguage->evaluate('str_contains(value, "o")', ['value' => 'foo'])); + } +} diff --git a/tests/Logger/ProcessorsTest.php b/tests/Logger/ProcessorsTest.php new file mode 100644 index 00000000..ed6442b8 --- /dev/null +++ b/tests/Logger/ProcessorsTest.php @@ -0,0 +1,119 @@ +createStub(ProcessManager::class); + + foreach ([ProcessProcessor::class, TaskProcessor::class, TransformerProcessor::class] as $class) { + $record = (new $class($processManager))($this->createRecord(['foo' => 'bar'])); + + self::assertSame([], $record->extra, $class); + self::assertSame(['foo' => 'bar'], $record->context, $class); + } + } + + public function testTheProcessProcessorAddsTheProcess(): void + { + [$processManager, $history] = $this->createProcessManager(); + + $record = (new ProcessProcessor($processManager))($this->createRecord()); + + self::assertSame( + ['process_id' => $history->getId(), 'process_code' => 'test.process', 'process_context' => ['key' => 'value']], + $record->extra + ); + } + + public function testTheTaskAndTransformerProcessorsAddTheTaskAndItsError(): void + { + [$processManager, $history, $state] = $this->createProcessManager(); + $exception = new \RuntimeException('Something went wrong'); + $state->setErrorOutput(['invalid' => 'item']); + $state->setException($exception); + + foreach ([TaskProcessor::class, TransformerProcessor::class] as $class) { + $record = (new $class($processManager))($this->createRecord()); + + self::assertSame( + [ + 'process_id' => $history->getId(), + 'process_code' => 'test.process', + 'process_context' => ['key' => 'value'], + 'task_code' => 'my_task', + 'task_service' => '@app.task', + 'error' => ['invalid' => 'item'], + 'exception' => $exception, + ], + $record->extra, + $class + ); + } + } + + /** + * @return array{ProcessManager, ProcessHistory, ProcessState} + */ + private function createProcessManager(): array + { + $processConfiguration = new ProcessConfiguration('test.process', []); + $history = new ProcessHistory($processConfiguration, ['key' => 'value']); + $taskConfiguration = new TaskConfiguration('my_task', '@app.task', []); + $state = new ProcessState($processConfiguration, $history); + $taskConfiguration->setState($state); + + $processManager = $this->createStub(ProcessManager::class); + $processManager->method('getProcessHistory')->willReturn($history); + $processManager->method('getTaskConfiguration')->willReturn($taskConfiguration); + + return [$processManager, $history, $state]; + } + + /** + * @param array $context + */ + private function createRecord(array $context = []): LogRecord + { + return new LogRecord(new \DateTimeImmutable(), 'cleverage_process', Level::Info, 'message', $context); + } +}