|
| 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\Manager; |
| 15 | + |
| 16 | +use CleverAge\ProcessBundle\Configuration\ProcessConfiguration; |
| 17 | +use CleverAge\ProcessBundle\Configuration\TaskConfiguration; |
| 18 | +use CleverAge\ProcessBundle\Context\ContextualOptionResolver; |
| 19 | +use CleverAge\ProcessBundle\Event\ProcessEvent; |
| 20 | +use CleverAge\ProcessBundle\Exception\ProcessFailedException; |
| 21 | +use CleverAge\ProcessBundle\Logger\AbstractLogger; |
| 22 | +use CleverAge\ProcessBundle\Logger\ProcessLogger; |
| 23 | +use CleverAge\ProcessBundle\Logger\TaskLogger; |
| 24 | +use CleverAge\ProcessBundle\Manager\ProcessManager; |
| 25 | +use CleverAge\ProcessBundle\Model\AbstractConfigurableTask; |
| 26 | +use CleverAge\ProcessBundle\Model\ProcessHistory; |
| 27 | +use CleverAge\ProcessBundle\Model\ProcessState; |
| 28 | +use CleverAge\ProcessBundle\Model\TaskInterface; |
| 29 | +use CleverAge\ProcessBundle\Registry\ProcessConfigurationRegistry; |
| 30 | +use CleverAge\ProcessBundle\Task\ConstantOutputTask; |
| 31 | +use PHPUnit\Framework\TestCase; |
| 32 | +use Psr\Log\NullLogger; |
| 33 | +use Symfony\Component\DependencyInjection\Container; |
| 34 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 35 | + |
| 36 | +#[\PHPUnit\Framework\Attributes\CoversClass(ProcessManager::class)] |
| 37 | +#[\PHPUnit\Framework\Attributes\CoversClass(ProcessFailedException::class)] |
| 38 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessConfiguration::class)] |
| 39 | +#[\PHPUnit\Framework\Attributes\UsesClass(TaskConfiguration::class)] |
| 40 | +#[\PHPUnit\Framework\Attributes\UsesClass(ContextualOptionResolver::class)] |
| 41 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessEvent::class)] |
| 42 | +#[\PHPUnit\Framework\Attributes\UsesClass(AbstractLogger::class)] |
| 43 | +#[\PHPUnit\Framework\Attributes\UsesClass(AbstractConfigurableTask::class)] |
| 44 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessHistory::class)] |
| 45 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessState::class)] |
| 46 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessConfigurationRegistry::class)] |
| 47 | +#[\PHPUnit\Framework\Attributes\UsesClass(ConstantOutputTask::class)] |
| 48 | +class ProcessManagerStopStrategyTest extends TestCase |
| 49 | +{ |
| 50 | + public function testStopStrategyThrowsAProcessFailedExceptionWithTheOriginalException(): void |
| 51 | + { |
| 52 | + $originalException = new \LogicException('Something went wrong', 42); |
| 53 | + $processManager = $this->createProcessManager($originalException); |
| 54 | + |
| 55 | + try { |
| 56 | + $processManager->execute('test.process'); |
| 57 | + self::fail('The process should have failed'); |
| 58 | + } catch (\Exception $exception) { // An \Exception, not an \Error: catchable with catch (\Exception) |
| 59 | + self::assertInstanceOf(ProcessFailedException::class, $exception); |
| 60 | + self::assertSame( |
| 61 | + "Process test.process has failed during process fail with message: 'Something went wrong'.\n", |
| 62 | + $exception->getMessage() |
| 63 | + ); |
| 64 | + self::assertSame($originalException, $exception->getPrevious()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public function testSkipStrategyDoesNotThrow(): void |
| 69 | + { |
| 70 | + $processManager = $this->createProcessManager(new \LogicException('Something went wrong'), 'skip'); |
| 71 | + |
| 72 | + self::assertNull($processManager->execute('test.process')); |
| 73 | + } |
| 74 | + |
| 75 | + private function createProcessManager(\Throwable $exception, string $errorStrategy = 'stop'): ProcessManager |
| 76 | + { |
| 77 | + $failingTask = new class($exception) implements TaskInterface { |
| 78 | + public function __construct( |
| 79 | + private readonly \Throwable $exception, |
| 80 | + ) { |
| 81 | + } |
| 82 | + |
| 83 | + public function execute(ProcessState $state): void |
| 84 | + { |
| 85 | + throw $this->exception; |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + $container = new Container(); |
| 90 | + $container->set('test.constant', new ConstantOutputTask()); |
| 91 | + $container->set('test.failing', $failingTask); |
| 92 | + |
| 93 | + $registry = new ProcessConfigurationRegistry( |
| 94 | + [ |
| 95 | + 'test.process' => [ |
| 96 | + 'options' => [], |
| 97 | + 'entry_point' => null, |
| 98 | + 'end_point' => null, |
| 99 | + 'description' => '', |
| 100 | + 'help' => '', |
| 101 | + 'public' => true, |
| 102 | + 'tasks' => [ |
| 103 | + 'entry' => $this->createTaskConfiguration('@test.constant', ['output' => 'value'], ['fail']), |
| 104 | + 'fail' => $this->createTaskConfiguration('@test.failing', [], [], $errorStrategy), |
| 105 | + ], |
| 106 | + ], |
| 107 | + ], |
| 108 | + 'stop' |
| 109 | + ); |
| 110 | + |
| 111 | + return new ProcessManager( |
| 112 | + $container, |
| 113 | + new ProcessLogger(new NullLogger()), |
| 114 | + new TaskLogger(new NullLogger()), |
| 115 | + $registry, |
| 116 | + new ContextualOptionResolver(), |
| 117 | + new EventDispatcher(), |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param array<string, mixed> $options |
| 123 | + * @param list<string> $outputs |
| 124 | + * |
| 125 | + * @return array<string, mixed> |
| 126 | + */ |
| 127 | + private function createTaskConfiguration( |
| 128 | + string $service, |
| 129 | + array $options = [], |
| 130 | + array $outputs = [], |
| 131 | + ?string $errorStrategy = null, |
| 132 | + ): array { |
| 133 | + return [ |
| 134 | + 'service' => $service, |
| 135 | + 'options' => $options, |
| 136 | + 'description' => '', |
| 137 | + 'help' => '', |
| 138 | + 'outputs' => $outputs, |
| 139 | + 'errors' => [], |
| 140 | + 'error_outputs' => [], |
| 141 | + 'error_strategy' => $errorStrategy, |
| 142 | + 'log_level' => null, |
| 143 | + ]; |
| 144 | + } |
| 145 | +} |
0 commit comments