|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Customer\Test\Fixture; |
| 10 | + |
| 11 | +use Magento\Customer\Api\AddressRepositoryInterface; |
| 12 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 13 | +use Magento\Customer\Api\Data\AddressInterface; |
| 14 | +use Magento\Customer\Api\Data\AddressInterfaceFactory; |
| 15 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 16 | +use Magento\Customer\Model\CustomerFactory; |
| 17 | +use Magento\Framework\DataObject; |
| 18 | +use Magento\Framework\Exception\LocalizedException; |
| 19 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 20 | +use Magento\TestFramework\Fixture\Api\DataMerger; |
| 21 | +use Magento\TestFramework\Fixture\Api\ServiceFactory; |
| 22 | +use Magento\TestFramework\Fixture\Data\ProcessorInterface; |
| 23 | +use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface; |
| 24 | + |
| 25 | +/** |
| 26 | + * Data fixture for customer |
| 27 | + */ |
| 28 | +class Customer implements RevertibleDataFixtureInterface |
| 29 | +{ |
| 30 | + private const DEFAULT_DATA = [ |
| 31 | + 'password' => null, |
| 32 | + CustomerInterface::ID => null, |
| 33 | + CustomerInterface::CONFIRMATION => null, |
| 34 | + CustomerInterface::CREATED_AT => null, |
| 35 | + CustomerInterface::UPDATED_AT => null, |
| 36 | + CustomerInterface::CREATED_IN => null, |
| 37 | + CustomerInterface::DOB => null, |
| 38 | + CustomerInterface::EMAIL => 'customer%uniqid%@mail.com', |
| 39 | + CustomerInterface::FIRSTNAME => 'Firstname %uniqid%', |
| 40 | + CustomerInterface::GENDER => null, |
| 41 | + CustomerInterface::GROUP_ID => null, |
| 42 | + CustomerInterface::LASTNAME => 'Lastname %uniqid%', |
| 43 | + CustomerInterface::MIDDLENAME => null, |
| 44 | + CustomerInterface::PREFIX => null, |
| 45 | + CustomerInterface::STORE_ID => null, |
| 46 | + CustomerInterface::SUFFIX => null, |
| 47 | + CustomerInterface::TAXVAT => null, |
| 48 | + CustomerInterface::WEBSITE_ID => null, |
| 49 | + CustomerInterface::DEFAULT_BILLING => null, |
| 50 | + CustomerInterface::DEFAULT_SHIPPING => null, |
| 51 | + CustomerInterface::KEY_ADDRESSES => [], |
| 52 | + CustomerInterface::DISABLE_AUTO_GROUP_CHANGE => null |
| 53 | + ]; |
| 54 | + |
| 55 | + private const DEFAULT_DATA_ADDRESS = [ |
| 56 | + AddressInterface::ID => null, |
| 57 | + AddressInterface::CUSTOMER_ID => null, |
| 58 | + AddressInterface::REGION => null, |
| 59 | + AddressInterface::REGION_ID => null, |
| 60 | + AddressInterface::COUNTRY_ID => null, |
| 61 | + AddressInterface::STREET => null, |
| 62 | + AddressInterface::COMPANY => null, |
| 63 | + AddressInterface::TELEPHONE => null, |
| 64 | + AddressInterface::FAX => null, |
| 65 | + AddressInterface::POSTCODE => null, |
| 66 | + AddressInterface::CITY => null, |
| 67 | + AddressInterface::FIRSTNAME => 'Firstname %uniqid%', |
| 68 | + AddressInterface::LASTNAME => 'Lastname %uniqid%', |
| 69 | + AddressInterface::MIDDLENAME => null, |
| 70 | + AddressInterface::PREFIX => null, |
| 71 | + AddressInterface::SUFFIX => null, |
| 72 | + AddressInterface::VAT_ID => null, |
| 73 | + AddressInterface::DEFAULT_BILLING => null, |
| 74 | + AddressInterface::DEFAULT_SHIPPING => null |
| 75 | + ]; |
| 76 | + |
| 77 | + /** |
| 78 | + * @var ServiceFactory |
| 79 | + */ |
| 80 | + private $serviceFactory; |
| 81 | + |
| 82 | + /** |
| 83 | + * @var CustomerRepositoryInterface |
| 84 | + */ |
| 85 | + private $customerRepository; |
| 86 | + |
| 87 | + /** |
| 88 | + * @var CustomerFactory |
| 89 | + */ |
| 90 | + private $customerFactory; |
| 91 | + |
| 92 | + /** |
| 93 | + * @var AddressRepositoryInterface |
| 94 | + */ |
| 95 | + private $addressRepository; |
| 96 | + |
| 97 | + /** |
| 98 | + * @var AddressInterfaceFactory |
| 99 | + */ |
| 100 | + private $addressDataFactory; |
| 101 | + |
| 102 | + /** |
| 103 | + * @var ProcessorInterface |
| 104 | + */ |
| 105 | + private $dataProcessor; |
| 106 | + |
| 107 | + /** |
| 108 | + * @var DataMerger |
| 109 | + */ |
| 110 | + private $dataMerger; |
| 111 | + |
| 112 | + /** |
| 113 | + * @var null |
| 114 | + */ |
| 115 | + private $customer; |
| 116 | + |
| 117 | + /** |
| 118 | + * @param ServiceFactory $serviceFactory |
| 119 | + * @param CustomerRepositoryInterface $customerRepository |
| 120 | + * @param CustomerFactory $customerFactory |
| 121 | + * @param AddressRepositoryInterface $addressRepository |
| 122 | + * @param AddressInterfaceFactory $addressDataFactory |
| 123 | + * @param ProcessorInterface $dataProcessor |
| 124 | + * @param DataMerger $dataMerger |
| 125 | + */ |
| 126 | + public function __construct( |
| 127 | + ServiceFactory $serviceFactory, |
| 128 | + CustomerRepositoryInterface $customerRepository, |
| 129 | + CustomerFactory $customerFactory, |
| 130 | + AddressRepositoryInterface $addressRepository, |
| 131 | + AddressInterfaceFactory $addressDataFactory, |
| 132 | + ProcessorInterface $dataProcessor, |
| 133 | + DataMerger $dataMerger, |
| 134 | + ) { |
| 135 | + $this->serviceFactory = $serviceFactory; |
| 136 | + $this->customerRepository = $customerRepository; |
| 137 | + $this->customerFactory = $customerFactory; |
| 138 | + $this->addressRepository = $addressRepository; |
| 139 | + $this->addressDataFactory = $addressDataFactory; |
| 140 | + $this->dataProcessor = $dataProcessor; |
| 141 | + $this->dataMerger = $dataMerger; |
| 142 | + $this->customer = null; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * {@inheritdoc} |
| 147 | + * @param array $data Parameters. Same format as Customer::DEFAULT_DATA. |
| 148 | + * @return DataObject|null |
| 149 | + * @throws LocalizedException |
| 150 | + * @throws NoSuchEntityException |
| 151 | + */ |
| 152 | + public function apply(array $data = []): ?DataObject |
| 153 | + { |
| 154 | + $customerSaveService = $this->serviceFactory->create(CustomerRepositoryInterface::class, 'save'); |
| 155 | + $data = $this->prepareData($data); |
| 156 | + if (count($data[CustomerInterface::KEY_ADDRESSES])) { |
| 157 | + $addresses = $this->prepareCustomerAddress($data[CustomerInterface::KEY_ADDRESSES]); |
| 158 | + $data[CustomerInterface::KEY_ADDRESSES] = $addresses; |
| 159 | + } |
| 160 | + return $customerSaveService->execute( |
| 161 | + ['customer' => $data, 'passwordHash' => $this->customer->getPasswordHash()] |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * @inheritdoc |
| 167 | + */ |
| 168 | + public function revert(DataObject $data): void |
| 169 | + { |
| 170 | + $service = $this->serviceFactory->create(CustomerRepositoryInterface::class, 'deleteById'); |
| 171 | + $service->execute( |
| 172 | + [ |
| 173 | + 'id' => $data->getId() |
| 174 | + ] |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Prepare customer data |
| 180 | + * |
| 181 | + * @param array $data |
| 182 | + * @return array |
| 183 | + */ |
| 184 | + private function prepareData(array $data): array |
| 185 | + { |
| 186 | + $data = $this->dataMerger->merge(self::DEFAULT_DATA, $data, false); |
| 187 | + |
| 188 | + $this->customer = $this->customerFactory->create()->setData($data); |
| 189 | + $this->customer->setPassword($data['password']); |
| 190 | + if (isset($data['password'])) { |
| 191 | + unset($data['password']); |
| 192 | + } |
| 193 | + |
| 194 | + return $this->dataProcessor->process($this, $data); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Prepare customer address |
| 199 | + * |
| 200 | + * @param array $data |
| 201 | + * @return array |
| 202 | + */ |
| 203 | + private function prepareCustomerAddress(array $data): array |
| 204 | + { |
| 205 | + $addresses = []; |
| 206 | + foreach ($data as $dataAddress) { |
| 207 | + $dataAddress = $this->dataMerger->merge(self::DEFAULT_DATA_ADDRESS, $dataAddress, false); |
| 208 | + $addresses[] = $this->dataProcessor->process($this, $dataAddress); |
| 209 | + } |
| 210 | + |
| 211 | + return $addresses; |
| 212 | + } |
| 213 | +} |
0 commit comments