-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LYNX-207: add rule coupon fixture (#180)
Co-authored-by: Sergio Vera <svera@adobe.com>
- Loading branch information
1 parent
6756b03
commit 8b63949
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/** | ||
* Copyright 2023 Adobe | ||
* All Rights Reserved. | ||
* | ||
* NOTICE: All information contained herein is, and remains | ||
* the property of Adobe and its suppliers, if any. The intellectual | ||
* and technical concepts contained herein are proprietary to Adobe | ||
* and its suppliers and are protected by all applicable intellectual | ||
* property laws, including trade secret and copyright laws. | ||
* Dissemination of this information or reproduction of this material | ||
* is strictly forbidden unless prior written permission is obtained from | ||
* Adobe. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\SalesRule\Test\Fixture; | ||
|
||
use Magento\Framework\DataObject; | ||
use Magento\SalesRule\Model\Spi\CouponResourceInterface; | ||
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface; | ||
use Magento\SalesRule\Model\CouponFactory; | ||
use Magento\SalesRule\Api\Data\CouponInterface; | ||
|
||
class RuleCoupon implements RevertibleDataFixtureInterface | ||
{ | ||
private const DEFAULT_DATA = [ | ||
'rule_id' => null, | ||
'code' => null, | ||
'usage_limit' => false, | ||
'usage_per_customer' => false, | ||
'type' => CouponInterface::TYPE_MANUAL | ||
]; | ||
|
||
/** | ||
* @var CouponFactory | ||
*/ | ||
private CouponFactory $couponFactory; | ||
|
||
/** | ||
* @var CouponResourceInterface | ||
*/ | ||
private CouponResourceInterface $couponRuleResourceModel; | ||
|
||
/** | ||
* @param CouponResourceInterface $couponRuleResourceModel | ||
* @param CouponFactory $couponFactory | ||
*/ | ||
public function __construct( | ||
CouponResourceInterface $couponRuleResourceModel, | ||
CouponFactory $couponFactory, | ||
) { | ||
$this->couponRuleResourceModel = $couponRuleResourceModel; | ||
$this->couponFactory = $couponFactory; | ||
} | ||
|
||
public function apply(array $data = []): ?DataObject | ||
{ | ||
$data = array_merge(self::DEFAULT_DATA, $data); | ||
$coupon = $this->couponFactory->create(); | ||
$coupon->setData($data); | ||
$this->couponRuleResourceModel->save($coupon); | ||
return $coupon; | ||
} | ||
|
||
public function revert(DataObject $data): void | ||
{ | ||
$coupon = $this->couponFactory->create(); | ||
$this->couponRuleResourceModel->load($coupon, $data->getId()); | ||
if ($coupon->getId()) { | ||
$this->couponRuleResourceModel->delete($coupon); | ||
} | ||
} | ||
} |