Skip to content

Commit

Permalink
Merge branch '1.12' into 1.13
Browse files Browse the repository at this point in the history
* 1.12:
  [Inventory] Add a BC layer for refactored PaymentPreCompleteListener
  Add a note to UPGRADE file about changing PaymentPreCompleteListener's constructor
  [Inventory] Extract service for checking reserved stock availability for given order item
  • Loading branch information
GSadee committed Jun 5, 2024
2 parents 84538a2 + 991fcb2 commit 017e4e0
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Inventory/Checker/OrderItemAvailabilityChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Core\Inventory\Checker;

use Sylius\Component\Core\Model\OrderItemInterface;

final class OrderItemAvailabilityChecker implements OrderItemAvailabilityCheckerInterface
{
public function isReservedStockSufficient(OrderItemInterface $orderItem): bool
{
$variant = $orderItem->getVariant();
if (!$variant->isTracked()) {
return true;
}

$quantity = $orderItem->getQuantity();

return
$variant->getOnHold() - $quantity >= 0 &&
$variant->getOnHand() - $quantity >= 0
;
}
}
21 changes: 21 additions & 0 deletions Inventory/Checker/OrderItemAvailabilityCheckerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Core\Inventory\Checker;

use Sylius\Component\Core\Model\OrderItemInterface;

interface OrderItemAvailabilityCheckerInterface
{
public function isReservedStockSufficient(OrderItemInterface $orderItem): bool;
}
79 changes: 79 additions & 0 deletions spec/Inventory/Checker/OrderItemAvailabilityCheckerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Component\Core\Inventory\Checker;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Inventory\Checker\OrderItemAvailabilityCheckerInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;

final class OrderItemAvailabilityCheckerSpec extends ObjectBehavior
{
function it_implements_an_order_item_availability_checker_interface(): void
{
$this->shouldImplement(OrderItemAvailabilityCheckerInterface::class);
}

function it_returns_true_if_variant_is_untracked(
OrderItemInterface $orderItem,
ProductVariantInterface $variant,
): void {
$orderItem->getVariant()->willReturn($variant);
$variant->isTracked()->willReturn(false);

$this->isReservedStockSufficient($orderItem)->shouldReturn(true);
}

function it_returns_true_if_stock_is_sufficient(
OrderItemInterface $orderItem,
ProductVariantInterface $variant,
): void {
$orderItem->getVariant()->willReturn($variant);
$orderItem->getQuantity()->willReturn(2);

$variant->isTracked()->willReturn(true);
$variant->getOnHold()->willReturn(2);
$variant->getOnHand()->willReturn(2);

$this->isReservedStockSufficient($orderItem)->shouldReturn(true);
}

function it_returns_false_if_on_hold_value_is_not_sufficient(
OrderItemInterface $orderItem,
ProductVariantInterface $variant,
): void {
$orderItem->getVariant()->willReturn($variant);
$orderItem->getQuantity()->willReturn(2);

$variant->isTracked()->willReturn(true);
$variant->getOnHold()->willReturn(1);
$variant->getOnHand()->willReturn(2);

$this->isReservedStockSufficient($orderItem)->shouldReturn(false);
}

function it_returns_false_if_on_hand_value_is_not_sufficient(
OrderItemInterface $orderItem,
ProductVariantInterface $variant,
): void {
$orderItem->getVariant()->willReturn($variant);
$orderItem->getQuantity()->willReturn(2);

$variant->isTracked()->willReturn(true);
$variant->getOnHold()->willReturn(2);
$variant->getOnHand()->willReturn(1);

$this->isReservedStockSufficient($orderItem)->shouldReturn(false);
}
}

0 comments on commit 017e4e0

Please sign in to comment.