Skip to content

Commit ce806d2

Browse files
author
Robert He
committed
MAGETWO-33109 : Refactor code that uses customer builders in Quote module
-- changed Quote module to use customerFactory -- fixed unit tests
1 parent 18a5ffa commit ce806d2

File tree

7 files changed

+94
-96
lines changed

7 files changed

+94
-96
lines changed

app/code/Magento/Quote/Model/CustomerManagement.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
1010
use Magento\Customer\Api\AccountManagementInterface as AccountManagement;
11-
use Magento\Customer\Api\Data\CustomerDataBuilder as CustomerBuilder;
1211
use Magento\Customer\Api\AddressRepositoryInterface as CustomerAddressRepository;
1312
use Magento\Quote\Model\Quote as QuoteEntity;
1413

@@ -32,27 +31,19 @@ class CustomerManagement
3231
*/
3332
protected $accountManagement;
3433

35-
/**
36-
* @var CustomerBuilder
37-
*/
38-
protected $customerBuilder;
39-
4034
/**
4135
* @param CustomerRepository $customerRepository
4236
* @param CustomerAddressRepository $customerAddressRepository
4337
* @param AccountManagement $accountManagement
44-
* @param CustomerBuilder $customerBuilder
4538
*/
4639
public function __construct(
4740
CustomerRepository $customerRepository,
4841
CustomerAddressRepository $customerAddressRepository,
49-
AccountManagement $accountManagement,
50-
CustomerBuilder $customerBuilder
42+
AccountManagement $accountManagement
5143
) {
5244
$this->customerRepository = $customerRepository;
5345
$this->customerAddressRepository = $customerAddressRepository;
5446
$this->accountManagement = $accountManagement;
55-
$this->customerBuilder = $customerBuilder;
5647
}
5748

5849
/**
@@ -67,7 +58,7 @@ public function populateCustomerInfo(QuoteEntity $quote)
6758

6859
if (!$customer->getId()) {
6960
$customer = $this->accountManagement->createAccountWithPasswordHash(
70-
$this->customerBuilder->populate($customer)->create(),
61+
$customer,
7162
$quote->getPasswordHash()
7263
);
7364
$quote->setCustomer($customer);

app/code/Magento/Quote/Model/Observer/Frontend/Quote/Address/CollectTotals.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class CollectTotals
2323
protected $vatValidator;
2424

2525
/**
26-
* @var \Magento\Customer\Api\Data\CustomerDataBuilder
26+
* @var \Magento\Customer\Api\Data\CustomerInterfaceFactory
2727
*/
28-
protected $customerBuilder;
28+
protected $customerDataFactory;
2929

3030
/**
3131
* Group Management
@@ -34,27 +34,35 @@ class CollectTotals
3434
*/
3535
protected $groupManagement;
3636

37+
/**
38+
* @var \Magento\Framework\Api\DataObjectHelper
39+
*/
40+
protected $dataObjectHelper;
41+
3742
/**
3843
* Initialize dependencies.
3944
*
4045
* @param \Magento\Customer\Helper\Address $customerAddressHelper
4146
* @param \Magento\Customer\Model\Vat $customerVat
4247
* @param VatValidator $vatValidator
43-
* @param \Magento\Customer\Api\Data\CustomerDataBuilder $customerBuilder
48+
* @param \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory
4449
* @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
50+
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
4551
*/
4652
public function __construct(
4753
\Magento\Customer\Helper\Address $customerAddressHelper,
4854
\Magento\Customer\Model\Vat $customerVat,
4955
VatValidator $vatValidator,
50-
\Magento\Customer\Api\Data\CustomerDataBuilder $customerBuilder,
51-
\Magento\Customer\Api\GroupManagementInterface $groupManagement
56+
\Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory,
57+
\Magento\Customer\Api\GroupManagementInterface $groupManagement,
58+
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper
5259
) {
5360
$this->customerVat = $customerVat;
5461
$this->customerAddressHelper = $customerAddressHelper;
5562
$this->vatValidator = $vatValidator;
56-
$this->customerBuilder = $customerBuilder;
63+
$this->customerDataFactory = $customerDataFactory;
5764
$this->groupManagement = $groupManagement;
65+
$this->dataObjectHelper = $dataObjectHelper;
5866
}
5967

