Skip to content

Commit e264e9b

Browse files
committed
ACP2E-72: Search results suggestion give total count of 0 even though there are products
- Fixed the CR Comments.
1 parent 5789047 commit e264e9b

File tree

5 files changed

+145
-216
lines changed

5 files changed

+145
-216
lines changed

app/code/Magento/Search/Model/QueryResult.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*/
66
namespace Magento\Search\Model;
77

8+
use Magento\Framework\Exception\NoSuchEntityException;
9+
use Magento\Search\Model\ResourceModel\Query\CollectionFactory as QueryCollectionFactory;
10+
use Magento\Store\Model\StoreManagerInterface;
11+
812
/**
913
* @api
1014
* @since 100.0.2
@@ -14,36 +18,63 @@ class QueryResult
1418
/**
1519
* @var string
1620
*/
17-
private $queryText;
21+
private string $queryText;
1822

1923
/**
2024
* @var int
2125
*/
2226
private $resultsCount;
2327

2428
/**
25-
* @param string $queryText
26-
* @param string $resultsCount
29+
* @var QueryCollectionFactory
2730
*/
28-
public function __construct($queryText, $resultsCount)
29-
{
31+
private QueryCollectionFactory $queryCollectionFactory;
32+
33+
/**
34+
* @var StoreManagerInterface
35+
*/
36+
private StoreManagerInterface $storeManager;
37+
38+
/**
39+
* @param $queryText
40+
* @param $resultsCount
41+
* @param QueryCollectionFactory $queryCollectionFactory
42+
* @param StoreManagerInterface $storeManager
43+
*/
44+
public function __construct(
45+
$queryText,
46+
$resultsCount,
47+
QueryCollectionFactory $queryCollectionFactory,
48+
StoreManagerInterface $storeManager
49+
) {
3050
$this->queryText = $queryText;
3151
$this->resultsCount = $resultsCount;
52+
$this->queryCollectionFactory = $queryCollectionFactory;
53+
$this->storeManager = $storeManager;
3254
}
3355

3456
/**
3557
* @return string
3658
*/
37-
public function getQueryText()
59+
public function getQueryText(): string
3860
{
3961
return $this->queryText;
4062
}
4163

4264
/**
4365
* @return int
66+
* @throws NoSuchEntityException
4467
*/
45-
public function getResultsCount()
68+
public function getResultsCount(): int
4669
{
70+
$collection = $this->queryCollectionFactory->create()->setStoreId(
71+
$this->storeManager->getStore()->getId()
72+
)->setQueryFilter(
73+
$this->getQueryText()
74+
);
75+
foreach ($collection as $item) {
76+
$this->resultsCount = $item->getData('num_results');
77+
}
4778
return $this->resultsCount;
4879
}
4980
}

app/code/Magento/Search/Plugin/Model/SearchSuggestionResultsCount.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

app/code/Magento/Search/Test/Unit/Model/QueryResultTest.php

Lines changed: 107 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,157 @@
77

88
namespace Magento\Search\Test\Unit\Model;
99

10+
use Magento\Framework\Data\Collection\AbstractDb;
1011
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1112
use Magento\Search\Model\QueryResult;
13+
use Magento\Search\Model\ResourceModel\Query\Collection;
14+
use Magento\Search\Model\ResourceModel\Query\CollectionFactory as QueryCollectionFactory;
15+
use Magento\Store\Api\Data\StoreInterface;
16+
use Magento\Store\Model\StoreManagerInterface;
1217
use PHPUnit\Framework\TestCase;
1318

