transformer for logged in customers added

This commit is contained in:
TheMilek 2022-05-26 13:30:48 +02:00
parent 315583c4ed
commit 7d6e0f3698
No known key found for this signature in database
GPG key ID: FCAC2B762BB5D833
9 changed files with 202 additions and 13 deletions

View file

@ -8,7 +8,7 @@ Feature: Requesting contact
Given the store operates on a single channel in "United States"
And this channel has contact email set as "contact@goodshop.com"
@ui @email
@ui @api @email
Scenario: Requesting contact as a logged in customer
Given there is a user "lucifer@morningstar.com"
And I am logged in as "lucifer@morningstar.com"

View file

@ -26,7 +26,7 @@ final class ContactContext implements Context
public function __construct(
private RequestFactoryInterface $requestFactory,
private ApiClientInterface $apiClient,
private ApiClientInterface $client,
private ResponseChecker $responseChecker,
) {
}
@ -65,12 +65,13 @@ final class ContactContext implements Context
$request = $this->requestFactory->create(
'shop',
Resources::CONTACT_REQUESTS,
'Bearer'
'Authorization',
$this->client->getToken()
);
$request->setContent($this->content);
$this->apiClient->request($request);
$this->client->request($request);
}
/**
@ -78,7 +79,7 @@ final class ContactContext implements Context
*/
public function iShouldBeNotifiedThatTheContactRequestHasBeenSubmittedSuccessfully(): void
{
$response = $this->apiClient->getLastResponse();
$response = $this->client->getLastResponse();
Assert::same($response->getStatusCode(), 202);
}
@ -88,7 +89,7 @@ final class ContactContext implements Context
*/
public function iShouldBeNotifiedThatEmailIsInvalid(): void
{
$response = $this->apiClient->getLastResponse();
$response = $this->client->getLastResponse();
Assert::same(
$this->responseChecker->getError($response),
@ -101,7 +102,7 @@ final class ContactContext implements Context
*/
public function iShouldBeNotifiedThatElementIsRequired(string $element): void
{
$response = $this->apiClient->getLastResponse();
$response = $this->client->getLastResponse();
Assert::same(
$this->responseChecker->getError($response),

View file

@ -175,7 +175,7 @@
<argument type="service" id="sylius.behat.shared_storage" />
</service>
<service id="sylius.behat.context.api.shop.contact_request" class="Sylius\Behat\Context\Api\Shop\ContactContext">
<service id="sylius.behat.context.api.shop.contact" class="Sylius\Behat\Context\Api\Shop\ContactContext">
<argument type="service" id="sylius.behat.request_factory" />
<argument type="service" id="sylius.behat.api_platform_client.shop" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />

View file

@ -11,11 +11,11 @@ default:
- sylius.behat.context.transform.shared_storage
- sylius.behat.context.setup.channel
- sylius.behat.context.setup.shop_security
- sylius.behat.context.setup.shop_api_security
- sylius.behat.context.setup.user
- sylius.behat.context.api.shop.contact_request
- sylius.behat.context.ui.email
- sylius.behat.context.api.shop.contact
- sylius.behat.context.api.email
filters:
tags: "@requesting_contact&&@api"

View file

@ -0,0 +1,22 @@
<?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\Command;
/** @experimental */
interface LoggedInCustomerEmailIfNotSetAwareInterface extends CommandAwareDataTransformerInterface
{
public function getEmail(): ?string;
public function setEmail(string $email): void;
}

View file

@ -14,7 +14,7 @@ declare(strict_types=1);
namespace Sylius\Bundle\ApiBundle\Command;
/** experimental */
class SendContactRequest implements ChannelCodeAwareInterface, LocaleCodeAwareInterface
class SendContactRequest implements ChannelCodeAwareInterface, LocaleCodeAwareInterface, LoggedInCustomerEmailIfNotSetAwareInterface
{
public ?string $localeCode = null;
public ?string $channelCode = null;
@ -45,11 +45,16 @@ class SendContactRequest implements ChannelCodeAwareInterface, LocaleCodeAwareIn
$this->localeCode = $localeCode;
}
public function getEmail(): string
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): void
{
$this->email = $email;
}
public function getMessage(): string
{
return $this->message;

View file

@ -0,0 +1,65 @@
<?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\DataTransformer;
use Sylius\Bundle\ApiBundle\Command\LoggedInCustomerEmailIfNotSetAwareInterface;
use Sylius\Bundle\ApiBundle\Command\CustomerEmailAwareInterface;
use Sylius\Bundle\ApiBundle\Context\UserContextInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\User\Model\UserInterface;
use Webmozart\Assert\Assert;
/** @experimental */
final class LoggedInCustomerEmailIfNotSetAwareCommandDataTransformer implements CommandDataTransformerInterface
{
public function __construct(private UserContextInterface $userContext)
{
}
public function transform($object, string $to, array $context = [])
{
/** @var CustomerInterface|null $customer */
$customer = $this->getCustomer();
/** @var CustomerEmailAwareInterface|mixed $object */
Assert::isInstanceOf($object, LoggedInCustomerEmailIfNotSetAwareInterface::class);
if ($customer === null) {
return $object;
}
if ($object->getEmail() === null) {
$object->setEmail($customer->getEmail());
}
return $object;
}
public function supportsTransformation($object): bool
{
return $object instanceof LoggedInCustomerEmailIfNotSetAwareInterface;
}
private function getCustomer(): ?CustomerInterface
{
/** @var UserInterface|null $user */
$user = $this->userContext->getUser();
if ($user instanceof ShopUserInterface) {
return $user->getCustomer();
}
return null;
}
}

View file

@ -64,6 +64,11 @@
<tag name="sylius.api.command_data_transformer" />
</service>
<service id="Sylius\Bundle\ApiBundle\DataTransformer\LoggedInCustomerEmailIfNotSetAwareCommandDataTransformer">
<argument type="service" id="Sylius\Bundle\ApiBundle\Context\UserContextInterface" />
<tag name="sylius.api.command_data_transformer" />
</service>
<service id="Sylius\Bundle\ApiBundle\DataTransformer\LoggedInCustomerEmailAwareCommandDataTransformer">
<argument type="service" id="Sylius\Bundle\ApiBundle\Context\UserContextInterface" />
<tag name="sylius.api.command_data_transformer" />

View file

@ -0,0 +1,91 @@
<?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\DataTransformer;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ApiBundle\Command\LoggedInCustomerEmailIfNotSetAwareInterface;
use Sylius\Bundle\ApiBundle\Command\LocaleCodeAwareInterface;
use Sylius\Bundle\ApiBundle\Context\UserContextInterface;
use Sylius\Component\Core\Model\AdminUserInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
final class LoggedInCustomerEmailIfNotSetAwareCommandDataTransformerSpec extends ObjectBehavior
{
function let(UserContextInterface $userContext)
{
$this->beConstructedWith($userContext);
}
function it_adds_email_to_contact_aware_command_from_logged_in_customer_if_its_not_set(
UserContextInterface $userContext,
LoggedInCustomerEmailIfNotSetAwareInterface $command,
ShopUserInterface $shopUser,
CustomerInterface $customer
): void {
$userContext->getUser()->willReturn($shopUser);
$shopUser->getCustomer()->willReturn($customer);
$command->getEmail()->willReturn(null);
$customer->getEmail()->willReturn('sample@email.com');
$command->setEmail('sample@email.com')->shouldBeCalled();
$this->transform($command,'', [])->shouldReturn($command);
}
function it_does_not_add_email_to_contact_aware_command_if_provided(
UserContextInterface $userContext,
LoggedInCustomerEmailIfNotSetAwareInterface $command,
ShopUserInterface $shopUser,
CustomerInterface $customer
): void {
$userContext->getUser()->willReturn($shopUser);
$shopUser->getCustomer()->willReturn($customer);
$command->getEmail()->willReturn('sample@email.com');
$customer->getEmail()->shouldNotBeCalled();
$command->setEmail('sample@email.com')->shouldNotBeCalled();
$this->transform($command,'', [])->shouldReturn($command);
}
function it_early_returns_contact_aware_command_if_admin_user_provided(
UserContextInterface $userContext,
LoggedInCustomerEmailIfNotSetAwareInterface $command,
AdminUserInterface $adminUser,
): void {
$userContext->getUser()->willReturn($adminUser);
$command->getEmail()->shouldNotBeCalled();
$this->transform($command,'', [])->shouldReturn($command);
}
function it_adds_nothing_for_visitor(
UserContextInterface $userContext,
LoggedInCustomerEmailIfNotSetAwareInterface $command
): void {
$userContext->getUser()->willReturn(null);
$command->setEmail('sample@email.com')->shouldNotBeCalled();
$this->transform($command,'',[])->shouldReturn($command);
}
function it_supports_command_with_contact_aware_interface(LoggedInCustomerEmailIfNotSetAwareInterface $command, LocaleCodeAwareInterface $locale): void
{
$this->supportsTransformation($command)->shouldReturn(true);
$this->supportsTransformation($locale)->shouldReturn(false);
}
}