Skip to content

Commit

Permalink
Last fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomanhez committed May 26, 2021
1 parent 7ad3be8 commit ebc19a9
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,3 @@ Feature: Allowing access only for correctly logged in users
And there is logged in the administrator
When the administrator try to see the summary of customer's cart
Then the administrator should see "Stark T-shirt" product with quantity 1 in the visitor cart

@api
Scenario: Trying to add incorrect address to the cart by the visitor
Given the visitor has product "Stark T-Shirt" in the cart
When the visitor specify the email as "jon.snow@example.com"
And the visitor try to specify the billing incorrect address as "Ankh Morpork", "Frost Alley", "90210", "United Russia" for "Jon Snow"
And the visitor complete the addressing step
Then I should be notified that "United Russia" country does not exist
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@checkout
Feature: Order address validation
In order to avoid making mistakes when addressing order
As Visitor
I want to be prevented from adding incorrect order address

Background:
Given the store operates on a single channel in "United States"
And the store has a product "Stark T-shirt" priced at "$12.00"
And the store allows paying offline
And the store has "UPS" shipping method with "$20.00" fee

@api
Scenario: Trying to add address with incorrect country to the cart by the visitor
Given the visitor has product "Stark T-Shirt" in the cart
When the visitor specify the email as "jon.snow@example.com"
And the visitor try to specify the billing incorrect address as "Ankh Morpork", "Frost Alley", "90210", "United Russia" for "Jon Snow"
And the visitor complete the addressing step
Then I should be notified that "United Russia" country does not exist

@api
Scenario: Trying to add address without country to the cart by the visitor
Given the visitor has product "Stark T-Shirt" in the cart
When the visitor specify the email as "jon.snow@example.com"
And the visitor try to specify the billing address without country as "Ankh Morpork", "Frost Alley", "90210" for "Jon Snow"
And the visitor complete the addressing step
Then I should be notified that address without country does not exist
46 changes: 40 additions & 6 deletions src/Sylius/Behat/Context/Api/Shop/CheckoutContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,21 @@ public function iTryToSpecifyTheBillingAddressAs(
): void {
$addressType = 'billingAddress';

[$firstName, $lastName] = explode(' ', $customerName);
$this->addAddressWithoutCountry($addressType, $city, $street, $postcode, $customerName);

$this->content[$addressType]['city'] = $city;
$this->content[$addressType]['street'] = $street;
$this->content[$addressType]['postcode'] = $postcode;
$this->content[$addressType]['countryCode'] = StringInflector::nameToLowercaseCode($countryName);
$this->content[$addressType]['firstName'] = $firstName;
$this->content[$addressType]['lastName'] = $lastName;
}

/**
* @When /^the visitor try to specify the billing address without country as "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)"$/
*/
public function iTryToSpecifyTheBillingAddressWithoutCountryAs(
string $city,
string $street,
string $postcode,
string $customerName
): void {
$this->addAddressWithoutCountry('billingAddress', $city, $street, $postcode, $customerName);
}

/**
Expand Down Expand Up @@ -652,6 +659,17 @@ public function iShouldNotBeNotifiedThatCountryDoesNotExist(string $countryName)
);
}

/**
* @Then I should be notified that address without country does not exist
*/
public function iShouldBeNotifiedThatAddressWithoutCountryDoesNotExist(): void
{
$this->responseChecker->hasViolationWithMessage(
$this->ordersClient->getLastResponse(),
'The address without country can not exist'
);
}

/**
* @Then I should see the thank you page
* @Then /^the (?:visitor|customer) should see the thank you page$/
Expand Down Expand Up @@ -1294,4 +1312,20 @@ private function completeOrder(): Response

return $this->ordersClient->executeCustomRequest($request);
}

private function addAddressWithoutCountry(
string $addressType,
string $city,
string $street,
string $postcode,
string $customerName
): void {
[$firstName, $lastName] = explode(' ', $customerName);

$this->content[$addressType]['city'] = $city;
$this->content[$addressType]['street'] = $street;
$this->content[$addressType]['postcode'] = $postcode;
$this->content[$addressType]['firstName'] = $firstName;
$this->content[$addressType]['lastName'] = $lastName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
<tag name="validator.constraint_validator" alias="sylius_api_validator_adding_eligible_product_variant_to_cart" />
</service>

<service id="sylius.api.validator.correct_address_order" class="Sylius\Bundle\ApiBundle\Validator\Constraints\CorrectAddressOrderValidator">
<tag name="validator.constraint_validator" alias="sylius_api_validator_correct_address_order" />
<service id="sylius.api.validator.correct_order_address" class="Sylius\Bundle\ApiBundle\Validator\Constraints\CorrectOrderAddressValidator">
<tag name="validator.constraint_validator" alias="sylius_api_validator_correct_order_address" />
</service>

<service id="sylius.api.validator.payment_method_eligibility" class="Sylius\Bundle\ApiBundle\Validator\Constraints\OrderPaymentMethodEligibilityValidator">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Bundle\ApiBundle\Command\Checkout\AddressOrder">
<constraint name="Sylius\Bundle\ApiBundle\Validator\Constraints\CorrectAddressOrder">
<constraint name="Sylius\Bundle\ApiBundle\Validator\Constraints\CorrectOrderAddress">
<option name="groups">
<value>sylius</value>
</option>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
sylius:
address:
without_country: 'The address without country can not exist'
country:
not_exist: 'The country %countryCode% does not exist.'
order:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@

use Symfony\Component\Validator\Constraint;

final class CorrectAddressOrder extends Constraint
final class CorrectOrderAddress extends Constraint
{
/** @var string */
public $countryWithCountryCodeNotExistMessage = 'sylius.country.not_exist';
public $countryCodeNotExistMessage = 'sylius.country.not_exist';

