|
| 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 | +use Magento\Customer\Model\Address; |
| 9 | +use Magento\Customer\Model\Customer; |
| 10 | +use Magento\Eav\Model\Config; |
| 11 | +use Magento\Eav\Model\Entity\Type; |
| 12 | +use Magento\Framework\Registry; |
| 13 | +use Magento\TestFramework\Helper\Bootstrap; |
| 14 | +use Magento\Customer\Model\Attribute; |
| 15 | +use Magento\Eav\Model\Entity\Attribute\Set; |
| 16 | + |
| 17 | +$objectManager = Bootstrap::getObjectManager(); |
| 18 | +/** @var $entityType Type */ |
| 19 | +$entityType = $objectManager |
| 20 | + ->create(Config::class) |
| 21 | + ->getEntityType('customer'); |
| 22 | + |
| 23 | +/** @var $attributeSet Set */ |
| 24 | +$attributeSet = Bootstrap::getObjectManager() |
| 25 | + ->create(Set::class); |
| 26 | + |
| 27 | +$select = Bootstrap::getObjectManager()->create( |
| 28 | + Attribute::class, |
| 29 | + [ |
| 30 | + 'data' => [ |
| 31 | + 'frontend_input' => 'text', |
| 32 | + 'frontend_label' => ['test_text_attribute'], |
| 33 | + 'sort_order' => 1, |
| 34 | + 'backend_type' => 'varchar', |
| 35 | + 'is_user_defined' => 1, |
| 36 | + 'is_system' => 0, |
| 37 | + 'is_used_in_grid' => 1, |
| 38 | + 'is_required' => '0', |
| 39 | + 'is_visible' => 1, |
| 40 | + 'used_in_forms' => [ |
| 41 | + 'customer_address_edit', |
| 42 | + 'adminhtml_customer_address' |
| 43 | + ], |
| 44 | + 'attribute_set_id' => $entityType->getDefaultAttributeSetId(), |
| 45 | + 'attribute_group_id' => $attributeSet->getDefaultGroupId($entityType->getDefaultAttributeSetId()), |
| 46 | + 'entity_type_id' => $entityType->getId(), |
| 47 | + 'default_value' => '', |
| 48 | + ], |
| 49 | + ] |
| 50 | +); |
| 51 | +$select->setAttributeCode('test_text_attribute'); |
| 52 | +$select->save(); |
| 53 | + |
| 54 | +$customer = $objectManager |
| 55 | + ->create(Customer::class); |
| 56 | +$customer->setWebsiteId(1) |
| 57 | + ->setEntityId(1) |
| 58 | + ->setEntityTypeId($entityType->getId()) |
| 59 | + ->setAttributeSetId($entityType->getDefaultAttributeSetId()) |
| 60 | + ->setEmail('JohnDoe@mail.com') |
| 61 | + ->setPassword('password') |
| 62 | + ->setGroupId(1) |
| 63 | + ->setStoreId(1) |
| 64 | + ->setIsActive(1) |
| 65 | + ->setFirstname('John') |
| 66 | + ->setLastname('Doe') |
| 67 | + ->setGender(2) |
| 68 | + ->setTestTextAttribute('123'); |
| 69 | +$customer->isObjectNew(true); |
| 70 | +// Create address |
| 71 | +$address = $objectManager->create(Address::class); |
| 72 | +// default_billing and default_shipping information would not be saved, it is needed only for simple check |
| 73 | +$address->addData( |
| 74 | + [ |
| 75 | + 'firstname' => 'Charles', |
| 76 | + 'lastname' => 'Alston', |
| 77 | + 'street' => '3781 Neuport Lane', |
| 78 | + 'city' => 'Panola', |
| 79 | + 'country_id' => 'US', |
| 80 | + 'region_id' => '51', |
| 81 | + 'postcode' => '30058', |
| 82 | + 'telephone' => '770-322-3514', |
| 83 | + 'default_billing' => 1, |
| 84 | + 'default_shipping' => 1, |
| 85 | + ] |
| 86 | +); |
| 87 | +// Assign customer and address |
| 88 | +$customer->addAddress($address); |
| 89 | +$customer->save(); |
| 90 | +// Mark last address as default billing and default shipping for current customer |
| 91 | +$customer->setDefaultBilling($address->getId()); |
| 92 | +$customer->setDefaultShipping($address->getId()); |
| 93 | +$customer->save(); |
| 94 | + |
| 95 | +$objectManager->get(Registry::class)->unregister('_fixture/Magento_ImportExport_Customer'); |
| 96 | +$objectManager->get(Registry::class)->register('_fixture/Magento_ImportExport_Customer', $customer); |
0 commit comments