Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle registration for exist customer #546

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/Handler/Customer/RegisterCustomerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace Sylius\ShopApiPlugin\Handler\Customer;

use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Sylius\ShopApiPlugin\Command\Customer\RegisterCustomer;
use Sylius\ShopApiPlugin\Event\CustomerRegistered;
use Sylius\ShopApiPlugin\Provider\CustomerProviderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Webmozart\Assert\Assert;

Expand All @@ -25,33 +25,33 @@ final class RegisterCustomerHandler
/** @var FactoryInterface */
private $userFactory;

/** @var FactoryInterface */
private $customerFactory;

/** @var EventDispatcherInterface */
private $eventDispatcher;

/** @var CustomerProviderInterface */
private $customerProvider;

public function __construct(
UserRepositoryInterface $userRepository,
ChannelRepositoryInterface $channelRepository,
FactoryInterface $userFactory,
FactoryInterface $customerFactory,
EventDispatcherInterface $eventDispatcher
EventDispatcherInterface $eventDispatcher,
CustomerProviderInterface $customerProvider
) {
$this->userRepository = $userRepository;
$this->channelRepository = $channelRepository;
$this->userFactory = $userFactory;
$this->customerFactory = $customerFactory;
$this->eventDispatcher = $eventDispatcher;
$this->customerProvider = $customerProvider;
}

public function __invoke(RegisterCustomer $command): void
{
$this->assertEmailIsNotTaken($command->email());
$this->assertChannelExists($command->channelCode());

/** @var CustomerInterface $customer */
$customer = $this->customerFactory->createNew();
$customer = $this->customerProvider->provide($command->email());

$customer->setFirstName($command->firstName());
$customer->setLastName($command->lastName());
$customer->setEmail($command->email());
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/ShopUserAwareCustomerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function provide(string $emailAddress): CustomerInterface
}

if ($customer->getUser() !== null) {
throw new WrongUserException('Customer already registered. Please log in to finish checkout.');
throw new WrongUserException('Customer already registered.');
}

return $customer;
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services/handler/customer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<argument type="service" id="sylius.repository.shop_user"/>
<argument type="service" id="sylius.repository.channel"/>
<argument type="service" id="sylius.factory.shop_user"/>
<argument type="service" id="sylius.factory.customer"/>
<argument type="service" id="event_dispatcher"/>
<argument type="service" id="sylius.shop_api_plugin.provider.customer_provider"/>
<tag name="messenger.message_handler" />
</service>

Expand Down
44 changes: 44 additions & 0 deletions tests/Controller/Customer/RegisterApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
use PHPUnit\Framework\Assert;
use Sylius\Component\Core\Test\Services\EmailCheckerInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Sylius\ShopApiPlugin\Command\Cart\AssignCustomerToCart;
use Sylius\ShopApiPlugin\Command\Cart\PickupCart;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Tests\Sylius\ShopApiPlugin\Controller\JsonApiTestCase;
use Tests\Sylius\ShopApiPlugin\Controller\Utils\PurgeSpooledMessagesTrait;

Expand Down Expand Up @@ -50,6 +53,47 @@ public function it_allows_to_register_in_shop_and_sends_a_verification_email_if_
Assert::assertTrue($emailChecker->hasRecipient('vinny@fandf.com'));
}

/**
* @test
*/
public function it_allows_to_register_a_customer_if_the_customer_did_a_guest_checkout_already(): void
{
$this->loadFixturesFromFiles(['channel.yml']);

$token = 'SDAOSLEFNWU35H3QLI5325';

/** @var MessageBusInterface $bus */
$bus = $this->get('sylius_shop_api_plugin.command_bus');
$bus->dispatch(new PickupCart($token, 'WEB_GB'));
$bus->dispatch(new AssignCustomerToCart($token, 'vinny@fandf.com'));

$data =
<<<JSON
{
"firstName": "Vin",
"lastName": "Diesel",
"email": "vinny@fandf.com",
"plainPassword": "bananas1234"
}
JSON;

$this->client->request('POST', '/shop-api/register', [], [], self::CONTENT_TYPE_HEADER, $data);

$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT);

/** @var UserRepositoryInterface $userRepository */
$userRepository = $this->get('sylius.repository.shop_user');
$user = $userRepository->findOneByEmail('vinny@fandf.com');

Assert::assertNotNull($user);
Assert::assertFalse($user->isEnabled());

/** @var EmailCheckerInterface $emailChecker */
$emailChecker = $this->get('sylius.behat.email_checker');
Assert::assertTrue($emailChecker->hasRecipient('vinny@fandf.com'));
}

/**
* @test
*/
Expand Down