Skip to content

Commit d9f8f03

Browse files
committed
[Search] Unit test for SynonymAnalyzer model
1 parent 0ddea98 commit d9f8f03

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Search\Test\Unit\Model;
7+
8+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9+
10+
/**
11+
* Class SynonymAnalyzerTest
12+
*/
13+
class SynonymAnalyzerTest extends \PHPUnit\Framework\TestCase
14+
{
15+
/**
16+
* @var \Magento\Search\Model\SynonymAnalyzer
17+
*/
18+
private $synonymAnalyzer;
19+
20+
/**
21+
* @var \Magento\Search\Model\SynonymReader |\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $synReaderModel;
24+
25+
/**
26+
* Test set up
27+
*/
28+
protected function setUp()
29+
{
30+
ini_set('memory_limit', '1024M');
31+
$helper = new ObjectManager($this);
32+
33+
$this->synReaderModel = $this->getMockBuilder(\Magento\Search\Model\SynonymReader::class)
34+
->disableOriginalConstructor()
35+
->getMock();
36+
37+
$this->synonymAnalyzer = $helper->getObject(
38+
\Magento\Search\Model\SynonymAnalyzer::class,
39+
[
40+
'synReader' => $this->synReaderModel,
41+
]
42+
);
43+
}
44+
45+
/**
46+
* @test
47+
*/
48+
public function testGetSynonymsForPhrase()
49+
{
50+
$phrase = 'Elizabeth is the british queen';
51+
$expected = [
52+
0 => [ 0 => "Elizabeth" ],
53+
1 => [ 0 => "is" ],
54+
2 => [ 0 => "the" ],
55+
3 => [ 0 => "british", 1 => "english" ],
56+
4 => [ 0 => "queen", 1 => "monarch" ],
57+
];
58+
$this->synReaderModel->expects($this->once())
59+
->method('loadByPhrase')
60+
->with($phrase)
61+
->willReturnSelf()
62+
;
63+
$this->synReaderModel->expects($this->once())
64+
->method('getData')
65+
->willReturn([
66+
['synonyms' => 'british,english'],
67+
['synonyms' => 'queen,monarch'],
68+
])
69+
;
70+
71+
$actual = $this->synonymAnalyzer->getSynonymsForPhrase($phrase);
72+
$this->assertEquals($expected, $actual);
73+
}
74+
75+
/**
76+
* @test
77+
*
78+
* Empty phrase scenario
79+
*/
80+
public function testGetSynonymsForPhraseEmptyPhrase()
81+
{
82+
$phrase = '';
83+
$expected = [];
84+
$actual = $this->synonymAnalyzer->getSynonymsForPhrase($phrase);
85+
$this->assertEquals($expected, $actual);
86+
}
87+
}

0 commit comments

Comments
 (0)