[API] Subscribing to newsletter on account register

This commit is contained in:
arti0090 2021-03-24 08:13:25 +01:00
parent fa4c1d30dd
commit dc202a69a4
8 changed files with 84 additions and 7 deletions

View file

@ -7,7 +7,7 @@ Feature: Subscribing to the newsletter during registration
Background:
Given the store operates on a single channel in "United States"
@ui
@ui @api
Scenario: Subscribing to the newsletter during registration
When I want to register a new account
And I specify the first name as "Saul"

View file

@ -14,6 +14,10 @@ declare(strict_types=1);
namespace Sylius\Behat\Context\Api\Shop;
use Behat\Behat\Context\Context;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Webmozart\Assert\Assert;
@ -22,15 +26,32 @@ final class RegistrationContext implements Context
/** @var AbstractBrowser */
private $client;
/** @var ApiClientInterface */
private $customerClient;
/** @var LoginContext */
private $loginContext;
/** @var SharedStorageInterface */
private $sharedStorage;
/** @var ResponseCheckerInterface */
private $responseChecker;
private $content = [];
public function __construct(AbstractBrowser $client, LoginContext $loginContext)
{
public function __construct(
AbstractBrowser $client,
ApiClientInterface $customerClient,
LoginContext $loginContext,
SharedStorageInterface $sharedStorage,
ResponseCheckerInterface $responseChecker
) {
$this->client = $client;
$this->customerClient = $customerClient;
$this->loginContext = $loginContext;
$this->sharedStorage = $sharedStorage;
$this->responseChecker = $responseChecker;
}
/**
@ -86,6 +107,33 @@ final class RegistrationContext implements Context
$this->content['phoneNumber'] = $phoneNumber;
}
/**
* @When I subscribe to the newsletter
*/
public function iSubscribeToTheNewsletter(): void
{
$this->content['subscribedToNewsletter'] = true;
}
/**
* @When I verify my account using link sent to :customer
*/
public function iVerifyMyAccount(CustomerInterface $customer): void
{
$this->sharedStorage->set('customer', $customer);
$token = $customer->getUser()->getEmailVerificationToken();
$this->client->request(
'PATCH',
\sprintf('/api/v2/shop/account-verification-requests/%s', $token),
[],
[],
['HTTP_ACCEPT' => 'application/ld+json', 'CONTENT_TYPE' => 'application/merge-patch+json'],
json_encode([], \JSON_THROW_ON_ERROR)
);
}
/**
* @When I confirm this password
*/
@ -201,6 +249,18 @@ final class RegistrationContext implements Context
// Intentionally left blank
}
/**
* @Then I should be subscribed to the newsletter
*/
public function iShouldBeSubscribedToTheNewsletter(): void
{
$customer = $this->sharedStorage->get('customer');
$response = $this->customerClient->show((string) $customer->getId());
Assert::true($this->responseChecker->getResponseContent($response)['subscribedToNewsletter']);
}
private function assertFieldValidationMessage(string $path, string $message): void
{
$decodedResponse = json_decode($this->client->getResponse()->getContent(), true, 512, \JSON_THROW_ON_ERROR);

View file

@ -87,7 +87,10 @@
<service id="sylius.behat.context.api.shop.registration" class="Sylius\Behat\Context\Api\Shop\RegistrationContext">
<argument type="service" id="test.client" />
<argument type="service" id="sylius.behat.api_platform_client.shop.account" />
<argument type="service" id="sylius.behat.context.api.shop.login" />
<argument type="service" id="sylius.behat.shared_storage" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
</service>
<service id="sylius.behat.context.api.shop.order" class="Sylius\Behat\Context\Api\Shop\OrderContext">

View file

@ -42,6 +42,12 @@ class RegisterShopUser implements ChannelCodeAwareInterface, LocaleCodeAwareInte
*/
public $password;
/**
* @var bool
* @psalm-immutable
*/
public $subscribedToNewsletter;
/**
* @var string|null
* @psalm-immutable
@ -59,12 +65,14 @@ class RegisterShopUser implements ChannelCodeAwareInterface, LocaleCodeAwareInte
string $lastName,
string $email,
string $password,
bool $subscribedToNewsletter,
?string $phoneNumber = null
) {
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->email = $email;
$this->password = $password;
$this->subscribedToNewsletter = $subscribedToNewsletter;
$this->phoneNumber = $phoneNumber;
}

View file

@ -78,6 +78,7 @@ final class RegisterShopUserHandler implements MessageHandlerInterface
$customer->setFirstName($command->firstName);
$customer->setLastName($command->lastName);
$customer->setSubscribedToNewsletter($command->subscribedToNewsletter);
$customer->setPhoneNumber($command->phoneNumber);
$customer->setUser($user);

View file

@ -28,5 +28,8 @@
<attribute name="password">
<group>shop:customer:create</group>
</attribute>
<attribute name="subscribedToNewsletter">
<group>shop:customer:create</group>
</attribute>
</class>
</serializer>

View file

@ -49,7 +49,7 @@
<group>shop:customer:update</group>
</attribute>
<attribute name="subscribedToNewsletter" >
<attribute name="subscribedToNewsletter">
<group>shop:customer:read</group>
<group>shop:customer:update</group>
</attribute>

View file

@ -66,7 +66,7 @@ final class RegisterShopUserHandlerSpec extends ObjectBehavior
GeneratorInterface $generator,
MessageBusInterface $commandBus
): void {
$command = new RegisterShopUser('Will', 'Smith', 'WILL.SMITH@example.com', 'iamrobot', '+13104322400');
$command = new RegisterShopUser('Will', 'Smith', 'WILL.SMITH@example.com', 'iamrobot', true,'+13104322400');
$command->setChannelCode('CHANNEL_CODE');
$command->setLocaleCode('en_US');
@ -79,6 +79,7 @@ final class RegisterShopUserHandlerSpec extends ObjectBehavior
$customer->setFirstName('Will')->shouldBeCalled();
$customer->setLastName('Smith')->shouldBeCalled();
$customer->setSubscribedToNewsletter(true)->shouldBeCalled();
$customer->setPhoneNumber('+13104322400')->shouldBeCalled();
$customer->setUser($shopUser)->shouldBeCalled();
@ -116,7 +117,7 @@ final class RegisterShopUserHandlerSpec extends ObjectBehavior
ChannelInterface $channel,
MessageBusInterface $commandBus
): void {
$command = new RegisterShopUser('Will', 'Smith', 'WILL.SMITH@example.com', 'iamrobot', '+13104322400');
$command = new RegisterShopUser('Will', 'Smith', 'WILL.SMITH@example.com', 'iamrobot', true, '+13104322400');
$command->setChannelCode('CHANNEL_CODE');
$command->setLocaleCode('en_US');
@ -129,6 +130,7 @@ final class RegisterShopUserHandlerSpec extends ObjectBehavior
$customer->setFirstName('Will')->shouldBeCalled();
$customer->setLastName('Smith')->shouldBeCalled();
$customer->setSubscribedToNewsletter(true)->shouldBeCalled();
$customer->setPhoneNumber('+13104322400')->shouldBeCalled();
$customer->setUser($shopUser)->shouldBeCalled();
@ -169,7 +171,7 @@ final class RegisterShopUserHandlerSpec extends ObjectBehavior
$this
->shouldThrow(\DomainException::class)
->during('__invoke', [new RegisterShopUser('Will', 'Smith', 'WILL.SMITH@example.com', 'iamrobot', '+13104322400')])
->during('__invoke', [new RegisterShopUser('Will', 'Smith', 'WILL.SMITH@example.com', 'iamrobot', true, '+13104322400')])
;
}
}