6068
/**
@@ -99,7 +107,8 @@ public function dispatch(\Magento\Framework\Event\Observer $observer)
99107
if ($groupId) {
100108
$quoteAddress->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId());
101109
$quote->setCustomerGroupId($groupId);
102-
$customer = $this->customerBuilder->mergeDataObjectWithArray($customer, ['group_id' => $groupId])->create();
110+
$customer = $this->customerDataFactory->create();
111+
$this->dataObjectHelper->populateWithArray($customer, ['group_id' => $groupId]);
103112
$quote->setCustomer($customer);
104113
}
105114
}

app/code/Magento/Quote/Model/Quote.php

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,14 @@ class Quote extends AbstractExtensibleModel implements \Magento\Quote\Api\Data\C
311311
protected $extensibleDataObjectConverter;
312312

313313
/**
314-
* @var \Magento\Customer\Api\Data\AttributeValueFactory
314+
* @var \Magento\Customer\Api\Data\AddressInterfaceFactory
315315
*/
316-
protected $addressFactory;
316+
protected $addressDataFactory;
317317

318318
/**
319-
* @var \Magento\Customer\Api\Data\CustomerDataBuilder
319+
* @var \Magento\Customer\Api\Data\CustomerInterfaceFactory
320320
*/
321-
protected $customerBuilder;
321+
protected $customerDataFactory;
322322

323323
/**
324324
* @var \Magento\Customer\Api\CustomerRepositoryInterface
@@ -330,6 +330,11 @@ class Quote extends AbstractExtensibleModel implements \Magento\Quote\Api\Data\C
330330
*/
331331
protected $currencyFactory;
332332

333+
/**
334+
* @var \Magento\Framework\Api\DataObjectHelper
335+
*/
336+
protected $dataObjectHelper;
337+
333338
/**
334339
* @param \Magento\Framework\Model\Context $context
335340
* @param \Magento\Framework\Registry $registry
@@ -357,9 +362,10 @@ class Quote extends AbstractExtensibleModel implements \Magento\Quote\Api\Data\C
357362
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
358363
* @param \Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder
359364
* @param \Magento\Framework\Api\FilterBuilder $filterBuilder
360-
* @param \Magento\Customer\Api\Data\AddressDataBuilder $addressBuilder
361-
* @param \Magento\Customer\Api\Data\CustomerDataBuilder $customerBuilder
365+
* @param \Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory
366+
* @param \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory
362367
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
368+
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
363369
* @param \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter
364370
* @param Cart\CurrencyFactory $currencyFactory
365371
* @param \Magento\Framework\Model\Resource\AbstractResource $resource
@@ -394,9 +400,10 @@ public function __construct(
394400
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
395401
\Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder,
396402
\Magento\Framework\Api\FilterBuilder $filterBuilder,
397-
\Magento\Customer\Api\Data\AddressDataBuilder $addressBuilder,
398-
\Magento\Customer\Api\Data\CustomerDataBuilder $customerBuilder,
403+
\Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory,
404+
\Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory,
399405
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
406+
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
400407
\Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter,
401408
\Magento\Quote\Model\Cart\CurrencyFactory $currencyFactory,
402409
\Magento\Framework\Model\Resource\AbstractResource $resource = null,
@@ -425,9 +432,10 @@ public function __construct(
425432
$this->stockRegistry = $stockRegistry;
426433
$this->itemProcessor = $itemProcessor;
427434
$this->objectFactory = $objectFactory;
428-
$this->addressFactory = $addressBuilder;
429-
$this->customerBuilder = $customerBuilder;
435+
$this->addressDataFactory = $addressDataFactory;
436+
$this->customerDataFactory = $customerDataFactory;
430437
$this->customerRepository = $customerRepository;
438+
$this->dataObjectHelper = $dataObjectHelper;
431439
$this->extensibleDataObjectConverter = $extensibleDataObjectConverter;
432440
$this->currencyFactory = $currencyFactory;
433441
parent::__construct(
@@ -823,14 +831,17 @@ public function setCustomer(\Magento\Customer\Api\Data\CustomerInterface $custom
823831
/* @TODO: Remove the method after all external usages are refactored in MAGETWO-19930 */
824832
$this->_customer = $customer;
825833
$this->setCustomerId($customer->getId());
826-
$customerData = $this->objectFactory->create(
834+
$customerData = $this->customerDataFactory->create();
835+
$customerData->populate($customer);
836+
$customerData->setAddresses([]);
837+
$customerDataFlatArray = $this->objectFactory->create(
827838
$this->extensibleDataObjectConverter->toFlatArray(
828-
$this->customerBuilder->populate($customer)->setAddresses([])->create(),
839+
$customerData,
829840
[],
830841
'\Magento\Customer\Api\Data\CustomerInterface'
831842
)
832843
);
833-
$this->_objectCopyService->copyFieldsetToTarget('customer_account', 'to_quote', $customerData, $this);
844+
$this->_objectCopyService->copyFieldsetToTarget('customer_account', 'to_quote', $customerDataFlatArray, $this);
834845

835846
return $this;
836847
}
@@ -850,7 +861,8 @@ public function getCustomer()
850861
try {
851862
$this->_customer = $this->customerRepository->getById($this->getCustomerId());
852863
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
853-
$this->_customer = $this->customerBuilder->setId(null)->create();
864+
$this->_customer = $this->customerDataFactory->create();
865+
$this->_customer->setId(null);
854866
}
855867
}
856868

