|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace ScriptFUSIONTest\Integration\Porter\Connector; |
| 5 | + |
| 6 | +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use ScriptFUSION\Porter\Connector\Connector; |
| 9 | +use ScriptFUSION\Porter\Connector\ImportConnector; |
| 10 | +use ScriptFUSION\Porter\Connector\Recoverable\RecoverableExceptionHandler; |
| 11 | +use ScriptFUSION\Porter\Connector\Recoverable\StatelessRecoverableExceptionHandler; |
| 12 | +use ScriptFUSION\Retry\FailingTooHardException; |
| 13 | +use ScriptFUSIONTest\FixtureFactory; |
| 14 | +use ScriptFUSIONTest\MockFactory; |
| 15 | +use ScriptFUSIONTest\Stubs\TestRecoverableException; |
| 16 | +use ScriptFUSIONTest\Stubs\TestRecoverableExceptionHandler; |
| 17 | + |
| 18 | +/** |
| 19 | + * @see ImportConnector |
| 20 | + */ |
| 21 | +final class ImportConnectorTest extends TestCase |
| 22 | +{ |
| 23 | + use MockeryPHPUnitIntegration; |
| 24 | + |
| 25 | + |
| 26 | + /** |
| 27 | + * Tests that when retry() is called multiple times, the original fetch exception handler is unmodified. |
| 28 | + * This is expected because the handler must be cloned using the prototype pattern to ensure multiple concurrent |
| 29 | + * fetches do not conflict. |
| 30 | + * |
| 31 | + * @dataProvider provideHandlerAndContext |
| 32 | + */ |
| 33 | + public function testFetchExceptionHandlerCloned( |
| 34 | + TestRecoverableExceptionHandler $handler, |
| 35 | + ImportConnector $connector |
| 36 | + ): void { |
| 37 | + $handler->initialize(); |
| 38 | + $initial = $handler->getCurrent(); |
| 39 | + |
| 40 | + $connector->fetch('foo'); |
| 41 | + |
| 42 | + self::assertSame($initial, $handler->getCurrent()); |
| 43 | + } |
| 44 | + |
| 45 | + public function provideHandlerAndContext(): \Generator |
| 46 | + { |
| 47 | + yield 'User exception handler' => [ |
| 48 | + $handler = new TestRecoverableExceptionHandler, |
| 49 | + $connector = FixtureFactory::buildImportConnector( |
| 50 | + \Mockery::mock(Connector::class) |
| 51 | + ->shouldReceive('fetch') |
| 52 | + ->andReturnUsing($this->createExceptionThrowingClosure()) |
| 53 | + ->getMock(), |
| 54 | + null, |
| 55 | + $handler |
| 56 | + ), |
| 57 | + ]; |
| 58 | + |
| 59 | + // It should be OK to reuse the handler here because the whole point of the test is that it's not modified. |
| 60 | + $connector->setRecoverableExceptionHandler($handler); |
| 61 | + yield 'Resource exception handler' => [$handler, $connector]; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Tests that when retry() is called, a stateless fetch exception handler is neither cloned nor reinitialized. |
| 66 | + * For stateless handlers, initialization is a NOOP, so avoiding cloning is a small optimization. |
| 67 | + */ |
| 68 | + public function testStatelessExceptionHandlerNotCloned(): void |
| 69 | + { |
| 70 | + $connector = FixtureFactory::buildImportConnector( |
| 71 | + \Mockery::mock(Connector::class) |
| 72 | + ->shouldReceive('fetch') |
| 73 | + ->twice() |
| 74 | + ->andReturnUsing($this->createExceptionThrowingClosure()) |
| 75 | + ->getMock(), |
| 76 | + null, |
| 77 | + $handler = new StatelessRecoverableExceptionHandler(static function (): void { |
| 78 | + // Intentionally empty. |
| 79 | + }) |
| 80 | + ); |
| 81 | + |
| 82 | + $connector->fetch('foo'); |
| 83 | + |
| 84 | + self::assertSame( |
| 85 | + $handler, |
| 86 | + \Closure::bind( |
| 87 | + function (): RecoverableExceptionHandler { |
| 88 | + return $this->userExceptionHandler; |
| 89 | + }, |
| 90 | + $connector, |
| 91 | + $connector |
| 92 | + )() |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Tests that a recoverable exception handler cannot return false. |
| 98 | + */ |
| 99 | + public function testExceptionHandlerCannotCancelRetries(): void |
| 100 | + { |
| 101 | + $this->expectException(\TypeError::class); |
| 102 | + |
| 103 | + FixtureFactory::buildImportConnector( |
| 104 | + \Mockery::mock(Connector::class) |
| 105 | + ->shouldReceive('fetch') |
| 106 | + ->andThrow(new TestRecoverableException) |
| 107 | + ->getMock(), |
| 108 | + null, |
| 109 | + new StatelessRecoverableExceptionHandler(static function () { |
| 110 | + return false; |
| 111 | + }) |
| 112 | + )->fetch('foo'); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Tests that when a user recoverable exception handler returns a promise, the promise is resolved. |
| 117 | + */ |
| 118 | + public function testAsyncUserRecoverableExceptionHandler(): void |
| 119 | + { |
| 120 | + $connector = FixtureFactory::buildImportConnector( |
| 121 | + \Mockery::mock(Connector::class) |
| 122 | + ->shouldReceive('fetchAsync') |
| 123 | + ->andThrow(new TestRecoverableException) |
| 124 | + ->getMock(), |
| 125 | + null, |
| 126 | + self::createAsyncRecoverableExceptionHandler() |
| 127 | + ); |
| 128 | + |
| 129 | + try { |
| 130 | + \Amp\Promise\wait($connector->fetchAsync('foo')); |
| 131 | + } catch (FailingTooHardException $exception) { |
| 132 | + // This is fine. |
| 133 | + } |
| 134 | + |
| 135 | + self::assertTrue(isset($exception)); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Tests that when a resource recoverable exception handler returns a promise, the promise is resolved. |
| 140 | + */ |
| 141 | + public function testAsyncResourceRecoverableExceptionHandler(): void |
| 142 | + { |
| 143 | + $connector = FixtureFactory::buildImportConnector( |
| 144 | + \Mockery::mock(Connector::class) |
| 145 | + ->shouldReceive('fetchAsync') |
| 146 | + ->andThrow(new TestRecoverableException) |
| 147 | + ->getMock() |
| 148 | + ); |
| 149 | + |
| 150 | + $connector->setRecoverableExceptionHandler(self::createAsyncRecoverableExceptionHandler()); |
| 151 | + |
| 152 | + try { |
| 153 | + \Amp\Promise\wait($connector->fetchAsync('foo')); |
| 154 | + } catch (FailingTooHardException $exception) { |
| 155 | + // This is fine. |
| 156 | + } |
| 157 | + |
| 158 | + self::assertTrue(isset($exception)); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Tests that when user and resource recoverable exception handlers both return promises, both promises are |
| 163 | + * resolved. |
| 164 | + */ |
| 165 | + public function testAsyncUserAndResourceRecoverablExceptionHandlers(): void |
| 166 | + { |
| 167 | + $connector = FixtureFactory::buildImportConnector( |
| 168 | + \Mockery::mock(Connector::class) |
| 169 | + ->shouldReceive('fetchAsync') |
| 170 | + ->andThrow(new TestRecoverableException) |
| 171 | + ->getMock(), |
| 172 | + null, |
| 173 | + self::createAsyncRecoverableExceptionHandler() |
| 174 | + ); |
| 175 | + |
| 176 | + $connector->setRecoverableExceptionHandler(self::createAsyncRecoverableExceptionHandler()); |
| 177 | + |
| 178 | + try { |
| 179 | + \Amp\Promise\wait($connector->fetchAsync('foo')); |
| 180 | + } catch (FailingTooHardException $exception) { |
| 181 | + // This is fine. |
| 182 | + } |
| 183 | + |
| 184 | + self::assertTrue(isset($exception)); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Creates a closure that only throws an exception on the first invocation. |
| 189 | + */ |
| 190 | + private static function createExceptionThrowingClosure(): \Closure |
| 191 | + { |
| 192 | + return static function (): void { |
| 193 | + static $invocationCount; |
| 194 | + |
| 195 | + if (!$invocationCount++) { |
| 196 | + throw new TestRecoverableException; |
| 197 | + } |
| 198 | + }; |
| 199 | + } |
| 200 | + |
| 201 | + private static function createAsyncRecoverableExceptionHandler(): RecoverableExceptionHandler |
| 202 | + { |
| 203 | + return new StatelessRecoverableExceptionHandler(static function () { |
| 204 | + return MockFactory::mockPromise(); |
| 205 | + }); |
| 206 | + } |
| 207 | +} |
0 commit comments