Skip to content

Commit

Permalink
Refactor taxRateProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKasp authored and GSadee committed Mar 16, 2021
1 parent 757def6 commit c6415d4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
17 changes: 17 additions & 0 deletions spec/Provider/TaxRateProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,21 @@ function it_returns_null_if_there_is_no_adjustment_with_details_with_tax_rate_am

$this->provide($orderItemUnit)->shouldReturn(null);
}

function it_throws_exception_if_order_item_unit_has_more_than_1_tax_adjustment(
OrderItemUnitInterface $orderItemUnit,
AdjustmentInterface $firstTaxAdjustment,
AdjustmentInterface $secondTaxAdjustment

): void {
$orderItemUnit
->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT)
->willReturn(new ArrayCollection([$firstTaxAdjustment->getWrappedObject(), $secondTaxAdjustment->getWrappedObject()]))
;

$this
->shouldThrow(\InvalidArgumentException::class)
->during('provide', [$orderItemUnit])
;
}
}
3 changes: 3 additions & 0 deletions src/Provider/TaxRateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Model\OrderItemUnitInterface;
use Sylius\RefundPlugin\Entity\AdjustmentInterface;
use Webmozart\Assert\Assert;

final class TaxRateProvider implements TaxRateProviderInterface
{
Expand All @@ -15,6 +16,8 @@ public function provide(OrderItemUnitInterface $orderItemUnit): ?string
/** @var Collection|AdjustmentInterface[] $taxAdjustments */
$taxAdjustments = $orderItemUnit->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT);

Assert::maxCount($taxAdjustments, 1, 'Every Order Item Unit Should have max 1 tax adjustment');

if ($taxAdjustments->isEmpty() || !key_exists('taxRateAmount', $taxAdjustments->first()->getDetails())) {
return null;
}
Expand Down

0 comments on commit c6415d4

Please sign in to comment.