Skip to content

Commit aa794e0

Browse files
authored
Merge pull request #125 from apisearch-io/fix/fixed-suggestions-format
Fixed suggestions format
2 parents 370af74 + c8cadcf commit aa794e0

File tree

4 files changed

+80
-18
lines changed

4 files changed

+80
-18
lines changed

.circleci/config.yml

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
version: 2
2+
23
jobs:
3-
build:
4+
php72:
45
docker:
56
- image: circleci/php:7.2-cli
67

78
working_directory: ~/project
89
steps:
910
- checkout
10-
1111
- run:
12-
name: Install PHPUnit
12+
name: Run tests / Symfony 4^3
1313
command: |
14-
composer require phpunit/phpunit:7.5.17 --prefer-dist --prefer-stable --prefer-lowest --no-suggest
14+
composer update -n --prefer-dist --prefer-stable --no-suggest
15+
php vendor/bin/phpunit
1516
1617
- run:
17-
name: Run tests / Symfony 3^3
18+
name: Run tests / Symfony 5^0
1819
command: |
20+
composer update -n --prefer-dist --no-suggest
1921
php vendor/bin/phpunit
2022
23+
php74:
24+
docker:
25+
- image: circleci/php:7.4-cli
26+
27+
working_directory: ~/project
28+
steps:
29+
- checkout
2130
- run:
2231
name: Run tests / Symfony 4^3
2332
command: |
@@ -28,4 +37,11 @@ jobs:
2837
name: Run tests / Symfony 5^0
2938
command: |
3039
composer update -n --prefer-dist --no-suggest
31-
php vendor/bin/phpunit
40+
php vendor/bin/phpunit
41+
42+
workflows:
43+
version: 2
44+
test:
45+
jobs:
46+
- php72
47+
- php74

Result/Result.php

+37-11
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class Result implements HttpTransportable
4040
/**
4141
* @var array
4242
*
43-
* Suggests
43+
* Suggestions
4444
*/
45-
private $suggests = [];
45+
private $suggestions = [];
4646

4747
/**
4848
* @var Aggregations|null
@@ -103,7 +103,7 @@ public function __construct(
103103
* @param int $totalItems
104104
* @param int $totalHits
105105
* @param Aggregations|null $aggregations
106-
* @param string[] $suggests
106+
* @param string[] $suggestions
107107
* @param Item[] $items
108108
*
109109
* @return Result
@@ -113,7 +113,7 @@ public static function create(
113113
int $totalItems,
114114
int $totalHits,
115115
? Aggregations $aggregations,
116-
array $suggests,
116+
array $suggestions,
117117
array $items
118118
): self {
119119
$result = new self(
@@ -123,7 +123,7 @@ public static function create(
123123
);
124124

125125
$result->aggregations = $aggregations;
126-
$result->suggests = $suggests;
126+
$result->suggestions = $suggestions;
127127
$result->items = $items;
128128

129129
return $result;
@@ -298,21 +298,45 @@ public function hasNotEmptyAggregation(string $name): bool
298298
/**
299299
* Add suggest.
300300
*
301-
* @param string $suggest
301+
* @param string $suggestion
302+
*
303+
* @deprecated Use addSuggestion instead
304+
*/
305+
public function addSuggest(string $suggestion)
306+
{
307+
$this->addSuggestion($suggestion);
308+
}
309+
310+
/**
311+
* Add suggestion.
312+
*
313+
* @param string $suggestion
302314
*/
303-
public function addSuggest(string $suggest)
315+
public function addSuggestion(string $suggestion)
304316
{
305-
$this->suggests[$suggest] = $suggest;
317+
$this->suggestions[$suggestion] = $suggestion;
306318
}
307319

308320
/**
309321
* Get suggests.
310322
*
311323
* @return string[]
324+
*
325+
* @deprecated Use getSuggestions instead
312326
*/
313327
public function getSuggests(): array
314328
{
315-
return array_values($this->suggests);
329+
return array_values($this->suggestions);
330+
}
331+
332+
/**
333+
* Get suggestions.
334+
*
335+
* @return string[]
336+
*/
337+
public function getSuggestions(): array
338+
{
339+
return array_values($this->suggestions);
316340
}
317341

318342
/**
@@ -372,7 +396,7 @@ public function toArray(): array
372396
'aggregations' => $this->aggregations instanceof Aggregations
373397
? $this->aggregations->toArray()
374398
: null,
375-
'suggests' => $this->suggests,
399+
'suggests' => array_keys($this->suggestions),
376400
'subresults' => array_map(function (Result $result) {
377401
return $result->toArray();
378402
}, $this->subresults),
@@ -401,7 +425,9 @@ public static function createFromArray(array $array): self
401425
isset($array['aggregations'])
402426
? Aggregations::createFromArray($array['aggregations'])
403427
: null,
404-
$array['suggests'] ?? [],
428+
isset($array['suggests'])
429+
? array_combine($array['suggests'], $array['suggests'])
430+
: [],
405431
array_map(function (array $item) {
406432
return Item::createFromArray($item);
407433
}, $array['items'] ?? [])

Tests/Result/ResultTest.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public function testCreateFromArrayAllValues()
186186
$this->assertEquals(10, $result->getTotalItems());
187187
$this->assertEquals(20, $result->getTotalHits());
188188
$this->assertInstanceof(Aggregation::class, $result->getAggregation('gogo'));
189-
$this->assertEquals(['sug1', 'sug2'], $result->getSuggests());
189+
$this->assertEquals(['sug1', 'sug2'], $result->getSuggestions());
190190
$this->assertCount(2, $result->getItems());
191191
$this->assertEquals('product', $result->getFirstItem()->getType());
192192
}
@@ -208,4 +208,21 @@ public function testMultiResult()
208208
$this->assertEquals(4, $subqueries['res2']->getTotalHits());
209209
$this->assertEquals(5, $subqueries['res3']->getTotalHits());
210210
}
211+
212+
/**
213+
* Test suggests are a simple array.
214+
*/
215+
public function testSuggest()
216+
{
217+
$result = new Result('aa', 1, 1);
218+
$result->addSuggest('str1');
219+
$result->addSuggestion('str1');
220+
$result->addSuggestion('str2');
221+
222+
$this->assertEquals(['str1', 'str2'], $result->getSuggests());
223+
$this->assertEquals($result->getSuggests(), $result->getSuggestions());
224+
$this->assertEquals(['str1', 'str2'], $result->toArray()['suggests']);
225+
$this->assertEquals(['str1', 'str2'], Result::createFromArray($result->toArray())->getSuggests());
226+
$this->assertEquals(['str1', 'str2'], Result::createFromArray($result->toArray())->getSuggestions());
227+
}
211228
}

composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
"symfony/yaml": "^3.4 || ^4.0 || ^5.0",
1717
"nesbot/carbon": "^1.22 || ^2.0"
1818
},
19+
"require-dev": {
20+
"phpunit/phpunit": "7.5.17"
21+
},
1922
"autoload": {
2023
"psr-4": {
2124
"Apisearch\\": ""

0 commit comments

Comments
 (0)