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
<?php
/**
* @copyright Vertex. All rights reserved. https://www.vertexinc.com/
* @author Mediotype https://www.mediotype.com/
*/
namespace Vertex\Tax\Test\Integration\Builder;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
/**
* Build a customer entity
*/
class CustomerBuilder
{
const EXAMPLE_CUSTOMER_EMAIL = 'jdoe@host.local';
const EXAMPLE_CUSTOMER_FIRSTNAME = 'John';
const EXAMPLE_CUSTOMER_LASTNAME = 'Doe';
/** @var CustomerInterfaceFactory */
private $customerFactory;
/** @var CustomerRepositoryInterface */
private $customerRepository;
/**
* @param CustomerInterfaceFactory $customerFactory
* @param CustomerRepositoryInterface $customerRepository
*/
public function __construct(
CustomerInterfaceFactory $customerFactory,
CustomerRepositoryInterface $customerRepository
) {
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
}
/**
* Create a customer
*
* @param callable $customerConfiguration Receives 1 parameter of CustomerInterface.
* Should return a CustomerInterface.
* @return CustomerInterface
* @throws \TypeError
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\State\InputMismatchException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function createCustomer(callable $customerConfiguration)
{
/** @var CustomerInterface $customer */
$customer = $customerConfiguration($this->customerFactory->create());
if (!($customer instanceof CustomerInterface)) {
throw new \TypeError('Result of createCustomer callback must return a CustomerInterface');
}
return $this->customerRepository->save($customer);
}
/**
* Creates a generic customer
*
* Identity: John Doe <jdoe@host.local>
*
* @param callable $customerConfiguration Receives 1 parameter of CustomerInterface.
* Should return a CustomerInterface.
* @return CustomerInterface
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\State\InputMismatchException
*/
public function createExampleCustomer(callable $customerConfiguration = null)
{
return $this->createCustomer(
function (CustomerInterface $customer) use ($customerConfiguration) {
$customer->setFirstname(static::EXAMPLE_CUSTOMER_FIRSTNAME);
$customer->setLastname(static::EXAMPLE_CUSTOMER_LASTNAME);
$customer->setEmail(static::EXAMPLE_CUSTOMER_EMAIL);
return $customerConfiguration !== null ? $customerConfiguration($customer) : $customer;
}
);
}
}