Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKasp committed May 23, 2021
1 parent 2e27e12 commit 8e79960
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ Feature: Being unable to buy products that are out of stock

@ui @api
Scenario: Placing an order with products that have sufficient quantity
Given I have added 3 products "Iron Maiden T-Shirt" to the cart
When I add 3 products "Iron Maiden T-Shirt" to the cart
And I proceed through checkout process
When I confirm my order
And I confirm my order
Then I should see the thank you page

@ui @api
Scenario: Being unable to place an order with product that is out of stock
Given I have added 5 products "Iron Maiden T-Shirt" to the cart
When I add 5 products "Iron Maiden T-Shirt" to the cart
And I proceed through checkout process
When other customer has bought 2 "Iron Maiden T-Shirt" products by this time
And other customer has bought 2 "Iron Maiden T-Shirt" products by this time
And I confirm my order
Then I should not see the thank you page
And I should be notified that "Iron Maiden T-Shirt" does not have sufficient stock
And I should be notified that product "Iron Maiden T-Shirt" does not have sufficient stock

@ui @api
Scenario: Being unable to place an order with products that are out of stock
Given I have added 5 products "Iron Maiden T-Shirt" to the cart
And I have added 5 products "2Pac T-Shirt" to the cart
And I have proceeded selecting "Offline" payment method
When other customer has bought 7 "2Pac T-Shirt" products by this time
When I add 5 products "Iron Maiden T-Shirt" to the cart
And I add 5 products "2Pac T-Shirt" to the cart
And I proceed selecting "Offline" payment method
And other customer has bought 7 "2Pac T-Shirt" products by this time
And I confirm my order
Then I should not see the thank you page
And I should be notified that this product does not have sufficient stock
1 change: 0 additions & 1 deletion src/Sylius/Behat/Context/Api/Shop/CartContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ public function iAddThisProductToTheCart(ProductInterface $product, ?string $tok
}

/**
* @Given /^I have(?:| added) (\d+) (products "[^"]+") (?:to|in) the (cart)$/
* @When /^I add (\d+) of (them) to (?:the|my) (cart)$/
* @When /^I add (\d+) (products "[^"]+") to the (cart)$/
* @When /^I try to add (\d+) (products "[^"]+") to the (cart)$/
Expand Down
12 changes: 3 additions & 9 deletions src/Sylius/Behat/Context/Api/Shop/CheckoutContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Core\OrderCheckoutStates;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Product\Resolver\DefaultProductVariantResolver;
use Sylius\Component\Product\Resolver\ProductVariantResolverInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\HttpFoundation\Request as HTTPRequest;
Expand Down Expand Up @@ -73,9 +72,6 @@ final class CheckoutContext implements Context
/** @var SharedStorageInterface */
private $sharedStorage;

/** @var DefaultProductVariantResolver */
private $defaultProductVariantResolver;

/** @var string[] */
private $content = [];

