mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-14 17:10:53 +00:00
[API] Remove validation from locale, add step with wrong locale in header
This commit is contained in:
parent
b5d71d2271
commit
4086959f74
8 changed files with 3 additions and 235 deletions
|
|
@ -25,4 +25,5 @@ Feature: Picking up the cart with the locale other than the default
|
|||
@api @no-ui
|
||||
Scenario: Picking up the cart with non valid locale
|
||||
When I pick up cart using wrong locale
|
||||
Then I should be notified that locale does not exist
|
||||
And I check details of my cart
|
||||
Then my cart's locale should be "French (France)"
|
||||
|
|
|
|||
|
|
@ -225,9 +225,7 @@ final class CartContext implements Context
|
|||
*/
|
||||
public function iPickUpMyCartUsingWrongLocale(): void
|
||||
{
|
||||
$this->cartsClient->buildCreateRequest();
|
||||
$this->cartsClient->addRequestData('localeCode', 'en');
|
||||
$this->cartsClient->create();
|
||||
$this->pickupCart('en');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -375,21 +373,6 @@ final class CartContext implements Context
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Then I should be notified that locale does not exist
|
||||
*/
|
||||
public function iShouldBeNotifiedThatLocaleDoesNotExist(): void
|
||||
{
|
||||
$response = $this->cartsClient->getLastResponse();
|
||||
|
||||
Assert::false(
|
||||
$this->responseChecker->isCreationSuccessful($response),
|
||||
SprintfResponseEscaper::provideMessageWithEscapedResponseContent('The given locale exists but it should not', $response)
|
||||
);
|
||||
|
||||
Assert::same($this->responseChecker->getError($response), 'The locale en does not exist.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @Then I should be notified that quantity of added product cannot be lower that 1
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -92,11 +92,6 @@
|
|||
<tag name="validator.constraint_validator" alias="sylius_api_confirm_reset_password" />
|
||||
</service>
|
||||
|
||||
<service id="Sylius\Bundle\ApiBundle\Validator\Constraints\PickupCartLocaleValidator">
|
||||
<argument type="service" id="sylius.repository.channel" />
|
||||
<tag name="validator.constraint_validator" alias="sylius_api_pickup_cart_locale" />
|
||||
</service>
|
||||
|
||||
<service id="Sylius\Bundle\ApiBundle\Validator\Constraints\PromotionCouponEligibilityValidator">
|
||||
<argument type="service" id="sylius.repository.promotion_coupon" />
|
||||
<argument type="service" id="sylius.repository.order" />
|
||||
|
|
|
|||
|
|
@ -1,25 +0,0 @@
|
|||
<?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\Cart\PickupCart">
|
||||
<constraint name="Sylius\Bundle\ApiBundle\Validator\Constraints\PickupCartLocale">
|
||||
<option name="groups">
|
||||
<value>sylius</value>
|
||||
</option>
|
||||
</constraint>
|
||||
</class>
|
||||
</constraint-mapping>
|
||||
|
|
@ -6,8 +6,6 @@ sylius:
|
|||
without_country: 'The address without country cannot exist'
|
||||
country:
|
||||
not_exist: 'The country %countryCode% does not exist.'
|
||||
locale:
|
||||
not_exist: 'The locale %localeCode% does not exist.'
|
||||
order:
|
||||
not_empty: 'An empty order cannot be completed.'
|
||||
payment_method:
|
||||
|
|
|
|||
|
|
@ -1,32 +0,0 @@
|
|||
<?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;
|
||||
|
||||
/** @experimental */
|
||||
final class PickupCartLocale extends Constraint
|
||||
{
|
||||
public string $notExist = 'sylius.locale.not_exist';
|
||||
|
||||
public function validatedBy(): string
|
||||
{
|
||||
return 'sylius_api_pickup_cart_locale';
|
||||
}
|
||||
|
||||
public function getTargets(): string
|
||||
{
|
||||
return self::CLASS_CONSTRAINT;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
<?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\Cart\PickupCart;
|
||||
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
|
||||
use Sylius\Component\Core\Model\ChannelInterface;
|
||||
use Sylius\Component\Locale\Model\LocaleInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
class PickupCartLocaleValidator extends ConstraintValidator
|
||||
{
|
||||
private ChannelRepositoryInterface $channelRepository;
|
||||
|
||||
public function __construct(ChannelRepositoryInterface $channelRepository)
|
||||
{
|
||||
$this->channelRepository = $channelRepository;
|
||||
}
|
||||
|
||||
public function validate($value, Constraint $constraint): void
|
||||
{
|
||||
/** @var PickupCart $value */
|
||||
Assert::isInstanceOf($value, PickupCart::class);
|
||||
|
||||
if ($value->localeCode === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var ChannelInterface $channel */
|
||||
$channel = $this->channelRepository->findOneByCode($value->getChannelCode());
|
||||
|
||||
$locales = $channel->getLocales()->filter(function (LocaleInterface $locale) use ($value): bool {
|
||||
return $locale->getCode() === $value->localeCode;
|
||||
});
|
||||
|
||||
if ($locales->isEmpty()) {
|
||||
$this->context->addViolation($constraint->notExist, ['%localeCode%' => $value->localeCode]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,99 +0,0 @@
|
|||
<?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 Doctrine\Common\Collections\ArrayCollection;
|
||||
use PhpSpec\ObjectBehavior;
|
||||
use Sylius\Bundle\ApiBundle\Command\Cart\PickupCart;
|
||||
use Sylius\Bundle\ApiBundle\Validator\Constraints\PickupCartLocale;
|
||||
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
|
||||
use Sylius\Component\Core\Model\ChannelInterface;
|
||||
use Sylius\Component\Locale\Model\LocaleInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidatorInterface;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
final class PickupCartLocaleValidatorSpec extends ObjectBehavior
|
||||
{
|
||||
function let(ChannelRepositoryInterface $channelRepository): void
|
||||
{
|
||||
$this->beConstructedWith($channelRepository);
|
||||
}
|
||||
|
||||
function it_is_a_constraint_validator(): void
|
||||
{
|
||||
$this->shouldImplement(ConstraintValidatorInterface::class);
|
||||
}
|
||||
|
||||
function it_does_not_add_violation_if_locale_code_exists(
|
||||
ExecutionContextInterface $executionContext,
|
||||
ChannelRepositoryInterface $channelRepository,
|
||||
ChannelInterface $channel,
|
||||
LocaleInterface $locale
|
||||
): void {
|
||||
$constraint = new PickupCartLocale();
|
||||
$this->initialize($executionContext);
|
||||
|
||||
$value = new PickupCart('token', 'en_US');
|
||||
$value->setChannelCode('code');
|
||||
|
||||
$channelRepository->findOneByCode('code')->willReturn($channel);
|
||||
|
||||
$locale->getCode()->willReturn('en_US');
|
||||
$channel->getLocales()->willReturn(new ArrayCollection([$locale->getWrappedObject()]));
|
||||
|
||||
$executionContext->addViolation('sylius.locale.not_exist', ["%localeCode%" => "en_US"])->shouldNotBeCalled();
|
||||
|
||||
$this->validate($value, $constraint);
|
||||
}
|
||||
|
||||
function it_does_not_add_violation_if_locale_code_is_not_set(
|
||||
ExecutionContextInterface $executionContext,
|
||||
ChannelRepositoryInterface $channelRepository
|
||||
): void {
|
||||
$constraint = new PickupCartLocale();
|
||||
$this->initialize($executionContext);
|
||||
|
||||
$value = new PickupCart('token', null);
|
||||
$value->setChannelCode('code');
|
||||
|
||||
$channelRepository->findOneByCode('code')->shouldNotBeCalled();
|
||||
|
||||
$executionContext->addViolation('sylius.locale.not_exist', ["%localeCode%" => "en_US"])->shouldNotBeCalled();
|
||||
|
||||
$this->validate($value, $constraint);
|
||||
}
|
||||
|
||||
function it_adds_violation_if_locale_code_does_not_exist(
|
||||
ExecutionContextInterface $executionContext,
|
||||
ChannelRepositoryInterface $channelRepository,
|
||||
ChannelInterface $channel,
|
||||
LocaleInterface $locale
|
||||
): void {
|
||||
$constraint = new PickupCartLocale();
|
||||
$this->initialize($executionContext);
|
||||
|
||||
$value = new PickupCart('token', 'en');
|
||||
$value->setChannelCode('code');
|
||||
|
||||
$channelRepository->findOneByCode('code')->willReturn($channel);
|
||||
|
||||
$locale->getCode()->willReturn('en_US');
|
||||
$channel->getLocales()->willReturn(new ArrayCollection([$locale->getWrappedObject()]));
|
||||
|
||||
$executionContext->addViolation('sylius.locale.not_exist', ["%localeCode%" => "en"])->shouldBeCalled();
|
||||
|
||||
$this->validate($value, $constraint);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue