Skip to content

Commit 149dec1

Browse files
committed
Code refactoring
1 parent bb7be7a commit 149dec1

File tree

2 files changed

+78
-72
lines changed

2 files changed

+78
-72
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/ApplyCouponToCartTest.php

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
use Magento\Quote\Model\Quote;
1313
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
1414
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;
1518
use Magento\SalesRule\Model\Coupon;
16-
use Magento\SalesRule\Model\Rule;
1719
use Magento\SalesRule\Model\Spi\CouponResourceInterface;
1820
use Magento\TestFramework\Helper\Bootstrap;
1921
use Magento\TestFramework\TestCase\GraphQlAbstract;
@@ -49,14 +51,19 @@ class ApplyCouponToCartTest extends GraphQlAbstract
4951
private $coupon;
5052

5153
/**
52-
* @var Rule
54+
* @var CustomerTokenServiceInterface
5355
*/
54-
private $salesRule;
56+
private $customerTokenService;
5557

5658
/**
57-
* @var CustomerTokenServiceInterface
59+
* @var RuleRepositoryInterface
5860
*/
59-
private $customerTokenService;
61+
private $ruleRepository;
62+
63+
/**
64+
* @var ConditionInterfaceFactory
65+
*/
66+
private $conditionFactory;
6067

6168
protected function setUp()
6269
{
@@ -66,8 +73,9 @@ protected function setUp()
6673
$this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class);
6774
$this->couponResource = $objectManager->get(CouponResourceInterface::class);
6875
$this->coupon = $objectManager->create(Coupon::class);
69-
$this->salesRule = $objectManager->create(Rule::class);
7076
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
77+
$this->ruleRepository = $objectManager->get(RuleRepositoryInterface::class);
78+
$this->conditionFactory = $objectManager->get(ConditionInterfaceFactory::class);
7179
}
7280

7381
/**
@@ -347,37 +355,32 @@ private function excludeProductPerCoupon(string $couponCode, string $sku)
347355
{
348356
$this->coupon->loadByCode($couponCode);
349357
$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);
381384
}
382385

383386
/**

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/ApplyCouponToCartTest.php

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
use Magento\Quote\Model\Quote;
1111
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
1212
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
13+
use Magento\SalesRule\Api\Data\ConditionInterface;
14+
use Magento\SalesRule\Api\Data\ConditionInterfaceFactory;
15+
use Magento\SalesRule\Api\RuleRepositoryInterface;
1316
use Magento\SalesRule\Model\Coupon;
14-
use Magento\SalesRule\Model\Rule;
1517
use Magento\SalesRule\Model\Spi\CouponResourceInterface;
1618
use Magento\TestFramework\Helper\Bootstrap;
1719
use Magento\TestFramework\TestCase\GraphQlAbstract;
@@ -47,9 +49,14 @@ class ApplyCouponToCartTest extends GraphQlAbstract
4749
private $coupon;
4850

4951
/**
50-
* @var Rule
52+
* @var RuleRepositoryInterface
5153
*/
52-
private $salesRule;
54+
private $ruleRepository;
55+
56+
/**
57+
* @var ConditionInterfaceFactory
58+
*/
59+
private $conditionFactory;
5360

5461
protected function setUp()
5562
{
@@ -59,7 +66,8 @@ protected function setUp()
5966
$this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class);
6067
$this->couponResource = $objectManager->get(CouponResourceInterface::class);
6168
$this->coupon = $objectManager->create(Coupon::class);
62-
$this->salesRule = $objectManager->create(Rule::class);
69+
$this->ruleRepository = $objectManager->get(RuleRepositoryInterface::class);
70+
$this->conditionFactory = $objectManager->get(ConditionInterfaceFactory::class);
6371
}
6472

6573
/**
@@ -282,37 +290,32 @@ private function excludeProductPerCoupon(string $couponCode, string $sku)
282290
{
283291
$this->coupon->loadByCode($couponCode);
284292
$ruleId = $this->coupon->getRuleId();
285-
$salesRule = $this->salesRule->load($ruleId);
286-
$salesRule->getConditions()->loadArray([
287-
'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
288-
'attribute' => null,
289-
'operator' => null,
290-
'value' => '1',
291-
'is_value_processed' => null,
292-
'aggregator' => 'all',
293-
'conditions' =>
294-
[
295-
[
296-
'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Found::class,
297-
'attribute' => null,
298-
'operator' => null,
299-
'value' => '1',
300-
'is_value_processed' => null,
301-
'aggregator' => 'all',
302-
'conditions' =>
303-
[
304-
[
305-
'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
306-
'attribute' => 'sku',
307-
'operator' => '!=',
308-
'value' => $sku,
309-
'is_value_processed' => false,
310-
],
311-
],
312-
],
313-
],
314-
]);
315-
$this->salesRule->save();
293+
$salesRule = $this->ruleRepository->getById($ruleId);
294+
295+
/** @var ConditionInterface $conditionProductSku */
296+
$conditionProductSku = $this->conditionFactory->create();
297+
$conditionProductSku->setConditionType(\Magento\SalesRule\Model\Rule\Condition\Product::class);
298+
$conditionProductSku->setAttributeName('sku');
299+
$conditionProductSku->setValue('1');
300+
$conditionProductSku->setOperator('!=');
301+
$conditionProductSku->setValue($sku);
302+
303+
/** @var ConditionInterface $conditionProductFound */
304+
$conditionProductFound = $this->conditionFactory->create();
305+
$conditionProductFound->setConditionType(\Magento\SalesRule\Model\Rule\Condition\Product\Found::class);
306+
$conditionProductFound->setValue('1');
307+
$conditionProductFound->setAggregatorType('all');
308+
$conditionProductFound->setConditions([$conditionProductSku]);
309+
310+
/** @var ConditionInterface $conditionCombine */
311+
$conditionCombine = $this->conditionFactory->create();
312+
$conditionCombine->setConditionType(\Magento\SalesRule\Model\Rule\Condition\Combine::class);
313+
$conditionCombine->setValue('1');
314+
$conditionCombine->setAggregatorType('all');
315+
$conditionCombine->setConditions([$conditionProductFound]);
316+
317+
$salesRule->setCondition($conditionCombine);
318+
$this->ruleRepository->save($salesRule);
316319
}
317320

318321
/**

0 commit comments

Comments
 (0)