Skip to content

Commit 96c6c2c

Browse files
committed
tests to check array based options, string based options and star(wild card *) based options while searching
1 parent b0b8eec commit 96c6c2c

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

src/Index.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,10 @@ public function updateAcceptNewFields($accept_new_fields)
257257
private function flattenOptions(array $options)
258258
{
259259
return array_map(function ($entry) {
260-
if(is_array($entry)){
261-
return implode(',',$entry);
260+
if (is_array($entry)) {
261+
return implode(',', $entry);
262262
}
263+
263264
return $entry;
264265
}, $options);
265266
}

tests/Endpoints/SearchTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,72 @@ public function testExceptionIfNoIndexWhenSearching()
5656
$index->search('prince');
5757
}
5858

59+
public function testParametersCanBeString()
60+
{
61+
$this->createFreshIndexAndSeedDocuments();
62+
$response = $this->index->search('prince', [
63+
'limit' => 5,
64+
'offset' => 0,
65+
'attributesToRetrieve' => 'id,title',
66+
'attributesToCrop' => 'id,title',
67+
'cropLength' => 6,
68+
'attributesToHighlight' => 'title',
69+
'filters' => 'title = "Le Petit Prince"',
70+
'matches' => 'true',
71+
]);
72+
73+
$this->assertArrayHasKey('_matchesInfo', $response['hits'][0]);
74+
$this->assertArrayHasKey('title', $response['hits'][0]['_matchesInfo']);
75+
$this->assertArrayHasKey('_formatted', $response['hits'][0]);
76+
$this->assertArrayNotHasKey('comment', $response['hits'][0]);
77+
$this->assertArrayNotHasKey('comment', $response['hits'][0]['_matchesInfo']);
78+
$this->assertSame('Petit <em>Prince</em>', $response['hits'][0]['_formatted']['title']);
79+
}
80+
81+
public function testParametersCanBeArray()
82+
{
83+
$this->createFreshIndexAndSeedDocuments();
84+
$response = $this->index->search('prince', [
85+
'limit' => 5,
86+
'offset' => 0,
87+
'attributesToRetrieve' => ['id', 'title'],
88+
'attributesToCrop' => ['id', 'title'],
89+
'cropLength' => 6,
90+
'attributesToHighlight' => 'title',
91+
'filters' => 'title = "Le Petit Prince"',
92+
'matches' => 'true',
93+
]);
94+
95+
$this->assertArrayHasKey('_matchesInfo', $response['hits'][0]);
96+
$this->assertArrayHasKey('title', $response['hits'][0]['_matchesInfo']);
97+
$this->assertArrayHasKey('_formatted', $response['hits'][0]);
98+
$this->assertArrayNotHasKey('comment', $response['hits'][0]);
99+
$this->assertArrayNotHasKey('comment', $response['hits'][0]['_matchesInfo']);
100+
$this->assertSame('Petit <em>Prince</em>', $response['hits'][0]['_formatted']['title']);
101+
}
102+
103+
public function testParametersCanBeAStar()
104+
{
105+
$this->createFreshIndexAndSeedDocuments();
106+
$response = $this->index->search('prince', [
107+
'limit' => 5,
108+
'offset' => 0,
109+
'attributesToRetrieve' => '*',
110+
'attributesToCrop' => '*',
111+
'cropLength' => 6,
112+
'attributesToHighlight' => '*',
113+
'filters' => 'title = "Le Petit Prince"',
114+
'matches' => 'true',
115+
]);
116+
117+
$this->assertArrayHasKey('_matchesInfo', $response['hits'][0]);
118+
$this->assertArrayHasKey('title', $response['hits'][0]['_matchesInfo']);
119+
$this->assertArrayHasKey('_formatted', $response['hits'][0]);
120+
$this->assertArrayHasKey('comment', $response['hits'][0]);
121+
$this->assertArrayNotHasKey('comment', $response['hits'][0]['_matchesInfo']);
122+
$this->assertSame('Petit <em>Prince</em>', $response['hits'][0]['_formatted']['title']);
123+
}
124+
59125
private function createFreshIndexAndSeedDocuments()
60126
{
61127
$this->client->deleteAllIndexes();

0 commit comments

Comments
 (0)