From 752159277476ca12b3117755c0c7e5d90256a8f8 Mon Sep 17 00:00:00 2001 From: Vincent Boon Date: Wed, 18 Sep 2024 15:36:23 +0200 Subject: [PATCH] Fix test --- src/Actions/RetryBulkRequest.php | 12 +++++++--- tests/Actions/RetryBulkRequestTest.php | 31 ++++++++++++++++++++++++++ tests/Models/BulkRequestTest.php | 30 +++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/Actions/RetryBulkRequest.php b/src/Actions/RetryBulkRequest.php index 6bd47e1..6e209d8 100644 --- a/src/Actions/RetryBulkRequest.php +++ b/src/Actions/RetryBulkRequest.php @@ -12,7 +12,9 @@ class RetryBulkRequest implements RetriesBulkRequest { - public function __construct(protected MagentoAsync $client) {} + public function __construct(protected MagentoAsync $client) + { + } public function retry(BulkRequest $bulkRequest, bool $onlyFailed): ?BulkRequest { @@ -28,12 +30,16 @@ public function retry(BulkRequest $bulkRequest, bool $onlyFailed): ?BulkRequest /** @var BulkOperation $operation */ $operation = $operations->where('operation_id', '=', $index)->firstOrFail(); - if ($onlyFailed && ! in_array($operation->status, OperationStatus::failedStatuses()) || $operation->subject === null) { + if ($onlyFailed && ! in_array($operation->status, OperationStatus::failedStatuses())) { continue; } $payload[] = $request; - $subjects[] = $operation->subject; + if ($operation->subject !== null) { + $subjects[] = $operation->subject; + } else { + $subjects[] = null; + } } if ($payload === []) { diff --git a/tests/Actions/RetryBulkRequestTest.php b/tests/Actions/RetryBulkRequestTest.php index 75680c2..9981b22 100644 --- a/tests/Actions/RetryBulkRequestTest.php +++ b/tests/Actions/RetryBulkRequestTest.php @@ -101,4 +101,35 @@ public function it_does_nothing_without_payload(): void Http::assertNothingSent(); } + + #[Test] + public function it_does_nothing_without_method(): void + { + Http::fake()->preventStrayRequests(); + + /** @var BulkRequest $request */ + $request = BulkRequest::query()->create([ + 'magento_connection' => '::magento-connection::', + 'store_code' => '::store-code::', + 'method' => '', + 'path' => '::path::', + 'bulk_uuid' => '::bulk-uuid-1::', + 'request' => [['call-1']], + 'response' => [], + 'created_at' => now(), + ]); + + $request->operations()->create([ + 'operation_id' => 0, + 'status' => OperationStatus::Complete, + 'updated_at' => now()->subHours(2), + ]); + + /** @var RetryBulkRequest $action */ + $action = app(RetryBulkRequest::class); + + $action->retry($request, false); + + Http::assertNothingSent(); + } } diff --git a/tests/Models/BulkRequestTest.php b/tests/Models/BulkRequestTest.php index 2828a6d..a8fdbad 100644 --- a/tests/Models/BulkRequestTest.php +++ b/tests/Models/BulkRequestTest.php @@ -36,4 +36,34 @@ public function it_can_have_operations(): void $this->assertCount(3, $request->operations); } + + #[Test] + public function it_has_retry_relationship(): void + { + /** @var BulkRequest $request */ + $request = BulkRequest::query()->create([ + 'magento_connection' => '::magento-connection::', + 'store_code' => '::store-code::', + 'method' => 'POST', + 'path' => '::path::', + 'bulk_uuid' => '::bulk-uuid::', + 'request' => [], + 'response' => [], + ]); + + /** @var BulkRequest $retry */ + $retry = BulkRequest::query()->create([ + 'retry_of' => $request->id, + 'magento_connection' => '::magento-connection::', + 'store_code' => '::store-code::', + 'method' => 'POST', + 'path' => '::path::', + 'bulk_uuid' => '::bulk-uuid::', + 'request' => [], + 'response' => [], + ]); + + $this->assertEquals($retry->id, $request->retries->first()?->id); + $this->assertEquals($request->id, $retry->retryOf?->id); + } }