-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #530 Addding assertions for product in current channel (mamazu)
This PR was merged into the 1.0-dev branch. Discussion ---------- Fixes #59 ## What does it do? Currently you can add any product to any cart. After this pull request the product needs to be available in the cart's channel. So that if a product is in channel A and the cart is picked up in channel B then the product-add endpoint will now throw an error (500). ## Why a 500 and not a 400 with validation errors? The problem is that the Validation would need to take two properties of the request into consideration: the current cart token, the product code. Which means that it has to be a "Class Constraint" and needs to access properties of the request object. This is currently not possible as the current request objects don't have getter and the properties are private. So there is no way of validating them. Therefore this pull request only adds an assertion that it is true. (validation may follow if it will be possible to do so in the future) PS: @JakobTolkemit you might be interested. Commits ------- 8b85fbb Addding validation for product in current channel ba20b63 Adding getter and using a class constraint for validation 5f1c839 Removing the 500 from add to product
- Loading branch information
Showing
27 changed files
with
554 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\ShopApiPlugin\Checker; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Core\Model\ChannelInterface; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Sylius\ShopApiPlugin\Checker\ProductInCartChannelCheckerInterface; | ||
|
||
final class ProductInCartChannelCheckerSpec extends ObjectBehavior | ||
{ | ||
function it_implements_product_in_cart_channel_checker_interface(): void | ||
{ | ||
$this->shouldImplement(ProductInCartChannelCheckerInterface::class); | ||
} | ||
|
||
function it_returns_true_if_the_channels_match( | ||
ProductInterface $product, | ||
OrderInterface $order, | ||
ChannelInterface $channel | ||
): void { | ||
$product->getChannels()->willReturn(new ArrayCollection([$channel->getWrappedObject()])); | ||
|
||
$order->getChannel()->willReturn($channel); | ||
|
||
$this->isProductInCartChannel($product, $order)->shouldReturn(true); | ||
} | ||
|
||
function it_returns_false_if_the_channels_do_not_match( | ||
ProductInterface $product, | ||
OrderInterface $order, | ||
ChannelInterface $orderChannel, | ||
ChannelInterface $productChannel1, | ||
ChannelInterface $productChannel2 | ||
): void { | ||
$product->getChannels()->willReturn(new ArrayCollection([ | ||
$productChannel1->getWrappedObject(), | ||
$productChannel2->getWrappedObject(), | ||
])); | ||
|
||
$order->getChannel()->willReturn($orderChannel); | ||
|
||
$this->isProductInCartChannel($product, $order)->shouldReturn(false); | ||
} | ||
} |
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
101 changes: 101 additions & 0 deletions
101
spec/Validator/Product/ProductInCartChannelValidatorSpec.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,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\ShopApiPlugin\Validator\Product; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Sylius\Component\Core\Repository\OrderRepositoryInterface; | ||
use Sylius\Component\Core\Repository\ProductRepositoryInterface; | ||
use Sylius\ShopApiPlugin\Checker\ProductInCartChannelCheckerInterface; | ||
use Sylius\ShopApiPlugin\Request\Cart\PutOptionBasedConfigurableItemToCartRequest; | ||
use Sylius\ShopApiPlugin\Validator\Constraints\ProductInCartChannel; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface; | ||
|
||
final class ProductInCartChannelValidatorSpec extends ObjectBehavior | ||
{ | ||
function let( | ||
ExecutionContextInterface $executionContext, | ||
ProductInCartChannelCheckerInterface $productInCartChannelChecker, | ||
ProductRepositoryInterface $productRepository, | ||
OrderRepositoryInterface $orderRepository | ||
): void { | ||
$this->beConstructedWith($productInCartChannelChecker, $productRepository, $orderRepository); | ||
|
||
$this->initialize($executionContext); | ||
} | ||
|
||
function it_is_constraint_validator(): void | ||
{ | ||
$this->shouldHaveType(ConstraintValidator::class); | ||
} | ||
|
||
function it_skips_validation_if_the_product_is_null( | ||
ProductInCartChannelCheckerInterface $productInCartChannelChecker, | ||
OrderRepositoryInterface $orderRepository, | ||
ProductRepositoryInterface $productRepository, | ||
PutOptionBasedConfigurableItemToCartRequest $request | ||
): void { | ||
$request->getProductCode()->willReturn('TEST'); | ||
$request->getToken()->willReturn('ORDER_TOKEN'); | ||
|
||
$productRepository->findOneByCode('TEST')->willReturn(null); | ||
$orderRepository->findOneBy(['tokenValue' => 'ORDER_TOKEN'])->willReturn(null); | ||
|
||
$productInCartChannelChecker->isProductInCartChannel(Argument::any())->shouldNotBeCalled(); | ||
|
||
$this->validate($request, new ProductInCartChannel()); | ||
} | ||
|
||
function it_does_not_add_validation_if_product_and_cart_share_a_channel( | ||
ProductInCartChannelCheckerInterface $productInCartChannelChecker, | ||
ProductInterface $product, | ||
OrderRepositoryInterface $orderRepository, | ||
OrderInterface $order, | ||
ProductRepositoryInterface $productRepository, | ||
PutOptionBasedConfigurableItemToCartRequest $request, | ||
ExecutionContextInterface $executionContext | ||
): void { | ||
$request->getProductCode()->willReturn('TEST'); | ||
$request->getToken()->willReturn('ORDER_TOKEN'); | ||
|
||
$productRepository->findOneByCode('TEST')->willReturn($product); | ||
$orderRepository->findOneBy(['tokenValue' => 'ORDER_TOKEN'])->willReturn($order); | ||
|
||
$productInCartChannelChecker->isProductInCartChannel($product, $order)->willReturn(true); | ||
|
||
$executionContext->buildViolation(Argument::any())->shouldNotBeCalled(); | ||
|
||
$this->validate($request, new ProductInCartChannel()); | ||
} | ||
|
||
function it_adds_a_violation_if_the_product_does_not_have_the_same_channel_as_cart( | ||
ProductInCartChannelCheckerInterface $productInCartChannelChecker, | ||
ProductInterface $product, | ||
OrderRepositoryInterface $orderRepository, | ||
OrderInterface $order, | ||
ProductRepositoryInterface $productRepository, | ||
PutOptionBasedConfigurableItemToCartRequest $request, | ||
ExecutionContextInterface $executionContext, | ||
ConstraintViolationBuilderInterface $constraintViolationBuilder | ||
): void { | ||
$request->getProductCode()->willReturn('TEST'); | ||
$request->getToken()->willReturn('ORDER_TOKEN'); | ||
|
||
$productRepository->findOneByCode('TEST')->willReturn($product); | ||
$orderRepository->findOneBy(['tokenValue' => 'ORDER_TOKEN'])->willReturn($order); | ||
|
||
$productInCartChannelChecker->isProductInCartChannel($product, $order)->willReturn(false); | ||
|
||
$executionContext->buildViolation('sylius.shop_api.product.not_in_cart_channel')->willReturn($constraintViolationBuilder); | ||
$constraintViolationBuilder->atPath('productCode')->willReturn($constraintViolationBuilder); | ||
$constraintViolationBuilder->addViolation()->shouldBeCalled(); | ||
|
||
$this->validate($request, new ProductInCartChannel()); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\ShopApiPlugin\Checker; | ||
|
||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
|
||
final class ProductInCartChannelChecker implements ProductInCartChannelCheckerInterface | ||
{ | ||
public function isProductInCartChannel(ProductInterface $product, OrderInterface $cart): bool | ||
{ | ||
return in_array($cart->getChannel(), $product->getChannels()->toArray(), true); | ||
} | ||
} |
Oops, something went wrong.