From 8b63949fee8645f42f12dae133a5741d966783a0 Mon Sep 17 00:00:00 2001 From: eliseacornejo Date: Tue, 2 Jan 2024 17:05:01 +0100 Subject: [PATCH] LYNX-207: add rule coupon fixture (#180) Co-authored-by: Sergio Vera --- .../SalesRule/Test/Fixture/RuleCoupon.php | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 app/code/Magento/SalesRule/Test/Fixture/RuleCoupon.php diff --git a/app/code/Magento/SalesRule/Test/Fixture/RuleCoupon.php b/app/code/Magento/SalesRule/Test/Fixture/RuleCoupon.php new file mode 100644 index 0000000000000..ad3980ac8832f --- /dev/null +++ b/app/code/Magento/SalesRule/Test/Fixture/RuleCoupon.php @@ -0,0 +1,74 @@ + 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); + } + } +}