forked from Sylius/Sylius
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PromotionCouponEligbilityValidator refactoring
- Loading branch information
Showing
6 changed files
with
228 additions
and
127 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/Sylius/Bundle/ApiBundle/Checker/AppliedCouponEligibilityChecker.php
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 | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* 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\Bundle\ApiBundle\Checker; | ||
|
||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\PromotionCouponInterface; | ||
use Sylius\Component\Core\Model\PromotionInterface; | ||
use Sylius\Component\Promotion\Checker\Eligibility\PromotionCouponEligibilityCheckerInterface; | ||
use Sylius\Component\Promotion\Checker\Eligibility\PromotionEligibilityCheckerInterface; | ||
|
||
final class AppliedCouponEligibilityChecker implements AppliedCouponEligibilityCheckerInterface | ||
{ | ||
/** @var PromotionEligibilityCheckerInterface */ | ||
private $promotionChecker; | ||
|
||
/** @var PromotionCouponEligibilityCheckerInterface */ | ||
private $promotionCouponChecker; | ||
|
||
public function __construct( | ||
PromotionEligibilityCheckerInterface $promotionChecker, | ||
PromotionCouponEligibilityCheckerInterface $promotionCouponChecker | ||
) { | ||
$this->promotionChecker = $promotionChecker; | ||
$this->promotionCouponChecker = $promotionCouponChecker; | ||
} | ||
|
||
public function isEligible(PromotionCouponInterface $promotionCoupon, OrderInterface $cart): bool | ||
{ | ||
/** @var PromotionInterface $promotion */ | ||
$promotion = $promotionCoupon->getPromotion(); | ||
|
||
if (!$promotion->getChannels()->contains($cart->getChannel())) { | ||
return false; | ||
} | ||
|
||
if (!$this->promotionCouponChecker->isEligible($cart, $promotionCoupon)) { | ||
return false; | ||
} | ||
|
||
if (!$this->promotionChecker->isEligible($cart, $promotionCoupon->getPromotion())) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Sylius/Bundle/ApiBundle/Checker/AppliedCouponEligibilityCheckerInterface.php
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Checker; | ||
|
||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\PromotionCouponInterface; | ||
|
||
interface AppliedCouponEligibilityCheckerInterface | ||
{ | ||
public function isEligible(PromotionCouponInterface $promotionCoupon, OrderInterface $cart): bool; | ||
} |
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
133 changes: 133 additions & 0 deletions
133
src/Sylius/Bundle/ApiBundle/spec/Checker/AppliedCouponEligibilityCheckerSpec.php
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,133 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* 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\Bundle\ApiBundle\Checker; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Sylius\Bundle\ApiBundle\Checker\AppliedCouponEligibilityCheckerInterface; | ||
use Sylius\Component\Core\Model\ChannelInterface; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\PromotionCouponInterface; | ||
use Sylius\Component\Core\Model\PromotionInterface; | ||
use Sylius\Component\Promotion\Checker\Eligibility\PromotionCouponEligibilityCheckerInterface; | ||
use Sylius\Component\Promotion\Checker\Eligibility\PromotionEligibilityCheckerInterface; | ||
|
||
final class AppliedCouponEligibilityCheckerSpec extends ObjectBehavior | ||
{ | ||
function let( | ||
PromotionEligibilityCheckerInterface $promotionChecker, | ||
PromotionCouponEligibilityCheckerInterface $promotionCouponChecker | ||
): void { | ||
$this->beConstructedWith($promotionChecker, $promotionCouponChecker); | ||
} | ||
|
||
function it_implements_promotion_coupon_eligibility_checker_interface(): void | ||
{ | ||
$this->shouldImplement(AppliedCouponEligibilityCheckerInterface::class); | ||
} | ||
|
||
function it_returns_false_if_cart_channel_is_not_one_of_promotion_channels( | ||
PromotionEligibilityCheckerInterface $promotionChecker, | ||
PromotionCouponEligibilityCheckerInterface $promotionCouponChecker, | ||
PromotionCouponInterface $promotionCoupon, | ||
PromotionInterface $promotion, | ||
OrderInterface $cart, | ||
ChannelInterface $firstChannel, | ||
ChannelInterface $secondChannel, | ||
ChannelInterface $thirdChannel | ||
): void { | ||
$promotionCoupon->getPromotion()->willReturn($promotion); | ||
|
||
$promotion->getChannels()->willReturn(new ArrayCollection([ | ||
$secondChannel->getWrappedObject(), | ||
$thirdChannel->getWrappedObject() | ||
])); | ||
$cart->getChannel()->willReturn($firstChannel); | ||
|
||
$promotionChecker->isEligible(Argument::any())->shouldNotBeCalled(); | ||
$promotionCouponChecker->isEligible(Argument::any())->shouldNotBeCalled(); | ||
|
||
$this->isEligible($promotionCoupon, $cart)->shouldReturn(false); | ||
} | ||
|
||
function it_returns_false_if_coupon_is_not_eligible( | ||
PromotionEligibilityCheckerInterface $promotionChecker, | ||
PromotionCouponEligibilityCheckerInterface $promotionCouponChecker, | ||
PromotionCouponInterface $promotionCoupon, | ||
PromotionInterface $promotion, | ||
OrderInterface $cart, | ||
ChannelInterface $firstChannel, | ||
ChannelInterface $secondChannel | ||
): void { | ||
$promotionCoupon->getPromotion()->willReturn($promotion); | ||
|
||
$promotion->getChannels()->willReturn(new ArrayCollection([ | ||
$firstChannel->getWrappedObject(), | ||
$secondChannel->getWrappedObject() | ||
])); | ||
$cart->getChannel()->willReturn($firstChannel); | ||
|
||
$promotionCouponChecker->isEligible($cart, $promotionCoupon)->willReturn(false); | ||
$promotionChecker->isEligible(Argument::any())->shouldNotBeCalled(); | ||
|
||
$this->isEligible($promotionCoupon, $cart)->shouldReturn(false); | ||
} | ||
|
||
function it_returns_false_if_promotion_is_not_eligible( | ||
PromotionEligibilityCheckerInterface $promotionChecker, | ||
PromotionCouponEligibilityCheckerInterface $promotionCouponChecker, | ||
PromotionCouponInterface $promotionCoupon, | ||
PromotionInterface $promotion, | ||
OrderInterface $cart, | ||
ChannelInterface $firstChannel, | ||
ChannelInterface $secondChannel | ||
): void { | ||
$promotionCoupon->getPromotion()->willReturn($promotion); | ||
|
||
$promotion->getChannels()->willReturn(new ArrayCollection([ | ||
$firstChannel->getWrappedObject(), | ||
$secondChannel->getWrappedObject() | ||
])); | ||
$cart->getChannel()->willReturn($firstChannel); | ||
|
||
$promotionCouponChecker->isEligible($cart, $promotionCoupon)->willReturn(true); | ||
$promotionChecker->isEligible($cart, $promotion)->willReturn(false); | ||
|
||
$this->isEligible($promotionCoupon, $cart)->shouldReturn(false); | ||
} | ||
|
||
function it_returns_true_if_promotion_and_coupon_are_eligible( | ||
PromotionEligibilityCheckerInterface $promotionChecker, | ||
PromotionCouponEligibilityCheckerInterface $promotionCouponChecker, | ||
PromotionCouponInterface $promotionCoupon, | ||
PromotionInterface $promotion, | ||
OrderInterface $cart, | ||
ChannelInterface $firstChannel, | ||
ChannelInterface $secondChannel | ||
): void { | ||
$promotionCoupon->getPromotion()->willReturn($promotion); | ||
|
||
$promotion->getChannels()->willReturn(new ArrayCollection([ | ||
$firstChannel->getWrappedObject(), | ||
$secondChannel->getWrappedObject() | ||
])); | ||
$cart->getChannel()->willReturn($firstChannel); | ||
|
||
$promotionCouponChecker->isEligible($cart, $promotionCoupon)->willReturn(true); | ||
$promotionChecker->isEligible($cart, $promotion)->willReturn(true); | ||
|
||
$this->isEligible($promotionCoupon, $cart)->shouldReturn(true); | ||
} | ||
} |
Oops, something went wrong.