Skip to content

Commit 7ea8155

Browse files
committed
Refine fake testing APIs
1 parent fed4e04 commit 7ea8155

6 files changed

Lines changed: 25 additions & 40 deletions

File tree

src/Search/SearchResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SearchResponse implements RawResponseInterface
1515
* @param array<string, mixed> $response
1616
*/
1717
public function __construct(
18-
protected array $response,
18+
protected array $response = [],
1919
) {}
2020

2121
/**
@@ -47,7 +47,7 @@ public static function fake(array $hits = [], string $index = 'test'): static
4747
*/
4848
public function hits(): array
4949
{
50-
$hits = $this->response['hits']['hits'];
50+
$hits = $this->response['hits']['hits'] ?? [];
5151

5252
return array_map(fn (array $hit) => new Hit($hit), $hits);
5353
}

src/Testing/Fakes/FakeDocumentManager.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,9 @@ class FakeDocumentManager implements DocumentManagerInterface
4646
* Create a new fake document manager instance.
4747
*/
4848
public function __construct(
49-
protected SearchResponse $response = new SearchResponse([
50-
'hits' => [
51-
'hits' => [],
52-
],
53-
]),
49+
protected SearchResponse $response = new SearchResponse,
5450
) {}
5551

56-
/**
57-
* Set the search response returned by the fake.
58-
*/
59-
public function respondWith(SearchResponse $response): static
60-
{
61-
$this->response = $response;
62-
63-
return $this;
64-
}
65-
6652
/**
6753
* Index the given documents into OpenSearch.
6854
*

src/Testing/Fakes/FakeIndexManager.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414
*/
1515
class FakeIndexManager implements IndexManagerInterface
1616
{
17-
/**
18-
* The index names that should exist.
19-
*
20-
* @var array<int, string>
21-
*/
22-
protected array $existing = [];
23-
2417
/**
2518
* The opened index names.
2619
*
@@ -85,14 +78,13 @@ class FakeIndexManager implements IndexManagerInterface
8578
protected array $deletedAliases = [];
8679

8780
/**
88-
* Seed the fake with an existing index.
81+
* Create a new fake index manager instance.
82+
*
83+
* @param array<int, string> $existing
8984
*/
90-
public function withIndex(string $index): static
91-
{
92-
$this->existing[] = $index;
93-
94-
return $this;
95-
}
85+
public function __construct(
86+
protected array $existing = [],
87+
) {}
9688

9789
/**
9890
* Open the given index.
@@ -130,7 +122,7 @@ public function exists(string $index): bool
130122
public function create(IndexBlueprint $index): static
131123
{
132124
$this->created[] = $index;
133-
$this->withIndex($index->name());
125+
$this->existing[] = $index->name();
134126

135127
return $this;
136128
}

tests/Unit/Documents/FakeDocumentManagerTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
});
5252

5353
test('fake document manager records searches and returns configured responses', function () {
54-
$documents = new FakeDocumentManager;
5554
$request = new SearchRequest([
5655
'match' => [
5756
'title' => 'OpenSearch',
@@ -76,9 +75,9 @@
7675
],
7776
]);
7877

79-
$result = $documents
80-
->respondWith($response)
81-
->search('posts', $request);
78+
$documents = new FakeDocumentManager($response);
79+
80+
$result = $documents->search('posts', $request);
8281

8382
expect($result)->toBe($response);
8483

tests/Unit/Indices/FakeIndexManagerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
expect($indices->exists('posts'))->toBeFalse();
1818

19-
$indices
20-
->withIndex('posts')
21-
->assertChecked('posts');
19+
$indices->assertChecked('posts');
20+
21+
$indices = new FakeIndexManager(existing: ['posts']);
2222

2323
expect($indices->exists('posts'))->toBeTrue();
2424
});
@@ -64,7 +64,7 @@
6464
});
6565

6666
test('fake index manager records deleted indices', function () {
67-
$indices = (new FakeIndexManager)->withIndex('posts');
67+
$indices = new FakeIndexManager(existing: ['posts']);
6868

6969
$indices->delete('posts');
7070

tests/Unit/Search/SearchResponseTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
use DirectoryTree\OpenSearchAdapter\Search\Suggestion;
1111
use DirectoryTree\OpenSearchAdapter\Search\TotalHits;
1212

13+
test('empty response can be created', function () {
14+
$searchResponse = new SearchResponse;
15+
16+
expect($searchResponse->hits())->toBe([])
17+
->and($searchResponse->total())->toBeNull()
18+
->and($searchResponse->raw())->toBe([]);
19+
});
20+
1321
test('hits can be retrieved', function () {
1422
$searchResponse = new SearchResponse([
1523
'hits' => [

0 commit comments

Comments
 (0)