Skip to content

Commit 11480e8

Browse files
committed
Refactored Unit-tests, Integration tests
1 parent cede04f commit 11480e8

File tree

3 files changed

+90
-47
lines changed

3 files changed

+90
-47
lines changed

app/code/Magento/Sales/Observer/AssignOrderToCustomerObserver.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ class AssignOrderToCustomerObserver implements ObserverInterface
2727
/**
2828
* @var CustomerAssignment
2929
*/
30-
private $customerAssignmentService;
30+
private $assignmentService;
3131

3232
/**
3333
* AssignOrderToCustomerObserver constructor.
3434
*
3535
* @param OrderRepositoryInterface $orderRepository
36-
* @param CustomerAssignment $customerAssignmentService
36+
* @param CustomerAssignment $assignmentService
3737
*/
3838
public function __construct(
3939
OrderRepositoryInterface $orderRepository,
40-
CustomerAssignment $customerAssignmentService
40+
CustomerAssignment $assignmentService
4141
) {
4242
$this->orderRepository = $orderRepository;
43-
$this->customerAssignmentService = $customerAssignmentService;
43+
$this->assignmentService = $assignmentService;
4444
}
4545

4646
/**
@@ -57,7 +57,7 @@ public function execute(Observer $observer)
5757
$orderId = $delegateData['__sales_assign_order_id'];
5858
$order = $this->orderRepository->get($orderId);
5959
if (!$order->getCustomerId() && $customer->getId()) {
60-
$this->customerAssignmentService->execute($order, $customer);
60+
$this->assignmentService->execute($order, $customer);
6161
}
6262
}
6363
}

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Event\Observer;
1313
use Magento\Sales\Api\Data\OrderInterface;
1414
use Magento\Sales\Api\OrderRepositoryInterface;
15+
use Magento\Sales\Model\Order\CustomerAssignment;
1516
use Magento\Sales\Observer\AssignOrderToCustomerObserver;
1617
use PHPUnit\Framework\TestCase;
1718
use PHPUnit_Framework_MockObject_MockObject;
@@ -27,6 +28,9 @@ class AssignOrderToCustomerObserverTest extends TestCase
2728
/** @var OrderRepositoryInterface|PHPUnit_Framework_MockObject_MockObject */
2829
protected $orderRepositoryMock;
2930

31+
/** @var CustomerAssignment | PHPUnit_Framework_MockObject_MockObject */
32+
protected $customerAssignmentMock;
33+
3034
/**
3135
* Set Up
3236
*/
@@ -35,7 +39,12 @@ protected function setUp()
3539
$this->orderRepositoryMock = $this->getMockBuilder(OrderRepositoryInterface::class)
3640
->disableOriginalConstructor()
3741
->getMock();
38-
$this->sut = new AssignOrderToCustomerObserver($this->orderRepositoryMock);
42+
43+
$this->customerAssignmentMock = $this->getMockBuilder(CustomerAssignment::class)
44+
->disableOriginalConstructor()
45+
->getMock();
46+
47+
$this->sut = new AssignOrderToCustomerObserver($this->orderRepositoryMock, $this->customerAssignmentMock);
3948
}
4049

4150
/**
@@ -69,13 +78,12 @@ public function testAssignOrderToCustomerAfterGuestOrder($customerId)
6978
$orderMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
7079
$this->orderRepositoryMock->expects($this->once())->method('get')->with($orderId)
7180
->willReturn($orderMock);
72-
if (!$customerId) {
73-
$this->orderRepositoryMock->expects($this->once())->method('save')->with($orderMock);
74-
$this->sut->execute($observerMock);
75-
return ;
81+
if ($customerId) {
82+
$this->customerAssignmentMock->expects($this->once())->method('execute')->with($orderMock, $customerMock);
83+
} else {
84+
$this->customerAssignmentMock->expects($this->never())->method('execute');
7685
}
7786

78-
$this->orderRepositoryMock->expects($this->never())->method('save')->with($orderMock);
7987
$this->sut->execute($observerMock);
8088
}
8189

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

Lines changed: 71 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@
88
use Magento\SalesRule\Model\Rule;
99
use Magento\Store\Model\StoreManagerInterface;
1010
use Magento\Customer\Model\Data\Customer;
11+
use Magento\TestFramework\Helper\Bootstrap;
1112

12-
13+
/**
14+
* Class AssignCouponDataAfterOrderCustomerAssignTest
15+
*
16+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17+
*/
1318
class AssignCouponDataAfterOrderCustomerAssignTest extends \PHPUnit\Framework\TestCase
1419
{
20+
/**
21+
* @var \Magento\Framework\ObjectManagerInterface
22+
*/
23+
private $objectManager;
24+
1525
/**
1626
* @var \Magento\Quote\Api\GuestCartManagementInterface
1727
*/
@@ -22,11 +32,6 @@ class AssignCouponDataAfterOrderCustomerAssignTest extends \PHPUnit\Framework\Te
2232
*/
2333
private $orderRepository;
2434

25-
/**
26-
* @var \Magento\Framework\ObjectManagerInterface
27-
*/
28-
private $objectManager;
29-
3035
/**
3136
* @var \Magento\Framework\Event\ManagerInterface
3237
*/
@@ -48,11 +53,32 @@ class AssignCouponDataAfterOrderCustomerAssignTest extends \PHPUnit\Framework\Te
4853
private $ruleCustomerFactory;
4954

5055
/**
51-
* @inheritdoc
56+
* @var Rule
5257
*/
53-
protected function setUp()
58+
private $salesRule;
59+
60+
/**
61+
* @var Coupon
62+
*/
63+
private $coupon;
64+
65+
/**
66+
* @var Order
67+
*/
68+
private $order;
69+
70+
/**
71+
* @var Customer
72+
*/
73+
private $customer;
74+
75+
/**
76+
* AssignCouponDataAfterOrderCustomerAssignTest constructor.
77+
*/
78+
public function __construct($name = null, array $data = [], $dataName = '')
5479
{
55-
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
80+
parent::__construct($name, $data, $dataName);
81+
$this->objectManager = Bootstrap::getObjectManager();
5682
$this->eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
5783
$this->orderRepository = $this->objectManager->get(Magento\Sales\Model\OrderRepository::class);
5884
$this->delegateCustomerService = $this->objectManager->get(Order\OrderCustomerDelegate::class);
@@ -63,25 +89,44 @@ protected function setUp()
6389
);
6490
}
6591

