Add spec for PersistProcessor

This commit is contained in:
Michał Pysiak 2024-09-30 09:27:45 +02:00
parent 5958c399dd
commit dba51af33d
No known key found for this signature in database
GPG key ID: 9C1F2D0F99830187
2 changed files with 93 additions and 2 deletions

View file

@ -15,6 +15,7 @@ namespace Sylius\Bundle\ApiBundle\StateProcessor\Admin\Customer;
use ApiPlatform\Metadata\DeleteOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\State\ProcessorInterface;
use Sylius\Component\Core\Model\Customer;
@ -35,10 +36,12 @@ final readonly class PersistProcessor implements ProcessorInterface
Assert::isInstanceOf($data, Customer::class);
Assert::notInstanceOf($operation, DeleteOperationInterface::class);
if ($operation instanceof Put) {
if ($operation instanceof Put || $operation instanceof Post) {
$shopUser = $data->getUser();
$this->passwordUpdater->updatePassword($shopUser);
if (null !== $shopUser) {
$this->passwordUpdater->updatePassword($shopUser);
}
}
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);

View file

@ -0,0 +1,88 @@
<?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 spec\Sylius\Bundle\ApiBundle\StateProcessor\Admin\Customer;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\State\ProcessorInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\Customer;
use Sylius\Component\Core\Model\ShopUser;
use Sylius\Component\User\Security\PasswordUpdaterInterface;
final class PersistProcessorSpec extends ObjectBehavior
{
function let(
ProcessorInterface $persistProcessor,
PasswordUpdaterInterface $passwordUpdater,
): void {
$this->beConstructedWith($persistProcessor, $passwordUpdater);
}
function it_does_not_process_delete_operation(
Customer $customer,
ShopUser $shopUser,
ProcessorInterface $persistProcessor,
PasswordUpdaterInterface $passwordUpdater,
): void {
$operation = new Delete();
$passwordUpdater->updatePassword($shopUser)->shouldNotBeCalled();
$persistProcessor->process($customer, $operation, [], [])->shouldNotBeCalled();
$this->shouldThrow(\InvalidArgumentException::class)->during('process', [$customer, $operation, [], []]);
}
function it_processes_post_operation(
Customer $customer,
ShopUser $shopUser,
ProcessorInterface $persistProcessor,
PasswordUpdaterInterface $passwordUpdater,
): void {
$operation = new Post();
$customer->getUser()->willReturn($shopUser);
$passwordUpdater->updatePassword($shopUser)->shouldBeCalled();
$persistProcessor->process($customer, $operation, [], [])->shouldBeCalled();
$this->process($customer, $operation, [], []);
}
function it_processes_put_operation(
Customer $customer,
ShopUser $shopUser,
ProcessorInterface $persistProcessor,
PasswordUpdaterInterface $passwordUpdater,
): void {
$operation = new Put();
$customer->getUser()->willReturn($shopUser);
$passwordUpdater->updatePassword($shopUser)->shouldBeCalled();
$persistProcessor->process($customer, $operation, [], [])->shouldBeCalled();
$this->process($customer, $operation, [], []);
}
function it_does_not_update_password_on_patch_operation(
Customer $customer,
ShopUser $shopUser,
ProcessorInterface $persistProcessor,
PasswordUpdaterInterface $passwordUpdater,
): void {
$operation = new Patch();
$passwordUpdater->updatePassword($shopUser)->shouldNotBeCalled();
$persistProcessor->process($customer, $operation, [], [])->shouldBeCalled();
$this->process($customer, $operation, [], []);
}
}