From c8a9b9f67e062e210b1158ecbe52ececbe49757a Mon Sep 17 00:00:00 2001 From: bencroker Date: Fri, 9 Aug 2024 09:18:34 +0200 Subject: [PATCH] Add commerce integration test --- tests/pest/Helpers.php | 5 +++-- tests/pest/Integration/CommerceTest.php | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/tests/pest/Helpers.php b/tests/pest/Helpers.php index 2eabb55d..82042a82 100644 --- a/tests/pest/Helpers.php +++ b/tests/pest/Helpers.php @@ -36,7 +36,7 @@ function getSiteId(): int function getChannelSectionId(): int { - return Craft::$app->sections->getSectionByHandle(App::env('TEST_CHANNEL_SECTION_HANDLE'))->id; + return Craft::$app->entries->getSectionByHandle(App::env('TEST_CHANNEL_SECTION_HANDLE'))->id; } function integrationIsActive(string $class): bool @@ -125,7 +125,7 @@ function createAsset(): Asset return $asset; } -function createProductVariantOrder(bool $batchMode = false): array +function createProductVariantOrder(bool $batchMode = false, bool $inventoryTracked = false): array { $originalBatchMode = Blitz::$plugin->refreshCache->batchMode; Blitz::$plugin->refreshCache->batchMode = $batchMode; @@ -143,6 +143,7 @@ function createProductVariantOrder(bool $batchMode = false): array 'sku' => 'test-sku', 'price' => 10, 'productId' => $product->id, + 'inventoryTracked' => $inventoryTracked, ]); Craft::$app->elements->saveElement($variant); diff --git a/tests/pest/Integration/CommerceTest.php b/tests/pest/Integration/CommerceTest.php index 4fa35755..8b456411 100644 --- a/tests/pest/Integration/CommerceTest.php +++ b/tests/pest/Integration/CommerceTest.php @@ -16,15 +16,28 @@ Blitz::$plugin->refreshCache->batchMode = false; })->skip(fn() => !integrationIsActive(CommerceIntegration::class), 'Commerce integration not found in active integrations.'); -test('Variants are refreshed on order completion', function() { +test('Variant with inventory is refreshed on order completion', function() { /** @var MockInterface $refreshCache */ $refreshCache = Blitz::$plugin->refreshCache; $refreshCache->shouldReceive('refresh')->once(); $refreshCache->shouldNotReceive('refreshAll'); - [$variant, $order] = createProductVariantOrder(batchMode: true); + [$variant, $order] = createProductVariantOrder(batchMode: true, inventoryTracked: true); $order->trigger(Order::EVENT_AFTER_COMPLETE_ORDER); expect(Blitz::$plugin->refreshCache->refreshData->getElementIds($variant::class)) ->toBe([$variant->id]); }); + +test('Variant without inventory is not refreshed on order completion', function() { + /** @var MockInterface $refreshCache */ + $refreshCache = Blitz::$plugin->refreshCache; + $refreshCache->shouldNotReceive('refresh'); + $refreshCache->shouldNotReceive('refreshAll'); + + [$variant, $order] = createProductVariantOrder(batchMode: true); + $order->trigger(Order::EVENT_AFTER_COMPLETE_ORDER); + + expect(Blitz::$plugin->refreshCache->refreshData->getElementIds($variant::class)) + ->toBe([]); +});