Skip to content

Commit ca221b9

Browse files
ENGCOM-4481: Resolve Issue : Search REST API returns wrong total_count #21713
- Merge Pull Request #21713 from ronak2ram/magento2:fixed-issue-17295 - Merged commits: 1. 7c55811 2. a546e14 3. 9dc26cc
2 parents bcb91dd + 9dc26cc commit ca221b9

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/internal/Magento/Framework/Search/Adapter/Mysql/Adapter.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Framework\App\ResourceConnection;
99
use Magento\Framework\DB\Ddl\Table;
10+
use Magento\Framework\DB\Select;
1011
use Magento\Framework\Search\Adapter\Mysql\Aggregation\Builder as AggregationBuilder;
1112
use Magento\Framework\Search\AdapterInterface;
1213
use Magento\Framework\Search\RequestInterface;
@@ -129,32 +130,35 @@ private function getConnection()
129130
/**
130131
* Get rows size
131132
*
133+
* @param Select $query
132134
* @return int
133135
*/
134-
private function getSize($query)
136+
private function getSize(Select $query): int
135137
{
136138
$sql = $this->getSelectCountSql($query);
137139
$parentSelect = $this->getConnection()->select();
138140
$parentSelect->from(['core_select' => $sql]);
139141
$parentSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
140142
$parentSelect->columns('COUNT(*)');
141143
$totalRecords = $this->getConnection()->fetchOne($parentSelect);
144+
142145
return intval($totalRecords);
143146
}
144147

145148
/**
146149
* Reset limit and offset
147150
*
151+
* @param Select $query
148152
* @return Select
149153
*/
150-
private function getSelectCountSql($query)
154+
private function getSelectCountSql(Select $query): Select
151155
{
152156
foreach ($this->countSqlSkipParts as $part => $toSkip) {
153157
if ($toSkip) {
154158
$query->reset($part);
155159
}
156160
}
161+
157162
return $query;
158163
}
159-
160164
}

lib/internal/Magento/Framework/Search/Test/Unit/Adapter/Mysql/AdapterTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,16 @@ public function testQuery()
161161
$select = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
162162
->disableOriginalConstructor()
163163
->getMock();
164-
$this->connectionAdapter->expects($this->once())
164+
165+
$this->connectionAdapter->expects($this->exactly(2))
165166
->method('select')
166167
->willReturn($select);
167168

169+
$this->connectionAdapter->expects($this->once())
170+
->method('fetchOne')
171+
->with($select)
172+
->willReturn($selectResult['total']);
173+
168174
$table = $this->getMockBuilder(\Magento\Framework\DB\Ddl\Table::class)
169175
->disableOriginalConstructor()
170176
->getMock();

0 commit comments

Comments
 (0)