diff --git a/src/Internal/Workflow/Process/DeferredGenerator.php b/src/Internal/Workflow/Process/DeferredGenerator.php index 99ef8571..f54fabc5 100644 --- a/src/Internal/Workflow/Process/DeferredGenerator.php +++ b/src/Internal/Workflow/Process/DeferredGenerator.php @@ -185,12 +185,14 @@ private function start(): void return; } - $this->generator = (static function (mixed $result) { + /** @psalm-suppress all */ + $this->generator = (static function (mixed $result): \Generator { return $result; yield; })($result); $this->finished = true; } catch (\Throwable $e) { + $this->generator = self::getDummyGenerator(); $this->handleException($e); } finally { unset($this->handler, $this->values); @@ -212,4 +214,18 @@ private function handleException(\Throwable $e): never $this->catchers = []; throw $e; } + + private static function getDummyGenerator(): \Generator + { + static $generator; + + if ($generator === null) { + $generator = (static function (): \Generator { + yield; + })(); + $generator->current(); + } + + return $generator; + } } diff --git a/src/Internal/Workflow/Process/Scope.php b/src/Internal/Workflow/Process/Scope.php index b757cd60..c83e8f08 100644 --- a/src/Internal/Workflow/Process/Scope.php +++ b/src/Internal/Workflow/Process/Scope.php @@ -336,7 +336,6 @@ function () use ($cancelID): void { protected function call(\Closure $handler, ValuesInterface $values): DeferredGenerator { $generator = DeferredGenerator::fromHandler($handler, $values) - ->catch(tr(...)) ->catch($this->onException(...)); // Run lazy generator $generator->current(); diff --git a/tests/Unit/Workflow/DeferredGeneratorTestCase.php b/tests/Unit/Workflow/DeferredGeneratorTestCase.php index e7cc8cc8..691c9b37 100644 --- a/tests/Unit/Workflow/DeferredGeneratorTestCase.php +++ b/tests/Unit/Workflow/DeferredGeneratorTestCase.php @@ -32,7 +32,7 @@ public function testSimple(): void ); } - public function testSendingValues(): void + public function testCompareSendingValues(): void { $this->compare( fn() => (function () { @@ -50,7 +50,7 @@ public function testSendingValues(): void ); } - public function testThrowingExceptions(): void + public function testCompareThrowingExceptions(): void { $this->compare( fn() => (function () { @@ -71,7 +71,7 @@ public function testThrowingExceptions(): void ); } - public function testReturn(): void + public function testCompareReturn(): void { $this->compare( fn() => (function () { @@ -86,7 +86,7 @@ public function testReturn(): void ); } - public function testEmpty(): void + public function testCompareEmpty(): void { $this->compare( fn() => (function () { @@ -100,7 +100,7 @@ public function testEmpty(): void ); } - public function testEmptyReturn(): void + public function testCompareEmptyReturn(): void { $this->compare( fn() => (function () { @@ -115,7 +115,7 @@ public function testEmptyReturn(): void ); } - public function testEmptyThrow(): void + public function testCompareEmptyThrow(): void { $this->compare( fn() => (function () { @@ -126,7 +126,7 @@ public function testEmptyThrow(): void ); } - public function testEmptyThrowValid(): void + public function testCompareEmptyThrowValid(): void { $this->compare( fn() => (function () { @@ -137,7 +137,7 @@ public function testEmptyThrowValid(): void ); } - public function testEmptyThrowGetReturn(): void + public function testCompareEmptyThrowGetReturn(): void { $this->compare( fn() => (function () { @@ -148,7 +148,7 @@ public function testEmptyThrowGetReturn(): void ); } - public function testEmptyThrowGetKey(): void + public function testCompareEmptyThrowGetKey(): void { $this->compare( fn() => (function () { @@ -159,6 +159,45 @@ public function testEmptyThrowGetKey(): void ); } + public function testLazyNotGeneratorValidGetReturn(): void + { + $lazy = DeferredGenerator::fromHandler(fn() => 42, EncodedValues::empty()); + + $this->assertFalse($lazy->valid()); + $this->assertSame(42, $lazy->getReturn()); + } + + public function testLazyNotGeneratorCurrent(): void + { + $lazy = DeferredGenerator::fromHandler(fn() => 42, EncodedValues::empty()); + + $this->assertNull($lazy->current()); + } + + public function testLazyNotGeneratorWithException(): void + { + $lazy = DeferredGenerator::fromHandler(fn() => throw new \Exception('foo'), EncodedValues::empty()); + + $this->expectException(\Exception::class); + $this->expectExceptionMessage('foo'); + + $lazy->current(); + } + + + public function testLazyNotGeneratorWithException2(): void + { + $lazy = DeferredGenerator::fromHandler(fn() => throw new \Exception('foo'), EncodedValues::empty()); + + try { + $lazy->current(); + } catch (\Exception) { + // ignore + } + + $this->assertNull($lazy->current()); + } + /** * @param callable(): \Generator $generatorFactory * @param iterable $actions