@@ -22,17 +22,17 @@ class BuilderTest extends TestCase
22
22
/**
23
23
* @var Builder
24
24
*/
25
- protected $ model ;
25
+ private $ model ;
26
26
27
27
/**
28
28
* @var LocaleResolver|MockObject
29
29
*/
30
- protected $ localeResolver ;
30
+ private $ localeResolver ;
31
31
32
32
/**
33
33
* @var EsConfigInterface|MockObject
34
34
*/
35
- protected $ esConfig ;
35
+ private $ esConfig ;
36
36
37
37
/**
38
38
* @var SynonymReader|MockObject
@@ -87,6 +87,26 @@ protected function setUp(): void
87
87
->disableOriginalConstructor ()
88
88
->getMock ();
89
89
90
+ $ this ->synonymReaderMock ->expects ($ this ->once ())
91
+ ->method ('getConnection ' )
92
+ ->willReturn ($ this ->connectionMock );
93
+
94
+ $ this ->connectionMock ->expects ($ this ->once ())
95
+ ->method ('select ' )
96
+ ->willReturn ($ this ->selectMock );
97
+
98
+ $ this ->selectMock ->expects ($ this ->once ())
99
+ ->method ('from ' )
100
+ ->willReturn ($ this ->selectMock );
101
+
102
+ $ this ->esConfig ->expects ($ this ->once ())
103
+ ->method ('getStemmerInfo ' )
104
+ ->willReturn ([
105
+ 'type ' => 'stemmer ' ,
106
+ 'default ' => 'english ' ,
107
+ 'en_US ' => 'english ' ,
108
+ ]);
109
+
90
110
$ objectManager = new ObjectManagerHelper ($ this );
91
111
$ this ->model = $ objectManager ->getObject (
92
112
Builder::class,
@@ -99,57 +119,103 @@ protected function setUp(): void
99
119
}
100
120
101
121
/**
102
- * Test build() method
122
+ * Test build() method without provided synonyms.
123
+ *
124
+ * In this case, synonyms filter must not be created or referenced
125
+ * in the prefix_search and sku_prefix_search analyzers.
103
126
*
104
127
* @param string $locale
105
128
* @dataProvider buildDataProvider
106
129
*/
107
- public function testBuild ( $ locale )
130
+ public function testBuildWithoutSynonymsProvided ( string $ locale )
108
131
{
109
- $ synonymsArray = [
110
- 'mp3,player,sound,audio ' ,
111
- 'tv,video,television,screen '
112
- ];
132
+ $ synonymsFilterName = 'synonyms ' ;
113
133
114
134
$ this ->localeResolver ->expects ($ this ->once ())
115
135
->method ('getLocale ' )
116
136
->willReturn ($ locale );
117
137
118
- $ this ->esConfig ->expects ($ this ->once ())
119
- ->method ('getStemmerInfo ' )
120
- ->willReturn ([
121
- 'type ' => 'stemmer ' ,
122
- 'default ' => 'english ' ,
123
- 'en_US ' => 'english ' ,
124
- ]);
125
-
126
- $ this ->synonymReaderMock ->expects ($ this ->once ())
127
- ->method ('getConnection ' )
128
- ->willReturn ($ this ->connectionMock );
129
-
130
- $ this ->connectionMock ->expects ($ this ->once ())
131
- ->method ('select ' )
132
- ->willReturn ($ this ->selectMock );
133
-
134
- $ this ->selectMock ->expects ($ this ->once ())
135
- ->method ('from ' )
136
- ->willReturn ($ this ->selectMock );
137
-
138
138
$ this ->connectionMock ->expects ($ this ->once ())
139
139
->method ('fetchCol ' )
140
- ->willReturn ($ synonymsArray );
140
+ ->willReturn ([] );
141
141
142
142
$ result = $ this ->model ->build ();
143
- $ this ->assertNotNull ($ result );
143
+
144
+ $ analysisFilters = $ result ["analysis " ]["filter " ];
145
+ $ prefixSearchAnalyzerFilters = $ result ["analysis " ]["analyzer " ]["prefix_search " ]["filter " ];
146
+ $ skuPrefixSearchAnalyzerFilters = $ result ["analysis " ]["analyzer " ]["sku_prefix_search " ]["filter " ];
147
+
148
+ $ this ->assertArrayNotHasKey (
149
+ $ synonymsFilterName ,
150
+ $ analysisFilters ,
151
+ 'Analysis filters must not contain synonyms when they are not defined '
152
+ );
153
+ $ this ->assertNotContains ($ synonymsFilterName ,
154
+ $ prefixSearchAnalyzerFilters ,
155
+ 'The prefix_search analyzer must not include synonyms filter when it is not present '
156
+ );
157
+ $ this ->assertNotContains (
158
+ $ synonymsFilterName ,
159
+ $ skuPrefixSearchAnalyzerFilters ,
160
+ 'The sku_prefix_search analyzer must include synonyms filter when it is not present '
161
+ );
144
162
}
145
163
146
164
/**
147
- * Test setStoreId() method
165
+ * Test build() method with synonyms provided.
166
+ *
167
+ * In this case synonyms filter should be created, populated with the list of available synonyms
168
+ * and referenced in the prefix_search and sku_prefix_search analyzers.
169
+ *
170
+ * @param string $locale
171
+ * @dataProvider buildDataProvider
148
172
*/
149
- public function testSetStoreId ( )
173
+ public function testBuildWithProvidedSynonyms ( string $ locale )
150
174
{
151
- $ result = $ this ->model ->setStoreId (1 );
152
- $ this ->assertNull ($ result );
175
+ $ synonymsFilterName = 'synonyms ' ;
176
+
177
+ $ synonyms = [
178
+ 'mp3,player,sound,audio ' ,
179
+ 'tv,video,television,screen '
180
+ ];
181
+
182
+ $ expectedFilter = [
183
+ 'type ' => 'synonym_graph ' ,
184
+ 'synonyms ' => $ synonyms
185
+ ];
186
+
187
+ $ this ->localeResolver ->expects ($ this ->once ())
188
+ ->method ('getLocale ' )
189
+ ->willReturn ($ locale );
190
+
191
+ $ this ->connectionMock ->expects ($ this ->once ())
192
+ ->method ('fetchCol ' )
193
+ ->willReturn ($ synonyms );
194
+
195
+ $ result = $ this ->model ->build ();
196
+
197
+ $ analysisFilters = $ result ["analysis " ]["filter " ];
198
+ $ prefixSearchAnalyzerFilters = $ result ["analysis " ]["analyzer " ]["prefix_search " ]["filter " ];
199
+ $ skuPrefixSearchAnalyzerFilters = $ result ["analysis " ]["analyzer " ]["sku_prefix_search " ]["filter " ];
200
+
201
+ $ this ->assertArrayHasKey (
202
+ $ synonymsFilterName ,
203
+ $ analysisFilters ,
204
+ 'Analysis filters must contain synonyms when defined '
205
+ );
206
+ $ this ->assertContains (
207
+ $ expectedFilter ,
208
+ $ analysisFilters ,
209
+ 'Analysis synonyms filter must match the expected result '
210
+ );
211
+ $ this ->assertContains ($ synonymsFilterName ,
212
+ $ prefixSearchAnalyzerFilters ,
213
+ 'The prefix_search analyzer must include synonyms filter '
214
+ );
215
+ $ this ->assertContains ($ synonymsFilterName ,
216
+ $ skuPrefixSearchAnalyzerFilters ,
217
+ 'The sku_prefix_search analyzer must include synonyms filter '
218
+ );
153
219
}
154
220
155
221
/**
0 commit comments