diff --git a/phpcs.xml b/phpcs.xml index bd548b7a..31de617e 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -23,12 +23,6 @@ - - - - - - @@ -117,13 +111,6 @@ - - - - - - - @@ -135,16 +122,8 @@ - - - - - - - - + @@ -155,15 +134,6 @@ - - - - error - Method name "%s" must not be prefixed with an underscore to indicate visibility - - - - @@ -196,20 +166,12 @@ - - - - 0 - - - 0 - @@ -251,8 +213,6 @@ - - @@ -261,9 +221,6 @@ - - - @@ -292,14 +249,6 @@ - - - 0 - - - 0 - - diff --git a/src/Client.php b/src/Client.php index 70eb9674..05ea05e1 100644 --- a/src/Client.php +++ b/src/Client.php @@ -43,6 +43,8 @@ */ class Client { + use CustomSleepMixin; + /** * Default owner for API products. * @@ -463,7 +465,7 @@ public function enqueueAndParse( ); error_log("Successfully enqueued document with job id: " . $enqueueResponse->job->id); - sleep($asyncOptions->initialDelaySec); + $this->customSleep($asyncOptions->initialDelaySec); $retryCounter = 1; $pollResults = $this->parseQueued($predictionType, $enqueueResponse->job->id, $options->endpoint); @@ -473,7 +475,7 @@ public function enqueueAndParse( } error_log("Polling server for parsing result with job id: " . $enqueueResponse->job->id); $retryCounter++; - sleep($asyncOptions->delaySec); + $this->customSleep($asyncOptions->delaySec); $pollResults = $this->parseQueued($predictionType, $enqueueResponse->job->id, $options->endpoint); } if ($pollResults->job->status != "completed") { @@ -540,7 +542,7 @@ public function parseQueued( public function loadPrediction( string $predictionType, LocalResponse $localResponse - ) { + ): AsyncPredictResponse|PredictResponse { try { $json = $localResponse->toArray(); if (isset($json['job'])) { diff --git a/src/ClientV2.php b/src/ClientV2.php index 50d34401..651f7ee1 100644 --- a/src/ClientV2.php +++ b/src/ClientV2.php @@ -14,6 +14,8 @@ */ class ClientV2 { + use CustomSleepMixin; + /** * @var MindeeApiV2 Mindee API V2. */ @@ -96,7 +98,7 @@ public function enqueueAndGetInference( $queueId = $enqueueResponse->job->id; error_log("Successfully enqueued document with job id: " . $queueId); - sleep($pollingOptions->initialDelaySec); + $this->customSleep($pollingOptions->initialDelaySec); $retryCounter = 1; $pollResults = $this->getJob($queueId); @@ -114,7 +116,7 @@ public function enqueueAndGetInference( ". Job status: " . $pollResults->job->status ); - sleep($pollingOptions->delaySec); + $this->customSleep($pollingOptions->delaySec); $pollResults = $this->getJob($queueId); $retryCounter++; } diff --git a/src/CustomSleepMixin.php b/src/CustomSleepMixin.php new file mode 100644 index 00000000..9c7cd0cd --- /dev/null +++ b/src/CustomSleepMixin.php @@ -0,0 +1,27 @@ +id = $rawResponse['id']; $this->status = $rawResponse['status']; - if (array_key_exists('available_at', $rawResponse) && $rawResponse['available_at'] !== null && strtotime($rawResponse['available_at'])) { + if ( + array_key_exists('available_at', $rawResponse) && + $rawResponse['available_at'] !== null && strtotime($rawResponse['available_at']) + ) { try { $this->availableAt = new DateTimeImmutable($rawResponse['available_at']); } catch (\Exception $e) { diff --git a/tests/CustomSleepMixinTest.php b/tests/CustomSleepMixinTest.php new file mode 100644 index 00000000..5e8cc0cf --- /dev/null +++ b/tests/CustomSleepMixinTest.php @@ -0,0 +1,70 @@ +customSleep(1); + $elapsed = microtime(true) - $start; + $this->assertGreaterThanOrEqual($lowerBound, $elapsed); + $this->assertLessThanOrEqual($upperBound, $elapsed); + } + + public function testCustomSleep0dot33Seconds(): void { + $lowerBound = 0.33; + $upperBound = 0.43; + + $start = microtime(true); + $this->customSleep(0.33); + $elapsed = microtime(true) - $start; + $this->assertGreaterThanOrEqual($lowerBound, $elapsed); + $this->assertLessThanOrEqual($upperBound, $elapsed); + } + + public function testCustomSleep2Seconds(): void { + $lowerBound = 2; + $upperBound = 2.1; + + $start = microtime(true); + $this->customSleep(2); + $elapsed = microtime(true) - $start; + $this->assertGreaterThanOrEqual($lowerBound, $elapsed); + $this->assertLessThanOrEqual($upperBound, $elapsed); + } + + public function testCustomSleep1dot5Seconds(): void { + $lowerBound = 1.5; + $upperBound = 1.6; + + $start = microtime(true); + $this->customSleep(1.5); + $elapsed = microtime(true) - $start; + $this->assertGreaterThanOrEqual($lowerBound, $elapsed); + $this->assertLessThanOrEqual($upperBound, $elapsed); + } + + public function testCustomSleep0Seconds(): void { + $start = microtime(true); + $this->customSleep(0); + $elapsed = microtime(true) - $start; + $this->assertLessThanOrEqual(0.0001, $elapsed); + } + + public function testCustomSleepMinus1Seconds(): void { + $start = microtime(true); + $this->customSleep(-1); + $elapsed = microtime(true) - $start; + $this->assertLessThanOrEqual(0.0001, $elapsed); + } +} diff --git a/tests/Input/URLInputSourceTestFunctional.php b/tests/Input/URLInputSourceTestFunctional.php index e18f0c8c..37524f20 100644 --- a/tests/Input/URLInputSourceTestFunctional.php +++ b/tests/Input/URLInputSourceTestFunctional.php @@ -18,7 +18,7 @@ protected function setUp(): void $this->client = new Client(); $this->outputFilePath = (getenv('GITHUB_WORKSPACE') ?: ".") . "/tests/resources/output/"; - $this->referenceFilePath = "https://github.com/mindee/client-lib-test-data/blob/main/products/invoice_splitter/invoice_5p.pdf?raw=true"; + $this->referenceFilePath = "https://github.com/mindee/client-lib-test-data/blob/main/v1/products/invoice_splitter/invoice_5p.pdf?raw=true"; } public static function tearDownAfterClass(): void