Skip to content

Commit

Permalink
Merge pull request #15 from justbetter/feature/stop-if-msi-stock-is-null
Browse files Browse the repository at this point in the history
Stop stock update if MSI stock is null
  • Loading branch information
VincentBean authored Feb 16, 2024
2 parents cc43bbf + 953cc56 commit 7f86b44
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
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
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
26 changes: 26 additions & 0 deletions tests/Actions/UpdateMsiStockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,32 @@ 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);

Http::assertNotSent(function (Request $request): bool {
return $request->method() === 'POST' &&
$request->url() === 'http://magento.test/rest/all/async/V1/inventory/source-items';
});
}

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

0 comments on commit 7f86b44

Please sign in to comment.