Skip to content

Commit c219919

Browse files
committed
MAGETWO-70163: Add quote generaion ability for fixtures generation tool
- Fix issues in quote generator - Covered code with unit tests - Static tests fixes - Code style issues are fixed - Fix integration tests fail - Fix unit tests fails
1 parent 72949d9 commit c219919

File tree

5 files changed

+491
-68
lines changed

5 files changed

+491
-68
lines changed

setup/src/Magento/Setup/Fixtures/Quote/QuoteConfiguration.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,24 @@ public function load()
115115
'fixture_data_filename',
116116
dirname(__DIR__) . DIRECTORY_SEPARATOR . "_files" . DIRECTORY_SEPARATOR . $this->fixtureDataFilename
117117
);
118+
$this->accumulateData();
118119

119-
\Magento\Framework\DataObject\Mapper::accumulateByMap(
120-
[$this->fixtureModel, 'getValue'],
121-
[$this, 'setNotEmptyData'],
122-
$this->_globalMap
123-
);
124120
return $this;
125121
}
126122

127123
/**
128-
* @param string|array $key
129-
* @param mixed $value
124+
* Accumulate data from fixute model to object values.
125+
*
130126
* @return $this
131127
*/
132-
public function setNotEmptyData($key, $value)
128+
private function accumulateData()
133129
{
134-
if (null === $value) {
135-
return $this;
130+
foreach ($this->_globalMap as $getKey => $setKey) {
131+
$value = $this->fixtureModel->getValue($getKey);
132+
if (null !== $value) {
133+
$this->setData($setKey, $value);
134+
}
136135
}
137-
return $this->setData($key, $value);
136+
return $this;
138137
}
139138
}

setup/src/Magento/Setup/Fixtures/Quote/QuoteGenerator.php

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function __construct(
113113
}
114114

115115
/**
116-
* Prepare and save quotes in database
116+
* Prepare and save quotes in database.
117117
*
118118
* @throws \Exception
119119
* @return void
@@ -135,13 +135,12 @@ public function generateQuotes()
135135
$maxItemsPerOrder
136136
);
137137
$this->productStubData = $this->prepareProductsForQuote();
138-
139138
$this->prepareQueryTemplates();
140139

141140
$entityId = $this->getMaxEntityId('quote', \Magento\Quote\Model\ResourceModel\Quote::class, 'entity_id');
142141
$quoteQty = $this->config->getExistsQuoteQuantity();
143142
$batchNumber = 0;
144-
while ($quoteQty <= $this->config->getRequiredQuoteQuantity()) {
143+
while ($quoteQty < $this->config->getRequiredQuoteQuantity()) {
145144
$entityId++;
146145
$batchNumber++;
147146
$quoteQty++;
@@ -171,7 +170,7 @@ public function generateQuotes()
171170
}
172171

173172
/**
174-
* Save quote and quote items
173+
* Save quote and quote items.
175174
*
176175
* @param int $entityId
177176
* @param \Generator $itemIdSequence
@@ -215,30 +214,18 @@ private function saveQuoteWithQuoteItems($entityId, \Generator $itemIdSequence)
215214
$itemIdSequence->next();
216215
}
217216

218-
$type = Configurable::TYPE_CODE;
219-
for ($i = 0; $i < $productCount[$type]; $i++) {
220-
// Generate parent item
221-
$parentItemId = $itemIdSequence->current();
222-
$this->saveParentItemConfigurableData($entityId, $i, $parentItemId, Configurable::TYPE_CODE, $quote);
223-
$itemIdSequence->next();
224-
225-
// Generate child item
226-
$itemId = $itemIdSequence->current();
227-
$this->saveChildItemConfigurable($entityId, $i, $itemId, $parentItemId, Configurable::TYPE_CODE, $quote);
228-
$itemIdSequence->next();
229-
}
230-
231-
$type = QuoteConfiguration::BIG_CONFIGURABLE_TYPE;
232-
for ($i = 0; $i < $productCount[$type]; $i++) {
233-
// Generate parent item
234-
$parentItemId = $itemIdSequence->current();
235-
$this->saveParentItemConfigurableData($entityId, $i, $parentItemId, $type, $quote);
236-
$itemIdSequence->next();
237-
238-
// Generate child item
239-
$itemId = $itemIdSequence->current();
240-
$this->saveChildItemConfigurable($entityId, $i, $itemId, $parentItemId, $type, $quote);
241-
$itemIdSequence->next();
217+
foreach ([Configurable::TYPE_CODE, QuoteConfiguration::BIG_CONFIGURABLE_TYPE] as $type) {
218+
for ($i = 0; $i < $productCount[$type]; $i++) {
219+
// Generate parent item
220+
$parentItemId = $itemIdSequence->current();
221+
$this->saveParentItemConfigurableData($entityId, $i, $parentItemId, $type, $quote);
222+
$itemIdSequence->next();
223+
224+
// Generate child item
225+
$itemId = $itemIdSequence->current();
226+
$this->saveChildItemConfigurable($entityId, $i, $itemId, $parentItemId, $type, $quote);
227+
$itemIdSequence->next();
228+
}
242229
}
243230
}
244231

@@ -351,23 +338,23 @@ private function saveChildItemConfigurable($entityId, $index, $itemId, $parentIt
351338
/**
352339
* Get store id for quote item by product index.
353340
*
354-
* @param int $index
341+
* @param int $entityId
355342
* @return int
356343
*/
357-
private function getStubProductStoreId($index)
344+
private function getStubProductStoreId($entityId)
358345
{
359-
return $this->productStubData[$index % count($this->productStubData)][0];
346+
return $this->productStubData[$this->getProductStubIndex($entityId)][0];
360347
}
361348

