[API][Behat] Add Customer Persist processor

This commit is contained in:
Michał Pysiak 2024-09-30 08:40:01 +02:00
parent 69f77e01bd
commit 13819ebaec
No known key found for this signature in database
GPG key ID: 9C1F2D0F99830187
5 changed files with 75 additions and 12 deletions

View file

@ -9,7 +9,7 @@ Feature: Changing shop user's password
And there is a user "kibsoon@example.com" identified by "goodGuy"
And I am logged in as an administrator
@todo-api @ui
@api @ui
Scenario: Changing a password of a shop user
When I change the password of user "kibsoon@example.com" to "veryGoodGuy"
Then I should be notified that it has been successfully edited

View file

@ -23,7 +23,7 @@ use Sylius\Component\User\Model\UserInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Symfony\Component\Messenger\MessageBusInterface;
final class UserContext implements Context
final readonly class UserContext implements Context
{
public function __construct(
private SharedStorageInterface $sharedStorage,
@ -40,8 +40,9 @@ final class UserContext implements Context
* @Given there was account of :email with password :password
* @Given there is a user :email
*/
public function thereIsUserIdentifiedBy($email, $password = 'sylius')
public function thereIsUserIdentifiedBy(string $email, string $password = 'sylius'): void
{
/** @var ShopUserInterface $user */
$user = $this->userFactory->create(['email' => $email, 'password' => $password, 'enabled' => true]);
$this->sharedStorage->set('user', $user);
@ -68,7 +69,7 @@ final class UserContext implements Context
* @Given the account of :email was deleted
* @Given my account :email was deleted
*/
public function accountWasDeleted($email)
public function accountWasDeleted(string $email)
{
/** @var ShopUserInterface $user */
$user = $this->userRepository->findOneByEmail($email);
@ -81,7 +82,7 @@ final class UserContext implements Context
/**
* @Given its account was deleted
*/
public function hisAccountWasDeleted()
public function hisAccountWasDeleted(): void
{
$user = $this->sharedStorage->get('user');
@ -93,7 +94,7 @@ final class UserContext implements Context
* @Given /^(this user) is not verified$/
* @Given /^(I) have not verified my account (?:yet)$/
*/
public function accountIsNotVerified(UserInterface $user)
public function accountIsNotVerified(UserInterface $user): void
{
$user->setVerifiedAt(null);
@ -103,7 +104,7 @@ final class UserContext implements Context
/**
* @Given /^(?:(I) have|(this user) has) already received a verification email$/
*/
public function iHaveReceivedVerificationEmail(UserInterface $user)
public function iHaveReceivedVerificationEmail(UserInterface $user): void
{
$this->prepareUserVerification($user);
}
@ -111,7 +112,7 @@ final class UserContext implements Context
/**
* @Given a verification email has already been sent to :email
*/
public function aVerificationEmailHasBeenSentTo($email)
public function aVerificationEmailHasBeenSentTo(string $email): void
{
$user = $this->userRepository->findOneByEmail($email);
@ -121,7 +122,7 @@ final class UserContext implements Context
/**
* @Given /^(I) have already verified my account$/
*/
public function iHaveAlreadyVerifiedMyAccount(UserInterface $user)
public function iHaveAlreadyVerifiedMyAccount(UserInterface $user): void
{
$user->setVerifiedAt(new \DateTime());
@ -136,7 +137,7 @@ final class UserContext implements Context
$this->prepareUserPasswordResetToken($user);
}
private function prepareUserVerification(UserInterface $user)
private function prepareUserVerification(UserInterface $user): void
{
$token = 'marryhadalittlelamb';
$this->sharedStorage->set('verification_token', $token);

View file

@ -72,7 +72,12 @@
</normalizationContext>
</operation>
<operation name="sylius_api_admin_customer_post" class="ApiPlatform\Metadata\Post" uriTemplate="/admin/customers" >
<operation
name="sylius_api_admin_customer_post"
class="ApiPlatform\Metadata\Post"
uriTemplate="/admin/customers"
processor="sylius_api.state_processor.admin.customer.persist"
>
<denormalizationContext>
<values>
<value name="groups">
@ -104,7 +109,12 @@
</validationContext>
</operation>
<operation name="sylius_api_admin_customer_put" class="ApiPlatform\Metadata\Put" uriTemplate="/admin/customers/{id}" >
<operation
name="sylius_api_admin_customer_put"
class="ApiPlatform\Metadata\Put"
uriTemplate="/admin/customers/{id}"
processor="sylius_api.state_processor.admin.customer.persist"
>
<denormalizationContext>
<values>
<value name="groups">

View file

@ -134,5 +134,11 @@
<argument type="service" id="validator"/>
<tag name="api_platform.state_processor"/>
</service>
<service id="sylius_api.state_processor.admin.customer.persist" class="Sylius\Bundle\ApiBundle\StateProcessor\Admin\Customer\PersistProcessor">
<argument type="service" id="api_platform.doctrine.orm.state.persist_processor" />
<argument type="service" id="sylius.security.password_updater" />
<tag name="api_platform.state_processor" />
</service>
</services>
</container>

View file

@ -0,0 +1,46 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* 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\StateProcessor\Admin\Customer;
use ApiPlatform\Metadata\DeleteOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Put;
use ApiPlatform\State\ProcessorInterface;
use Sylius\Component\Core\Model\Customer;
use Sylius\Component\User\Security\PasswordUpdaterInterface;
use Webmozart\Assert\Assert;
/** @implements ProcessorInterface<Customer> */
final readonly class PersistProcessor implements ProcessorInterface
{
public function __construct(
private ProcessorInterface $persistProcessor,
private PasswordUpdaterInterface $passwordUpdater,
) {
}
public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
{
Assert::isInstanceOf($data, Customer::class);
Assert::notInstanceOf($operation, DeleteOperationInterface::class);
if ($operation instanceof Put) {
$shopUser = $data->getUser();
$this->passwordUpdater->updatePassword($shopUser);
}
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
}
}