Skip to content

Commit d8add8a

Browse files
committed
#19230: Refactoring and unit test update.
1 parent ae76c57 commit d8add8a

File tree

4 files changed

+186
-17
lines changed

4 files changed

+186
-17
lines changed

app/code/Magento/Sales/Model/Order/CustomerAssignment.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
namespace Magento\Sales\Model\Order;
99

10-
use Magento\Sales\Api\Data\OrderInterface;
11-
use Magento\Sales\Api\OrderRepositoryInterface;
1210
use Magento\Customer\Api\Data\CustomerInterface;
1311
use Magento\Framework\Event\ManagerInterface;
12+
use Magento\Sales\Api\Data\OrderInterface;
13+
use Magento\Sales\Api\OrderRepositoryInterface;
1414

1515
/**
1616
* Assign customer to order.
@@ -47,10 +47,18 @@ public function __construct(
4747
* @param OrderInterface $order
4848
* @param CustomerInterface $customer
4949
*/
50-
public function execute(OrderInterface $order, CustomerInterface $customer)/*: void*/
50+
public function execute(OrderInterface $order, CustomerInterface $customer): void
5151
{
52-
$order->setCustomerId($customer->getId());
53-
$order->setCustomerIsGuest(false);
52+
$order->setCustomerId($customer->getId())
53+
->setCustomerIsGuest(false)
54+
->setCustomerEmail($customer->getEmail())
55+
->setCustomerFirstname($customer->getFirstname())
56+
->setCustomerLastname($customer->getLastname())
57+
->setCustomerMiddlename($customer->getMiddlename())
58+
->setCustomerPrefix($customer->getPrefix())
59+
->setCustomerSuffix($customer->getSuffix())
60+
->setCustomerGroupId($customer->getGroupId());
61+
5462
$this->orderRepository->save($order);
5563

5664
$this->eventManager->dispatch(
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Test\Unit\Model\Order;
9+
10+
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Framework\Event\ManagerInterface;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use Magento\Sales\Api\Data\OrderInterface;
14+
use Magento\Sales\Api\OrderRepositoryInterface;
15+
use Magento\Sales\Model\Order\CustomerAssignment;
16+
17+
/**
18+
* Test for Magento\Sales\Model\Order\CustomerAssignment class.
19+
*/
20+
class CustomerAssigmentTest extends \PHPUnit\Framework\TestCase
21+
{
22+
/**
23+
* @var CustomerAssignment
24+
*/
25+
private $customerAssignment;
26+
27+
/**
28+
* @var OrderInterface|\PHPUnit\Framework\MockObject\MockObject
29+
*/
30+
private $orderMock;
31+
32+
/**
33+
* @var CustomerInterface|\PHPUnit\Framework\MockObject\MockObject
34+
*/
35+
private $customerMock;
36+
37+
/**
38+
* @var OrderRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
39+
*/
40+
private $orderRepositoryMock;
41+
42+
/**
43+
* @var ManagerInterface|\PHPUnit\Framework\MockObject\MockObject
44+
*/
45+
private $eventManagerMock;
46+
47+
/**
48+
* Tests 'execute' method.
49+
*
50+
* @dataProvider executeDataProvider
51+
* @param array $data
52+
*/
53+
public function testExecute(array $data): void
54+
{
55+
$this->configureOrderMock($data);
56+
$this->configureCustomerMock($data);
57+
$this->orderRepositoryMock->expects($this->once())->method('save')->with($this->orderMock);
58+
$this->eventManagerMock->expects($this->once())->method('dispatch')->with(
59+
'sales_order_customer_assign_after',
60+
[
61+
'order' => $this->orderMock,
62+
'customer' => $this->customerMock
63+
]
64+
);
65+
66+
$this->customerAssignment->execute($this->orderMock, $this->customerMock);
67+
}
68+
69+
/**
70+
*
71+
* Data provider for testExecute.
72+
* @return array
73+
*/
74+
public function executeDataProvider(): array
75+
{
76+
return [
77+
[
78+
[
79+
'customerId' => 1,
80+
'customerIsGuest' => false,
81+
'customerEmail' => 'customerEmail',
82+
'customerFirstname' => 'customerFirstname',
83+
'customerLastname' => 'customerLastname',
84+
'customerMiddlename' => 'customerMiddlename',
85+
'customerPrefix' => 'customerPrefix',
86+
'customerSuffix' => 'customerSuffix',
87+
'customerGroupId' => 'customerGroupId',
88+
],
89+
],
90+
];
91+
}
92+
93+
/**
94+
* @return void
95+
*/
96+
protected function setUp()
97+
{
98+
$objectManager = new ObjectManager($this);
99+
$this->orderMock = $this->createMock(OrderInterface::class);
100+
$this->customerMock = $this->createMock(CustomerInterface::class);
101+
$this->orderRepositoryMock = $this->createMock(OrderRepositoryInterface::class);
102+
$this->eventManagerMock = $this->createMock(ManagerInterface::class);
103+
$this->customerAssignment = $objectManager->getObject(
104+
CustomerAssignment::class,
105+
[
106+
'eventManager' => $this->eventManagerMock,
107+
'orderRepository' => $this->orderRepositoryMock
108+
]
109+
);
110+
}
111+
112+
/**
113+
* Set up order mock.
114+
*
115+
* @param array $data
116+
*/
117+
private function configureOrderMock(array $data): void
118+
{
119+
$this->orderMock->expects($this->once())->method('setCustomerId')->with($data['customerId'])
120+
->willReturn($this->orderMock);
121+
$this->orderMock->expects($this->once())->method('setCustomerIsGuest')->with($data['customerIsGuest'])
122+
->willReturn($this->orderMock);
123+
$this->orderMock->expects($this->once())->method('setCustomerEmail')->with($data['customerEmail'])
124+
->willReturn($this->orderMock);
125+
$this->orderMock->expects($this->once())->method('setCustomerFirstname')->with($data['customerFirstname'])
126+
->willReturn($this->orderMock);
127+
$this->orderMock->expects($this->once())->method('setCustomerLastname')->with($data['customerLastname'])
128+
->willReturn($this->orderMock);
129+
$this->orderMock->expects($this->once())->method('setCustomerMiddlename')->with($data['customerMiddlename'])
130+
->willReturn($this->orderMock);
131+
$this->orderMock->expects($this->once())->method('setCustomerPrefix')->with($data['customerPrefix'])
132+
->willReturn($this->orderMock);
133+
$this->orderMock->expects($this->once())->method('setCustomerSuffix')->with($data['customerSuffix'])
134+
->willReturn($this->orderMock);
135+
$this->orderMock->expects($this->once())->method('setCustomerGroupId')->with($data['customerGroupId'])
136+
->willReturn($this->orderMock);
137+
}
138+
139+
/**
140+
* Set up customer mock.
141+
*
142+
* @param array $data
143+
*/
144+
private function configureCustomerMock(array $data): void
145+
{
146+
$this->customerMock->expects($this->once())->method('getId')->willReturn($data['customerId']);
147+
$this->customerMock->expects($this->once())->method('getEmail')->willReturn($data['customerEmail']);
148+
$this->customerMock->expects($this->once())->method('getFirstname')->willReturn($data['customerFirstname']);
149+
$this->customerMock->expects($this->once())->method('getLastname')->willReturn($data['customerLastname']);
150+
$this->customerMock->expects($this->once())->method('getMiddlename')->willReturn($data['customerMiddlename']);
151+
$this->customerMock->expects($this->once())->method('getPrefix')->willReturn($data['customerPrefix']);
152+
$this->customerMock->expects($this->once())->method('getSuffix')->willReturn($data['customerSuffix']);
153+
$this->customerMock->expects($this->once())->method('getGroupId')->willReturn($data['customerGroupId']);
154+
}
155+
}

app/code/Magento/Sales/Test/Unit/Observer/AssignOrderToCustomerObserverTest.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ protected function setUp()
5151
* Test assigning order to customer after issuing guest order
5252
*
5353
* @dataProvider getCustomerIds
54+
* @param null|int $orderCustomerId
5455
* @param null|int $customerId
5556
* @return void
5657
*/
57-
public function testAssignOrderToCustomerAfterGuestOrder($customerId)
58+
public function testAssignOrderToCustomerAfterGuestOrder($orderCustomerId, $customerId)
5859
{
5960
$orderId = 1;
6061
/** @var Observer|PHPUnit_Framework_MockObject_MockObject $observerMock */
@@ -71,15 +72,18 @@ public function testAssignOrderToCustomerAfterGuestOrder($customerId)
7172
->getMockForAbstractClass();
7273
$observerMock->expects($this->once())->method('getEvent')->willReturn($eventMock);
7374
$eventMock->expects($this->any())->method('getData')
74-
->willReturnMap([
75-
['delegate_data', null, ['__sales_assign_order_id' => $orderId]],
76-
['customer_data_object', null, $customerMock]
77-
]);
78-
$orderMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
75+
->willReturnMap(
76+
[
77+
['delegate_data', null, ['__sales_assign_order_id' => $orderId]],
78+
['customer_data_object', null, $customerMock]
79+
]
80+
);
81+
$orderMock->expects($this->once())->method('getCustomerId')->willReturn($orderCustomerId);
7982
$this->orderRepositoryMock->expects($this->once())->method('get')->with($orderId)
8083
->willReturn($orderMock);
8184

82-
if ($customerId) {
85+
if (!$orderCustomerId) {
86+
$customerMock->expects($this->once())->method('getId')->willReturn($customerId);
8387
$this->assignmentMock->expects($this->once())->method('execute')->with($orderMock, $customerMock);
8488
$this->sut->execute($observerMock);
8589
return;
@@ -96,6 +100,9 @@ public function testAssignOrderToCustomerAfterGuestOrder($customerId)
96100
*/
97101
public function getCustomerIds()
98102
{
99-
return [[null, 1]];
103+
return [
104+
[null, 1],
105+
[1, 1],
106+
];
100107
}
101108
}

dev/tests/integration/testsuite/Magento/SalesRule/Model/Observer/AssignCouponDataAfterOrderCustomerAssignTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
namespace Magento\SalesRule\Model\Observer;
88

9+
use Magento\Customer\Model\Data\Customer;
10+
use Magento\Customer\Model\GroupManagement;
911
use Magento\Framework\Controller\Result\Redirect;
1012
use Magento\Sales\Model\Order;
11-
use Magento\Customer\Model\GroupManagement;
1213
use Magento\SalesRule\Api\CouponRepositoryInterface;
1314
use Magento\SalesRule\Model\Coupon;
1415
use Magento\SalesRule\Model\Rule;
1516
use Magento\Store\Model\StoreManagerInterface;
16-
use Magento\Customer\Model\Data\Customer;
1717
use Magento\TestFramework\Helper\Bootstrap;
1818

1919
/**
@@ -252,9 +252,8 @@ private function makeOrderWithCouponAsGuest(Coupon $coupon) : Order
252252

253253
/**
254254
* @param Order $order
255-
* @return Redirect
256255
*/
257-
private function delegateOrderToBeAssigned(Order $order): Redirect
256+
private function delegateOrderToBeAssigned(Order $order)
258257
{
259258
$this->delegateCustomerService->delegateNew($order->getId());
260259
}

0 commit comments

Comments
 (0)