-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Showing
6 changed files
with
234 additions
and
0 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
22 changes: 22 additions & 0 deletions
22
src/Sylius/Bundle/ApiBundle/Resources/config/validation/CorrectAddressOrder.xml
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,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> |
2 changes: 2 additions & 0 deletions
2
src/Sylius/Bundle/ApiBundle/Resources/translations/validators.en.yaml
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
32 changes: 32 additions & 0 deletions
32
src/Sylius/Bundle/ApiBundle/Validator/Constraints/CorrectAddressOrder.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,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; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/Sylius/Bundle/ApiBundle/Validator/Constraints/CorrectAddressOrderValidator.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,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, Constraint $constraint): void | ||
{ | ||
if ($address === null) { | ||
return; | ||
} | ||
|
||
$countryCode = $address->getCountryCode(); | ||
|
||
if (!Countries::exists($countryCode)) { | ||
$this->context->addViolation( | ||
$constraint->countryWithCountryCodeNotExistMessage, | ||
['%countryCode%' => $countryCode] | ||
); | ||
} | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
src/Sylius/Bundle/ApiBundle/spec/Validator/Constraints/CorrectAddressOrderValidatorSpec.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,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() | ||
); | ||
} | ||
} |