Expand All @@ -88,8 +84,7 @@ public function __construct(
RepositoryInterface $paymentMethodRepository,
ProductVariantResolverInterface $productVariantResolver,
IriConverterInterface $iriConverter,
SharedStorageInterface $sharedStorage,
DefaultProductVariantResolver $defaultProductVariantResolver
SharedStorageInterface $sharedStorage
) {
$this->ordersClient = $ordersClient;
$this->addressesClient = $addressesClient;
Expand All @@ -100,7 +95,6 @@ public function __construct(
$this->productVariantResolver = $productVariantResolver;
$this->iriConverter = $iriConverter;
$this->sharedStorage = $sharedStorage;
$this->defaultProductVariantResolver = $defaultProductVariantResolver;
}

/**
Expand Down Expand Up @@ -962,12 +956,12 @@ public function iShouldNotBeAbleToSpecifyProvinceNameManuallyForBillingAddress()

/**
* @Then /^I should be notified that (this product) does not have sufficient stock$/
* @Then I should be notified that :product does not have sufficient stock
* @Then I should be notified that product :product does not have sufficient stock
*/
public function iShouldBeNotifiedThatThisProductDoesNotHaveSufficientStock(ProductInterface $product): void
{
/** @var ProductVariantInterface $variant */
$variant = $this->defaultProductVariantResolver->getVariant($product);
$variant = $this->productVariantResolver->getVariant($product);

Assert::true($this->responseChecker->hasViolationWithMessage(
$this->ordersClient->getLastResponse(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public function theProductShouldHaveUnitPrice(ProductInterface $product, $price)

/**
* @Then /^I should be notified that (this product) does not have sufficient stock$/
* @Then I should be notified that :product does not have sufficient stock
* @Then I should be notified that product :product does not have sufficient stock
*/
public function iShouldBeNotifiedThatThisProductDoesNotHaveSufficientStock(ProductInterface $product)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<tag name="validator.constraint_validator" alias="sylius_api_validator_order_product_eligibility" />
</service>

<service id="sylius.api.validator.order_product_in_stock_eligibility" class="Sylius\Bundle\ApiBundle\Validator\Constraints\OrderProductInStockEligibilityValidator">
<service id="sylius.api.validator.order_product_in_stock_eligibility" class="Sylius\Bundle\ApiBundle\Validator\Constraints\OrderItemAvailabilityValidator">
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="sylius.availability_checker" />
<tag name="validator.constraint_validator" alias="sylius_api_validator_order_product_in_stock_eligibility" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<value>sylius_checkout_complete</value>
</option>
</constraint>
<constraint name="Sylius\Bundle\ApiBundle\Validator\Constraints\OrderProductInStockEligibility">
<constraint name="Sylius\Bundle\ApiBundle\Validator\Constraints\OrderItemAvailability">
<option name="groups">
<value>sylius_checkout_complete</value>
</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Symfony\Component\Validator\Constraint;

/** @experimental */
final class OrderProductInStockEligibility extends Constraint
final class OrderItemAvailability extends Constraint
{
/** @var string */
public $message = 'sylius.product_variant.product_variant_with_name_not_sufficient';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Webmozart\Assert\Assert;

/** @experimental */
final class OrderProductInStockEligibilityValidator extends ConstraintValidator
final class OrderItemAvailabilityValidator extends ConstraintValidator
{
/** @var OrderRepositoryInterface */
private $orderRepository;
Expand All @@ -43,8 +43,8 @@ public function validate($value, Constraint $constraint)
{
Assert::isInstanceOf($value, OrderTokenValueAwareInterface::class);

/** @var OrderProductInStockEligibility $constraint */
Assert::isInstanceOf($constraint, OrderProductInStockEligibility::class);
/** @var OrderItemAvailability $constraint */
Assert::isInstanceOf($constraint, OrderItemAvailability::class);

$order = $this->orderRepository->findOneBy(['tokenValue' => $value->getOrderTokenValue()]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Doctrine\Common\Collections\Collection;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder;
use Sylius\Bundle\ApiBundle\Validator\Constraints\OrderProductInStockEligibility;
use Sylius\Bundle\ApiBundle\Validator\Constraints\OrderItemAvailability;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
Expand All @@ -27,11 +27,15 @@
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

final class OrderProductInStockEligibilityValidatorSpec extends ObjectBehavior
final class OrderItemAvailabilityValidatorSpec extends ObjectBehavior
{
function let(OrderRepositoryInterface $orderRepository, AvailabilityCheckerInterface $availabilityChecker): void
{
function let(
OrderRepositoryInterface $orderRepository,
AvailabilityCheckerInterface $availabilityChecker,
ExecutionContextInterface $executionContext
): void {
$this->beConstructedWith($orderRepository, $availabilityChecker);
$this->initialize($executionContext);
}

function it_is_a_constraint_validator(): void
Expand All @@ -51,16 +55,14 @@ function it_throws_exception_if_constraint_is_not_an_instance_of_order_product_i
}

function it_adds_violation_if_product_variant_does_not_have_sufficient_stock(
ExecutionContextInterface $executionContext,
OrderRepositoryInterface $orderRepository,
AvailabilityCheckerInterface $availabilityChecker,
ExecutionContextInterface $executionContext,
OrderInterface $order,
OrderItemInterface $orderItem,
ProductVariantInterface $productVariant,
AvailabilityCheckerInterface $availabilityChecker,
Collection $orderItems
): void {
$this->initialize($executionContext);

$command = new CompleteOrder();
$command->setOrderTokenValue('cartToken');

Expand All @@ -81,20 +83,18 @@ function it_adds_violation_if_product_variant_does_not_have_sufficient_stock(
->shouldBeCalled()
;

$this->validate($command, new OrderProductInStockEligibility());
$this->validate($command, new OrderItemAvailability());
}

function it_does_nothing_if_product_variant_has_sufficient_stock(
ExecutionContextInterface $executionContext,
OrderRepositoryInterface $orderRepository,
AvailabilityCheckerInterface $availabilityChecker,
ExecutionContextInterface $executionContext,
OrderInterface $order,
OrderItemInterface $orderItem,
ProductVariantInterface $productVariant,
AvailabilityCheckerInterface $availabilityChecker,
Collection $orderItems
): void {
$this->initialize($executionContext);

$command = new CompleteOrder();
$command->setOrderTokenValue('cartToken');

Expand All @@ -115,6 +115,6 @@ function it_does_nothing_if_product_variant_has_sufficient_stock(
->shouldNotBeCalled()
;

$this->validate($command, new OrderProductInStockEligibility());
$this->validate($command, new OrderItemAvailability());
}
}

0 comments on commit 8e79960

Please sign in to comment.