6
6
7
7
namespace Magento \SalesRule \Model \Quote \Address \Total ;
8
8
9
- class ShippingTest extends \PHPUnit \Framework \TestCase
9
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
10
+ use Magento \Framework \Exception \NoSuchEntityException ;
11
+ use Magento \Framework \ObjectManagerInterface ;
12
+ use Magento \Framework \Registry ;
13
+ use Magento \Quote \Api \CartRepositoryInterface ;
14
+ use Magento \Quote \Api \Data \AddressInterface ;
15
+ use Magento \Quote \Api \Data \AddressInterfaceFactory ;
16
+ use Magento \Quote \Api \Data \CartInterface ;
17
+ use Magento \Quote \Api \Data \ShippingMethodInterface ;
18
+ use Magento \Quote \Api \GuestCartItemRepositoryInterface ;
19
+ use Magento \Quote \Api \GuestCartManagementInterface ;
20
+ use Magento \Quote \Api \GuestShipmentEstimationInterface ;
21
+ use Magento \Quote \Model \MaskedQuoteIdToQuoteIdInterface ;
22
+ use Magento \SalesRule \Model \Rule \Condition \Combine ;
23
+ use Magento \SalesRule \Model \Rule \Condition \Product ;
24
+ use Magento \TestFramework \Helper \Bootstrap ;
25
+ use PHPUnit \Framework \TestCase ;
26
+
27
+ /**
28
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
29
+ */
30
+ class ShippingTest extends TestCase
10
31
{
11
32
/**
12
- * @var \Magento\Quote\Api\ GuestCartManagementInterface
33
+ * @var GuestCartManagementInterface
13
34
*/
14
35
private $ cartManagement ;
15
36
16
37
/**
17
- * @var \Magento\Quote\Api\ GuestCartItemRepositoryInterface
38
+ * @var GuestCartItemRepositoryInterface
18
39
*/
19
40
private $ itemRepository ;
20
41
21
42
/**
22
- * @var \Magento\Framework\ ObjectManagerInterface
43
+ * @var ObjectManagerInterface
23
44
*/
24
45
private $ objectManager ;
25
46
26
47
protected function setUp (): void
27
48
{
28
- $ this ->objectManager = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ();
29
- $ this ->cartManagement = $ this ->objectManager ->get (\ Magento \ Quote \ Api \ GuestCartManagementInterface::class);
30
- $ this ->itemRepository = $ this ->objectManager ->get (\ Magento \ Quote \ Api \ GuestCartItemRepositoryInterface::class);
49
+ $ this ->objectManager = Bootstrap::getObjectManager ();
50
+ $ this ->cartManagement = $ this ->objectManager ->get (GuestCartManagementInterface::class);
51
+ $ this ->itemRepository = $ this ->objectManager ->get (GuestCartItemRepositoryInterface::class);
31
52
}
32
53
33
54
/**
@@ -37,7 +58,8 @@ protected function setUp(): void
37
58
*/
38
59
public function testRuleByProductWeightWithFreeShipping ()
39
60
{
40
- $ cartId = $ this ->prepareQuote (1 );
61
+ $ cartId = $ this ->cartManagement ->createEmptyCart ();
62
+ $ this ->addToCart ($ cartId , 'simple-99 ' , 1 );
41
63
$ methods = $ this ->estimateShipping ($ cartId );
42
64
43
65
$ this ->assertTrue (count ($ methods ) > 0 );
@@ -52,55 +74,131 @@ public function testRuleByProductWeightWithFreeShipping()
52
74
*/
53
75
public function testRuleByProductWeightWithoutFreeShipping ()
54
76
{
55
- $ cartId = $ this ->prepareQuote (5 );
77
+ $ cartId = $ this ->cartManagement ->createEmptyCart ();
78
+ $ this ->addToCart ($ cartId , 'simple-99 ' , 5 );
56
79
$ methods = $ this ->estimateShipping ($ cartId );
57
80
58
81
$ this ->assertTrue (count ($ methods ) > 0 );
59
82
$ this ->assertEquals ('flatrate ' , $ methods [0 ]->getMethodCode ());
60
83
$ this ->assertEquals (25 , $ methods [0 ]->getAmount ());
61
84
}
62
85
86
+ /**
87
+ * @magentoAppIsolation enabled
88
+ * @magentoDataFixture Magento/SalesRule/_files/cart_rule_free_shipping.php
89
+ * @magentoDataFixture Magento/Catalog/_files/products_list.php
90
+ */
91
+ public function testFreeMethodWeight ()
92
+ {
93
+ $ this ->setFreeShippingForProduct ('simple-249 ' );
94
+ $ cartId = $ this ->cartManagement ->createEmptyCart ();
95
+ $ this ->addToCart ($ cartId , 'simple-249 ' , 3 );
96
+ $ this ->addToCart ($ cartId , 'simple-156 ' , 1 );
97
+ $ this ->estimateShipping ($ cartId );
98
+ $ quote = $ this ->getQuote ($ cartId );
99
+ $ this ->assertEquals (10 , $ quote ->getShippingAddress ()->getFreeMethodWeight ());
100
+ }
101
+
102
+ /**
103
+ * @magentoAppIsolation enabled
104
+ * @magentoDataFixture Magento/SalesRule/_files/cart_rule_free_shipping.php
105
+ * @magentoDataFixture Magento/Catalog/_files/products_list.php
106
+ */
107
+ public function testFreeMethodWeightWithMaximumQtyDiscount ()
108
+ {
109
+ $ this ->setFreeShippingForProduct ('simple-249 ' , 2 );
110
+ $ cartId = $ this ->cartManagement ->createEmptyCart ();
111
+ $ this ->addToCart ($ cartId , 'simple-249 ' , 5 );
112
+ $ this ->addToCart ($ cartId , 'simple-156 ' , 1 );
113
+ $ this ->estimateShipping ($ cartId );
114
+ $ quote = $ this ->getQuote ($ cartId );
115
+ $ this ->assertEquals (40 , $ quote ->getShippingAddress ()->getFreeMethodWeight ());
116
+ }
117
+
63
118
/**
64
119
* Estimate shipment for guest cart
65
120
*
66
121
* @param int $cartId
67
- * @return \Magento\Quote\Api\Data\ ShippingMethodInterface[]
122
+ * @return ShippingMethodInterface[]
68
123
*/
69
124
private function estimateShipping ($ cartId )
70
125
{
71
- $ addressFactory = $ this ->objectManager ->get (\ Magento \ Quote \ Api \ Data \ AddressInterfaceFactory::class);
72
- /** @var \Magento\Quote\Api\Data\ AddressInterface $address */
126
+ $ addressFactory = $ this ->objectManager ->get (AddressInterfaceFactory::class);
127
+ /** @var AddressInterface $address */
73
128
$ address = $ addressFactory ->create ();
74
129
$ address ->setCountryId ('US ' );
75
130
$ address ->setRegionId (2 );
76
131
77
- /** @var \Magento\Quote\Api\ GuestShipmentEstimationInterface $estimation */
78
- $ estimation = $ this ->objectManager ->get (\ Magento \ Quote \ Api \ GuestShipmentEstimationInterface::class);
132
+ /** @var GuestShipmentEstimationInterface $estimation */
133
+ $ estimation = $ this ->objectManager ->get (GuestShipmentEstimationInterface::class);
79
134
return $ estimation ->estimateByExtendedAddress ($ cartId , $ address );
80
135
}
81
136
82
137
/**
83
- * Create guest quote with products
84
- *
85
- * @param int $itemQty
86
- * @return int
138
+ * @param string $cartMaskId
139
+ * @return CartInterface
140
+ * @throws NoSuchEntityException
87
141
*/
88
- private function prepareQuote ( $ itemQty )
142
+ private function getQuote ( string $ cartMaskId ): CartInterface
89
143
{
90
- $ cartId = $ this ->cartManagement ->createEmptyCart ();
91
-
92
- /** @var \Magento\Quote\Api\Data\CartItemInterfaceFactory $cartItemFactory */
93
- $ cartItemFactory = $ this ->objectManager ->get (\Magento \Quote \Api \Data \CartItemInterfaceFactory::class);
94
-
95
- /** @var \Magento\Quote\Api\Data\CartItemInterface $cartItem */
96
- $ cartItem = $ cartItemFactory ->create ();
97
- $ cartItem ->setQuoteId ($ cartId );
98
- $ cartItem ->setQty ($ itemQty );
99
- $ cartItem ->setSku ('simple-99 ' );
100
- $ cartItem ->setProductType (\Magento \Catalog \Model \Product \Type::TYPE_SIMPLE );
144
+ /** @var CartRepositoryInterface $cartRepository */
145
+ $ cartRepository = $ this ->objectManager ->get (CartRepositoryInterface::class);
146
+ /** @var MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId */
147
+ $ maskedQuoteIdToQuoteId = $ this ->objectManager ->get (MaskedQuoteIdToQuoteIdInterface::class);
148
+ $ cartId = $ maskedQuoteIdToQuoteId ->execute ($ cartMaskId );
149
+ return $ cartRepository ->get ($ cartId );
150
+ }
101
151
102
- $ this ->itemRepository ->save ($ cartItem );
152
+ /**
153
+ * @param string $cartMaskId
154
+ * @param string $sku
155
+ * @param int $qty
156
+ * @throws NoSuchEntityException
157
+ */
158
+ private function addToCart (string $ cartMaskId , string $ sku , int $ qty ): void
159
+ {
160
+ /** @var ProductRepositoryInterface $productRepository */
161
+ $ productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
162
+ /** @var CartRepositoryInterface $cartRepository */
163
+ $ cartRepository = $ this ->objectManager ->get (CartRepositoryInterface::class);
164
+ $ product = $ productRepository ->get ($ sku );
165
+ $ quote = $ this ->getQuote ($ cartMaskId );
166
+ $ quote ->addProduct ($ product , $ qty );
167
+ $ cartRepository ->save ($ quote );
168
+ }
103
169
104
- return $ cartId ;
170
+ /**
171
+ * @param string $sku
172
+ * @param int $qty
173
+ */
174
+ public function setFreeShippingForProduct (string $ sku , int $ qty = 0 ): void
175
+ {
176
+ /** @var Registry $registry */
177
+ $ registry = $ this ->objectManager ->get (Registry::class);
178
+ $ salesRule = $ registry ->registry ('cart_rule_free_shipping ' );
179
+ $ salesRule ->setDiscountQty ($ qty );
180
+ $ data = [
181
+ 'actions ' => [
182
+ 1 => [
183
+ 'type ' => Combine::class,
184
+ 'attribute ' => null ,
185
+ 'operator ' => null ,
186
+ 'value ' => '1 ' ,
187
+ 'is_value_processed ' => null ,
188
+ 'aggregator ' => 'all ' ,
189
+ 'actions ' => [
190
+ 1 => [
191
+ 'type ' => Product::class,
192
+ 'attribute ' => 'sku ' ,
193
+ 'operator ' => '== ' ,
194
+ 'value ' => $ sku ,
195
+ 'is_value_processed ' => false ,
196
+ ]
197
+ ]
198
+ ]
199
+ ],
200
+ ];
201
+ $ salesRule ->loadPost ($ data );
202
+ $ salesRule ->save ();
105
203
}
106
204
}
0 commit comments