12
12
use Magento \Quote \Model \Quote ;
13
13
use Magento \Quote \Model \QuoteIdToMaskedQuoteIdInterface ;
14
14
use Magento \Quote \Model \ResourceModel \Quote as QuoteResource ;
15
+ use Magento \SalesRule \Api \Data \ConditionInterface ;
16
+ use Magento \SalesRule \Api \Data \ConditionInterfaceFactory ;
17
+ use Magento \SalesRule \Api \RuleRepositoryInterface ;
15
18
use Magento \SalesRule \Model \Coupon ;
16
- use Magento \SalesRule \Model \Rule ;
17
19
use Magento \SalesRule \Model \Spi \CouponResourceInterface ;
18
20
use Magento \TestFramework \Helper \Bootstrap ;
19
21
use Magento \TestFramework \TestCase \GraphQlAbstract ;
@@ -49,14 +51,19 @@ class ApplyCouponToCartTest extends GraphQlAbstract
49
51
private $ coupon ;
50
52
51
53
/**
52
- * @var Rule
54
+ * @var CustomerTokenServiceInterface
53
55
*/
54
- private $ salesRule ;
56
+ private $ customerTokenService ;
55
57
56
58
/**
57
- * @var CustomerTokenServiceInterface
59
+ * @var RuleRepositoryInterface
58
60
*/
59
- private $ customerTokenService ;
61
+ private $ ruleRepository ;
62
+
63
+ /**
64
+ * @var ConditionInterfaceFactory
65
+ */
66
+ private $ conditionFactory ;
60
67
61
68
protected function setUp ()
62
69
{
@@ -66,8 +73,9 @@ protected function setUp()
66
73
$ this ->quoteIdToMaskedId = $ objectManager ->create (QuoteIdToMaskedQuoteIdInterface::class);
67
74
$ this ->couponResource = $ objectManager ->get (CouponResourceInterface::class);
68
75
$ this ->coupon = $ objectManager ->create (Coupon::class);
69
- $ this ->salesRule = $ objectManager ->create (Rule::class);
70
76
$ this ->customerTokenService = $ objectManager ->get (CustomerTokenServiceInterface::class);
77
+ $ this ->ruleRepository = $ objectManager ->get (RuleRepositoryInterface::class);
78
+ $ this ->conditionFactory = $ objectManager ->get (ConditionInterfaceFactory::class);
71
79
}
72
80
73
81
/**
@@ -347,37 +355,32 @@ private function excludeProductPerCoupon(string $couponCode, string $sku)
347
355
{
348
356
$ this ->coupon ->loadByCode ($ couponCode );
349
357
$ ruleId = $ this ->coupon ->getRuleId ();
350
- $ salesRule = $ this ->salesRule ->load ($ ruleId );
351
- $ salesRule ->getConditions ()->loadArray ([
352
- 'type ' => \Magento \SalesRule \Model \Rule \Condition \Combine::class,
353
- 'attribute ' => null ,
354
- 'operator ' => null ,
355
- 'value ' => '1 ' ,
356
- 'is_value_processed ' => null ,
357
- 'aggregator ' => 'all ' ,
358
- 'conditions ' =>
359
- [
360
- [
361
- 'type ' => \Magento \SalesRule \Model \Rule \Condition \Product \Found::class,
362
- 'attribute ' => null ,
363
- 'operator ' => null ,
364
- 'value ' => '1 ' ,
365
- 'is_value_processed ' => null ,
366
- 'aggregator ' => 'all ' ,
367
- 'conditions ' =>
368
- [
369
- [
370
- 'type ' => \Magento \SalesRule \Model \Rule \Condition \Product::class,
371
- 'attribute ' => 'sku ' ,
372
- 'operator ' => '!= ' ,
373
- 'value ' => $ sku ,
374
- 'is_value_processed ' => false ,
375
- ],
376
- ],
377
- ],
378
- ],
379
- ]);
380
- $ this ->salesRule ->save ();
358
+ $ salesRule = $ this ->ruleRepository ->getById ($ ruleId );
359
+
360
+ /** @var ConditionInterface $conditionProductSku */
361
+ $ conditionProductSku = $ this ->conditionFactory ->create ();
362
+ $ conditionProductSku ->setConditionType (\Magento \SalesRule \Model \Rule \Condition \Product::class);
363
+ $ conditionProductSku ->setAttributeName ('sku ' );
364
+ $ conditionProductSku ->setValue ('1 ' );
365
+ $ conditionProductSku ->setOperator ('!= ' );
366
+ $ conditionProductSku ->setValue ($ sku );
367
+
368
+ /** @var ConditionInterface $conditionProductFound */
369
+ $ conditionProductFound = $ this ->conditionFactory ->create ();
370
+ $ conditionProductFound ->setConditionType (\Magento \SalesRule \Model \Rule \Condition \Product \Found::class);
371
+ $ conditionProductFound ->setValue ('1 ' );
372
+ $ conditionProductFound ->setAggregatorType ('all ' );
373
+ $ conditionProductFound ->setConditions ([$ conditionProductSku ]);
374
+
375
+ /** @var ConditionInterface $conditionCombine */
376
+ $ conditionCombine = $ this ->conditionFactory ->create ();
377
+ $ conditionCombine ->setConditionType (\Magento \SalesRule \Model \Rule \Condition \Combine::class);
378
+ $ conditionCombine ->setValue ('1 ' );
379
+ $ conditionCombine ->setAggregatorType ('all ' );
380
+ $ conditionCombine ->setConditions ([$ conditionProductFound ]);
381
+
382
+ $ salesRule ->setCondition ($ conditionCombine );
383
+ $ this ->ruleRepository ->save ($ salesRule );
381
384
}
382
385
383
386
/**
0 commit comments