1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Setup\Model\FixtureGenerator;
use Magento\Customer\Model\Address;
use Magento\Customer\Model\AddressFactory;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\CustomerFactory;
use Magento\Store\Model\StoreManagerInterface;
/**
* Product template generator
*/
class CustomerTemplateGenerator implements TemplateEntityGeneratorInterface
{
/**
* @var CustomerFactory
*/
private $customerFactory;
/**
* @var AddressFactory
*/
private $addressFactory;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @param CustomerFactory $customerFactory
* @param AddressFactory $addressFactory
* @param StoreManagerInterface $storeManager
*/
public function __construct(
CustomerFactory $customerFactory,
AddressFactory $addressFactory,
StoreManagerInterface $storeManager
) {
$this->customerFactory = $customerFactory;
$this->addressFactory = $addressFactory;
$this->storeManager = $storeManager;
}
/**
* {@inheritdoc}
*/
public function generateEntity()
{
$customer = $this->getCustomerTemplate();
$customer->save();
$address = $this->getAddressTemplate($customer->getId());
$address->save();
return $customer;
}
/**
* Get customer template
*
* @return Customer
*/
private function getCustomerTemplate()
{
$customerRandomizerNumber = crc32(mt_rand(1, PHP_INT_MAX));
$now = new \DateTime();
return $this->customerFactory->create([
'data' => [
'email' => sprintf('user_%s@example.com', $customerRandomizerNumber),
'confirmation' => null,
'created_at' => $now->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
'created_in' => 'Default',
'default_billing' => '1',
'default_shipping' => '1',
'disable_auto_group_change' => '0',
'dob' => '12-10-1991',
'firstname' => 'Firstname',
'gender' => 1,
'group_id' => '1',
'lastname' => 'Lastname',
'middlename' => '',
'password_hash' => '',
'prefix' => null,
'rp_token' => null,
'rp_token_created_at' => null,
'store_id' => $this->storeManager->getDefaultStoreView()->getId(),
'suffix' => null,
'taxvat' => null,
'website_id' => $this->storeManager->getDefaultStoreView()->getWebsiteId(),
'password' => '123123q',
]
]);
}
/**
* @param int $customerId
* @return Address
*/
private function getAddressTemplate($customerId)
{
return $this->addressFactory->create([
'data' => [
'parent_id' => $customerId,
'attribute_set_id' => 2,
'telephone' => 3468676,
'postcode' => 75477,
'country_id' => 'US',
'city' => 'CityM',
'company' => 'CompanyName',
'street' => 'Green str, 67',
'lastname' => 'Smith',
'firstname' => 'John',
'region_id' => 1,
'fax' => '04040404',
'middlename' => '',
'prefix' => '',
'region' => 'Arkansas',
'suffix' => '',
'vat_id' => '',
'default_billing_' => '1',
'default_shipping_' => '1',
]
]);
}
}