11
11
use Magento \Catalog \Model \Product ;
12
12
use Magento \Catalog \Model \ProductRepository ;
13
13
use Magento \Framework \Api \SearchCriteriaBuilder ;
14
+ use Magento \Quote \Api \CartRepositoryInterface ;
14
15
use Magento \Quote \Api \Data \CartItemInterface ;
15
16
use Magento \Quote \Api \GuestCartItemRepositoryInterface ;
16
17
use Magento \Quote \Api \GuestCartManagementInterface ;
17
18
use Magento \Quote \Api \GuestCartTotalRepositoryInterface ;
18
19
use Magento \Quote \Api \GuestCouponManagementInterface ;
20
+ use Magento \Quote \Model \Quote ;
21
+ use Magento \Quote \Model \QuoteIdMask ;
19
22
use Magento \Sales \Api \Data \OrderInterface ;
20
23
use Magento \Sales \Api \OrderRepositoryInterface ;
21
24
use Magento \TestFramework \Helper \Bootstrap ;
@@ -47,15 +50,28 @@ class CartFixedTest extends \PHPUnit\Framework\TestCase
47
50
*/
48
51
private $ objectManager ;
49
52
53
+ /**
54
+ * @var SearchCriteriaBuilder
55
+ */
56
+ private $ criteriaBuilder ;
57
+
58
+ /**
59
+ * @var CartRepositoryInterface
60
+ */
61
+ private $ quoteRepository ;
62
+
50
63
/**
51
64
* @inheritdoc
52
65
*/
53
66
protected function setUp ()
54
67
{
55
- $ this ->objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
56
- $ this ->cartManagement = Bootstrap::getObjectManager ()->create (GuestCartManagementInterface::class);
57
- $ this ->couponManagement = Bootstrap::getObjectManager ()->create (GuestCouponManagementInterface::class);
58
- $ this ->cartItemRepository = Bootstrap::getObjectManager ()->create (GuestCartItemRepositoryInterface::class);
68
+ $ objectManager = Bootstrap::getObjectManager ();
69
+ $ this ->cartManagement = $ objectManager ->create (GuestCartManagementInterface::class);
70
+ $ this ->couponManagement = $ objectManager ->create (GuestCouponManagementInterface::class);
71
+ $ this ->cartItemRepository = $ objectManager ->create (GuestCartItemRepositoryInterface::class);
72
+ $ this ->criteriaBuilder = $ objectManager ->get (SearchCriteriaBuilder::class);
73
+ $ this ->quoteRepository = $ objectManager ->get (CartRepositoryInterface::class);
74
+ $ this ->objectManager = $ objectManager ;
59
75
}
60
76
61
77
/**
@@ -64,6 +80,7 @@ protected function setUp()
64
80
* @param array $productPrices
65
81
* @return void
66
82
* @magentoDbIsolation enabled
83
+ * @magentoAppIsolation enabled
67
84
* @magentoDataFixture Magento/SalesRule/_files/coupon_cart_fixed_discount.php
68
85
* @dataProvider applyFixedDiscountDataProvider
69
86
*/
@@ -96,25 +113,51 @@ public function testApplyFixedDiscount(array $productPrices): void
96
113
/**
97
114
* Applies fixed discount amount on whole cart and created order with it
98
115
*
99
- * @return void
100
- * @magentoDataFixture Magento/SalesRule/_files/coupon_cart_fixed_discount_subtotal_with_discount.php
101
- * @magentoDataFixture Magento/SalesRule/_files/quote_with_coupon.php
102
- *
116
+ * @magentoDbIsolation enabled
117
+ * @magentoAppIsolation enabled
118
+ * @magentoConfigFixture default_store carriers/freeshipping/active 1
119
+ * @magentoDataFixture Magento/Sales/_files/quote.php
120
+ * @magentoDataFixture Magento/SalesRule/_files/coupon_cart_fixed_subtotal_with_discount.php
103
121
*/
104
122
public function testOrderWithFixedDiscount (): void
105
123
{
106
- /** @var $quote \Magento\Quote\Model\Quote */
107
- $ quote = $ this ->objectManager ->create (\Magento \Quote \Model \Quote::class);
124
+ $ expectedGrandTotal = 5 ;
125
+
126
+ $ quote = $ this ->getQuote ();
127
+ $ quote ->getShippingAddress ()
128
+ ->setShippingMethod ('freeshipping_freeshipping ' )
129
+ ->setCollectShippingRates (true );
130
+ $ quote ->setCouponCode ('CART_FIXED_DISCOUNT_15 ' );
131
+ $ quote ->collectTotals ();
132
+ $ this ->quoteRepository ->save ($ quote );
133
+
134
+ $ this ->assertEquals ($ expectedGrandTotal , $ quote ->getGrandTotal ());
135
+
108
136
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
109
- $ quoteIdMask = $ this ->objectManager ->create (\Magento \Quote \Model \QuoteIdMask::class);
110
- $ quote ->load ('test01 ' , 'reserved_order_id ' );
137
+ $ quoteIdMask = $ this ->objectManager ->create (QuoteIdMask::class);
111
138
$ quoteIdMask ->load ($ quote ->getId (), 'quote_id ' );
112
139
Bootstrap::getInstance ()->reinitialize ();
113
-
114
140
$ cartManagement = Bootstrap::getObjectManager ()->create (GuestCartManagementInterface::class);
115
141
$ cartManagement ->placeOrder ($ quoteIdMask ->getMaskedId ());
116
142
$ order = $ this ->getOrder ('test01 ' );
117
- $ this ->assertEquals ($ quote ->getGrandTotal (), $ order ->getGrandTotal ());
143
+ $ this ->assertEquals ($ expectedGrandTotal , $ order ->getGrandTotal ());
144
+ }
145
+
146
+ /**
147
+ * Load cart from fixture.
148
+ *
149
+ * @return Quote
150
+ */
151
+ private function getQuote (): Quote
152
+ {
153
+ $ searchCriteria = $ this ->criteriaBuilder ->addFilter ('reserved_order_id ' , 'test01 ' )->create ();
154
+ $ carts = $ this ->quoteRepository ->getList ($ searchCriteria )
155
+ ->getItems ();
156
+ if (!$ carts ) {
157
+ throw new \RuntimeException ('Cart from fixture not found ' );
158
+ }
159
+
160
+ return array_shift ($ carts );
118
161
}
119
162
120
163
/**
0 commit comments