88use Magento \SalesRule \Model \Rule ;
99use Magento \Store \Model \StoreManagerInterface ;
1010use Magento \Customer \Model \Data \Customer ;
11+ use Magento \TestFramework \Helper \Bootstrap ;
1112
12-
13+ /**
14+ * Class AssignCouponDataAfterOrderCustomerAssignTest
15+ *
16+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17+ */
1318class 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