Skip to content

Commit f836c69

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-75530-2.3-develop' into PANDA-PR-2.3
2 parents 3cdd4f6 + 35aba5c commit f836c69

File tree

3 files changed

+125
-16
lines changed

3 files changed

+125
-16
lines changed

app/code/Magento/Sales/Model/ResourceModel/Grid.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ class Grid extends AbstractGrid
4545
*/
4646
private $notSyncedDataProvider;
4747

48+
/**
49+
* Order grid rows batch size
50+
*/
51+
const BATCH_SIZE = 100;
52+
4853
/**
4954
* @param Context $context
5055
* @param string $mainTableName
@@ -104,25 +109,20 @@ public function refresh($value, $field = null)
104109
*
105110
* Only orders created/updated since the last method call will be added.
106111
*
107-
* @return \Zend_Db_Statement_Interface
112+
* @return void
108113
*/
109114
public function refreshBySchedule()
110115
{
111-
$select = $this->getGridOriginSelect()
112-
->where(
113-
$this->mainTableName . '.entity_id IN (?)',
114-
$this->notSyncedDataProvider->getIds($this->mainTableName, $this->gridTableName)
116+
$notSyncedIds = $this->notSyncedDataProvider->getIds($this->mainTableName, $this->gridTableName);
117+
foreach (array_chunk($notSyncedIds, self::BATCH_SIZE) as $bunch) {
118+
$select = $this->getGridOriginSelect()->where($this->mainTableName . '.entity_id IN (?)', $bunch);
119+
$fetchResult = $this->getConnection()->fetchAll($select);
120+
$this->getConnection()->insertOnDuplicate(
121+
$this->getTable($this->gridTableName),
122+
$fetchResult,
123+
array_keys($this->columns)
115124
);
116-
117-
return $this->getConnection()->query(
118-
$this->getConnection()
119-
->insertFromSelect(
120-
$select,
121-
$this->getTable($this->gridTableName),
122-
array_keys($this->columns),
123-
AdapterInterface::INSERT_ON_DUPLICATE
124-
)
125-
);
125+
}
126126
}
127127

128128
/**

app/code/Magento/Sales/Model/ResourceModel/GridInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function refresh($value, $field = null);
2929
*
3030
* Only rows created/updated since the last method call should be added.
3131
*
32-
* @return \Zend_Db_Statement_Interface
32+
* @return void
3333
*/
3434
public function refreshBySchedule();
3535

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Test\Unit\Model\ResourceModel;
9+
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Sales\Model\ResourceModel\Provider\NotSyncedDataProviderInterface;
12+
use Magento\Framework\DB\Adapter\AdapterInterface as ConnectionAdapterInterface;
13+
use Magento\Sales\Model\ResourceModel\Grid;
14+
15+
/**
16+
* Unit tests for \Magento\Sales\Model\ResourceModel\Grid class
17+
*/
18+
class GridTest extends \PHPUnit\Framework\TestCase
19+
{
20+
/**
21+
* @var Grid
22+
*/
23+
private $grid;
24+
25+
/**
26+
* @var NotSyncedDataProviderInterface|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $notSyncedDataProvider;
29+
30+
/**
31+
* @var ConnectionAdapterInterface|\PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
private $connection;
34+
35+
/**
36+
* @var string
37+
*/
38+
private $mainTable = 'sales_order';
39+
40+
/**
41+
* @var string
42+
*/
43+
private $gridTable = 'sales_order_grid';
44+
45+
/**
46+
* @var array
47+
*/
48+
private $columns = [
49+
'column_1_key' => 'column_1_value',
50+
'column_2_key' => 'column_2_value'
51+
];
52+
53+
/**
54+
* @inheritdoc
55+
*/
56+
protected function setUp()
57+
{
58+
$objectManager = new ObjectManager($this);
59+
$this->notSyncedDataProvider = $this->getMockBuilder(NotSyncedDataProviderInterface::class)
60+
->disableOriginalConstructor()
61+
->setMethods(['getIds'])
62+
->getMockForAbstractClass();
63+
$this->connection = $this->getMockBuilder(ConnectionAdapterInterface::class)
64+
->disableOriginalConstructor()
65+
->setMethods(['select', 'fetchAll', 'insertOnDuplicate'])
66+
->getMockForAbstractClass();
67+
68+
$this->grid = $objectManager->getObject(
69+
\Magento\Sales\Model\ResourceModel\Grid::class,
70+
[
71+
'notSyncedDataProvider' => $this->notSyncedDataProvider,
72+
'mainTableName' => $this->mainTable,
73+
'gridTableName' => $this->gridTable,
74+
'connection' => $this->connection,
75+
'_tables' => ['sales_order' => $this->mainTable, 'sales_order_grid' => $this->gridTable],
76+
'columns' => $this->columns
77+
]
78+
);
79+
}
80+
81+
/**
82+
* Test for refreshBySchedule() method
83+
*/
84+
public function testRefreshBySchedule()
85+
{
86+
$notSyncedIds = ['1', '2', '3'];
87+
$fetchResult = ['column_1' => '1', 'column_2' => '2'];
88+
89+
$this->notSyncedDataProvider->expects($this->atLeastOnce())->method('getIds')->willReturn($notSyncedIds);
90+
$select = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
91+
->disableOriginalConstructor()
92+
->setMethods(['from', 'columns', 'where'])
93+
->getMock();
94+
$select->expects($this->atLeastOnce())->method('from')->with(['sales_order' => $this->mainTable], [])
95+
->willReturnSelf();
96+
$select->expects($this->atLeastOnce())->method('columns')->willReturnSelf();
97+
$select->expects($this->atLeastOnce())->method('where')
98+
->with($this->mainTable . '.entity_id IN (?)', $notSyncedIds)
99+
->willReturnSelf();
100+
101+
$this->connection->expects($this->atLeastOnce())->method('select')->willReturn($select);
102+
$this->connection->expects($this->atLeastOnce())->method('fetchAll')->with($select)->willReturn($fetchResult);
103+
$this->connection->expects($this->atLeastOnce())->method('insertOnDuplicate')
104+
->with($this->gridTable, $fetchResult, array_keys($this->columns))
105+
->willReturn(array_count_values($notSyncedIds));
106+
107+
$this->grid->refreshBySchedule();
108+
}
109+
}

0 commit comments

Comments
 (0)