92+
/**
93+
* @inheritdoc
94+
*/
95+
protected function setUp()
96+
{
97+
$this->salesRule = $this->prepareSalesRule();
98+
$this->coupon = $this->attachSalesruleCoupon($this->salesRule);
99+
$this->order = $this->makeOrderWithCouponAsGuest($this->coupon);
100+
$this->delegateOrderToBeAssigned($this->order);
101+
$this->customer = $this->registerNewCustomer();
102+
$this->order->setCustomerId($this->customer->getId());
103+
}
104+
105+
/**
106+
* @inheritdoc
107+
*/
108+
protected function tearDown()
109+
{
110+
unset(
111+
$this->order,
112+
$this->coupon,
113+
$this->customer,
114+
$this->salesRule
115+
);
116+
}
117+
66118
/**
67119
* @magentoAppIsolation enabled
68120
* @magentoDataFixture Magento/Sales/_files/order.php
69121
*/
70122
public function testCouponDataHasBeenAssignedTest()
71123
{
72-
$rule = $this->prepareSalesRule();
73-
$coupon = $this->attachSalesruleCoupon($rule);
74-
75-
$order = $this->makeOrderWithCouponAsGuest($coupon);
76-
$this->delegateOrderToBeAssigned($order);
77-
78-
$customer = $this->registerNewCustomer();
79-
$ruleCustomer = $this->getSalesruleCustomerUsage($customer, $rule);
124+
$ruleCustomer = $this->getSalesruleCustomerUsage($this->customer, $this->salesRule);
80125

81126
// Assert, that rule customer model has been created for specific customer
82127
$this->assertEquals(
83128
$ruleCustomer->getCustomerId(),
84-
$customer->getId()
129+
$this->customer->getId()
85130
);
86131

87132
// Assert, that customer has increased coupon usage of specific rule
@@ -97,33 +142,23 @@ public function testCouponDataHasBeenAssignedTest()
97142
*/
98143
public function testOrderCancelingDecreasesCouponUsages()
99144
{
100-
$rule = $this->prepareSalesRule();
101-
$coupon = $this->attachSalesruleCoupon($rule);
102-
103-
$order = $this->makeOrderWithCouponAsGuest($coupon);
104-
$this->delegateOrderToBeAssigned($order);
105-
106-
$customer = $this->registerNewCustomer();
107-
108-
$order->setCustomerId($customer->getId());
109-
$this->processOrder($order);
145+
$this->processOrder($this->order);
110146

111147
// Should not throw exception as bux is fixed now
112-
$this->cancelOrder($order);
113-
$ruleCustomer = $this->getSalesruleCustomerUsage($customer, $rule);
148+
$this->cancelOrder($this->order);
149+
$ruleCustomer = $this->getSalesruleCustomerUsage($this->customer, $this->salesRule);
114150

115151
// Assert, that rule customer model has been created for specific customer
116152
$this->assertEquals(
117153
$ruleCustomer->getCustomerId(),
118-
$customer->getId()
154+
$this->customer->getId()
119155
);
120156

121157
// Assert, that customer has increased coupon usage of specific rule
122158
$this->assertEquals(
123159
0,
124160
$ruleCustomer->getTimesUsed()
125161
);
126-
127162
}
128163

129164
/**
@@ -186,7 +221,7 @@ private function prepareSalesRule() : Rule
186221
],
187222
]
188223
);
189-
$this->objectManager->get(
224+
Bootstrap::getObjectManager()->get(
190225
\Magento\SalesRule\Model\ResourceModel\Rule::class
191226
)->save($salesRule);
192227

@@ -204,7 +239,7 @@ private function attachSalesruleCoupon(Rule $salesRule) : Coupon
204239
->setCode('CART_FIXED_DISCOUNT_15')
205240
->setType(0);
206241

207-
$this->objectManager->get(CouponRepositoryInterface::class)->save($coupon);
242+
Bootstrap::getObjectManager()->get(CouponRepositoryInterface::class)->save($coupon);
208243

209244
return $coupon;
210245
}
@@ -215,7 +250,7 @@ private function attachSalesruleCoupon(Rule $salesRule) : Coupon
215250
*/
216251
private function makeOrderWithCouponAsGuest(Coupon $coupon) : Order
217252
{
218-
$order = $this->objectManager->create(\Magento\Sales\Model\Order::class);
253+
$order = Bootstrap::getObjectManager()->create(\Magento\Sales\Model\Order::class);
219254
$order->loadByIncrementId('100000001')
220255
->setCustomerIsGuest(true)
221256
->setCouponCode($coupon->getCode())
@@ -242,7 +277,7 @@ private function delegateOrderToBeAssigned(Order $order)
242277
*/
243278
private function registerNewCustomer() : Customer
244279
{
245-
$customer = $this->objectManager->create(
280+
$customer = Bootstrap::getObjectManager()->create(
246281
\Magento\Customer\Api\Data\CustomerInterface::class
247282
);
248283

0 commit comments

Comments
 (0)