-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor #181 Extract tax rate provider from line items converter (GS…
…adee) This PR was merged into the 1.0-dev branch. Discussion ---------- After #179 Commits ------- 146c371 Extract tax rate provider from line items converter 6af2b26 Fixes for tax rate provider after PR review
- Loading branch information
Showing
7 changed files
with
125 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\RefundPlugin\Provider; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Core\Model\AdjustmentInterface; | ||
use Sylius\Component\Core\Model\OrderItemUnitInterface; | ||
use Sylius\RefundPlugin\Provider\TaxRateProviderInterface; | ||
|
||
final class LabelBasedTaxRateProviderSpec extends ObjectBehavior | ||
{ | ||
function it_implements_tax_rate_provider_interface(): void | ||
{ | ||
$this->shouldImplement(TaxRateProviderInterface::class); | ||
} | ||
|
||
function it_provides_a_tax_rate_from_tax_adjustment_label( | ||
OrderItemUnitInterface $orderItemUnit, | ||
AdjustmentInterface $taxAdjustment | ||
): void { | ||
$orderItemUnit | ||
->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT) | ||
->willReturn(new ArrayCollection([$taxAdjustment->getWrappedObject()])) | ||
; | ||
|
||
$taxAdjustment->getLabel()->willReturn('VAT (20%)'); | ||
|
||
$this->provide($orderItemUnit)->shouldReturn('20%'); | ||
} | ||
|
||
function it_provides_a_tax_adjustment_label_if_the_value_does_not_match_the_pattern( | ||
OrderItemUnitInterface $orderItemUnit, | ||
AdjustmentInterface $taxAdjustment | ||
): void { | ||
$orderItemUnit | ||
->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT) | ||
->willReturn(new ArrayCollection([$taxAdjustment->getWrappedObject()])) | ||
; | ||
|
||
$taxAdjustment->getLabel()->willReturn('20%'); | ||
|
||
$this->provide($orderItemUnit)->shouldReturn('20%'); | ||
} | ||
|
||
function it_returns_null_if_there_is_no_tax_adjustment(OrderItemUnitInterface $orderItemUnit): void | ||
{ | ||
$orderItemUnit | ||
->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT) | ||
->willReturn(new ArrayCollection([])) | ||
; | ||
|
||
$this->provide($orderItemUnit)->shouldReturn(null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\RefundPlugin\Provider; | ||
|
||
use Doctrine\Common\Collections\Collection; | ||
use Sylius\Component\Core\Model\AdjustmentInterface; | ||
use Sylius\Component\Core\Model\OrderItemUnitInterface; | ||
|
||
final class LabelBasedTaxRateProvider implements TaxRateProviderInterface | ||
{ | ||
public function provide(OrderItemUnitInterface $orderItemUnit): ?string | ||
{ | ||
/** @var Collection|AdjustmentInterface[] $taxAdjustments */ | ||
$taxAdjustments = $orderItemUnit->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT); | ||
|
||
if ($taxAdjustments->isEmpty()) { | ||
return null; | ||
} | ||
|
||
$label = $taxAdjustments->first()->getLabel(); | ||
|
||
if (preg_match('/\((.*?)\)/', $label, $matches)) { | ||
return end($matches); // returns percent tax rate from tax adjustment label | ||
} | ||
|
||
return $label; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\RefundPlugin\Provider; | ||
|
||
use Sylius\Component\Core\Model\OrderItemUnitInterface; | ||
|
||
interface TaxRateProviderInterface | ||
{ | ||
public function provide(OrderItemUnitInterface $orderItemUnit): ?string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters