Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentBean committed Sep 18, 2024
1 parent 9701650 commit 7521592
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Actions/RetryBulkRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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 === []) {
Expand Down
31 changes: 31 additions & 0 deletions tests/Actions/RetryBulkRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
30 changes: 30 additions & 0 deletions tests/Models/BulkRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 7521592

Please sign in to comment.