362349
/**
363350
* Get store name for quote item by product index.
364351
*
365-
* @param int $index
352+
* @param int $entityId
366353
* @return string
367354
*/
368-
private function getStubProductStoreName($index)
355+
private function getStubProductStoreName($entityId)
369356
{
370-
return $this->productStubData[$index % count($this->productStubData)][1];
357+
return $this->productStubData[$this->getProductStubIndex($entityId)][1];
371358
}
372359

373360
/**
@@ -380,7 +367,7 @@ private function getStubProductStoreName($index)
380367
*/
381368
private function getStubProductId($entityId, $index, $type)
382369
{
383-
return $this->productStubData[$entityId % count($this->productStubData)][2][$type][$index]['id'];
370+
return $this->productStubData[$this->getProductStubIndex($entityId)][2][$type][$index]['id'];
384371
}
385372

386373
/**
@@ -393,7 +380,7 @@ private function getStubProductId($entityId, $index, $type)
393380
*/
394381
private function getStubProductSku($entityId, $index, $type)
395382
{
396-
return $this->productStubData[$entityId % count($this->productStubData)][2][$type][$index]['sku'];
383+
return $this->productStubData[$this->getProductStubIndex($entityId)][2][$type][$index]['sku'];
397384
}
398385

399386
/**
@@ -406,7 +393,7 @@ private function getStubProductSku($entityId, $index, $type)
406393
*/
407394
private function getStubProductName($entityId, $index, $type)
408395
{
409-
return $this->productStubData[$entityId % count($this->productStubData)][2][$type][$index]['name'];
396+
return $this->productStubData[$this->getProductStubIndex($entityId)][2][$type][$index]['name'];
410397
}
411398

412399
/**
@@ -419,7 +406,7 @@ private function getStubProductName($entityId, $index, $type)
419406
*/
420407
private function getStubProductBuyRequest($entityId, $index, $type)
421408
{
422-
return $this->productStubData[$entityId % count($this->productStubData)][2][$type][$index]['buyRequest'];
409+
return $this->productStubData[$this->getProductStubIndex($entityId)][2][$type][$index]['buyRequest'];
423410
}
424411

425412
/**
@@ -432,7 +419,7 @@ private function getStubProductBuyRequest($entityId, $index, $type)
432419
*/
433420
private function getStubProductChildBuyRequest($entityId, $index, $type)
434421
{
435-
return $this->productStubData[$entityId % count($this->productStubData)][2][$type][$index]['childBuyRequest'];
422+
return $this->productStubData[$this->getProductStubIndex($entityId)][2][$type][$index]['childBuyRequest'];
436423
}
437424

438425
/**
@@ -445,7 +432,20 @@ private function getStubProductChildBuyRequest($entityId, $index, $type)
445432
*/
446433
private function getStubProductChildId($entityId, $index, $type)
447434
{
448-
return $this->productStubData[$entityId % count($this->productStubData)][2][$type][$index]['childId'];
435+
return $this->productStubData[$this->getProductStubIndex($entityId)][2][$type][$index]['childId'];
436+
}
437+
438+
/**
439+
* Get index of item in product stub array.
440+
*
441+
* @param int $entityId
442+
* @return int
443+
*/
444+
private function getProductStubIndex($entityId)
445+
{
446+
$storeCount = count($this->productStubData);
447+
$qty = intdiv($this->config->getRequiredQuoteQuantity(), $storeCount);
448+
return intdiv($entityId, $qty) % $storeCount;
449449
}
450450

