Skip to content

Commit 1760043

Browse files
committed
cover changes with unit test
1 parent 1e14f00 commit 1760043

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Search\Test\Unit\Block;
7+
8+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9+
use Magento\Framework\Url;
10+
use Magento\Framework\UrlFactory;
11+
use Magento\Framework\View\Element\Template\Context;
12+
use Magento\Search\Block\Term;
13+
use Magento\Search\Model\Query;
14+
use Magento\Search\Model\ResourceModel\Query\Collection;
15+
use Magento\Search\Model\ResourceModel\Query\CollectionFactory;
16+
use Magento\Store\Model\Store;
17+
use Magento\Store\Model\StoreManager;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* Tests for Terms block
23+
*/
24+
class TermsTest extends TestCase
25+
{
26+
/**
27+
* @var Context|MockObject
28+
*/
29+
private $contextMock;
30+
31+
/**
32+
* @var CollectionFactory|MockObject
33+
*/
34+
private $collectionFactoryMock;
35+
36+
/**
37+
* @var UrlFactory|MockObject
38+
*/
39+
private $urlFactoryMock;
40+
41+
/**
42+
* @var Term
43+
*/
44+
private $termsModel;
45+
46+
/**
47+
* @var StoreManager
48+
*/
49+
private $storeManagerMock;
50+
51+
/**
52+
* @inheritdoc
53+
*/
54+
public function setUp()
55+
{
56+
$objectManager = new ObjectManager($this);
57+
58+
$this->contextMock = $this->createMock(Context::class);
59+
$this->collectionFactoryMock = $this->createMock(CollectionFactory::class);
60+
$this->urlFactoryMock = $this->createMock(UrlFactory::class);
61+
$this->storeManagerMock = $this->createMock(StoreManager::class);
62+
63+
$this->contextMock->expects($this->once())
64+
->method('getStoreManager')
65+
->willReturn($this->storeManagerMock);
66+
$this->termsModel = $objectManager->getObject(
67+
Term::class,
68+
[
69+
'context' => $this->contextMock,
70+
'_queryCollectionFactory' => $this->collectionFactoryMock,
71+
'_urlFactory' => $this->urlFactoryMock
72+
]
73+
);
74+
}
75+
76+
/**
77+
* Verify terms
78+
*
79+
* @dataProvider termKeysProvider
80+
* @param string $termKey
81+
* @param bool $popularity
82+
*/
83+
public function testGetTerms(string $termKey, bool $popularity): void
84+
{
85+
$terms = $this->createMock(Collection::class);
86+
$dataObjectMock = $this->getMockBuilder(Query::class)
87+
->disableOriginalConstructor()
88+
->setMethods(['getPopularity', 'getQueryText'])
89+
->getMock();
90+
$storeMock = $this->createMock(Store::class);
91+
92+
$this->storeManagerMock->expects($this->once())
93+
->method('getStore')
94+
->willReturn($storeMock);
95+
$storeMock->expects($this->once())
96+
->method('getId')
97+
->willReturn(1);
98+
99+
$this->collectionFactoryMock->expects($this->once())
100+
->method('create')
101+
->willReturn($terms);
102+
$terms->expects($this->once())
103+
->method('setPopularQueryFilter')
104+
->willReturnSelf();
105+
$terms->expects($this->once())
106+
->method('setPageSize')
107+
->willReturnSelf();
108+
$terms->expects($this->once())
109+
->method('load')
110+
->willReturnSelf();
111+
$terms->expects($this->once())
112+
->method('getItems')
113+
->willReturn([$dataObjectMock]);
114+
$dataObjectMock->expects($this->exactly(!$popularity ? 3 : 4))
115+
->method('getPopularity')
116+
->willReturn($popularity);
117+
$dataObjectMock->expects($this->exactly(!$popularity ? 0 : 2))
118+
->method('getQueryText')
119+
->willReturn($termKey);
120+
121+
$this->assertEquals(!$popularity ? [] : [$termKey => $dataObjectMock], $this->termsModel->getTerms());
122+
}
123+
124+
/**
125+
* Verify get search Url
126+
*
127+
* @return void
128+
*/
129+
public function testGetSearchResult(): void
130+
{
131+
$urlMock = $this->getMockBuilder(Url::class)
132+
->disableOriginalConstructor()
133+
->setMethods(['setQueryParam', 'getUrl'])
134+
->getMock();
135+
136+
$dataObjectMock = $this->getMockBuilder(Query::class)
137+
->disableOriginalConstructor()
138+
->setMethods(['getPopularity', 'getQueryText'])
139+
->getMock();
140+
$this->urlFactoryMock->expects($this->once())
141+
->method('create')
142+
->willReturn($urlMock);
143+
$dataObjectMock->expects($this->once())
144+
->method('getQueryText')
145+
->willReturn('url');
146+
$urlMock->expects($this->once())->method('setQueryParam');
147+
$urlMock->expects($this->once())
148+
->method('getUrl')
149+
->with('catalogsearch/result')
150+
->willReturn('url');
151+
152+
$this->assertEquals('url', $this->termsModel->getSearchUrl($dataObjectMock));
153+
}
154+
155+
/**
156+
* Terms data key provider
157+
*
158+
* @return array
159+
*/
160+
public function termKeysProvider(): array
161+
{
162+
return [
163+
[
164+
'search',
165+
true
166+
],
167+
[
168+
'',
169+
false
170+
]
171+
];
172+
}
173+
}

0 commit comments

Comments
 (0)