Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Dec 16, 2024
1 parent e82da4c commit 2e46b89
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
18 changes: 17 additions & 1 deletion src/Internal/Workflow/Process/DeferredGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}
1 change: 0 additions & 1 deletion src/Internal/Workflow/Process/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
57 changes: 48 additions & 9 deletions tests/Unit/Workflow/DeferredGeneratorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testSimple(): void
);
}

public function testSendingValues(): void
public function testCompareSendingValues(): void
{
$this->compare(
fn() => (function () {
Expand All @@ -50,7 +50,7 @@ public function testSendingValues(): void
);
}

public function testThrowingExceptions(): void
public function testCompareThrowingExceptions(): void
{
$this->compare(
fn() => (function () {
Expand All @@ -71,7 +71,7 @@ public function testThrowingExceptions(): void
);
}

public function testReturn(): void
public function testCompareReturn(): void
{
$this->compare(
fn() => (function () {
Expand All @@ -86,7 +86,7 @@ public function testReturn(): void
);
}

public function testEmpty(): void
public function testCompareEmpty(): void
{
$this->compare(
fn() => (function () {
Expand All @@ -100,7 +100,7 @@ public function testEmpty(): void
);
}

public function testEmptyReturn(): void
public function testCompareEmptyReturn(): void
{
$this->compare(
fn() => (function () {
Expand All @@ -115,7 +115,7 @@ public function testEmptyReturn(): void
);
}

public function testEmptyThrow(): void
public function testCompareEmptyThrow(): void
{
$this->compare(
fn() => (function () {
Expand All @@ -126,7 +126,7 @@ public function testEmptyThrow(): void
);
}

public function testEmptyThrowValid(): void
public function testCompareEmptyThrowValid(): void
{
$this->compare(
fn() => (function () {
Expand All @@ -137,7 +137,7 @@ public function testEmptyThrowValid(): void
);
}

public function testEmptyThrowGetReturn(): void
public function testCompareEmptyThrowGetReturn(): void
{
$this->compare(
fn() => (function () {
Expand All @@ -148,7 +148,7 @@ public function testEmptyThrowGetReturn(): void
);
}

public function testEmptyThrowGetKey(): void
public function testCompareEmptyThrowGetKey(): void
{
$this->compare(
fn() => (function () {
Expand All @@ -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<Action|int, array{Action, mixed}> $actions
Expand Down

0 comments on commit 2e46b89

Please sign in to comment.