|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Temporal\Tests\Unit\Workflow; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Temporal\DataConverter\EncodedValues; |
| 9 | +use Temporal\Internal\Workflow\Process\DeferredGenerator; |
| 10 | + |
| 11 | +/** |
| 12 | + * @psalm-type Action = 'current'|'send'|'key'|'next'|'valid'|'rewind'|'getReturn' |
| 13 | + */ |
| 14 | +final class DeferredGeneratorTestCase extends TestCase |
| 15 | +{ |
| 16 | + public function testSimple(): void |
| 17 | + { |
| 18 | + $this->compare( |
| 19 | + fn() => (function () { |
| 20 | + yield 1; |
| 21 | + yield 42 => 2; |
| 22 | + yield 3; |
| 23 | + })(), |
| 24 | + [ |
| 25 | + 'current', 'key', 'current', 'key', |
| 26 | + 'next', |
| 27 | + 'current', 'key', 'current', 'key', 'valid', |
| 28 | + 'next', |
| 29 | + ['send', 'foo'], |
| 30 | + 'current', 'key', 'current', 'key', 'valid', |
| 31 | + ], |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + public function testSendingValues(): void |
| 36 | + { |
| 37 | + $this->compare( |
| 38 | + fn() => (function () { |
| 39 | + $a = yield; |
| 40 | + $b = yield $a; |
| 41 | + $c = yield $b; |
| 42 | + return [$a, $b, $c]; |
| 43 | + })(), |
| 44 | + [ |
| 45 | + ['send', 'foo'], |
| 46 | + ['send', 'bar'], |
| 47 | + ['send', 'baz'], |
| 48 | + 'current', 'key', 'current', 'key', 'valid', |
| 49 | + ], |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + public function testThrowingExceptions(): void |
| 54 | + { |
| 55 | + $this->compare( |
| 56 | + fn() => (function () { |
| 57 | + try { |
| 58 | + yield; |
| 59 | + throw new \Exception('foo'); |
| 60 | + } catch (\Exception $e) { |
| 61 | + yield $e->getMessage(); |
| 62 | + } |
| 63 | + })(), |
| 64 | + [ |
| 65 | + 'current', 'key', 'current', 'key', 'valid', |
| 66 | + 'next', |
| 67 | + 'current', 'key', 'current', 'key', 'valid', |
| 68 | + 'next', |
| 69 | + 'rewind', |
| 70 | + ], |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + public function testReturn(): void |
| 75 | + { |
| 76 | + $this->compare( |
| 77 | + fn() => (function () { |
| 78 | + yield 1; |
| 79 | + return 2; |
| 80 | + })(), |
| 81 | + [ |
| 82 | + 'current', 'key', 'current', 'key', 'valid', |
| 83 | + 'next', |
| 84 | + 'getReturn', |
| 85 | + ], |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + public function testEmpty(): void |
| 90 | + { |
| 91 | + $this->compare( |
| 92 | + fn() => (function () { |
| 93 | + yield from []; |
| 94 | + })(), |
| 95 | + [ |
| 96 | + 'current', 'key', 'current', 'key', 'valid', |
| 97 | + 'next', |
| 98 | + 'rewind', |
| 99 | + ], |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + public function testEmptyReturn(): void |
| 104 | + { |
| 105 | + $this->compare( |
| 106 | + fn() => (function () { |
| 107 | + return; |
| 108 | + yield; |
| 109 | + })(), |
| 110 | + [ |
| 111 | + 'current', 'key', 'current', 'key', 'valid', |
| 112 | + 'next', |
| 113 | + 'getReturn', |
| 114 | + ], |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + public function testEmptyThrow(): void |
| 119 | + { |
| 120 | + $this->compare( |
| 121 | + fn() => (function () { |
| 122 | + throw new \Exception('foo'); |
| 123 | + yield; |
| 124 | + })(), |
| 125 | + ['current', 'key', 'current', 'key', 'valid', 'getReturn', 'next', 'rewind'], |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + public function testEmptyThrowValid(): void |
| 130 | + { |
| 131 | + $this->compare( |
| 132 | + fn() => (function () { |
| 133 | + throw new \Exception('foo'); |
| 134 | + yield; |
| 135 | + })(), |
| 136 | + [ |
| 137 | + 'valid', 'valid', |
| 138 | + ], |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + public function testEmptyThrowGetReturn(): void |
| 143 | + { |
| 144 | + $this->compare( |
| 145 | + fn() => (function () { |
| 146 | + throw new \Exception('foo'); |
| 147 | + yield; |
| 148 | + })(), |
| 149 | + [ |
| 150 | + 'getReturn', 'getReturn', |
| 151 | + ], |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * @param callable(): \Generator $generatorFactory |
| 157 | + * @param iterable<Action|int, array{Action, mixed}> $actions |
| 158 | + * @return void |
| 159 | + */ |
| 160 | + private function compare( |
| 161 | + callable $generatorFactory, |
| 162 | + iterable $actions, |
| 163 | + ): void { |
| 164 | + $c1 = $c2 = null; |
| 165 | + $caught = false; |
| 166 | + $gen = $generatorFactory(); |
| 167 | + $def = DeferredGenerator::fromGenerator($generatorFactory()); |
| 168 | + $def->catch(function (\Throwable $e) use (&$c1) { |
| 169 | + $c1 = $e; |
| 170 | + }); |
| 171 | + $lazy = DeferredGenerator::fromHandler($generatorFactory, EncodedValues::empty()); |
| 172 | + $lazy->catch(function (\Throwable $e) use (&$c2) { |
| 173 | + $c2 = $e; |
| 174 | + }); |
| 175 | + |
| 176 | + |
| 177 | + $i = 0; |
| 178 | + foreach ($actions as $tuple) { |
| 179 | + ++$i; |
| 180 | + $argLess = \is_string($tuple); |
| 181 | + $method = $argLess ? $tuple : $tuple[0]; |
| 182 | + $arg = $argLess ? null : $tuple[1]; |
| 183 | + $c1 = $c2 = $e = $e2 = $e3 = $result = $result2 = $result3 = null; |
| 184 | + |
| 185 | + try { |
| 186 | + $result = $argLess ? $gen->$method() : $gen->$method($arg); |
| 187 | + } catch (\Throwable $e) { |
| 188 | + # ignore |
| 189 | + } |
| 190 | + |
| 191 | + try { |
| 192 | + $result2 = $argLess ? $def->$method() : $def->$method($arg); |
| 193 | + } catch (\Throwable $e2) { |
| 194 | + # ignore |
| 195 | + } |
| 196 | + |
| 197 | + try { |
| 198 | + $result3 = $argLess ? $lazy->$method() : $lazy->$method($arg); |
| 199 | + } catch (\Throwable $e3) { |
| 200 | + # ignore |
| 201 | + } |
| 202 | + |
| 203 | + $this->assertSame($result, $result2, "Generator and DeferredGenerator results differ [$i] `$method`"); |
| 204 | + $this->assertSame($result, $result3, "Generator and DeferredGenerator results differ [$i] `$method`"); |
| 205 | + if ($caught) { |
| 206 | + $this->assertNull($c1, "Error was caught twice [$i] `$method`"); |
| 207 | + $this->assertNull($c2, "Error was caught twice [$i] `$method`"); |
| 208 | + } |
| 209 | + if ($e !== null) { |
| 210 | + $this->assertNotNull($e2, "Generator and DeferredGenerator exceptions differ [$i] `$method`"); |
| 211 | + $this->assertNotNull($e3, "Generator and DeferredGenerator exceptions differ [$i] `$method`"); |
| 212 | + if (!$caught && !\in_array($method, ['rewind'], true)) { |
| 213 | + $this->assertNotNull($c1, "Error was not caught [$i] `$method`"); |
| 214 | + $this->assertNotNull($c2, "Error was not caught [$i] `$method`"); |
| 215 | + $caught = true; |
| 216 | + } |
| 217 | + } else { |
| 218 | + $this->assertNull($e2, "Generator and DeferredGenerator exceptions differ [$i] `$method`"); |
| 219 | + $this->assertNull($e3, "Generator and DeferredGenerator exceptions differ [$i] `$method`"); |
| 220 | + $this->assertNull($c1, "There must be no error caught [$i] `$method`"); |
| 221 | + $this->assertNull($c2, "There must be no error caught [$i] `$method`"); |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments