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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Test\Unit\Model\Order;
use Magento\Quote\Model\Quote\Address;
/**
* Class BuilderTest
*/
class CustomerManagementTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $objectCopyService;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $accountManagement;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $customerFactory;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $addressFactory;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $orderRepository;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $regionFactory;
/**
* @var \Magento\Sales\Model\Order\CustomerManagement
*/
protected $service;
protected function setUp()
{
$this->objectCopyService = $this->getMock('\Magento\Framework\DataObject\Copy', [], [], '', false);
$this->accountManagement = $this->getMock('\Magento\Customer\Api\AccountManagementInterface');
$this->customerFactory = $this->getMock(
'\Magento\Customer\Api\Data\CustomerInterfaceFactory',
['create'],
[],
'',
false
);
$this->addressFactory = $this->getMock(
'\Magento\Customer\Api\Data\AddressInterfaceFactory',
['create'],
[],
'',
false
);
$this->regionFactory = $this->getMock(
'Magento\Customer\Api\Data\RegionInterfaceFactory',
['create'],
[],
'',
false
);
$this->orderRepository = $this->getMock('\Magento\Sales\Api\OrderRepositoryInterface');
$this->service = new \Magento\Sales\Model\Order\CustomerManagement(
$this->objectCopyService,
$this->accountManagement,
$this->customerFactory,
$this->addressFactory,
$this->regionFactory,
$this->orderRepository
);
}
/**
* @expectedException \Magento\Framework\Exception\AlreadyExistsException
*/
public function testCreateThrowsExceptionIfCustomerAlreadyExists()
{
$orderMock = $this->getMock('\Magento\Sales\Api\Data\OrderInterface');
$orderMock->expects($this->once())->method('getCustomerId')->will($this->returnValue('customer_id'));
$this->orderRepository->expects($this->once())->method('get')->with(1)->will($this->returnValue($orderMock));
$this->service->create(1);
}
public function testCreateCreatesCustomerBasedonGuestOrder()
{
$orderMock = $this->getMock('\Magento\Sales\Model\Order', [], [], '', false);
$orderMock->expects($this->once())->method('getCustomerId')->will($this->returnValue(null));
$orderMock->expects($this->any())->method('getBillingAddress')->will($this->returnValue('billing_address'));
$orderBillingAddress = $this->getMock('\Magento\Sales\Api\Data\OrderAddressInterface');
$orderBillingAddress->expects($this->once())
->method('getAddressType')
->willReturn(Address::ADDRESS_TYPE_BILLING);
$orderShippingAddress = $this->getMock('\Magento\Sales\Api\Data\OrderAddressInterface');
$orderShippingAddress->expects($this->once())
->method('getAddressType')
->willReturn(Address::ADDRESS_TYPE_SHIPPING);
$orderMock->expects($this->any())
->method('getAddresses')
->will($this->returnValue([$orderBillingAddress, $orderShippingAddress]));
$this->orderRepository->expects($this->once())->method('get')->with(1)->will($this->returnValue($orderMock));
$this->objectCopyService->expects($this->any())->method('copyFieldsetToTarget')->will($this->returnValueMap(
[
['order_address', 'to_customer', 'billing_address', [], 'global', ['customer_data' => []]],
['order_address', 'to_customer_address', $orderBillingAddress, [], 'global', 'address_data'],
['order_address', 'to_customer_address', $orderShippingAddress, [], 'global', 'address_data'],
]
));
$addressMock = $this->getMock('\Magento\Customer\Api\Data\AddressInterface');
$addressMock->expects($this->any())
->method('setIsDefaultBilling')
->with(true)
->willReturnSelf();
$addressMock->expects($this->any())
->method('setIsDefaultShipping')
->with(true)
->willReturnSelf();
$this->addressFactory->expects($this->any())->method('create')->with(['data' => 'address_data'])->will(
$this->returnValue($addressMock)
);
$customerMock = $this->getMock('\Magento\Customer\Api\Data\CustomerInterface');
$customerMock->expects($this->any())->method('getId')->will($this->returnValue('customer_id'));
$this->customerFactory->expects($this->once())->method('create')->with(
['data' => ['customer_data' => [], 'addresses' => [$addressMock, $addressMock]]]
)->will($this->returnValue($customerMock));
$this->accountManagement->expects($this->once())->method('createAccount')->with($customerMock)->will(
$this->returnValue($customerMock)
);
$orderMock->expects($this->once())->method('setCustomerId')->with('customer_id');
$this->orderRepository->expects($this->once())->method('save')->with($orderMock);
$this->assertEquals($customerMock, $this->service->create(1));
}
}