Skip to content

Commit

Permalink
Merge branch 'pr/closed-generator-exception'
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Oct 23, 2023
2 parents 3375501 + 4c45509 commit 87cd2b6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,46 @@ final class Deferred implements Interfaces\Deferred
*/
private $output;

/**
* @var ?\Throwable
*/
private $error;

/**
* @param iterable<T> $input
*/
public function __construct(iterable $input)
{
$this->input = $input;
$this->error = null;
}

/**
* @return T
*/
public function get()
{
if (null !== $this->error) {
throw $this->error;
}

if (null === $this->input) {
return $this->output;
}

foreach ($this->input as $output) {
$this->output = $output;
try {
foreach ($this->input as $output) {
$this->output = $output;

break;
}
break;
}
} catch (\Throwable $e) {
$this->error = $e;

$this->input = null;
throw $e;
} finally {
$this->input = null;
}

return $this->output;
}
Expand Down
36 changes: 36 additions & 0 deletions tests/DeferredTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,40 @@ private function yieldsNothing(bool $false = false): iterable
yield 1;
}
}

public function testThrowsSame(): void
{
$later = new Deferred($this->generatorThrows(true));
$e = null;

try {
$this->assertDeferredSame(1, $later);
$this->fail('Should have thrown');
} catch (\InvalidArgumentException $e) {
$this->assertSame(__CLASS__, $e->getMessage());
}

try {
$this->assertDeferredSame(1, $later);
} catch (\InvalidArgumentException $e2) {
$this->assertSame($e, $e2);
}

$reflectionClass = new \ReflectionClass($later);
$property = $reflectionClass->getProperty('input');
$property->setAccessible(true);
$this->assertNull($property->getValue($later));
}

/**
* @return iterable<int>
*/
private function generatorThrows(bool $throw = false): iterable
{
if ($throw) {
throw new \InvalidArgumentException(__CLASS__);
}

yield 1;
}
}

0 comments on commit 87cd2b6

Please sign in to comment.