Skip to content

Commit 5789047

Browse files
committed
ACP2E-72: Search results suggestion give total count of 0 even though there are products
- Added the test coverage and fixed the static test failure.
1 parent 8d904e5 commit 5789047

File tree

2 files changed

+131
-5
lines changed

2 files changed

+131
-5
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@
1717
class SearchSuggestionResultsCount
1818
{
1919
/**
20-
* Query collection factory
21-
*
2220
* @var QueryCollectionFactory
2321
*/
2422
protected QueryCollectionFactory $_queryCollectionFactory;
2523

2624
/**
27-
* Store manager
28-
*
2925
* @var StoreManagerInterface
3026
*/
3127
protected StoreManagerInterface $_storeManager;
@@ -47,6 +43,9 @@ public function __construct(
4743
/**
4844
* Get the search suggestion results count.
4945
*
46+
* @param QueryResult $subject
47+
* @param array $result
48+
* @return array
5049
* @throws NoSuchEntityException
5150
*/
5251
public function afterGetResultsCount(QueryResult $subject, $result)
@@ -57,7 +56,7 @@ public function afterGetResultsCount(QueryResult $subject, $result)
5756
$subject->getQueryText()
5857
);
5958
foreach ($collection as $item) {
60-
$result = $item->getNumResults();
59+
$result = $item->getData('num_results');
6160
}
6261
return $result;
6362
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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\Search\Test\Unit\Plugin\Model;
9+
10+
use Magento\Search\Model\QueryResult;
11+
use Magento\Search\Model\ResourceModel\Query\Collection;
12+
use Magento\Search\Model\ResourceModel\Query\CollectionFactory as QueryCollectionFactory;
13+
use Magento\Search\Plugin\Model\SearchSuggestionResultsCount;
14+
use Magento\Store\Api\Data\StoreInterface;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\Framework\Data\Collection\AbstractDb;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class SearchSuggestionResultsCountTest extends TestCase
20+
{
21+
/**
22+
* @var SearchSuggestionResultsCount
23+
*/
24+
private SearchSuggestionResultsCount $model;
25+
26+
/**
27+
* @var QueryCollectionFactory
28+
*/
29+
private QueryCollectionFactory $queryCollectionFactoryMock;
30+
31+
/**
32+
* @var StoreManagerInterface
33+
*/
34+
private StoreManagerInterface $storeManagerMock;
35+
36+
/**
37+
* @var QueryResult
38+
*/
39+
private QueryResult $queryResultMock;
40+
41+
/**
42+
* @var Collection
43+
*/
44+
private Collection $queryCollectionMock;
45+
46+
/**
47+
* @var StoreInterface
48+
*/
49+
private StoreInterface $storeInterfaceMock;
50+
51+
/**
52+
* @var AbstractDb
53+
*/
54+
private AbstractDb $abstractDbMock;
55+
56+
/**
57+
* @return void
58+
*/
59+
protected function setUp(): void
60+
{
61+
$this->queryCollectionFactoryMock = $this->getMockBuilder(QueryCollectionFactory::class)
62+
->disableOriginalConstructor()
63+
->onlyMethods(['create'])
64+
->getMock();
65+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
66+
->disableOriginalConstructor()
67+
->getMock();
68+
$this->queryResultMock = $this->getMockBuilder(QueryResult::class)
69+
->disableOriginalConstructor()
70+
->getMock();
71+
$this->queryCollectionMock = $this->getMockBuilder(Collection::class)
72+
->disableOriginalConstructor()
73+
->getMock();
74+
$this->storeInterfaceMock = $this->getMockBuilder(StoreInterface::class)
75+
->disableOriginalConstructor()
76+
->onlyMethods(['getId'])
77+
->getMockForAbstractClass();
78+
$this->abstractDbMock = $this->getMockBuilder(AbstractDb::class)
79+
->disableOriginalConstructor()
80+
->onlyMethods(['getData'])
81+
->getMockForAbstractClass();
82+
$this->model = new SearchSuggestionResultsCount(
83+
$this->queryCollectionFactoryMock,
84+
$this->storeManagerMock
85+
);
86+
}
87+
88+
/**
89+
* @return void
90+
* @throws \Magento\Framework\Exception\NoSuchEntityException
91+
*/
92+
public function testAfterGetResultsCount()
93+
{
94+
$expectedArr = ['num_results' => 5];
95+
$this->queryCollectionFactoryMock->expects($this->once())
96+
->method('create')
97+
->willReturn($this->queryCollectionMock);
98+
$this->storeManagerMock->expects($this->once())
99+
->method('getStore')
100+
->willReturn($this->storeInterfaceMock);
101+
$this->storeInterfaceMock->expects($this->once())
102+
->method('getId')
103+
->willReturn(1);
104+
$this->queryCollectionMock->expects($this->once())
105+
->method('setStoreId')
106+
->willReturnSelf();
107+
$this->queryResultMock->expects($this->once())
108+
->method('getQueryText')
109+
->willReturn('mint');
110+
$this->queryCollectionMock->expects($this->once())
111+
->method('setQueryFilter')
112+
->willReturn($this->queryCollectionMock);
113+
$this->abstractDbMock->expects($this->once())
114+
->method('getData')
115+
->willReturn(['num_results' => 5]);
116+
$this->queryCollectionMock->expects($this->once())
117+
->method('getIterator')
118+
->willReturn(new \ArrayIterator([$this->abstractDbMock]));
119+
$this->assertEquals(
120+
$expectedArr,
121+
$this->model->afterGetResultsCount(
122+
$this->queryResultMock,
123+
0
124+
)
125+
);
126+
}
127+
}

0 commit comments

Comments
 (0)