From c0ae059f38a123bce86c3a89f54865b2c761e5e5 Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Tue, 3 Nov 2026 14:55:30 +0100 Subject: [PATCH] #422 - Tests: behaviors shown untested by the surviving mutants Co-Authored-By: Claude Opus 5.5 --- .../ProcessConfigurationTest.php | 193 +++++++ tests/Filesystem/XmlFileTest.php | 56 ++ tests/Manager/ProcessManagerFlowTest.php | 489 ++++++++++++++++++ tests/Task/File/Csv/CsvReaderTaskTest.php | 21 + tests/Task/File/FolderBrowserTaskTest.php | 46 ++ tests/Task/File/LineReaderTaskTest.php | 8 + .../Task/File/Xml/XmlStreamReaderTaskTest.php | 66 +++ tests/Transformer/CachedTransformerTest.php | 109 ++++ .../Xml/XpathEvaluatorTransformerTest.php | 108 ++++ 9 files changed, 1096 insertions(+) create mode 100644 tests/Configuration/ProcessConfigurationTest.php create mode 100644 tests/Manager/ProcessManagerFlowTest.php diff --git a/tests/Configuration/ProcessConfigurationTest.php b/tests/Configuration/ProcessConfigurationTest.php new file mode 100644 index 00000000..8694377d --- /dev/null +++ b/tests/Configuration/ProcessConfigurationTest.php @@ -0,0 +1,193 @@ + + */ + public static function provideSeeds(): iterable + { + foreach (range(1, 30) as $seed) { + yield 'graph '.$seed => [$seed]; + } + } + + /** + * Random acyclic graphs (fixed seeds): each group contains the connected tasks once, each task after its + * ancestors. + */ + #[DataProvider('provideSeeds')] + public function testDependencyGroupsAreSorted(int $seed): void + { + mt_srand($seed); + $count = mt_rand(2, 14); + // Tasks declared in a random order, edges from a lower to a higher rank: no cycle + $ranks = range(0, $count - 1); + shuffle($ranks); + $tasks = []; + foreach ($ranks as $rank) { + $tasks['t'.$rank] = ['outputs' => [], 'error_outputs' => []]; + } + for ($from = 0; $from < $count; ++$from) { + for ($to = $from + 1; $to < $count; ++$to) { + $random = mt_rand(0, 9); + if ($random < 2) { + $tasks['t'.$from]['outputs'][] = 't'.$to; + } elseif (2 === $random) { + $tasks['t'.$from]['error_outputs'][] = 't'.$to; + } + } + } + $processConfiguration = $this->createProcessConfiguration($tasks); + + $groups = $processConfiguration->getDependencyGroups(); + + $grouped = array_merge(...$groups); + sort($grouped); + $codes = array_keys($tasks); + sort($codes); + self::assertSame($codes, $grouped, 'Each task in a single group'); + foreach ($groups as $group) { + $positions = array_flip($group); + foreach ($group as $code) { + $task = $processConfiguration->getTaskConfiguration($code); + foreach ($group as $otherCode) { + if ($task->hasAncestor($processConfiguration->getTaskConfiguration($otherCode))) { + self::assertLessThan($positions[$code], $positions[$otherCode], "{$otherCode} before {$code}"); + } + } + // Connected tasks are in the same group + foreach ([...$task->getNextTasksConfigurations(), ...$task->getErrorTasksConfigurations()] as $next) { + self::assertArrayHasKey($next->getCode(), $positions); + } + } + } + } + + public function testChainDeclaredInReverseOrder(): void + { + $processConfiguration = $this->createProcessConfiguration([ + 'e' => [], + 'd' => ['outputs' => ['e']], + 'c' => ['outputs' => ['d']], + 'b' => ['outputs' => ['c']], + 'a' => ['outputs' => ['b']], + ]); + + self::assertSame([['a', 'b', 'c', 'd', 'e']], $processConfiguration->getDependencyGroups()); + } + + /** + * The main group is the one of the entry point, else of the end point, else of the first task. + */ + public function testMainTaskGroup(): void + { + $tasks = [ + 'a1' => ['outputs' => ['a2']], + 'a2' => [], + 'b1' => ['outputs' => ['b2']], + 'b2' => [], + 'c1' => [], + ]; + + self::assertSame(['a1', 'a2'], $this->createProcessConfiguration($tasks)->getMainTaskGroup()); + self::assertSame('a1', $this->createProcessConfiguration($tasks)->getMainTask()?->getCode()); + self::assertSame(['b1', 'b2'], $this->createProcessConfiguration($tasks, ['end_point' => 'b2'])->getMainTaskGroup()); + self::assertSame(['c1'], $this->createProcessConfiguration($tasks, ['entry_point' => 'c1', 'end_point' => 'b2'])->getMainTaskGroup()); + self::assertSame( + [['a1', 'a2'], ['b1', 'b2'], ['c1']], + $this->createProcessConfiguration($tasks)->getDependencyGroups() + ); + } + + public function testWithoutTask(): void + { + $processConfiguration = new ProcessConfiguration('test.process', []); + + self::assertNull($processConfiguration->getMainTask()); + self::assertSame([], $processConfiguration->getMainTaskGroup()); + self::assertSame([], $processConfiguration->getDependencyGroups()); + self::assertTrue($processConfiguration->isPublic(), 'Public by default'); + } + + /** + * With a cycle, the tasks are not sorted (the cycle is reported when the process is executed). + */ + public function testCycleIsNotSorted(): void + { + $processConfiguration = $this->createProcessConfiguration([ + 'c' => ['outputs' => ['a']], + 'b' => ['outputs' => ['c']], + 'a' => ['outputs' => ['b']], + 'd' => ['outputs' => ['a']], + ]); + + self::assertSame([['c', 'b', 'a', 'd']], $processConfiguration->getDependencyGroups()); + + $this->expectException(CircularProcessException::class); + $processConfiguration->checkCircularDependencies(); + } + + /** + * @param array> $tasks + * @param array $processConfiguration + */ + private function createProcessConfiguration(array $tasks, array $processConfiguration = []): ProcessConfiguration + { + $registry = new ProcessConfigurationRegistry([ + 'test.process' => $processConfiguration + [ + 'options' => [], + 'entry_point' => null, + 'end_point' => null, + 'description' => '', + 'help' => '', + 'public' => true, + 'tasks' => array_map(static fn (array $task): array => $task + [ + 'service' => '@app.task', + 'options' => [], + 'description' => '', + 'help' => '', + 'outputs' => [], + 'errors' => [], + 'error_outputs' => [], + 'error_strategy' => null, + 'log_level' => null, + ], $tasks), + ], + ], 'stop'); + + return $registry->getProcessConfiguration('test.process'); + } +} diff --git a/tests/Filesystem/XmlFileTest.php b/tests/Filesystem/XmlFileTest.php index cae50e9a..60e9584d 100644 --- a/tests/Filesystem/XmlFileTest.php +++ b/tests/Filesystem/XmlFileTest.php @@ -105,6 +105,62 @@ public function testReadRestoresLibxmlErrorHandling(): void } } + /** + * The errors left by another code (libxml_use_internal_errors(true)) are neither reported nor cleared by a valid + * file, and none is left by an invalid one. + */ + public function testReadWithPreviousLibxmlErrors(): void + { + $previous = libxml_use_internal_errors(true); + try { + (new \DOMDocument())->loadXML(''); + file_put_contents($this->path, ''); + (new XmlFile($this->path))->read(); + + file_put_contents($this->path, ''); + try { + (new XmlFile($this->path))->read(); + self::fail('An exception should have been thrown'); + } catch (\UnexpectedValueException $e) { + // Only the error of the file, trimmed, with its position + self::assertSame( + \sprintf('Invalid XML in file "%s": Premature end of data in tag root line 1 (line 1, column 7)', $this->path), + $e->getMessage() + ); + } + self::assertSame([], libxml_get_errors()); + self::assertTrue(libxml_use_internal_errors(), 'Restored'); + } finally { + libxml_clear_errors(); + libxml_use_internal_errors($previous); + } + } + + /** + * A warning (e.g. a relative namespace URI) does not prevent the reading. + */ + public function testReadToleratesWarnings(): void + { + file_put_contents($this->path, 'ok'); + + $dom = (new XmlFile($this->path))->read(); + + self::assertSame('ok', $dom->documentElement?->textContent); + } + + public function testReadTwice(): void + { + file_put_contents($this->path, 'ok'); + $file = new XmlFile($this->path); + + $first = $file->read(); + $second = $file->read(); + + self::assertNotSame($first, $second); + self::assertSame('ok', $first->documentElement?->textContent); + self::assertSame('ok', $second->documentElement?->textContent); + } + public function testWriteThrowsWhenTheXmlCannotBeGenerated(): void { $dom = new class extends \DOMDocument { diff --git a/tests/Manager/ProcessManagerFlowTest.php b/tests/Manager/ProcessManagerFlowTest.php new file mode 100644 index 00000000..6208d2bb --- /dev/null +++ b/tests/Manager/ProcessManagerFlowTest.php @@ -0,0 +1,489 @@ +events ("::"). + */ +#[CoversClass(ProcessManager::class)] +#[UsesClass(ProcessFailedException::class)] +#[UsesClass(ProcessConfiguration::class)] +#[UsesClass(TaskConfiguration::class)] +#[UsesClass(TaskRegistry::class)] +#[UsesClass(ContextualOptionResolver::class)] +#[UsesClass(ProcessEvent::class)] +#[UsesClass(AbstractLogger::class)] +#[UsesClass(ProcessHistory::class)] +#[UsesClass(ProcessState::class)] +#[UsesClass(ProcessConfigurationRegistry::class)] +#[UsesClass(DeprecatedCodeAliases::class)] +class ProcessManagerFlowTest extends TestCase +{ + /** + * @var list + */ + private array $events = []; + + /** + * @var list + */ + private array $stoppedProcesses = []; + + /** + * A failing item with the skip strategy: the next tasks are not executed for it, its error outputs receive its + * input (default error output), the next items go on. + */ + public function testSkipStrategy(): void + { + $this->execute([ + 'items' => ['type' => 'iterable', 'items' => [1, 2, 3], 'outputs' => ['check']], + 'check' => ['fail_on' => 2, 'error_strategy' => 'skip', 'outputs' => ['after'], 'error_outputs' => ['errors']], + 'after' => [], + 'errors' => [], + ]); + + self::assertSame( + ['check:execute:1', 'after:execute:1', 'check:execute:2', 'errors:execute:2', 'check:execute:3', 'after:execute:3'], + $this->getExecutions() + ); + } + + /** + * The error output set by the failing task is given to its error outputs, instead of its input. + */ + public function testErrorOutputOfTheTask(): void + { + $this->execute([ + 'items' => ['type' => 'iterable', 'items' => [1, 2], 'outputs' => ['check']], + 'check' => ['fail_on' => 2, 'error_output' => 'error 2', 'error_strategy' => 'skip', 'error_outputs' => ['errors']], + 'errors' => [], + ]); + + self::assertSame(['check:execute:1', 'check:execute:2', 'errors:execute:error 2'], $this->getExecutions()); + } + + /** + * A task stopping the process (ProcessState::stop(), without error): the next items of the parent iterable task + * are not processed, the process ends normally (not flagged as stopped: only the stop task does it). + */ + public function testStopIsPropagatedToTheParentTasks(): void + { + $result = $this->execute([ + 'items' => ['type' => 'iterable', 'items' => [1, 2, 3], 'outputs' => ['check']], + 'check' => ['stop_on' => 2, 'outputs' => ['after']], + 'after' => [], + ], ['end_point' => 'after']); + + self::assertSame(['check:execute:1', 'after:execute:1', 'check:execute:2'], $this->getExecutions()); + self::assertSame(1, $result, 'Output of the end point'); + self::assertSame([false], $this->stoppedProcesses); + } + + /** + * The stop is propagated through every parent task, up to the iterable task. + */ + public function testStopIsPropagatedThroughTheParentTasks(): void + { + $this->execute([ + 'items' => ['type' => 'iterable', 'items' => [1, 2, 3], 'outputs' => ['middle']], + 'middle' => ['outputs' => ['check']], + 'check' => ['stop_on' => 2], + ]); + + self::assertSame(['middle:execute:1', 'check:execute:1', 'middle:execute:2', 'check:execute:2'], $this->getExecutions()); + } + + /** + * The task of an error branch gets the state of the failing task, with its error. + */ + public function testErrorBranchGetsThePreviousState(): void + { + $this->execute([ + 'items' => ['type' => 'iterable', 'items' => [1, 2], 'outputs' => ['check']], + 'check' => ['fail_on' => 2, 'error_strategy' => 'skip', 'error_outputs' => ['errors']], + 'errors' => ['record_previous' => true], + ]); + + self::assertContains('errors:previous:check:check failed', $this->events); + } + + /** + * The context of the process is given to the tasks. + */ + public function testContext(): void + { + $this->createProcessManager(['single' => ['record_context' => true]])->execute('test.process', null, ['key' => 'value']); + + self::assertContains('single:context:{"key":"value"}', $this->events); + } + + /** + * A task of an error branch stopping the process. + */ + public function testStopInAnErrorBranchIsPropagated(): void + { + $this->execute([ + 'items' => ['type' => 'iterable', 'items' => [1, 2, 3], 'outputs' => ['check']], + 'check' => ['fail_on' => 2, 'error_strategy' => 'skip', 'error_outputs' => ['errors']], + 'errors' => ['stop_on' => 2], + ]); + + self::assertSame(['check:execute:1', 'check:execute:2', 'errors:execute:2'], $this->getExecutions()); + } + + /** + * Once an iterable task has no more item, its next tasks and their error outputs are flushed, down to the + * blocking tasks. (Each task is flushed again with its next tasks once resolved: only the first flushes are + * checked.). + */ + public function testFlushAfterAnIterableTask(): void + { + $this->execute([ + 'items' => ['type' => 'iterable', 'items' => [1, 2], 'outputs' => ['a']], + 'a' => ['type' => 'flushable', 'outputs' => ['b'], 'error_outputs' => ['c']], + 'b' => ['type' => 'flushable'], + 'c' => ['type' => 'flushable'], + ]); + + self::assertSame( + ['a:execute:1', 'b:execute:1', 'a:execute:2', 'b:execute:2', 'a:flush', 'b:flush', 'c:flush'], + \array_slice($this->events, 0, 7) + ); + self::assertSame([], array_diff(\array_slice($this->events, 7), ['a:flush', 'b:flush', 'c:flush']), 'Then only flushes'); + } + + /** + * A task stopping the process when it is flushed after its iterable parent: its next tasks are not flushed then. + */ + public function testStopDuringTheFlush(): void + { + $this->execute([ + 'items' => ['type' => 'iterable', 'items' => [1], 'outputs' => ['a']], + 'a' => ['type' => 'flushable', 'stop_on_flush' => true, 'outputs' => ['b']], + 'b' => ['type' => 'flushable'], + ]); + + self::assertSame(['a:execute:1', 'b:execute:1', 'a:flush'], \array_slice($this->events, 0, 3)); + self::assertNotSame('b:flush', $this->events[3] ?? null); + } + + /** + * An iterable task without item: its next tasks are not executed. + */ + public function testEmptyIterableTask(): void + { + $this->execute([ + 'items' => ['type' => 'iterable', 'items' => [], 'outputs' => ['a']], + 'a' => ['type' => 'flushable', 'outputs' => ['b']], + 'b' => [], + ]); + + self::assertSame([], $this->getExecutions()); + } + + /** + * A blocking task receives every item, then gives them to its next tasks, which are not flushed (the flush stops at + * the blocking task). + */ + public function testBlockingTask(): void + { + $this->execute([ + 'items' => ['type' => 'iterable', 'items' => [1, 2], 'outputs' => ['aggregate']], + 'aggregate' => ['type' => 'blocking', 'outputs' => ['after']], + 'after' => ['type' => 'flushable'], + ]); + + // Not flushed before the blocking task is proceeded + self::assertSame(['aggregate:execute:1', 'aggregate:execute:2', 'aggregate:proceed', 'after:execute:1,2'], \array_slice($this->events, 0, 4)); + } + + /** + * A blocking task without input is not proceeded. + */ + public function testBlockingTaskWithoutInput(): void + { + $this->execute([ + 'items' => ['type' => 'iterable', 'items' => [], 'outputs' => ['aggregate']], + 'aggregate' => ['type' => 'blocking', 'outputs' => ['after']], + 'after' => [], + ]); + + self::assertSame([], $this->events); + } + + /** + * A task which is not iterable nor blocking is flushed once resolved. + */ + public function testRootTaskIsFlushed(): void + { + $this->execute([ + 'single' => ['type' => 'flushable', 'outputs' => ['after']], + 'after' => ['type' => 'flushable'], + ]); + + self::assertSame(['single:execute:', 'after:execute:', 'single:flush', 'after:flush'], \array_slice($this->events, 0, 4)); + } + + /** + * The process can be executed again: no item of the previous execution is left (e.g. after a failure). + */ + public function testExecutedAgainAfterAFailure(): void + { + $tasks = [ + 'items' => ['type' => 'iterable', 'items' => [1, 2], 'outputs' => ['aggregate']], + 'aggregate' => ['type' => 'blocking', 'outputs' => ['check']], + 'check' => ['fail_on' => '1,2'], + ]; + $processManager = $this->createProcessManager($tasks); + try { + $processManager->execute('test.process'); + self::fail('The process should have failed'); + } catch (ProcessFailedException) { + } + $this->events = []; + + $this->expectException(ProcessFailedException::class); + try { + $processManager->execute('test.process'); + } finally { + self::assertSame(['aggregate:execute:1', 'aggregate:execute:2', 'aggregate:proceed', 'check:execute:1,2'], $this->events); + } + } + + /** + * @return list + */ + private function getExecutions(): array + { + return array_values(array_filter($this->events, static fn (string $event): bool => str_contains($event, ':execute:'))); + } + + /** + * @param array> $tasks + * @param array $process + */ + private function execute(array $tasks, array $process = []): mixed + { + return $this->createProcessManager($tasks, $process)->execute('test.process'); + } + + /** + * @param array> $tasks + * @param array $process + */ + private function createProcessManager(array $tasks, array $process = []): ProcessManager + { + $container = new Container(); + $taskConfigurations = []; + foreach ($tasks as $code => $task) { + $container->set('test.'.$code, $this->createTask($code, $task)); + $taskConfigurations[$code] = [ + 'service' => '@test.'.$code, + 'options' => [], + 'description' => '', + 'help' => '', + 'outputs' => $task['outputs'] ?? [], + 'errors' => [], + 'error_outputs' => $task['error_outputs'] ?? [], + 'error_strategy' => $task['error_strategy'] ?? null, + 'log_level' => null, + ]; + } + $registry = new ProcessConfigurationRegistry([ + 'test.process' => $process + [ + 'options' => [], + 'entry_point' => null, + 'end_point' => null, + 'description' => '', + 'help' => '', + 'public' => true, + 'tasks' => $taskConfigurations, + ], + ], 'stop'); + $eventDispatcher = new EventDispatcher(); + $eventDispatcher->addListener(ProcessEvent::EVENT_PROCESS_ENDED, function (ProcessEvent $event): void { + $this->stoppedProcesses[] = $event->isProcessStopped(); + }); + $logger = new NullLogger(); + + return new ProcessManager( + $container, + new ProcessLogger($logger), + new TaskLogger($logger), + $registry, + new ContextualOptionResolver(), + $eventDispatcher, + taskRegistry: new TaskRegistry($container), + ); + } + + /** + * @param array $options + */ + private function createTask(string $code, array $options): TaskInterface + { + $record = function (string $event): void { + $this->events[] = $event; + }; + $execute = static function (ProcessState $state) use ($code, $options, $record): void { + $input = $state->getInput(); + $inputString = \is_array($input) ? implode(',', $input) : (string) $input; + $record("{$code}:execute:{$inputString}"); + if ($options['record_previous'] ?? false) { + $previousState = $state->getPreviousState(); + $record(\sprintf('%s:previous:%s:%s', $code, $previousState?->getTaskConfiguration()->getCode(), $previousState?->getException()?->getMessage())); + } + if ($options['record_context'] ?? false) { + $record("{$code}:context:".json_encode($state->getContext())); + } + if (isset($options['stop_on']) && $options['stop_on'] === $input) { + $state->stop(); + + return; + } + // fail_on: an item, or the imploded items received by the task + if (isset($options['fail_on']) && \in_array($options['fail_on'], [$input, $inputString], true)) { + if (isset($options['error_output'])) { + $state->setErrorOutput($options['error_output']); + } + + throw new \RuntimeException("{$code} failed"); + } + $state->setOutput($input); + }; + + return match ($options['type'] ?? null) { + 'iterable' => new class($options['items']) implements IterableTaskInterface { + private int $position = 0; + + /** + * @param list $items + */ + public function __construct(private readonly array $items) + { + } + + public function execute(ProcessState $state): void + { + if ([] === $this->items) { + $state->setSkipped(true); + + return; + } + $state->setOutput($this->items[$this->position]); + } + + public function next(ProcessState $state): bool + { + ++$this->position; + if ($this->position >= \count($this->items)) { + $this->position = 0; + + return false; + } + + return true; + } + }, + 'flushable' => new class($execute, $record, $code, $options['stop_on_flush'] ?? false) implements FlushableTaskInterface { + public function __construct( + private readonly \Closure $execute, + private readonly \Closure $record, + private readonly string $code, + private readonly bool $stopOnFlush, + ) { + } + + public function execute(ProcessState $state): void + { + ($this->execute)($state); + } + + public function flush(ProcessState $state): void + { + ($this->record)("{$this->code}:flush"); + if ($this->stopOnFlush) { + $state->stop(); + + return; + } + $state->setSkipped(true); + } + }, + 'blocking' => new class($record, $code) implements BlockingTaskInterface { + /** + * @var list + */ + private array $inputs = []; + + public function __construct( + private readonly \Closure $record, + private readonly string $code, + ) { + } + + public function execute(ProcessState $state): void + { + ($this->record)("{$this->code}:execute:{$state->getInput()}"); + $this->inputs[] = $state->getInput(); + } + + public function proceed(ProcessState $state): void + { + ($this->record)("{$this->code}:proceed"); + $state->setOutput($this->inputs); + $this->inputs = []; + } + }, + default => new class($execute) implements TaskInterface { + public function __construct(private readonly \Closure $execute) + { + } + + public function execute(ProcessState $state): void + { + ($this->execute)($state); + } + }, + }; + } +} diff --git a/tests/Task/File/Csv/CsvReaderTaskTest.php b/tests/Task/File/Csv/CsvReaderTaskTest.php index bb38a8dd..dd448bc4 100644 --- a/tests/Task/File/Csv/CsvReaderTaskTest.php +++ b/tests/Task/File/Csv/CsvReaderTaskTest.php @@ -29,6 +29,7 @@ use PHPUnit\Framework\TestCase; use Psr\Log\NullLogger; use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; #[\PHPUnit\Framework\Attributes\CoversClass(CsvReaderTask::class)] #[\PHPUnit\Framework\Attributes\CoversClass(InputCsvReaderTask::class)] @@ -111,6 +112,26 @@ public function testInputCsvReaderWithoutPathThrows(): void $task->execute($state); } + /** + * The input is a path relative to base_path, with or without its trailing slash. + */ + public function testInputCsvReaderBasePath(): void + { + $expected = [['id' => '3', 'name' => 'baz']]; + foreach ([$this->tmpDir, $this->tmpDir.'/'] as $basePath) { + $task = new InputCsvReaderTask(new NullLogger()); + self::assertSame($expected, $this->iterate($task, $this->createState(['base_path' => $basePath]), 'b.csv'), $basePath); + $task->finalize($this->createState([])); + } + } + + public function testInputCsvReaderBasePathMustBeAString(): void + { + $this->expectException(InvalidOptionsException::class); + + $this->iterate(new InputCsvReaderTask(new NullLogger()), $this->createState(['base_path' => ['a']]), 'b.csv'); + } + /** * Mimics the ProcessManager loop over an iterable task and returns the non-skipped outputs. * diff --git a/tests/Task/File/FolderBrowserTaskTest.php b/tests/Task/File/FolderBrowserTaskTest.php index 0c3cee5a..89391cff 100644 --- a/tests/Task/File/FolderBrowserTaskTest.php +++ b/tests/Task/File/FolderBrowserTaskTest.php @@ -16,6 +16,7 @@ use CleverAge\ProcessBundle\Configuration\ProcessConfiguration; use CleverAge\ProcessBundle\Configuration\TaskConfiguration; use CleverAge\ProcessBundle\Context\ContextualOptionResolver; +use CleverAge\ProcessBundle\Exception\LogicException; use CleverAge\ProcessBundle\Model\AbstractConfigurableTask; use CleverAge\ProcessBundle\Model\ProcessHistory; use CleverAge\ProcessBundle\Model\ProcessState; @@ -24,6 +25,7 @@ use PHPUnit\Framework\TestCase; use Psr\Log\NullLogger; use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; #[\PHPUnit\Framework\Attributes\CoversClass(FolderBrowserTask::class)] #[\PHPUnit\Framework\Attributes\CoversClass(InputFolderBrowserTask::class)] @@ -109,6 +111,50 @@ public function testInputFolderBrowserRequiresAFolderPathAsInput(): void $this->iterate($task, $state); } + /** + * The input is concatenated to base_folder_path (without separator). + */ + public function testInputFolderBrowserBaseFolderPath(): void + { + $task = new InputFolderBrowserTask(new NullLogger()); + $state = $this->createState(['base_folder_path' => $this->tmpDir.'/']); + + self::assertSame([$this->tmpDir.'/dirB/b1.txt'], $this->iterate($task, $state, 'dirB')); + } + + /** + * The options are validated when the task is initialized, before any input. + */ + public function testInputFolderBrowserOptionsAreValidatedOnInitialization(): void + { + $task = new InputFolderBrowserTask(new NullLogger()); + + $this->expectException(InvalidOptionsException::class); + + $task->initialize($this->createState(['base_folder_path' => ['a']])); + } + + /** + * Another folder cannot be given while a folder is browsed. + */ + public function testInputFolderBrowserRejectsAnotherFolderWhileBrowsing(): void + { + $task = new InputFolderBrowserTask(new NullLogger()); + $state = $this->createState([]); + $state->setInput($this->tmpDir.'/dirA'); + $task->execute($state); + self::assertTrue($task->next($state)); + + // The same folder is accepted + $task->execute($state); + + $state->setInput($this->tmpDir.'/dirB'); + $this->expectException(LogicException::class); + $this->expectExceptionMessage("Folder path '{$this->tmpDir}/dirB' already initialized with a different value {$this->tmpDir}/dirA"); + + $task->execute($state); + } + /** * Mimics the ProcessManager loop over an iterable task and returns the non-skipped outputs. * diff --git a/tests/Task/File/LineReaderTaskTest.php b/tests/Task/File/LineReaderTaskTest.php index 1da68ee0..fff1bc3b 100644 --- a/tests/Task/File/LineReaderTaskTest.php +++ b/tests/Task/File/LineReaderTaskTest.php @@ -23,6 +23,7 @@ use CleverAge\ProcessBundle\Task\File\LineReaderTask; use PHPUnit\Framework\TestCase; use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; #[\PHPUnit\Framework\Attributes\CoversClass(LineReaderTask::class)] #[\PHPUnit\Framework\Attributes\CoversClass(InputLineReaderTask::class)] @@ -93,6 +94,13 @@ public function testInputLineReaderReadsDifferentFilesSuccessively(): void self::assertSame(["b1\n"], $this->iterate($task, $state, $this->tmpDir.'/b.txt')); } + public function testFilenameMustBeAString(): void + { + $this->expectException(InvalidOptionsException::class); + + $this->iterate(new LineReaderTask(), $this->createState(['filename' => ['a.txt']])); + } + /** * Mimics the ProcessManager loop over an iterable task and returns the non-skipped outputs. * diff --git a/tests/Task/File/Xml/XmlStreamReaderTaskTest.php b/tests/Task/File/Xml/XmlStreamReaderTaskTest.php index 0cbc6bb3..eb7d3000 100644 --- a/tests/Task/File/Xml/XmlStreamReaderTaskTest.php +++ b/tests/Task/File/Xml/XmlStreamReaderTaskTest.php @@ -225,6 +225,72 @@ public function testFilePathMustBeAString(): void $this->iterate(['file_path' => 42, 'node_name' => 'product']); } + /** + * The root element can be iterated. + */ + public function testRootElement(): void + { + $outputs = $this->iterate(['file_path' => $this->tmpDir.'/catalog.xml', 'node_name' => 'catalog']); + + self::assertCount(1, $outputs); + self::assertSame('catalog', $outputs[0]->nodeName); + } + + public function testDirectoryThrows(): void + { + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionMessage(\sprintf('XML file "%s" does not exist or is not readable', $this->tmpDir)); + + $this->iterate(['file_path' => $this->tmpDir, 'node_name' => 'product']); + } + + /** + * A warning (e.g. a relative namespace URI) does not prevent the reading. + */ + public function testWarningsAreTolerated(): void + { + file_put_contents($this->tmpDir.'/warning.xml', 'Foo'); + + $outputs = $this->iterate(['file_path' => $this->tmpDir.'/warning.xml', 'node_name' => 'product']); + + self::assertCount(1, $outputs); + self::assertSame('Foo', $outputs[0]->textContent); + } + + /** + * The errors left by another code (libxml_use_internal_errors(true)) are not reported, none is left by an invalid + * file, and the setting is restored. + */ + public function testLibxmlStateIsRestored(): void + { + file_put_contents($this->tmpDir.'/invalid.xml', ''); + $previous = libxml_use_internal_errors(true); + try { + (new \DOMDocument())->loadXML(''); + + self::assertCount(2, $this->iterate(['file_path' => $this->tmpDir.'/catalog.xml', 'node_name' => 'product'])); + + try { + $this->iterate(['file_path' => $this->tmpDir.'/invalid.xml', 'node_name' => 'product']); + self::fail('An exception should have been thrown'); + } catch (\UnexpectedValueException $exception) { + // Trimmed messages, with their position + self::assertStringStartsWith( + \sprintf('Invalid XML in file "%s": Opening and ending tag mismatch: product line 1 and catalog (line 1, column ', $this->tmpDir.'/invalid.xml'), + $exception->getMessage() + ); + } + self::assertSame([], libxml_get_errors()); + self::assertTrue(libxml_use_internal_errors(false), 'Restored'); + + self::assertCount(2, $this->iterate(['file_path' => $this->tmpDir.'/catalog.xml', 'node_name' => 'product'])); + self::assertFalse(libxml_use_internal_errors(false), 'Restored'); + } finally { + libxml_clear_errors(); + libxml_use_internal_errors($previous); + } + } + /** * @param array $options * diff --git a/tests/Transformer/CachedTransformerTest.php b/tests/Transformer/CachedTransformerTest.php index 5d1a6663..a5a6a901 100644 --- a/tests/Transformer/CachedTransformerTest.php +++ b/tests/Transformer/CachedTransformerTest.php @@ -16,10 +16,17 @@ use CleverAge\ProcessBundle\Registry\TransformerRegistry; use CleverAge\ProcessBundle\Transformer\CachedTransformer; use CleverAge\ProcessBundle\Transformer\CastTransformer; +use CleverAge\ProcessBundle\Transformer\TransformerInterface; +use Monolog\Handler\TestHandler; +use Monolog\Logger; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; +use Psr\Cache\CacheItemInterface; +use Psr\Cache\CacheItemPoolInterface; use Psr\Log\NullLogger; use Symfony\Component\Cache\Adapter\ArrayAdapter; use Symfony\Component\DependencyInjection\ServiceLocator; +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; use Symfony\Component\OptionsResolver\OptionsResolver; #[\PHPUnit\Framework\Attributes\CoversClass(CachedTransformer::class)] @@ -93,6 +100,108 @@ public function testTransformNonStringKeyValueBypassesCache(): void $this->assertSame([], $this->cache->getValues()); } + /** + * On a miss, the transformers are applied once, and the value is cached until the ttl. + */ + public function testTtl(): void + { + $counter = new class implements TransformerInterface { + public int $calls = 0; + + public function transform(mixed $value, array $options = []): mixed + { + ++$this->calls; + + return $value.'!'; + } + + public function getCode(): string + { + return 'count'; + } + }; + $registry = new TransformerRegistry(new ServiceLocator(['count' => static fn (): TransformerInterface => $counter])); + $transformer = new CachedTransformer($registry, $this->cache, new NullLogger()); + $resolve = static function (array $options) use ($transformer): array { + $resolver = new OptionsResolver(); + $transformer->configureOptions($resolver); + + return $resolver->resolve($options + ['cache_key' => 'prefix', 'transformers' => ['count' => null]]); + }; + + self::assertSame('a!', $transformer->transform('a', $resolve([]))); + self::assertSame(1, $counter->calls); + self::assertSame('a!', $transformer->transform('a', $resolve([]))); + self::assertSame(1, $counter->calls, 'From the cache'); + + // Already expired: not cached + self::assertSame('b!', $transformer->transform('b', $resolve(['ttl' => '-1 second']))); + self::assertSame('b!', $transformer->transform('b', $resolve(['ttl' => new \DateTimeImmutable('-1 second')]))); + self::assertSame(3, $counter->calls); + + self::assertSame('c!', $transformer->transform('c', $resolve(['ttl' => '+1 hour']))); + self::assertSame('c!', $transformer->transform('c', $resolve(['ttl' => '+1 hour']))); + self::assertSame(4, $counter->calls); + } + + public function testTtlIsNormalized(): void + { + $ttl = $this->resolveOptions(['cache_key' => 'prefix', 'ttl' => '+1 hour'])['ttl']; + self::assertInstanceOf(\DateTime::class, $ttl); + self::assertEqualsWithDelta(time() + 3600, $ttl->getTimestamp(), 5); + + $dateTime = new \DateTimeImmutable(); + self::assertSame($dateTime, $this->resolveOptions(['cache_key' => 'prefix', 'ttl' => $dateTime])['ttl']); + self::assertNull($this->resolveOptions(['cache_key' => 'prefix'])['ttl']); + } + + /** + * @return iterable}> + */ + public static function provideInvalidOptions(): iterable + { + yield 'cache_key not a string' => [['cache_key' => 1]]; + yield 'ttl not a string nor a date' => [['cache_key' => 'prefix', 'ttl' => 3600]]; + } + + /** + * @param array $options + */ + #[DataProvider('provideInvalidOptions')] + public function testInvalidOptions(array $options): void + { + $this->expectException(InvalidOptionsException::class); + + $this->resolveOptions($options); + } + + public function testFailureToSaveIsLogged(): void + { + $item = $this->createStub(CacheItemInterface::class); + $item->method('isHit')->willReturn(false); + $item->method('set')->willReturnSelf(); + $cache = $this->createMock(CacheItemPoolInterface::class); + $cache->method('getItem')->willReturn($item); + $cache->expects(self::once())->method('saveDeferred')->with($item)->willReturn(false); + $logs = new TestHandler(); + $transformer = new CachedTransformer($this->createStub(TransformerRegistry::class), $cache, new Logger('test', [$logs])); + + self::assertSame('a', $transformer->transform('a', ['cache_key' => 'prefix', 'transformers' => [], 'key_transformers' => [], 'ttl' => null])); + self::assertTrue($logs->hasWarning(['message' => 'Cannot save cache item', 'context' => ['cache_key' => 'prefix|a']])); + } + + /** + * A key value which is not a string: the cache is not used. + */ + public function testNonStringKeyValueDoesNotUseTheCache(): void + { + $cache = $this->createMock(CacheItemPoolInterface::class); + $cache->expects(self::never())->method('getItem'); + $transformer = new CachedTransformer($this->createStub(TransformerRegistry::class), $cache, new NullLogger()); + + self::assertSame(42, $transformer->transform(42, ['cache_key' => 'prefix', 'transformers' => [], 'key_transformers' => [], 'ttl' => null])); + } + public function testGetCodeReturnsCorrectCode(): void { $this->assertSame('cached', $this->transformer->getCode()); diff --git a/tests/Transformer/Xml/XpathEvaluatorTransformerTest.php b/tests/Transformer/Xml/XpathEvaluatorTransformerTest.php index deb2e256..1bee8249 100644 --- a/tests/Transformer/Xml/XpathEvaluatorTransformerTest.php +++ b/tests/Transformer/Xml/XpathEvaluatorTransformerTest.php @@ -222,6 +222,114 @@ public function testInvalidNamespaces(mixed $namespaces): void $this->resolve(['query' => '/a', 'namespaces' => $namespaces]); } + public function testMissingResult(): void + { + $domDocument = new \DOMDocument(); + $domDocument->loadXML('1'); + $transformer = new XpathEvaluatorTransformer(); + + // ignore_missing (default): null, or an empty list without single_result + $this->assertNull($transformer->transform($domDocument, $this->resolve(['query' => '//missing/text()']))); + $this->assertSame([], $transformer->transform($domDocument, $this->resolve(['query' => '//missing/text()', 'single_result' => false]))); + // A result is still returned without ignore_missing + $this->assertSame('1', $transformer->transform($domDocument, $this->resolve(['query' => '//item/text()', 'ignore_missing' => false]))); + $this->assertSame([], $transformer->transform($domDocument, $this->resolve(['query' => '//missing/text()', 'single_result' => false, 'ignore_missing' => false]))); + + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionMessage("There is not enough results for query '//missing/text()'"); + + $transformer->transform($domDocument, $this->resolve(['query' => '//missing/text()', 'ignore_missing' => false])); + } + + public function testTooManyResults(): void + { + $domDocument = new \DOMDocument(); + $domDocument->loadXML('12'); + + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionMessage("There is too much results for query '//item/text()'"); + + (new XpathEvaluatorTransformer())->transform($domDocument, $this->resolve(['query' => '//item/text()'])); + } + + /** + * The options of a subquery override the ones of the root level. + */ + public function testSubqueryOptions(): void + { + $domDocument = new \DOMDocument(); + $domDocument->loadXML('12'); + + $this->assertSame( + ['all' => ['1', '2'], 'first' => '1', 'missing' => []], + (new XpathEvaluatorTransformer())->transform($domDocument, $this->resolve([ + 'query' => [ + 'all' => '//item/text()', + 'first' => ['subquery' => '//item[1]/text()', 'single_result' => true], + 'missing' => '//missing/text()', + ], + 'single_result' => false, + ])) + ); + } + + /** + * @return iterable}> + */ + public static function provideInvalidOptions(): iterable + { + yield 'query not a string nor an array' => [['query' => 1]]; + yield 'subquery not a string' => [['query' => ['a' => ['subquery' => 1]]]]; + yield 'single_result not a boolean' => [['query' => '/a', 'single_result' => 'yes']]; + yield 'ignore_missing not a boolean' => [['query' => '/a', 'ignore_missing' => 1]]; + yield 'unwrap_value not a boolean' => [['query' => '/a', 'unwrap_value' => 'no']]; + yield 'subquery option not a boolean' => [['query' => ['a' => ['subquery' => '/a', 'single_result' => 'yes']]]]; + } + + /** + * @param array $options + */ + #[DataProvider('provideInvalidOptions')] + public function testInvalidOptions(array $options): void + { + $this->expectException(InvalidOptionsException::class); + + $this->resolve($options); + } + + /** + * The libxml errors are collected for the query only: the previous ones are not reported, none is left, and the + * previous libxml_use_internal_errors() setting is restored. + */ + public function testLibxmlStateIsRestored(): void + { + $domDocument = new \DOMDocument(); + $domDocument->loadXML('ok'); + $transformer = new XpathEvaluatorTransformer(); + $previousUseErrors = libxml_use_internal_errors(true); + try { + // An error left by another code + (new \DOMDocument())->loadXML(''); + self::assertNotSame([], libxml_get_errors()); + + try { + $transformer->transform($domDocument, $this->resolve(['query' => '//['])); + self::fail('The query is invalid'); + } catch (\UnexpectedValueException $exception) { + // Only the error of the query, trimmed + self::assertSame("Invalid xpath query '//[': Invalid expression", $exception->getMessage()); + } + self::assertSame([], libxml_get_errors()); + self::assertTrue(libxml_use_internal_errors(false), 'Restored'); + + $this->assertSame('ok', $transformer->transform($domDocument, $this->resolve(['query' => '/a/text()']))); + self::assertFalse(libxml_use_internal_errors(false), 'Restored'); + } finally { + libxml_clear_errors(); + libxml_use_internal_errors($previousUseErrors); + } + } + /** * @param array $options *