-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nenad Stefanovic
authored and
Nenad Stefanovic
committed
Dec 8, 2020
1 parent
4f51cf3
commit e7348bb
Showing
20 changed files
with
318 additions
and
6 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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\ShopApiPlugin\Validator\Cart; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Repository\OrderRepositoryInterface; | ||
use Sylius\ShopApiPlugin\Validator\Constraints\CartEmpty; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
|
||
final class CartEmptyValidatorSpec extends ObjectBehavior | ||
{ | ||
function let( | ||
OrderRepositoryInterface $repository, | ||
ExecutionContextInterface $context | ||
): void { | ||
$this->beConstructedWith($repository); | ||
$this->initialize($context); | ||
} | ||
|
||
function it_add_no_violation_if_cart_is_not_empty( | ||
OrderRepositoryInterface $repository, | ||
OrderInterface $order, | ||
ArrayCollection $collection, | ||
ExecutionContextInterface $context | ||
): void { | ||
$repository->findOneBy(['tokenValue' => 'CART_TOKEN', 'state' => OrderInterface::STATE_CART])->willReturn($order); | ||
|
||
$order->getItems()->willReturn($collection); | ||
$collection->isEmpty()->willReturn(false); | ||
|
||
$context->addViolation('sylius.shop_api.checkout.cart.empty')->shouldNotBeCalled(); | ||
|
||
$this->validate('CART_TOKEN', new CartEmpty()); | ||
} | ||
|
||
function it_add_violation_if_cart_is_empty( | ||
OrderRepositoryInterface $repository, | ||
OrderInterface $order, | ||
ArrayCollection $collection, | ||
ExecutionContextInterface $context | ||
): void { | ||
$repository->findOneBy(['tokenValue' => 'CART_TOKEN', 'state' => OrderInterface::STATE_CART])->willReturn($order); | ||
|
||
$order->getItems()->willReturn($collection); | ||
$collection->isEmpty()->willReturn(true); | ||
|
||
$context->addViolation('sylius.shop_api.checkout.cart.empty')->shouldBeCalled(); | ||
|
||
$this->validate('CART_TOKEN', new CartEmpty()); | ||
} | ||
} |
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
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
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
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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\ShopApiPlugin\Validator\Cart; | ||
|
||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Repository\OrderRepositoryInterface; | ||
use Sylius\ShopApiPlugin\Validator\Constraints\CartEmpty; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class CartEmptyValidator extends ConstraintValidator | ||
{ | ||
/** | ||
* @var OrderRepositoryInterface | ||
*/ | ||
private $_cartRepository; | ||
|
||
/** | ||
* CartEmptyValidator constructor. | ||
* | ||
* @param OrderRepositoryInterface $cartRepository | ||
*/ | ||
public function __construct(OrderRepositoryInterface $cartRepository) | ||
{ | ||
$this->_cartRepository = $cartRepository; | ||
} | ||
|
||
/** | ||
* @param mixed $token | ||
* @param Constraint $constraint | ||
* | ||
* @return void | ||
*/ | ||
public function validate($token, Constraint $constraint): void | ||
{ | ||
Assert::isInstanceOf($constraint, CartEmpty::class); | ||
|
||
$cart = $this->_cartRepository->findOneBy( | ||
[ | ||
'tokenValue' => $token, | ||
'state' => OrderInterface::STATE_CART | ||
] | ||
); | ||
|
||
if ($cart === null) { | ||
return; | ||
} | ||
|
||
Assert::isInstanceOf($cart, OrderInterface::class); | ||
|
||
if ($cart->getItems()->isEmpty()) { | ||
$this->context->addViolation( | ||
$constraint->emptyCartMessage | ||
); | ||
} | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\ShopApiPlugin\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
final class CartEmpty extends Constraint | ||
{ | ||
/** @var string */ | ||
public $emptyCartMessage = 'sylius.shop_api.checkout.cart.empty'; | ||
|
||
/** {@inheritdoc} */ | ||
public function getTargets() | ||
{ | ||
return self::PROPERTY_CONSTRAINT; | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function validatedBy(): string | ||
{ | ||
return 'sylius_shop_api_cart_empty_validator'; | ||
} | ||
} |
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
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
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
Oops, something went wrong.