Skip to content

Commit

Permalink
Add commerce integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Aug 9, 2024
1 parent 5184ae4 commit c8a9b9f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 3 additions & 2 deletions tests/pest/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
17 changes: 15 additions & 2 deletions tests/pest/Integration/CommerceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
});

0 comments on commit c8a9b9f

Please sign in to comment.