@@ -886,10 +898,7 @@ public function addCustomerAddress(\Magento\Customer\Api\Data\AddressInterface $
886898
{
887899
$addresses = (array)$this->getCustomer()->getAddresses();
888900
$addresses[] = $address;
889-
$customer = $this->customerBuilder->populate($this->getCustomer())
890-
->setAddresses($addresses)
891-
->create();
892-
$this->setCustomer($customer);
901+
$this->getCustomer()->setAddresses($addresses);
893902
return $this;
894903
}
895904

@@ -901,8 +910,8 @@ public function addCustomerAddress(\Magento\Customer\Api\Data\AddressInterface $
901910
*/
902911
public function updateCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
903912
{
904-
$customer = $this->customerBuilder->mergeDataObjects($this->getCustomer(), $customer)
905-
->create();
913+
$customer = $this->customerDataFactory->mergeDataObjects(get_class($this->getCustomer()),
914+
$this->getCustomer(), $customer);
906915
$this->setCustomer($customer);
907916
return $this;
908917
}

dev/tests/unit/testsuite/Magento/Quote/Model/CustomerManagementTest.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,11 @@ class CustomerManagementTest extends \PHPUnit_Framework_TestCase
2121
/**
2222
* @var \Magento\Customer\Api\AccountManagementInterface|\PHPUnit_Framework_MockObject_MockObject
2323
*/
24-
protected $customerAddressRepositoryMock;
25-
/**
26-
* @var \Magento\Customer\Api\Data\CustomerDataBuilder|\PHPUnit_Framework_MockObject_MockObject
27-
*
28-
*/
2924
protected $accountManagementMock;
3025
/**
3126
* @var \Magento\Customer\Api\AddressRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
3227
*/
33-
protected $customerBuilderMock;
28+
protected $customerAddressRepositoryMock;
3429
/**
3530
* @var \Magento\Quote\Model\Quote|\PHPUnit_Framework_MockObject_MockObject
3631
*/
@@ -39,7 +34,6 @@ class CustomerManagementTest extends \PHPUnit_Framework_TestCase
3934
* @var \Magento\Quote\Model\Quote\Address|\PHPUnit_Framework_MockObject_MockObject
4035
*/
4136
protected $quoteAddressMock;
42-
4337
/**
4438
* @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject
4539
*/
@@ -79,13 +73,6 @@ public function setUp()
7973
true,
8074
[]
8175
);
82-
$this->customerBuilderMock = $this->getMock(
83-
'Magento\Customer\Api\Data\CustomerDataBuilder',
84-
['populate', 'create'],
85-
[],
86-
'',
87-
false
88-
);
8976
$this->quoteMock = $this->getMock(
9077
'Magento\Quote\Model\Quote',
9178
['getId', 'getCustomer', 'getBillingAddress', 'getShippingAddress', 'setCustomer', 'getPasswordHash'],
@@ -121,8 +108,7 @@ public function setUp()
121108
$this->customerManagement = new \Magento\Quote\Model\CustomerManagement(
122109
$this->customerRepositoryMock,
123110
$this->customerAddressRepositoryMock,
124-
$this->accountManagementMock,
125-
$this->customerBuilderMock
111+
$this->accountManagementMock
126112
);
127113
}
128114

@@ -134,16 +120,9 @@ public function testPopulateCustomerInfo()
134120
$this->customerMock->expects($this->atLeastOnce())
135121
->method('getId')
136122
->willReturn(null);
137-
$this->customerBuilderMock->expects($this->once())
138-
->method('populate')
139-
->with($this->customerMock)
140-
->willReturnSelf();
141123
$this->customerMock->expects($this->atLeastOnce())
142124
->method('getDefaultBilling')
143125
->willReturn(100500);
144-
$this->customerBuilderMock->expects($this->once())
145-
->method('create')
146-
->willReturn($this->customerMock);
147126
$this->quoteMock->expects($this->atLeastOnce())
148127
->method('getBillingAddress')
149128
->willReturn($this->quoteAddressMock);

dev/tests/unit/testsuite/Magento/Quote/Model/Observer/Frontend/Quote/Address/CollectTotalsTest.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class CollectTotalsTest extends \PHPUnit_Framework_TestCase
6161
/**
6262
* @var \PHPUnit_Framework_MockObject_MockObject
6363
*/
64-
protected $customerBuilderMock;
64+
protected $customerDataFactoryMock;
6565

6666
/**
6767
* @var \Magento\TestFramework\Helper\ObjectManager
@@ -78,6 +78,11 @@ class CollectTotalsTest extends \PHPUnit_Framework_TestCase
7878
*/
7979
protected $groupInterfaceMock;
8080

81+
/**
82+
* @var \PHPUnit_Framework_MockObject_MockObject
83+
*/
84+
protected $dataObjectHelperMock;
85+
8186
/**
8287
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
8388
*/
@@ -96,8 +101,8 @@ protected function setUp()
96101
);
97102
$this->customerAddressMock = $this->getMock('Magento\Customer\Helper\Address', [], [], '', false);
98103
$this->customerVatMock = $this->getMock('Magento\Customer\Model\Vat', [], [], '', false);
99-
$this->customerBuilderMock = $this->getMock(
100-
'Magento\Customer\Api\Data\CustomerDataBuilder',
104+
$this->customerDataFactoryMock = $this->getMock(
105+
'Magento\Customer\Api\Data\CustomerInterfaceFactory',
101106
['mergeDataObjectWithArray', 'create'],
102107
[],
103108
'',
@@ -158,6 +163,10 @@ protected function setUp()
158163
['getId']
159164
);
160165

166+
$this->dataObjectHelperMock = $this->getMockBuilder('Magento\Framework\Api\DataObjectHelper')
167+
->disableOriginalConstructor()
168+
->getMock();
169+
161170
$this->observerMock->expects($this->any())
162171
->method('getQuoteAddress')
163172
->will($this->returnValue($this->quoteAddressMock));
@@ -176,8 +185,9 @@ protected function setUp()
176185
'customerAddressHelper' => $this->customerAddressMock,
177186
'customerVat' => $this->customerVatMock,
178187
'vatValidator' => $this->vatValidatorMock,
179-
'customerBuilder' => $this->customerBuilderMock,
180-
'groupManagement' => $this->groupManagementMock
188+
'customerDataFactory' => $this->customerDataFactoryMock,
189+
'groupManagement' => $this->groupManagementMock,
190+
'dataObjectHelper' => $this->dataObjectHelperMock,
181191
]
182192
);
183193
}
@@ -235,7 +245,7 @@ public function testDispatchWithCustomerCountryNotInEUAndNotLoggedCustomerInGrou
235245

