Skip to content

Commit df413f5

Browse files
committed
MC-15779: Create coupon test functionality for our benchmark
1 parent c1d1ca2 commit df413f5

File tree

7 files changed

+367
-0
lines changed

7 files changed

+367
-0
lines changed

setup/performance-toolkit/profiles/ce/extra_large.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<catalog_price_rules>20</catalog_price_rules> <!-- Number of catalog price rules -->
4040
<cart_price_rules>20</cart_price_rules> <!-- Number of cart price rules -->
4141
<cart_price_rules_floor>2</cart_price_rules_floor>
42+
<coupon_codes>20</coupon_codes> <!-- Number of coupon codes -->
4243

4344
<product_attribute_sets>100</product_attribute_sets> <!-- Number of product attribute sets -->
4445
<product_attribute_sets_attributes>30</product_attribute_sets_attributes> <!-- Number of attributes per set -->

setup/performance-toolkit/profiles/ce/large.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<catalog_price_rules>20</catalog_price_rules> <!-- Number of catalog price rules -->
4040
<cart_price_rules>20</cart_price_rules> <!-- Number of cart price rules -->
4141
<cart_price_rules_floor>2</cart_price_rules_floor>
42+
<coupon_codes>20</coupon_codes> <!-- Number of coupon codes -->
4243

4344
<product_attribute_sets>50</product_attribute_sets> <!-- Number of product attribute sets -->
4445
<product_attribute_sets_attributes>20</product_attribute_sets_attributes> <!-- Number of attributes per set -->

setup/performance-toolkit/profiles/ce/medium.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<catalog_price_rules>20</catalog_price_rules> <!-- Number of catalog price rules -->
4040
<cart_price_rules>20</cart_price_rules> <!-- Number of cart price rules -->
4141
<cart_price_rules_floor>2</cart_price_rules_floor>
42+
<coupon_codes>20</coupon_codes> <!-- Number of coupon codes -->
4243

4344
<product_attribute_sets>30</product_attribute_sets> <!-- Number of product attribute sets -->
4445
<product_attribute_sets_attributes>10</product_attribute_sets_attributes> <!-- Number of attributes per set -->

setup/performance-toolkit/profiles/ce/medium_msite.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<catalog_price_rules>20</catalog_price_rules> <!-- Number of catalog price rules -->
4646
<cart_price_rules>20</cart_price_rules> <!-- Number of cart price rules -->
4747
<cart_price_rules_floor>2</cart_price_rules_floor>
48+
<coupon_codes>20</coupon_codes> <!-- Number of coupon codes -->
4849

4950
<product_attribute_sets>30</product_attribute_sets> <!-- Number of product attribute sets -->
5051
<product_attribute_sets_attributes>10</product_attribute_sets_attributes> <!-- Number of attributes per set -->

setup/performance-toolkit/profiles/ce/small.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<catalog_price_rules>20</catalog_price_rules> <!-- Number of catalog price rules -->
4040
<cart_price_rules>20</cart_price_rules> <!-- Number of cart price rules -->
4141
<cart_price_rules_floor>2</cart_price_rules_floor>
42+
<coupon_codes>20</coupon_codes> <!-- Number of coupon codes -->
4243

