|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace ScriptFUSIONTest\Functional; |
| 5 | + |
| 6 | +use Amp\PHPUnit\AsyncTestCase; |
| 7 | +use Amp\Success; |
| 8 | +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; |
| 9 | +use ScriptFUSION\Porter\Connector\AsyncConnector; |
| 10 | +use ScriptFUSION\Porter\Connector\AsyncDataSource; |
| 11 | +use ScriptFUSION\Porter\Connector\ImportConnectorFactory; |
| 12 | +use ScriptFUSION\Porter\Connector\ThrottledConnector; |
| 13 | +use ScriptFUSION\Porter\Specification\AsyncImportSpecification; |
| 14 | +use ScriptFUSIONTest\MockFactory; |
| 15 | + |
| 16 | +/** |
| 17 | + * Tests the throttle hierarchy of precedence. Only applies to async imports. |
| 18 | + * |
| 19 | + * Specification throttle (preferred) > Connector throttle (default). |
| 20 | + * |
| 21 | + * @see ImportConnectorFactory |
| 22 | + */ |
| 23 | +final class ThrottlePrecedenceHierarchyTest extends AsyncTestCase |
| 24 | +{ |
| 25 | + use MockeryPHPUnitIntegration; |
| 26 | + |
| 27 | + private $specificationThrottle; |
| 28 | + private $connectorThrottle; |
| 29 | + private $specification; |
| 30 | + private $provider; |
| 31 | + |
| 32 | + protected function setUp(): void |
| 33 | + { |
| 34 | + parent::setUp(); |
| 35 | + |
| 36 | + $this->specificationThrottle = MockFactory::mockThrottle(); |
| 37 | + $this->connectorThrottle = MockFactory::mockThrottle(); |
| 38 | + |
| 39 | + $this->specification = new AsyncImportSpecification(MockFactory::mockResource( |
| 40 | + $this->provider = MockFactory::mockProvider() |
| 41 | + )); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Tests that when the connector is non-throttling, the specification's throttle is used. |
| 46 | + */ |
| 47 | + public function testNonThrottledConnector(): \Generator |
| 48 | + { |
| 49 | + $this->specification->setThrottle($this->specificationThrottle); |
| 50 | + $this->specificationThrottle->expects('await')->once()->andReturn(new Success()); |
| 51 | + $this->connectorThrottle->expects('await')->never(); |
| 52 | + |
| 53 | + $connector = ImportConnectorFactory::create($this->provider->getAsyncConnector(), $this->specification); |
| 54 | + |
| 55 | + yield $connector->fetchAsync(\Mockery::mock(AsyncDataSource::class)); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Tests that when the connector is throttled, and the specification's throttle is not set, the connector's |
| 60 | + * throttle is used. |
| 61 | + */ |
| 62 | + public function testThrottledConnector(): \Generator |
| 63 | + { |
| 64 | + $this->specificationThrottle->expects('await')->never(); |
| 65 | + $this->connectorThrottle->expects('await')->once()->andReturn(new Success()); |
| 66 | + |
| 67 | + $connector = ImportConnectorFactory::create($this->mockThrottledConnector(), $this->specification); |
| 68 | + |
| 69 | + yield $connector->fetchAsync(\Mockery::mock(AsyncDataSource::class)); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Tests that when both the connector is throttled and the specification's throttle are set, the specification's |
| 74 | + * throttle overrides that of the connector. |
| 75 | + */ |
| 76 | + public function testThrottledConnectorOverride(): \Generator |
| 77 | + { |
| 78 | + $this->specification->setThrottle($this->specificationThrottle); |
| 79 | + $this->specificationThrottle->expects('await')->once()->andReturn(new Success()); |
| 80 | + $this->connectorThrottle->expects('await')->never(); |
| 81 | + |
| 82 | + $connector = ImportConnectorFactory::create($this->mockThrottledConnector(), $this->specification); |
| 83 | + |
| 84 | + yield $connector->fetchAsync(\Mockery::mock(AsyncDataSource::class)); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @return AsyncConnector|ThrottledConnector |
| 89 | + */ |
| 90 | + private function mockThrottledConnector(): AsyncConnector |
| 91 | + { |
| 92 | + return \Mockery::mock(AsyncConnector::class, ThrottledConnector::class) |
| 93 | + ->shouldReceive('getThrottle') |
| 94 | + ->andReturn($this->connectorThrottle) |
| 95 | + ->shouldReceive('fetchAsync') |
| 96 | + ->andReturn(MockFactory::mockPromise()) |
| 97 | + ->getMock() |
| 98 | + ; |
| 99 | + } |
| 100 | +} |
0 commit comments