diff --git a/src/Handler/Customer/RegisterCustomerHandler.php b/src/Handler/Customer/RegisterCustomerHandler.php index c2a9d1463..22323e498 100644 --- a/src/Handler/Customer/RegisterCustomerHandler.php +++ b/src/Handler/Customer/RegisterCustomerHandler.php @@ -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; @@ -25,24 +25,24 @@ 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 @@ -50,8 +50,8 @@ 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()); diff --git a/src/Provider/ShopUserAwareCustomerProvider.php b/src/Provider/ShopUserAwareCustomerProvider.php index 8c8dd8ad8..78446374b 100644 --- a/src/Provider/ShopUserAwareCustomerProvider.php +++ b/src/Provider/ShopUserAwareCustomerProvider.php @@ -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; diff --git a/src/Resources/config/services/handler/customer.xml b/src/Resources/config/services/handler/customer.xml index 7826329e1..500d0c0a8 100644 --- a/src/Resources/config/services/handler/customer.xml +++ b/src/Resources/config/services/handler/customer.xml @@ -7,8 +7,8 @@ - + diff --git a/tests/Controller/Customer/RegisterApiTest.php b/tests/Controller/Customer/RegisterApiTest.php index b5608dff0..6688a5004 100644 --- a/tests/Controller/Customer/RegisterApiTest.php +++ b/tests/Controller/Customer/RegisterApiTest.php @@ -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; @@ -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 = +<<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 */