236246
/** Assertions */
237247
$this->quoteAddressMock->expects($this->never())->method('setPrevQuoteCustomerGroupId');
238-
$this->customerBuilderMock->expects($this->never())->method('mergeDataObjectWithArray');
248+
$this->customerDataFactoryMock->expects($this->never())->method('mergeDataObjectWithArray');
239249
$this->quoteMock->expects($this->never())->method('setCustomerGroupId');
240250

241251
/** SUT execution */
@@ -270,11 +280,11 @@ public function testDispatchWithDefaultCustomerGroupId()
270280
->method('setPrevQuoteCustomerGroupId')
271281
->with('customerGroupId');
272282
$this->quoteMock->expects($this->once())->method('setCustomerGroupId')->with('defaultCustomerGroupId');
273-
$this->customerBuilderMock->expects($this->once())
274-
->method('mergeDataObjectWithArray')
283+
$this->dataObjectHelperMock->expects($this->once())
284+
->method('populateWithArray')
275285
->with($this->customerMock, ['group_id' => 'defaultCustomerGroupId'])
276286
->will($this->returnSelf());
277-
$this->customerBuilderMock->expects($this->any())
287+
$this->customerDataFactoryMock->expects($this->any())
278288
->method('create')
279289
->willReturn($this->customerMock);
280290

@@ -326,11 +336,11 @@ public function testDispatchWithCustomerCountryInEU()
326336

327337
$this->quoteMock->expects($this->once())->method('setCustomerGroupId')->with('customerGroupId');
328338
$this->quoteMock->expects($this->once())->method('setCustomer')->with($this->customerMock);
329-
$this->customerBuilderMock->expects($this->once())
330-
->method('mergeDataObjectWithArray')
339+
$this->dataObjectHelperMock->expects($this->once())
340+
->method('populateWithArray')
331341
->with($this->customerMock, ['group_id' => 'customerGroupId'])
332342
->will($this->returnSelf());
333-
$this->customerBuilderMock->expects($this->any())
343+
$this->customerDataFactoryMock->expects($this->any())
334344
->method('create')
335345
->willReturn($this->customerMock);
336346
$this->model->dispatch($this->observerMock);

0 commit comments

Comments
 (0)