|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Customer\Model\AccountManagement; |
| 9 | + |
| 10 | +use Magento\Customer\Api\AccountManagementInterface; |
| 11 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 12 | +use Magento\Customer\Api\Data\AddressInterfaceFactory; |
| 13 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 14 | +use Magento\Customer\Api\Data\CustomerInterfaceFactory; |
| 15 | +use Magento\Framework\ObjectManagerInterface; |
| 16 | +use Magento\Framework\Registry; |
| 17 | +use Magento\TestFramework\Helper\Bootstrap; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Test for creation customer with address via customer account management service. |
| 22 | + * |
| 23 | + * @magentoDbIsolation enabled |
| 24 | + */ |
| 25 | +class CreateAccountWithAddressTest extends TestCase |
| 26 | +{ |
| 27 | + /** @var ObjectManagerInterface */ |
| 28 | + private $objectManager; |
| 29 | + |
| 30 | + /** @var AccountManagementInterface */ |
| 31 | + private $accountManagement; |
| 32 | + |
| 33 | + /** @var CustomerInterfaceFactory */ |
| 34 | + private $customerFactory; |
| 35 | + |
| 36 | + /** @var CustomerRepositoryInterface */ |
| 37 | + private $customerRepository; |
| 38 | + |
| 39 | + /** @var AddressInterfaceFactory */ |
| 40 | + private $addressFactory; |
| 41 | + |
| 42 | + /** @var CustomerInterface */ |
| 43 | + private $customer; |
| 44 | + |
| 45 | + /** @var Registry */ |
| 46 | + private $registry; |
| 47 | + |
| 48 | + /** |
| 49 | + * @inheritdoc |
| 50 | + */ |
| 51 | + protected function setUp(): void |
| 52 | + { |
| 53 | + parent::setUp(); |
| 54 | + |
| 55 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 56 | + $this->accountManagement = $this->objectManager->get(AccountManagementInterface::class); |
| 57 | + $this->customerFactory = $this->objectManager->get(CustomerInterfaceFactory::class); |
| 58 | + $this->customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class); |
| 59 | + $this->addressFactory = $this->objectManager->get(AddressInterfaceFactory::class); |
| 60 | + $this->registry = $this->objectManager->get(Registry::class); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @inheritdoc |
| 65 | + */ |
| 66 | + protected function tearDown(): void |
| 67 | + { |
| 68 | + if ($this->customer instanceof CustomerInterface) { |
| 69 | + $this->registry->unregister('isSecureArea'); |
| 70 | + $this->registry->register('isSecureArea', true); |
| 71 | + $this->customerRepository->delete($this->customer); |
| 72 | + $this->registry->unregister('isSecureArea'); |
| 73 | + $this->registry->register('isSecureArea', false); |
| 74 | + } |
| 75 | + |
| 76 | + parent::tearDown(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @magentoDataFixture Magento/Store/_files/second_website_with_store_group_and_store.php |
| 81 | + * @magentoConfigFixture default_store general/country/allow BD,BB,AF |
| 82 | + * @magentoConfigFixture fixture_second_store_store general/country/allow AS,BM |
| 83 | + * @return void |
| 84 | + */ |
| 85 | + public function testCreateNewCustomerWithAddress(): void |
| 86 | + { |
| 87 | + $availableCountry = 'BD'; |
| 88 | + $address = $this->addressFactory->create(); |
| 89 | + $address->setCountryId($availableCountry) |
| 90 | + ->setPostcode('75477') |
| 91 | + ->setRegionId(1) |
| 92 | + ->setStreet(['Green str, 67']) |
| 93 | + ->setTelephone('3468676') |
| 94 | + ->setCity('CityM') |
| 95 | + ->setFirstname('John') |
| 96 | + ->setLastname('Smith') |
| 97 | + ->setIsDefaultShipping(true) |
| 98 | + ->setIsDefaultBilling(true); |
| 99 | + $customerEntity = $this->customerFactory->create(); |
| 100 | + $customerEntity->setEmail('test@example.com') |
| 101 | + ->setFirstname('John') |
| 102 | + ->setLastname('Smith') |
| 103 | + ->setStoreId(1); |
| 104 | + $customerEntity->setAddresses([$address]); |
| 105 | + $this->customer = $this->accountManagement->createAccount($customerEntity); |
| 106 | + $this->assertCount(1, $this->customer->getAddresses(), 'The available address wasn\'t saved.'); |
| 107 | + $this->assertSame( |
| 108 | + $availableCountry, |
| 109 | + $this->customer->getAddresses()[0]->getCountryId(), |
| 110 | + 'The address was saved with disallowed country.' |
| 111 | + ); |
| 112 | + } |
| 113 | +} |
0 commit comments