10
10
use Magento \Catalog \Api \Data \ProductInterface ;
11
11
use Magento \Catalog \Model \Product ;
12
12
use Magento \Catalog \Model \ProductRepository ;
13
+ use Magento \Framework \Api \SearchCriteriaBuilder ;
14
+ use Magento \Quote \Api \CartRepositoryInterface ;
13
15
use Magento \Quote \Api \Data \CartItemInterface ;
14
16
use Magento \Quote \Api \GuestCartItemRepositoryInterface ;
15
17
use Magento \Quote \Api \GuestCartManagementInterface ;
16
18
use Magento \Quote \Api \GuestCartTotalRepositoryInterface ;
17
19
use Magento \Quote \Api \GuestCouponManagementInterface ;
20
+ use Magento \Quote \Model \Quote ;
21
+ use Magento \Quote \Model \QuoteIdMask ;
22
+ use Magento \Sales \Api \Data \OrderInterface ;
23
+ use Magento \Sales \Api \OrderRepositoryInterface ;
18
24
use Magento \TestFramework \Helper \Bootstrap ;
19
25
20
26
/**
21
27
* Tests for Magento\SalesRule\Model\Rule\Action\Discount\CartFixed.
28
+ *
29
+ * @magentoAppArea frontend
30
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22
31
*/
23
32
class CartFixedTest extends \PHPUnit \Framework \TestCase
24
33
{
@@ -37,14 +46,33 @@ class CartFixedTest extends \PHPUnit\Framework\TestCase
37
46
*/
38
47
private $ couponManagement ;
39
48
49
+ /**
50
+ * @var \Magento\Framework\ObjectManagerInterface
51
+ */
52
+ private $ objectManager ;
53
+
54
+ /**
55
+ * @var SearchCriteriaBuilder
56
+ */
57
+ private $ criteriaBuilder ;
58
+
59
+ /**
60
+ * @var CartRepositoryInterface
61
+ */
62
+ private $ quoteRepository ;
63
+
40
64
/**
41
65
* @inheritdoc
42
66
*/
43
67
protected function setUp ()
44
68
{
45
- $ this ->cartManagement = Bootstrap::getObjectManager ()->create (GuestCartManagementInterface::class);
46
- $ this ->couponManagement = Bootstrap::getObjectManager ()->create (GuestCouponManagementInterface::class);
47
- $ this ->cartItemRepository = Bootstrap::getObjectManager ()->create (GuestCartItemRepositoryInterface::class);
69
+ $ objectManager = Bootstrap::getObjectManager ();
70
+ $ this ->cartManagement = $ objectManager ->create (GuestCartManagementInterface::class);
71
+ $ this ->couponManagement = $ objectManager ->create (GuestCouponManagementInterface::class);
72
+ $ this ->cartItemRepository = $ objectManager ->create (GuestCartItemRepositoryInterface::class);
73
+ $ this ->criteriaBuilder = $ objectManager ->get (SearchCriteriaBuilder::class);
74
+ $ this ->quoteRepository = $ objectManager ->get (CartRepositoryInterface::class);
75
+ $ this ->objectManager = $ objectManager ;
48
76
}
49
77
50
78
/**
@@ -53,6 +81,7 @@ protected function setUp()
53
81
* @param array $productPrices
54
82
* @return void
55
83
* @magentoDbIsolation enabled
84
+ * @magentoAppIsolation enabled
56
85
* @magentoDataFixture Magento/SalesRule/_files/coupon_cart_fixed_discount.php
57
86
* @dataProvider applyFixedDiscountDataProvider
58
87
*/
@@ -82,6 +111,56 @@ public function testApplyFixedDiscount(array $productPrices): void
82
111
$ this ->assertEquals ($ expectedDiscount , $ total ->getBaseDiscountAmount ());
83
112
}
84
113
114
+ /**
115
+ * Applies fixed discount amount on whole cart and created order with it
116
+ *
117
+ * @magentoDbIsolation enabled
118
+ * @magentoAppIsolation enabled
119
+ * @magentoConfigFixture default_store carriers/freeshipping/active 1
120
+ * @magentoDataFixture Magento/Sales/_files/quote.php
121
+ * @magentoDataFixture Magento/SalesRule/_files/coupon_cart_fixed_subtotal_with_discount.php
122
+ */
123
+ public function testOrderWithFixedDiscount (): void
124
+ {
125
+ $ expectedGrandTotal = 5 ;
126
+
127
+ $ quote = $ this ->getQuote ();
128
+ $ quote ->getShippingAddress ()
129
+ ->setShippingMethod ('freeshipping_freeshipping ' )
130
+ ->setCollectShippingRates (true );
131
+ $ quote ->setCouponCode ('CART_FIXED_DISCOUNT_15 ' );
132
+ $ quote ->collectTotals ();
133
+ $ this ->quoteRepository ->save ($ quote );
134
+
135
+ $ this ->assertEquals ($ expectedGrandTotal , $ quote ->getGrandTotal ());
136
+
137
+ /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
138
+ $ quoteIdMask = $ this ->objectManager ->create (QuoteIdMask::class);
139
+ $ quoteIdMask ->load ($ quote ->getId (), 'quote_id ' );
140
+ Bootstrap::getInstance ()->reinitialize ();
141
+ $ cartManagement = Bootstrap::getObjectManager ()->create (GuestCartManagementInterface::class);
142
+ $ cartManagement ->placeOrder ($ quoteIdMask ->getMaskedId ());
143
+ $ order = $ this ->getOrder ('test01 ' );
144
+ $ this ->assertEquals ($ expectedGrandTotal , $ order ->getGrandTotal ());
145
+ }
146
+
147
+ /**
148
+ * Load cart from fixture.
149
+ *
150
+ * @return Quote
151
+ */
152
+ private function getQuote (): Quote
153
+ {
154
+ $ searchCriteria = $ this ->criteriaBuilder ->addFilter ('reserved_order_id ' , 'test01 ' )->create ();
155
+ $ carts = $ this ->quoteRepository ->getList ($ searchCriteria )
156
+ ->getItems ();
157
+ if (!$ carts ) {
158
+ throw new \RuntimeException ('Cart from fixture not found ' );
159
+ }
160
+
161
+ return array_shift ($ carts );
162
+ }
163
+
85
164
/**
86
165
* @return array
87
166
*/
@@ -150,4 +229,25 @@ private function createProduct(float $price): ProductInterface
150
229
151
230
return $ productRepository ->save ($ product );
152
231
}
232
+
233
+ /**
234
+ * Gets order entity by increment id.
235
+ *
236
+ * @param string $incrementId
237
+ * @return OrderInterface
238
+ */
239
+ private function getOrder (string $ incrementId ): OrderInterface
240
+ {
241
+ /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
242
+ $ searchCriteriaBuilder = $ this ->objectManager ->get (SearchCriteriaBuilder::class);
243
+ $ searchCriteria = $ searchCriteriaBuilder ->addFilter ('increment_id ' , $ incrementId )
244
+ ->create ();
245
+
246
+ /** @var OrderRepositoryInterface $repository */
247
+ $ repository = $ this ->objectManager ->get (OrderRepositoryInterface::class);
248
+ $ items = $ repository ->getList ($ searchCriteria )
249
+ ->getItems ();
250
+
251
+ return array_pop ($ items );
252
+ }
153
253
}
0 commit comments