8
8
use Magento \SalesRule \Model \Rule ;
9
9
use Magento \Store \Model \StoreManagerInterface ;
10
10
use Magento \Customer \Model \Data \Customer ;
11
+ use Magento \TestFramework \Helper \Bootstrap ;
11
12
12
-
13
+ /**
14
+ * Class AssignCouponDataAfterOrderCustomerAssignTest
15
+ *
16
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17
+ */
13
18
class AssignCouponDataAfterOrderCustomerAssignTest extends \PHPUnit \Framework \TestCase
14
19
{
20
+ /**
21
+ * @var \Magento\Framework\ObjectManagerInterface
22
+ */
23
+ private $ objectManager ;
24
+
15
25
/**
16
26
* @var \Magento\Quote\Api\GuestCartManagementInterface
17
27
*/
@@ -22,11 +32,6 @@ class AssignCouponDataAfterOrderCustomerAssignTest extends \PHPUnit\Framework\Te
22
32
*/
23
33
private $ orderRepository ;
24
34
25
- /**
26
- * @var \Magento\Framework\ObjectManagerInterface
27
- */
28
- private $ objectManager ;
29
-
30
35
/**
31
36
* @var \Magento\Framework\Event\ManagerInterface
32
37
*/
@@ -48,11 +53,32 @@ class AssignCouponDataAfterOrderCustomerAssignTest extends \PHPUnit\Framework\Te
48
53
private $ ruleCustomerFactory ;
49
54
50
55
/**
51
- * @inheritdoc
56
+ * @var Rule
52
57
*/
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 = '' )
54
79
{
55
- $ this ->objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
80
+ parent ::__construct ($ name , $ data , $ dataName );
81
+ $ this ->objectManager = Bootstrap::getObjectManager ();
56
82
$ this ->eventManager = $ this ->createMock (\Magento \Framework \Event \ManagerInterface::class);
57
83
$ this ->orderRepository = $ this ->objectManager ->get (Magento \Sales \Model \OrderRepository::class);
58
84
$ this ->delegateCustomerService = $ this ->objectManager ->get (Order \OrderCustomerDelegate::class);
@@ -63,25 +89,44 @@ protected function setUp()
63
89
);
64
90
}
65
91
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
+
66
118
/**
67
119
* @magentoAppIsolation enabled
68
120
* @magentoDataFixture Magento/Sales/_files/order.php
69
121
*/
70
122
public function testCouponDataHasBeenAssignedTest ()
71
123
{
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 );
80
125
81
126
// Assert, that rule customer model has been created for specific customer
82
127
$ this ->assertEquals (
83
128
$ ruleCustomer ->getCustomerId (),
84
- $ customer ->getId ()
129
+ $ this -> customer ->getId ()
85
130
);
86
131
87
132
// Assert, that customer has increased coupon usage of specific rule
@@ -97,33 +142,23 @@ public function testCouponDataHasBeenAssignedTest()
97
142
*/
98
143
public function testOrderCancelingDecreasesCouponUsages ()
99
144
{
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 );
110
146
111
147
// 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 );
114
150
115
151
// Assert, that rule customer model has been created for specific customer
116
152
$ this ->assertEquals (
117
153
$ ruleCustomer ->getCustomerId (),
118
- $ customer ->getId ()
154
+ $ this -> customer ->getId ()
119
155
);
120
156
121
157
// Assert, that customer has increased coupon usage of specific rule
122
158
$ this ->assertEquals (
123
159
0 ,
124
160
$ ruleCustomer ->getTimesUsed ()
125
161
);
126
-
127
162
}
128
163
129
164
/**
@@ -186,7 +221,7 @@ private function prepareSalesRule() : Rule
186
221
],
187
222
]
188
223
);
189
- $ this -> objectManager ->get (
224
+ Bootstrap:: getObjectManager () ->get (
190
225
\Magento \SalesRule \Model \ResourceModel \Rule::class
191
226
)->save ($ salesRule );
192
227
@@ -204,7 +239,7 @@ private function attachSalesruleCoupon(Rule $salesRule) : Coupon
204
239
->setCode ('CART_FIXED_DISCOUNT_15 ' )
205
240
->setType (0 );
206
241
207
- $ this -> objectManager ->get (CouponRepositoryInterface::class)->save ($ coupon );
242
+ Bootstrap:: getObjectManager () ->get (CouponRepositoryInterface::class)->save ($ coupon );
208
243
209
244
return $ coupon ;
210
245
}
@@ -215,7 +250,7 @@ private function attachSalesruleCoupon(Rule $salesRule) : Coupon
215
250
*/
216
251
private function makeOrderWithCouponAsGuest (Coupon $ coupon ) : Order
217
252
{
218
- $ order = $ this -> objectManager ->create (\Magento \Sales \Model \Order::class);
253
+ $ order = Bootstrap:: getObjectManager () ->create (\Magento \Sales \Model \Order::class);
219
254
$ order ->loadByIncrementId ('100000001 ' )
220
255
->setCustomerIsGuest (true )
221
256
->setCouponCode ($ coupon ->getCode ())
@@ -242,7 +277,7 @@ private function delegateOrderToBeAssigned(Order $order)
242
277
*/
243
278
private function registerNewCustomer () : Customer
244
279
{
245
- $ customer = $ this -> objectManager ->create (
280
+ $ customer = Bootstrap:: getObjectManager () ->create (
246
281
\Magento \Customer \Api \Data \CustomerInterface::class
247
282
);
248
283
0 commit comments