4344
<product_attribute_sets>10</product_attribute_sets> <!-- Number of product attribute sets -->
4445
<product_attribute_sets_attributes>5</product_attribute_sets_attributes> <!-- Number of attributes per set -->
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Setup\Fixtures;
8+
9+
/**
10+
* Fixture for generating coupon codes
11+
*
12+
* Support the following format:
13+
* <!-- Number of coupon codes -->
14+
* <coupon_codes>{int}</coupon_codes>
15+
*
16+
* @see setup/performance-toolkit/profiles/ce/small.xml
17+
*/
18+
class CouponCodesFixture extends Fixture
19+
{
20+
/**
21+
* @var int
22+
*/
23+
protected $priority = 130;
24+
25+
/**
26+
* @var int
27+
*/
28+
protected $couponCodesCount = 0;
29+
30+
/**
31+
* @var \Magento\SalesRule\Model\RuleFactory
32+
*/
33+
private $ruleFactory;
34+
35+
/**
36+
* @var \Magento\SalesRule\Model\CouponFactory
37+
*/
38+
private $couponCodeFactory;
39+
40+
/**
41+
* Constructor
42+
*
43+
* @param FixtureModel $fixtureModel
44+
* @param \Magento\SalesRule\Model\RuleFactory|null $ruleFactory
45+
* @param \Magento\SalesRule\Model\CouponFactory|null $couponCodeFactory
46+
*/
47+
public function __construct(
48+
FixtureModel $fixtureModel,
49+
\Magento\SalesRule\Model\RuleFactory $ruleFactory = null,
50+
\Magento\SalesRule\Model\CouponFactory $couponCodeFactory = null
51+
) {
52+
parent::__construct($fixtureModel);
53+
$this->ruleFactory = $ruleFactory ?: $this->fixtureModel->getObjectManager()
54+
->get(\Magento\SalesRule\Model\RuleFactory::class);
55+
$this->couponCodeFactory = $couponCodeFactory ?: $this->fixtureModel->getObjectManager()
56+
->get(\Magento\SalesRule\Model\CouponFactory::class);
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
* @SuppressWarnings(PHPMD)
62+
*/
63+
public function execute()
64+
{
65+
$this->fixtureModel->resetObjectManager();
66+
$this->couponCodesCount = $this->fixtureModel->getValue('coupon_codes', 0);
67+
if (!$this->couponCodesCount) {
68+
return;
69+
}
70+
71+
/** @var \Magento\Store\Model\StoreManager $storeManager */
72+
$storeManager = $this->fixtureModel->getObjectManager()->create(\Magento\Store\Model\StoreManager::class);
73+
/** @var $category \Magento\Catalog\Model\Category */
74+
$category = $this->fixtureModel->getObjectManager()->get(\Magento\Catalog\Model\Category::class);
75+
76+
//Get all websites
77+
$categoriesArray = [];
78+
$websites = $storeManager->getWebsites();
79+
foreach ($websites as $website) {
80+
//Get all groups
81+
$websiteGroups = $website->getGroups();
82+
foreach ($websiteGroups as $websiteGroup) {
83+
$websiteGroupRootCategory = $websiteGroup->getRootCategoryId();
84+
$category->load($websiteGroupRootCategory);
85+
$categoryResource = $category->getResource();
86+
//Get all categories
87+
$resultsCategories = $categoryResource->getAllChildren($category);
88+
foreach ($resultsCategories as $resultsCategory) {
89+
$category->load($resultsCategory);
90+
$structure = explode('/', $category->getPath());
91+
if (count($structure) > 2) {
92+
$categoriesArray[] = [$category->getId(), $website->getId()];
93+
}
94+
}
95+
}
96+
}
97+
asort($categoriesArray);
98+
$categoriesArray = array_values($categoriesArray);
99+
100+
$this->generateCouponCodes($this->ruleFactory, $this->couponCodeFactory, $categoriesArray);
101+
}
102+
103+
/**
104+
* @param \Magento\SalesRule\Model\RuleFactory $ruleFactory
105+
* @param \Magento\SalesRule\Model\CouponFactory $couponCodeFactory
106+
* @param array $categoriesArray
107+
* @return void
108+
*/
109+
public function generateCouponCodes($ruleFactory, $couponCodeFactory, $categoriesArray)
110+
{
111+
for ($i = 0; $i < $this->couponCodesCount; $i++) {
112+
$ruleName = sprintf('Coupon Code %1$d', $i);
113+
$data = [
114+
'rule_id' => null,
115+
'name' => $ruleName,
116+
'is_active' => '1',
117+
'website_ids' => $categoriesArray[$i % count($categoriesArray)][1],
118+
'customer_group_ids' => [
119+
0 => '0',
120+
1 => '1',
121+
2 => '2',
122+
3 => '3',
123+
],
124+
'coupon_type' => \Magento\SalesRule\Model\Rule::COUPON_TYPE_SPECIFIC,
125+
'conditions' => [],
126+
'simple_action' => \Magento\SalesRule\Model\Rule::BY_PERCENT_ACTION,
127+
'discount_amount' => 5,
128+
'discount_step' => 0,
129+
'stop_rules_processing' => 1,
130+
];
131+
132+
$model = $ruleFactory->create();
133+
$model->loadPost($data);
134+
$useAutoGeneration = (int)!empty($data['use_auto_generation']);
135+
$model->setUseAutoGeneration($useAutoGeneration);
136+
$model->save();
137+
138+
$coupon = $couponCodeFactory->create();
139+
$coupon->setRuleId($model->getId())
140+
->setCode('CouponCode' . $i)
141+
->setIsPrimary(true)
142+
->setType(0);
143+
$coupon->save();
144+
}
145+
}
146+
147+
/**
148+
* {@inheritdoc}
149+
*/
150+
public function getActionTitle()
151+
{
152+
return 'Generating coupon codes';
153+
}
154+
155+
/**
156+
* {@inheritdoc}
157+
*/
158+
public function introduceParamLabels()
159+
{
160+
return [
161+
'coupon_codes' => 'Coupon Codes'
162+
];
163+
}
164+
}

0 commit comments

Comments
 (0)