451451
/**
@@ -476,6 +476,7 @@ private function getAddressDataFixture()
476476
private function prepareProductsForQuote()
477477
{
478478
$result = [];
479+
479480
foreach ($this->storeManager->getStores() as $store) {
480481
$productsResult = [];
481482
$this->storeManager->setCurrentStore($store->getId());
@@ -485,23 +486,21 @@ private function prepareProductsForQuote()
485486
$this->getProductIds($store, Type::TYPE_SIMPLE, $this->config->getSimpleCountTo())
486487
);
487488
}
488-
if ($this->config->getConfigurableCountTo() > 0) {
489-
$productsResult[Configurable::TYPE_CODE] = $this->prepareConfigurableProducts(
490-
$this->getProductIds(
491-
$store,
492-
Configurable::TYPE_CODE,
493-
$this->config->getConfigurableCountTo()
494-
)
495-
);
496-
}
497-
if ($this->config->getBigConfigurableCountTo() > 0) {
498-
$productsResult[QuoteConfiguration::BIG_CONFIGURABLE_TYPE] = $this->prepareConfigurableProducts(
499-
$this->getProductIds(
500-
$store,
501-
QuoteConfiguration::BIG_CONFIGURABLE_TYPE,
502-
$this->config->getBigConfigurableCountTo()
503-
)
504-
);
489+
$configurables = [
490+
Configurable::TYPE_CODE => $this->config->getConfigurableCountTo(),
491+
QuoteConfiguration::BIG_CONFIGURABLE_TYPE => $this->config->getBigConfigurableCountTo(),
492+
];
493+
494+
foreach ($configurables as $type => $qty) {
495+
if ($qty > 0) {
496+
$productsResult[$type] = $this->prepareConfigurableProducts(
497+
$this->getProductIds(
498+
$store,
499+
$type,
500+
$qty
501+
)
502+
);
503+
}
505504
}
506505

507506
$result[] = [
@@ -514,6 +513,7 @@ private function prepareProductsForQuote()
514513
$productsResult
515514
];
516515
}
516+
517517
return $result;
518518
}
519519

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Setup\Test\Unit\Fixtures\Quote;
8+
9+
/**
10+
* Test for Magento\Setup\Fixtures\Quote\QuoteConfiguration class.
11+
*/
12+
class QuoteConfigurationTest extends \PHPUnit\Framework\TestCase
13+
{
14+
/**
15+
* @var \Magento\Setup\Fixtures\FixtureModel|\PHPUnit_Framework_MockObject_MockObject
16+
*/
17+
private $fixtureModelMock;
18+
19+
/**
20+
* @var \Magento\Setup\Fixtures\Quote\QuoteConfiguration
21+
*/
22+
private $fixture;
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
public function setUp()
28+
{
29+
$this->fixtureModelMock = $this->getMockBuilder(\Magento\Setup\Fixtures\FixtureModel::class)
30+
->disableOriginalConstructor()
31+
->getMock();
32+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
33+
34+
$this->fixture = $objectManager->getObject(
35+
\Magento\Setup\Fixtures\Quote\QuoteConfiguration::class,
36+
[
37+
'fixtureModel' => $this->fixtureModelMock
38+
]
39+
);
40+
}
41+
42+
/**
43+
* Test load method.
44+
*
45+
* @return void
46+
*/
47+
public function testLoad()
48+
{
49+
$dir = str_replace('Test/Unit/', '', dirname(__DIR__));
50+
$expectedResult = [
51+
'simple_count_to' => 1,
52+
'simple_count_from' => 1,
53+
'configurable_count_to' => 1,
54+
'configurable_count_from' => 1,
55+
'big_configurable_count_to' => 1,
56+
'big_configurable_count_from' => 1,
57+
'fixture_data_filename' =>
58+
$dir . DIRECTORY_SEPARATOR . "_files" . DIRECTORY_SEPARATOR . 'orders_fixture_data.json',
59+
'order_quotes_enable' => 1,
60+
];
61+
$this->fixtureModelMock->expects($this->atLeastOnce())
62+
->method('getValue')
63+
->withConsecutive(
64+
['order_simple_product_count_to'],
65+
['order_simple_product_count_from'],
66+
['order_configurable_product_count_to'],
67+
['order_configurable_product_count_from'],
68+
['order_big_configurable_product_count_to'],
69+
['order_big_configurable_product_count_from'],
70+
['order_quotes_enable',]
71+
)->willReturn(1);
72+
$this->assertSame($expectedResult, $this->fixture->load()->getData());
73+
}
74+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Setup\Test\Unit\Fixtures\Quote;
8+
9+
/**
10+
* Test for Magento\Setup\Fixtures\Quote\QuoteGeneratorFactory class.
11+
*/
12+
class QuoteGeneratorFactoryTest extends \PHPUnit\Framework\TestCase
13+
{
14+
/**
15+
* @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
16+
*/
17+
private $objectManager;
18+
19+
/**
20+
* @var \Magento\Setup\Fixtures\Quote\QuoteGeneratorFactory
21+
*/
22+
private $fixture;
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
public function setUp()
28+
{
29+
$this->objectManager = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
30+
->disableOriginalConstructor()
31+
->getMockForAbstractClass();
32+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
33+
34+
$this->fixture = $objectManager->getObject(
35+
\Magento\Setup\Fixtures\Quote\QuoteGeneratorFactory::class,
36+
[
37+
'objectManager' => $this->objectManager,
38+
'instanceName' => \Magento\Setup\Fixtures\Quote\QuoteGenerator::class,
39+
]
40+
);
41+
}
42+
43+
/**
44+
* Test create method.
45+
*
46+
* @return void
47+
*/
48+
public function testCreate()
49+
{
50+
$result = $this->getMockBuilder(\Magento\Setup\Fixtures\Quote\QuoteGenerator::class)
51+
->disableOriginalConstructor()
52+
->getMock();
53+
$this->objectManager->expects($this->once())
54+
->method('create')
55+
->with(\Magento\Setup\Fixtures\Quote\QuoteGenerator::class, [])
56+
->willReturn($result);
57+
58+
$this->assertSame($result, $this->fixture->create([]));
59+
}
60+
}

0 commit comments

Comments
 (0)