Skip to content

Commit

Permalink
Add CorrectAddressOrderValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomanhez committed May 25, 2021
1 parent 7460184 commit 7ad3be8
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
<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>

<service id="sylius.api.validator.payment_method_eligibility" class="Sylius\Bundle\ApiBundle\Validator\Constraints\OrderPaymentMethodEligibilityValidator">
<argument type="service" id="sylius.repository.order" />
<tag name="validator.constraint_validator" alias="sylius_api_order_payment_method_eligibility" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
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.
-->

<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">
<option name="groups">
<value>sylius</value>
</option>
</constraint>
</class>
</constraint-mapping>
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
sylius:
country:
not_exist: 'The country %countryCode% does not exist.'
order:
not_empty: 'An empty order cannot be completed.'
product:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

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

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

public function getTargets(): string
{
return self::CLASS_CONSTRAINT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\Validator\Constraints;

use Sylius\Bundle\ApiBundle\Command\Checkout\AddressOrder;
use Sylius\Component\Core\Model\AddressInterface;
use Symfony\Component\Intl\Countries;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Webmozart\Assert\Assert;

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

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

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

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

$countryCode = $address->getCountryCode();

if (!Countries::exists($countryCode)) {
$this->context->addViolation(
$constraint->countryWithCountryCodeNotExistMessage,
['%countryCode%' => $countryCode]
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?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\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\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
{
function it_is_a_constraint_validator(): void
{
$this->shouldImplement(ConstraintValidatorInterface::class);
}

function it_throws_an_exception_if_value_is_not_an_instance_of_address_order_command(): void
{
$this
->shouldThrow(\InvalidArgumentException::class)
->during('validate', [new CompleteOrder(), new AddingEligibleProductVariantToCart()]);
}

function it_throws_an_exception_if_constraint_is_not_an_instance_of_adding_eligible_product_variant_to_cart(): void
{
$this
->shouldThrow(\InvalidArgumentException::class)
->during('validate', [
new AddItemToCart('productCode', 'productVariantCode', 1),
new class() extends Constraint {}
])
;
}

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

$billingAddress->getCountryCode()->willReturn('united_russia');

$executionContext
->addViolation('sylius.country.not_exist', ['%countryCode%' => 'united_russia'])
->shouldBeCalled()
;

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

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

$billingAddress->getCountryCode()->willReturn('US');
$shippingAddress->getCountryCode()->willReturn('united_russia');

$executionContext
->addViolation('sylius.country.not_exist', ['%countryCode%' => 'united_russia'])
->shouldBeCalled()
;

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

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

$billingAddress->getCountryCode()->willReturn('euroland');
$shippingAddress->getCountryCode()->willReturn('united_russia');

$executionContext
->addViolation('sylius.country.not_exist', ['%countryCode%' => 'euroland'])
->shouldBeCalled()
;

$executionContext
->addViolation('sylius.country.not_exist', ['%countryCode%' => 'united_russia'])
->shouldBeCalled()
;

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

0 comments on commit 7ad3be8

Please sign in to comment.