Skip to content

Commit b851ca8

Browse files
committed
[Core] Extending PHPUnit coverage
1 parent 6a2790e commit b851ca8

1 file changed

Lines changed: 392 additions & 0 deletions

File tree

Lines changed: 392 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,392 @@
1+
<?php
2+
/**
3+
* DISCLAIMER
4+
*
5+
* Do not edit or add to this file if you wish to upgrade this module to newer
6+
* versions in the future.
7+
*
8+
* @category Smile
9+
* @package Smile\ElasticsuiteCore
10+
* @author Richard BAYET <richard.bayet@smile.fr>
11+
* @copyright 2026 Smile
12+
* @license Open Software License ("OSL") v. 3.0
13+
*/
14+
15+
namespace Smile\ElasticsuiteCore\Test\Unit\Search;
16+
17+
use Magento\Framework\Search\Request\Dimension;
18+
use Magento\Framework\Search\Request\QueryInterface;
19+
use PHPUnit\Framework\TestCase;
20+
use Smile\ElasticsuiteCore\Api\Search\SpellcheckerInterface;
21+
use Smile\ElasticsuiteCore\Search\Request;
22+
use Smile\ElasticsuiteCore\Search\Request\BucketInterface;
23+
use Smile\ElasticsuiteCore\Search\Request\SortOrderInterface;
24+
25+
/**
26+
* Default implementation of ElasticSuite search request test case.
27+
*
28+
* @category Smile
29+
* @package Smile\ElasticsuiteCore
30+
* @author Richard BAYET <richard.bayet@smile.fr>
31+
*/
32+
class RequestTest extends TestCase
33+
{
34+
/**
35+
* Tests that the Request constructor stores all provided parameters correctly.
36+
*
37+
* Verifies that when a Request object is instantiated with all parameters,
38+
* the getter methods return the exact same values that were passed in.
39+
*
40+
* @return void
41+
*/
42+
public function testConstructWithAllParametersStoresValuesCorrectly()
43+
{
44+
$name = 'test_request';
45+
$indexName = 'test_index';
46+
$query = $this->createMock(QueryInterface::class);
47+
$filter = $this->createMock(QueryInterface::class);
48+
$sortOrders = [$this->createMock(SortOrderInterface::class)];
49+
$from = 10;
50+
$size = 20;
51+
$dimensions = [$this->createMock(Dimension::class)];
52+
$buckets = [$this->createMock(BucketInterface::class)];
53+
$spellingType = 'fuzzy_spelling';
54+
$trackTotalHits = 5000;
55+
$minScore = 0.5;
56+
57+
$request = new Request(
58+
$name,
59+
$indexName,
60+
$query,
61+
$filter,
62+
$sortOrders,
63+
$from,
64+
$size,
65+
$dimensions,
66+
$buckets,
67+
$spellingType,
68+
$trackTotalHits,
69+
$minScore
70+
);
71+
72+
$this->assertSame($filter, $request->getFilter());
73+
$this->assertSame($sortOrders, $request->getSortOrders());
74+
$this->assertSame($trackTotalHits, $request->getTrackTotalHits());
75+
$this->assertSame($minScore, $request->getMinScore());
76+
}
77+
78+
/**
79+
* Tests that the Request constructor uses default values for optional parameters.
80+
*
81+
* Verifies that when a Request object is instantiated with only required parameters,
82+
* optional parameters default to null or false as appropriate.
83+
*
84+
* @return void
85+
*/
86+
public function testConstructWithOnlyRequiredParametersUsesDefaultValues()
87+
{
88+
$name = 'test_request';
89+
$indexName = 'test_index';
90+
$query = $this->createMock(QueryInterface::class);
91+
92+
$request = new Request(
93+
$name,
94+
$indexName,
95+
$query
96+
);
97+
98+
$this->assertNull($request->getFilter());
99+
$this->assertNull($request->getSortOrders());
100+
$this->assertNull($request->getMinScore());
101+
$this->assertFalse($request->hasCollapse());
102+
$this->assertFalse($request->hasSourceConfig());
103+
}
104+
105+
/**
106+
* Tests that the Request constructor uses the default value for trackTotalHits when not provided.
107+
*
108+
* Verifies that when trackTotalHits is not specified, it defaults to
109+
* PER_SHARD_MAX_RESULT_WINDOW from IndexSettings.
110+
*
111+
* @return void
112+
*/
113+
public function testConstructWithoutTrackTotalHitsUsesDefaultValue()
114+
{
115+
$name = 'test_request';
116+
$indexName = 'test_index';
117+
$query = $this->createMock(QueryInterface::class);
118+
119+
$request = new Request(
120+
$name,
121+
$indexName,
122+
$query
123+
);
124+
125+
$this->assertEquals(
126+
\Smile\ElasticsuiteCore\Helper\IndexSettings::PER_SHARD_MAX_RESULT_WINDOW,
127+
$request->getTrackTotalHits()
128+
);
129+
}
130+
131+
/**
132+
* Tests that numeric string values for trackTotalHits are converted to integers.
133+
*
134+
* Verifies that when trackTotalHits is provided as a numeric string,
135+
* it is properly parsed and converted to an integer type.
136+
*
137+
* @return void
138+
*/
139+
public function testParseTrackTotalHitsWithNumericStringConvertsToInteger()
140+
{
141+
$name = 'test_request';
142+
$indexName = 'test_index';
143+
$query = $this->createMock(QueryInterface::class);
144+
$trackTotalHitsString = '5000';
145+
146+
$request = new Request(
147+
$name,
148+
$indexName,
149+
$query,
150+
null,
151+
null,
152+
null,
153+
null,
154+
[],
155+
[],
156+
null,
157+
$trackTotalHitsString
158+
);
159+
160+
$this->assertIsInt($request->getTrackTotalHits());
161+
$this->assertEquals(5000, $request->getTrackTotalHits());
162+
}
163+
164+
/**
165+
* Tests that boolean string values for trackTotalHits are converted correctly.
166+
*
167+
* Verifies that when trackTotalHits is provided as the string 'true',
168+
* it is properly parsed and converted to a boolean true value.
169+
*
170+
* @return void
171+
*/
172+
public function testParseTrackTotalHitsWithBooleanStringConvertsCorrectly()
173+
{
174+
$name = 'test_request';
175+
$indexName = 'test_index';
176+
$query = $this->createMock(QueryInterface::class);
177+
$trackTotalHitsString = 'true';
178+
179+
$request = new Request(
180+
$name,
181+
$indexName,
182+
$query,
183+
null,
184+
null,
185+
null,
186+
null,
187+
[],
188+
[],
189+
null,
190+
$trackTotalHitsString
191+
);
192+
193+
$this->assertTrue($request->getTrackTotalHits());
194+
}
195+
196+
/**
197+
* Tests that a false value for trackTotalHits is converted to zero.
198+
*
199+
* Verifies that when trackTotalHits is provided as boolean false,
200+
* it is converted to the integer value 0.
201+
*
202+
* @return void
203+
*/
204+
public function testParseTrackTotalHitsWithFalseReturnsZero()
205+
{
206+
$name = 'test_request';
207+
$indexName = 'test_index';
208+
$query = $this->createMock(QueryInterface::class);
209+
$trackTotalHitsFalse = false;
210+
211+
$request = new Request(
212+
$name,
213+
$indexName,
214+
$query,
215+
null,
216+
null,
217+
null,
218+
null,
219+
[],
220+
[],
221+
null,
222+
$trackTotalHitsFalse
223+
);
224+
225+
$this->assertEquals(0, $request->getTrackTotalHits());
226+
}
227+
228+
/**
229+
* Tests that isSpellchecked returns true for fuzzy spelling type.
230+
*
231+
* Verifies that when a Request is created with the SPELLING_TYPE_FUZZY spelling type,
232+
* the isSpellchecked method returns true.
233+
*
234+
* @return void
235+
*/
236+
public function testIsSpellcheckedReturnsTrueForFuzzySpellingType()
237+
{
238+
$name = 'test_request';
239+
$indexName = 'test_index';
240+
$query = $this->createMock(QueryInterface::class);
241+
$spellingType = SpellcheckerInterface::SPELLING_TYPE_FUZZY;
242+
243+
$request = new Request(
244+
$name,
245+
$indexName,
246+
$query,
247+
null,
248+
null,
249+
null,
250+
null,
251+
[],
252+
[],
253+
$spellingType
254+
);
255+
256+
$this->assertTrue($request->isSpellchecked());
257+
}
258+
259+
/**
260+
* Tests that isSpellchecked returns true for most fuzzy spelling type.
261+
*
262+
* Verifies that when a Request is created with the SPELLING_TYPE_MOST_FUZZY spelling type,
263+
* the isSpellchecked method returns true.
264+
*
265+
* @return void
266+
*/
267+
public function testIsSpellcheckedReturnsTrueForMostFuzzySpellingType()
268+
{
269+
$name = 'test_request';
270+
$indexName = 'test_index';
271+
$query = $this->createMock(QueryInterface::class);
272+
$spellingType = SpellcheckerInterface::SPELLING_TYPE_MOST_FUZZY;
273+
274+
$request = new Request(
275+
$name,
276+
$indexName,
277+
$query,
278+
null,
279+
null,
280+
null,
281+
null,
282+
[],
283+
[],
284+
$spellingType
285+
);
286+
287+
$this->assertTrue($request->isSpellchecked());
288+
}
289+
290+
/**
291+
* Tests that setSourceConfig stores the value and returns the Request instance.
292+
*
293+
* Verifies that the setSourceConfig method properly stores the provided source configuration
294+
* and returns the Request instance itself for method chaining.
295+
*
296+
* @param mixed $sourceConfig The source configuration value to set (null, empty array, or array of field names).
297+
*
298+
* @return void
299+
*
300+
* @dataProvider sourceConfigDataProvider
301+
*/
302+
public function testSetSourceConfigStoresValueAndReturnsInstance($sourceConfig)
303+
{
304+
$name = 'test_request';
305+
$indexName = 'test_index';
306+
$query = $this->createMock(QueryInterface::class);
307+
308+
$request = new Request(
309+
$name,
310+
$indexName,
311+
$query
312+
);
313+
314+
$result = $request->setSourceConfig($sourceConfig);
315+
316+
$this->assertSame($sourceConfig, $request->getSourceConfig());
317+
$this->assertSame($request, $result);
318+
}
319+
320+
/**
321+
* Provides test data for source configuration testing.
322+
*
323+
* Returns an array of test cases with different source configuration values:
324+
* null, empty array, and non-empty array of field names.
325+
*
326+
* @return array An associative array of test cases with descriptive keys and configuration values.
327+
*/
328+
public function sourceConfigDataProvider()
329+
{
330+
return [
331+
'null_value' => [null],
332+
'empty_array' => [[]],
333+
'non_empty_array' => [['field1', 'field2', 'field3']],
334+
];
335+
}
336+
337+
/**
338+
* Tests that getSourceConfig returns an empty array when source config is never set.
339+
*
340+
* Verifies that when a Request is created without setting source configuration,
341+
* getSourceConfig returns an empty array rather than null.
342+
*
343+
* @return void
344+
*/
345+
public function testGetSourceConfigReturnsEmptyArrayWhenNeverSet()
346+
{
347+
$name = 'test_request';
348+
$indexName = 'test_index';
349+
$query = $this->createMock(QueryInterface::class);
350+
351+
$request = new Request(
352+
$name,
353+
$indexName,
354+
$query
355+
);
356+
357+
$this->assertIsArray($request->getSourceConfig());
358+
$this->assertEmpty($request->getSourceConfig());
359+
}
360+
361+
/**
362+
* Tests that getSourceConfig and hasSourceConfig work together correctly.
363+
*
364+
* Verifies that hasSourceConfig returns false and getSourceConfig returns an empty array
365+
* initially, and after setting source configuration, both methods reflect the change appropriately.
366+
*
367+
* @return void
368+
*/
369+
public function testGetSourceConfigAndHasSourceConfigWorkTogether()
370+
{
371+
$name = 'test_request';
372+
$indexName = 'test_index';
373+
$query = $this->createMock(QueryInterface::class);
374+
$sourceConfig = ['field1', 'field2'];
375+
376+
$request = new Request(
377+
$name,
378+
$indexName,
379+
$query
380+
);
381+
382+
// Initially, hasSourceConfig should return false and getSourceConfig should return empty array
383+
$this->assertFalse($request->hasSourceConfig());
384+
$this->assertEmpty($request->getSourceConfig());
385+
386+
// After setting source config, both methods should reflect the change
387+
$request->setSourceConfig($sourceConfig);
388+
$this->assertTrue($request->hasSourceConfig());
389+
$this->assertSame($sourceConfig, $request->getSourceConfig());
390+
}
391+
392+
}

0 commit comments

Comments
 (0)