|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the CleverAge/ProcessBundle package. |
| 7 | + * |
| 8 | + * Copyright (c) Clever-Age |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CleverAge\ProcessBundle\Tests\Task; |
| 15 | + |
| 16 | +use CleverAge\ProcessBundle\Configuration\ProcessConfiguration; |
| 17 | +use CleverAge\ProcessBundle\Configuration\TaskConfiguration; |
| 18 | +use CleverAge\ProcessBundle\Context\ContextualOptionResolver; |
| 19 | +use CleverAge\ProcessBundle\Model\AbstractConfigurableTask; |
| 20 | +use CleverAge\ProcessBundle\Model\ProcessHistory; |
| 21 | +use CleverAge\ProcessBundle\Model\ProcessState; |
| 22 | +use CleverAge\ProcessBundle\Task\AbstractIterableOutputTask; |
| 23 | +use CleverAge\ProcessBundle\Task\ConstantIterableOutputTask; |
| 24 | +use CleverAge\ProcessBundle\Task\InputIteratorTask; |
| 25 | +use CleverAge\ProcessBundle\Task\SplitJoinLineTask; |
| 26 | +use PHPUnit\Framework\TestCase; |
| 27 | + |
| 28 | +#[\PHPUnit\Framework\Attributes\CoversClass(AbstractIterableOutputTask::class)] |
| 29 | +#[\PHPUnit\Framework\Attributes\UsesClass(ConstantIterableOutputTask::class)] |
| 30 | +#[\PHPUnit\Framework\Attributes\UsesClass(InputIteratorTask::class)] |
| 31 | +#[\PHPUnit\Framework\Attributes\UsesClass(SplitJoinLineTask::class)] |
| 32 | +#[\PHPUnit\Framework\Attributes\UsesClass(AbstractConfigurableTask::class)] |
| 33 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessConfiguration::class)] |
| 34 | +#[\PHPUnit\Framework\Attributes\UsesClass(TaskConfiguration::class)] |
| 35 | +#[\PHPUnit\Framework\Attributes\UsesClass(ContextualOptionResolver::class)] |
| 36 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessHistory::class)] |
| 37 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessState::class)] |
| 38 | +class AbstractIterableOutputTaskTest extends TestCase |
| 39 | +{ |
| 40 | + public function testEmptyConstantIterableIsSkipped(): void |
| 41 | + { |
| 42 | + $task = new ConstantIterableOutputTask(); |
| 43 | + $state = $this->createState(['output' => []]); |
| 44 | + $task->initialize($state); |
| 45 | + |
| 46 | + $task->execute($state); |
| 47 | + |
| 48 | + self::assertTrue($state->isSkipped()); |
| 49 | + self::assertNull($state->getException()); |
| 50 | + self::assertSame([], $state->getErrorContext()); |
| 51 | + self::assertFalse($task->next($state)); |
| 52 | + } |
| 53 | + |
| 54 | + public function testEmptyInputIteratorIsSkipped(): void |
| 55 | + { |
| 56 | + $task = new InputIteratorTask(); |
| 57 | + $state = $this->createState([], []); |
| 58 | + $task->initialize($state); |
| 59 | + |
| 60 | + $task->execute($state); |
| 61 | + |
| 62 | + self::assertTrue($state->isSkipped()); |
| 63 | + self::assertSame([], $state->getErrorContext()); |
| 64 | + self::assertFalse($task->next($state)); |
| 65 | + } |
| 66 | + |
| 67 | + public function testIterationSetsIteratorKeyInErrorContext(): void |
| 68 | + { |
| 69 | + $task = new InputIteratorTask(); |
| 70 | + $state = $this->createState([], ['a' => 'foo', 'b' => 'bar']); |
| 71 | + $task->initialize($state); |
| 72 | + |
| 73 | + $outputs = []; |
| 74 | + $keys = []; |
| 75 | + do { |
| 76 | + $state->setSkipped(false); |
| 77 | + $task->execute($state); |
| 78 | + $outputs[] = $state->getOutput(); |
| 79 | + $keys[] = $state->getErrorContext()['iterator_key'] ?? null; |
| 80 | + } while ($task->next($state)); |
| 81 | + |
| 82 | + self::assertSame(['foo', 'bar'], $outputs); |
| 83 | + self::assertSame(['a', 'b'], $keys); |
| 84 | + self::assertSame([], $state->getErrorContext()); |
| 85 | + |
| 86 | + // A new input starts a new iteration cycle; an empty one is skipped |
| 87 | + $state->setInput([]); |
| 88 | + $task->execute($state); |
| 89 | + self::assertTrue($state->isSkipped()); |
| 90 | + self::assertSame([], $state->getErrorContext()); |
| 91 | + } |
| 92 | + |
| 93 | + public function testSplitJoinLineWithoutSplitColumnIsSkipped(): void |
| 94 | + { |
| 95 | + $task = new SplitJoinLineTask(); |
| 96 | + $state = $this->createState(['split_columns' => [], 'join_column' => 'value'], ['name' => 'Item1']); |
| 97 | + $task->initialize($state); |
| 98 | + |
| 99 | + $task->execute($state); |
| 100 | + |
| 101 | + self::assertTrue($state->isSkipped()); |
| 102 | + self::assertSame([], $state->getErrorContext()); |
| 103 | + } |
| 104 | + |
| 105 | + private function createState(array $options, mixed $input = null): ProcessState |
| 106 | + { |
| 107 | + $processConfiguration = new ProcessConfiguration('test', []); |
| 108 | + $state = new ProcessState($processConfiguration, new ProcessHistory($processConfiguration)); |
| 109 | + $state->setContextualOptionResolver(new ContextualOptionResolver()); |
| 110 | + $state->setContext([]); |
| 111 | + $state->setTaskConfiguration(new TaskConfiguration('iterate', AbstractIterableOutputTask::class, $options)); |
| 112 | + $state->setInput($input); |
| 113 | + |
| 114 | + return $state; |
| 115 | + } |
| 116 | +} |
0 commit comments