77
88namespace Magento \QuoteGraphQl \Model ;
99
10+ use Exception ;
1011use Magento \Catalog \Test \Fixture \Product as ProductFixture ;
1112use Magento \Catalog \Test \Fixture \Virtual as VirtualProductFixture ;
1213use Magento \Customer \Test \Fixture \Customer ;
@@ -29,6 +30,8 @@ class DiscountsTest extends GraphQlAbstract
2930 /** @var CustomerTokenServiceInterface */
3031 private CustomerTokenServiceInterface $ customerTokenService ;
3132
33+ protected ?Rule $ createdRule = null ;
34+
3235 /** @inheritdoc */
3336 protected function setUp (): void
3437 {
@@ -95,44 +98,12 @@ public function testDiscountsNonVirtualQuote()
9598 ]
9699 /**
97100 * Test discounts resolver for a virtual quote with conditional discount on specific SKU
101+ *
102+ * @throws Exception
98103 */
99104 public function testDiscountsVirtualQuote ()
100105 {
101- // Using SalesRule DataFixture is not working for this case
102- $ objectManager = Bootstrap::getObjectManager ();
103- /** @var Rule $rule */
104- $ rule = $ objectManager ->create (Rule::class);
105- $ rule ->setName ('10% off for virtual222 ' )
106- ->setIsActive (1 )
107- ->setSimpleAction ('by_percent ' );
108- $ rule ->loadPost ([
109- 'name ' => '10% off for virtual222 ' ,
110- 'is_active ' => 1 ,
111- 'simple_action ' => 'by_percent ' ,
112- 'discount_amount ' => 10 ,
113- 'website_ids ' => [1 ],
114- 'customer_group_ids ' => [0 , 1 , 2 , 3 ],
115- 'actions ' => [
116- 1 => [
117- 'type ' => Combine::class,
118- 'attribute ' => null ,
119- 'operator ' => null ,
120- 'value ' => '1 ' ,
121- 'is_value_processed ' => null ,
122- 'aggregator ' => 'all ' ,
123- 'actions ' => [
124- 1 => [
125- 'type ' => Product::class,
126- 'attribute ' => 'sku ' ,
127- 'operator ' => '== ' ,
128- 'value ' => 'virtual222 ' ,
129- 'is_value_processed ' => false ,
130- ]
131- ]
132- ]
133- ],
134- ]);
135- $ rule ->save ();
106+ $ this ->createSalesRuleForSku ("virtual222 " );
136107 $ quoteIdMask = DataFixtureStorageManager::getStorage ()->get ('quoteIdMask ' );
137108 $ maskedQuoteId = $ quoteIdMask ->getMaskedId ();
138109 $ query = $ this ->getCartDiscountsQueryWithItems ($ maskedQuoteId );
@@ -284,4 +255,60 @@ private function getHeaderMap(string $username = 'customer@example.com', string
284255 $ customerToken = $ this ->customerTokenService ->createCustomerAccessToken ($ username , $ password );
285256 return ['Authorization ' => 'Bearer ' . $ customerToken ];
286257 }
258+
259+ /**
260+ * Create SalesRule with specific sku condition
261+ *
262+ * @param string $sku
263+ * @param int $discountPercent
264+ * @return Rule
265+ * @throws Exception
266+ */
267+ protected function createSalesRuleForSku (string $ sku , int $ discountPercent = 10 ): Rule
268+ {
269+ $ objectManager = Bootstrap::getObjectManager ();
270+ /** @var Rule $rule */
271+ $ rule = $ objectManager ->create (Rule::class);
272+ $ rule ->setName ("{$ discountPercent }% off for {$ sku }" )
273+ ->setIsActive (1 )
274+ ->setSimpleAction ('by_percent ' );
275+ $ rule ->loadPost ([
276+ 'name ' => "{$ discountPercent }% " . "off for virtual222 " ,
277+ 'is_active ' => 1 ,
278+ 'simple_action ' => 'by_percent ' ,
279+ 'discount_amount ' => $ discountPercent ,
280+ 'website_ids ' => [1 ],
281+ 'customer_group_ids ' => [0 , 1 , 2 , 3 ],
282+ 'actions ' => [
283+ 1 => [
284+ 'type ' => Combine::class,
285+ 'attribute ' => null ,
286+ 'operator ' => null ,
287+ 'value ' => '1 ' ,
288+ 'is_value_processed ' => null ,
289+ 'aggregator ' => 'all ' ,
290+ 'actions ' => [
291+ 1 => [
292+ 'type ' => Product::class,
293+ 'attribute ' => 'sku ' ,
294+ 'operator ' => '== ' ,
295+ 'value ' => 'virtual222 ' ,
296+ 'is_value_processed ' => false ,
297+ ]
298+ ]
299+ ]
300+ ],
301+ ]);
302+ $ rule ->save ();
303+ $ this ->createdRule = $ rule ;
304+ return $ rule ;
305+ }
306+
307+ protected function tearDown (): void
308+ {
309+ if ($ this ->createdRule && $ this ->createdRule ->getId ()) {
310+ $ this ->createdRule ->delete ();
311+ }
312+ parent ::tearDown ();
313+ }
287314}
0 commit comments