Skip to content

Commit

Permalink
Small refactor of orderItemSubTotal
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKasp committed Jun 22, 2022
1 parent 865f1a4 commit 83009ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
4 changes: 1 addition & 3 deletions src/Sylius/Component/Core/Model/OrderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ public function getSubtotal(): int
{
return array_reduce(
$this->getUnits()->toArray(),
function (int $subtotal, BaseOrderItemUnitInterface $unit) {
return $subtotal + $this->unitPrice + $unit->getAdjustmentsTotal(AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT);
},
fn (int $subtotal, BaseOrderItemUnitInterface $unit) => $subtotal + $this->unitPrice + $unit->getAdjustmentsTotal(AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT),
0
);
}
Expand Down
38 changes: 17 additions & 21 deletions src/Sylius/Component/Core/spec/Model/OrderItemSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,29 +99,25 @@ function it_returns_unit_price_as_discounted_unit_price_if_there_are_no_units():
$this->getDiscountedUnitPrice()->shouldReturn(10000);
}

function its_subtotal_consist_of_sum_of_units_discounted_price(): void
{
function its_subtotal_consists_of_sum_of_units_discounted_price(
OrderItemUnitInterface $firstUnit,
OrderItemUnitInterface $secondUnit,
): void {
$this->setUnitPrice(10000);

$firstUnit = new OrderItemUnit($this->getWrappedObject());
$adjustment1 = new Adjustment();
$adjustment1->setType(AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT);
$adjustment1->setAmount(-1667);
$firstUnit->addAdjustment($adjustment1);

$secondUnit = new OrderItemUnit($this->getWrappedObject());
$adjustment2 = new Adjustment();
$adjustment2->setType(AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT);
$adjustment2->setAmount(-1667);
$secondUnit->addAdjustment($adjustment2);

$secondUnit = new OrderItemUnit($this->getWrappedObject());
$adjustment3 = new Adjustment();
$adjustment3->setType(AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT);
$adjustment3->setAmount(-1666);
$secondUnit->addAdjustment($adjustment3);

$this->getSubtotal()->shouldReturn(25000);
$firstUnit->getAdjustmentsTotal(AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT)->willReturn(-1667);
$firstUnit->getTotal()->willReturn(10000);
$firstUnit->getOrderItem()->willReturn($this->getWrappedObject());

$secondUnit->getAdjustmentsTotal(AdjustmentInterface::TAX_ADJUSTMENT)->willReturn(400);
$secondUnit->getAdjustmentsTotal(AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT)->willReturn(-3333);
$secondUnit->getTotal()->willReturn(10000);
$secondUnit->getOrderItem()->willReturn($this->getWrappedObject());

$this->addUnit($firstUnit->getWrappedObject());
$this->addUnit($secondUnit->getWrappedObject());

$this->getSubtotal()->shouldReturn(15000);
}

function it_has_no_variant_by_default(): void
Expand Down

0 comments on commit 83009ce

Please sign in to comment.