/** @var string */
public $addressWithoutCountryCodeCanNotExistMessage = 'sylius.address.without_country';

public function validatedBy(): string
{
return 'sylius_api_validator_correct_address_order';
return 'sylius_api_validator_correct_order_address';
}

public function getTargets(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,40 @@
use Symfony\Component\Validator\ConstraintValidator;
use Webmozart\Assert\Assert;

final class CorrectAddressOrderValidator extends ConstraintValidator
final class CorrectOrderAddressValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
/** @var AddressOrder $value */
Assert::isInstanceOf($value, AddressOrder::class);

/** @var CorrectAddressOrder $constraint */
Assert::isInstanceOf($constraint, CorrectAddressOrder::class);
/** @var CorrectOrderAddress $constraint */
Assert::isInstanceOf($constraint, CorrectOrderAddress::class);

$this->validateAddress($value->billingAddress, $constraint);
$this->validateAddress($value->shippingAddress, $constraint);
}

private function validateAddress(?AddressInterface $address, CorrectAddressOrder $constraint): void
private function validateAddress(?AddressInterface $address, CorrectOrderAddress $constraint): void
{
if ($address === null) {
return;
}

/** @var string|null $countryCode */
$countryCode = $address->getCountryCode();

if ($countryCode === null) {
$this->context->addViolation(
$constraint->addressWithoutCountryCodeCanNotExistMessage
);

return;
}

if (!Countries::exists($countryCode)) {
$this->context->addViolation(
$constraint->countryWithCountryCodeNotExistMessage,
$constraint->countryCodeNotExistMessage,
['%countryCode%' => $countryCode]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@
namespace spec\Sylius\Bundle\ApiBundle\Validator\Constraints;

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ApiBundle\Command\Cart\AddItemToCart;
use Sylius\Bundle\ApiBundle\Command\Checkout\AddressOrder;
use Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder;
use Sylius\Bundle\ApiBundle\Validator\Constraints\AddingEligibleProductVariantToCart;
use Sylius\Bundle\ApiBundle\Validator\Constraints\CorrectAddressOrder;
use Sylius\Bundle\ApiBundle\Validator\Constraints\CorrectAddressOrderValidator;
use Sylius\Bundle\ApiBundle\Validator\Constraints\CorrectOrderAddress;
use Sylius\Component\Core\Model\AddressInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

final class CorrectAddressOrderValidatorSpec extends ObjectBehavior
final class CorrectOrderAddressValidatorSpec extends ObjectBehavior
{
function it_is_a_constraint_validator(): void
{
Expand All @@ -36,15 +33,21 @@ function it_throws_an_exception_if_value_is_not_an_instance_of_address_order_com
{
$this
->shouldThrow(\InvalidArgumentException::class)
->during('validate', [new CompleteOrder(), new AddingEligibleProductVariantToCart()]);
->during('validate', [new CompleteOrder(), new CorrectOrderAddress()]);
}

function it_throws_an_exception_if_constraint_is_not_an_instance_of_adding_eligible_product_variant_to_cart(): void
{
function it_throws_an_exception_if_constraint_is_not_an_instance_of_adding_eligible_product_variant_to_cart(
AddressInterface $billingAddress,
AddressInterface $shippingAddress
): void {
$this
->shouldThrow(\InvalidArgumentException::class)
->during('validate', [
new AddItemToCart('productCode', 'productVariantCode', 1),
new AddressOrder(
'john@doe.com',
$billingAddress->getWrappedObject(),
$shippingAddress->getWrappedObject()
),
new class() extends Constraint {}
])
;
Expand All @@ -65,7 +68,26 @@ function it_adds_violation_if_billing_address_has_incorrect_country_code(

$this->validate(
new AddressOrder('john@doe.com', $billingAddress->getWrappedObject()),
new CorrectAddressOrder()
new CorrectOrderAddress()
);
}

function it_adds_violation_if_billing_address_has_not_country_code(
ExecutionContextInterface $executionContext,
AddressInterface $billingAddress
): void {
$this->initialize($executionContext);

$billingAddress->getCountryCode()->willReturn(null);

$executionContext
->addViolation('sylius.address.without_country')
->shouldBeCalled()
;

$this->validate(
new AddressOrder('john@doe.com', $billingAddress->getWrappedObject()),
new CorrectOrderAddress()
);
}

Expand All @@ -90,7 +112,7 @@ function it_adds_violation_if_shipping_address_has_incorrect_country_code(
$billingAddress->getWrappedObject(),
$shippingAddress->getWrappedObject()
),
new CorrectAddressOrder()
new CorrectOrderAddress()
);
}

Expand All @@ -116,7 +138,7 @@ function it_adds_violation_if_shipping_address_and_billing_address_have_incorrec

$this->validate(
new AddressOrder('john@doe.com', $billingAddress->getWrappedObject(), $shippingAddress->getWrappedObject()),
new CorrectAddressOrder()
new CorrectOrderAddress()
);
}
}

0 comments on commit ebc19a9

Please sign in to comment.