Skip to content

Commit

Permalink
[Address] Handle adress exists validation in case when address does n…
Browse files Browse the repository at this point in the history
…ot belong to any customer
  • Loading branch information
hurricane-voronin authored and mamazu committed Mar 31, 2021
1 parent e6bd790 commit c91a315
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
22 changes: 22 additions & 0 deletions spec/Validator/Address/AddressExistsValidatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,26 @@ function it_adds_constraint_if_current_user_is_not_address_owner(

$this->validate('ADDRESS_ID', new AddressExists());
}

function it_adds_constraint_if_the_address_is_not_owned_by_anyone(
AddressInterface $address,
AddressRepositoryInterface $addressRepository,
LoggedInShopUserProviderInterface $currentUserProvider,
ShopUserInterface $shopUser,
CustomerInterface $customerOwner,
ExecutionContextInterface $executionContext
): void {
$addressRepository->findOneBy(['id' => 'ADDRESS_ID'])->willReturn($address);

$customerOwner->getEmail()->willReturn('oliver@queen.com');
$address->getCustomer()->willReturn(null);

$currentUserProvider->provide()->willReturn($shopUser);

$shopUser->getEmail()->willReturn('shop@example.com');

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

$this->validate('ADDRESS_ID', new AddressExists());
}
}
3 changes: 2 additions & 1 deletion src/Validator/Address/AddressExistsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ public function validate($id, Constraint $constraint): void
}

$user = $this->loggedInUserProvider->provide();
$customer = $address->getCustomer();

if ($address->getCustomer()->getEmail() !== $user->getEmail()) {
if (null === $customer || $customer->getEmail() !== $user->getEmail()) {
$this->context->addViolation($constraint->message);

return;
Expand Down

0 comments on commit c91a315

Please sign in to comment.