1419
class QueryResultTest extends TestCase
1520
{
21+
/**
22+
* @var QueryResult
23+
*/
24+
private QueryResult $model;
25+
1626
/**
1727
* @var ObjectManager
1828
*/
19-
private $objectManager;
29+
private ObjectManager $objectManager;
30+
31+
/**
32+
* @var QueryCollectionFactory
33+
*/
34+
private QueryCollectionFactory $queryCollectionFactoryMock;
35+
36+
/**
37+
* @var StoreManagerInterface
38+
*/
39+
private StoreManagerInterface $storeManagerMock;
40+
41+
/**
42+
* @var StoreInterface
43+
*/
44+
private StoreInterface $storeInterfaceMock;
45+
46+
/**
47+
* @var Collection
48+
*/
49+
private Collection $queryCollectionMock;
50+
51+
/**
52+
* @var QueryResult
53+
*/
54+
private QueryResult $queryResultMock;
55+
56+
/**
57+
* @var AbstractDb
58+
*/
59+
private AbstractDb $abstractDbMock;
2060

2161
protected function setUp(): void
2262
{
2363
$this->objectManager = new ObjectManager($this);
64+
$this->queryCollectionFactoryMock = $this->getMockBuilder(QueryCollectionFactory::class)
65+
->disableOriginalConstructor()
66+
->onlyMethods(['create'])
67+
->getMock();
68+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
69+
->disableOriginalConstructor()
70+
->getMock();
71+
$this->storeInterfaceMock = $this->getMockBuilder(StoreInterface::class)
72+
->disableOriginalConstructor()
73+
->onlyMethods(['getId'])
74+
->getMockForAbstractClass();
75+
$this->queryCollectionMock = $this->getMockBuilder(Collection::class)
76+
->disableOriginalConstructor()
77+
->getMock();
78+
$this->queryResultMock = $this->getMockBuilder(QueryResult::class)
79+
->disableOriginalConstructor()
80+
->getMock();
81+
$this->abstractDbMock = $this->getMockBuilder(AbstractDb::class)
82+
->disableOriginalConstructor()
83+
->onlyMethods(['getData'])
84+
->getMockForAbstractClass();
85+
$this->model = new QueryResult(
86+
'mint',
87+
'resultsCount',
88+
$this->queryCollectionFactoryMock,
89+
$this->storeManagerMock
90+
);
2491
}
2592

2693
/**
2794
* @dataProvider getPropertiesDataProvider
2895
*/
29-
public function testGetProperties($queryText, $resultsCount)
96+
public function testGetProperties($queryText)
3097
{
3198
/** @var QueryResult $queryResult */
3299
$queryResult = $this->objectManager->getObject(
33100
QueryResult::class,
34101
[
35-
'queryText' => $queryText,
36-
'resultsCount' => $resultsCount,
102+
'queryText' => $queryText
37103
]
38104
);
39105
$this->assertEquals($queryText, $queryResult->getQueryText());
40-
$this->assertEquals($resultsCount, $queryResult->getResultsCount());
41106
}
42107

43108
/**
44109
* Data provider for testGetProperties
45110
* @return array
46111
*/
47-
public function getPropertiesDataProvider()
112+
public function getPropertiesDataProvider(): array
48113
{
49114
return [
50115
[
51-
'queryText' => 'Some kind of query text',
52-
'resultsCount' => 0,
116+
'queryText' => 'Some kind of query text'
53117
],
54118
[
55-
'queryText' => 'Another query',
56-
'resultsCount' => 322312312,
119+
'queryText' => 'Another query'
57120
],
58121
[
59-
'queryText' => 'It\' a query too',
60-
'resultsCount' => -100,
122+
'queryText' => 'It\' a query too'
61123
],
62124
[
63-
'queryText' => '',
64-
'resultsCount' => null,
125+
'queryText' => ''
65126
],
66127
[
67-
'queryText' => 42,
68-
'resultsCount' => false,
128+
'queryText' => 42
69129
],
70130
];
71131
}
132+
133+
/**
134+
* @return void
135+
* @throws \Magento\Framework\Exception\NoSuchEntityException
136+
*/
137+
public function testAfterGetResultsCount()
138+
{
139+
$expectedArr = ['num_results' => 5];
140+
$this->queryCollectionFactoryMock->expects($this->once())
141+
->method('create')
142+
->willReturn($this->queryCollectionMock);
143+
$this->storeManagerMock->expects($this->once())
144+
->method('getStore')
145+
->willReturn($this->storeInterfaceMock);
146+
$this->storeInterfaceMock->expects($this->once())
147+
->method('getId')
148+
->willReturn(1);
149+
$this->queryCollectionMock->expects($this->once())
150+
->method('setStoreId')
151+
->willReturnSelf();
152+
$this->queryCollectionMock->expects($this->once())
153+
->method('setQueryFilter')
154+
->willReturn($this->queryCollectionMock);
155+
$this->abstractDbMock->expects($this->once())
156+
->method('getData')
157+
->willReturn(['num_results' => 5]);
158+
$this->queryCollectionMock->expects($this->once())
159+
->method('getIterator')
160+
->willReturn(new \ArrayIterator([$this->abstractDbMock]));
161+
$this->assertEquals($expectedArr, $this->model->getResultsCount());
162+
}
72163
}

0 commit comments

Comments
 (0)