Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop stock update if MSI stock is null #15

Merged
merged 4 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Actions/UpdateMsiStock.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public function update(MagentoStock $model): void

$availableSources = $this->getAvailableSources();

if ($model->msi_stock === null) {
return;
}

foreach ($model->msi_stock as $location => $quantity) {
if (! in_array($location, $availableSources)) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/RetrievesStockSkus.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ interface RetrievesStockSkus
public function retrieveAll(): Enumerable;

/** @return Enumerable<int, string> */
public function retrieveUpdated(Carbon $from = null): Enumerable;
public function retrieveUpdated(?Carbon $from = null): Enumerable;
}
2 changes: 1 addition & 1 deletion src/Exceptions/UpdateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class UpdateException extends Exception
{
public function __construct(string $sku, string $message, public array $payload = [], Throwable $previous = null)
public function __construct(string $sku, string $message, public array $payload = [], ?Throwable $previous = null)
{
parent::__construct("Failed to update $sku: $message", 0, $previous);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Models/MagentoStock.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
* @property bool $backorders
* @property bool $magento_backorders_enabled
* @property int $quantity
* @property array $msi_stock
* @property array $msi_status
* @property ?array $msi_stock
* @property ?array $msi_status
* @property bool $retrieve
* @property bool $update
* @property ?Carbon $last_retrieved
Expand Down
2 changes: 1 addition & 1 deletion src/Retriever/DummySkuRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function retrieveAll(): Enumerable
return collect(['::sku_1::', '::sku_2::']);
}

public function retrieveUpdated(Carbon $from = null): Enumerable
public function retrieveUpdated(?Carbon $from = null): Enumerable
{
return collect(['::sku_1::']);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Actions/UpdateMsiStockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,27 @@ function (Request $request) use ($expectedPayload) {
Event::assertDispatched(StockUpdatedEvent::class);
}

public function test_it_stops_when_stockdata_is_empty(): void
{
Http::fake([
'*/rest/all/V1/inventory/source-items*' => Http::response(),
]);

/** @var UpdateMsiStock $action */
$action = app(UpdateMsiStock::class);

/** @var MagentoStock $stock */
$stock = MagentoStock::query()->first();

$stock->msi_stock = null;

$stock->msi_status = null;

$action->update($stock);

Event::assertNotDispatched(StockUpdatedEvent::class);
FinnPaes marked this conversation as resolved.
Show resolved Hide resolved
}

public function test_it_logs_error(): void
{
Http::fake([
Expand Down
Loading