Skip to content

Commit 47e605c

Browse files
committed
clean up and make use of the underlying assertValidPromise method
1 parent d0679bd commit 47e605c

File tree

3 files changed

+75
-53
lines changed

3 files changed

+75
-53
lines changed

tests/ClientTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ public function testPrettySysInfo()
167167
public function testStats()
168168
{
169169
$response = $this->client->stats();
170+
170171
$this->assertArrayHasKey('databaseSize', $response);
171172
$this->assertArrayHasKey('lastUpdate', $response);
172173
$this->assertArrayHasKey('indexes', $response);

tests/DocumentsTest.php

+72-53
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,24 @@ public function setUp(): void
1414
public function testAddDocuments()
1515
{
1616
$index = $this->client->createIndex('documents');
17-
$response = $index->addDocuments(self::DOCUMENTS);
18-
$this->assertIsArray($response);
19-
$this->assertArrayHasKey('updateId', $response);
20-
$index->waitForPendingUpdate($response['updateId']);
21-
}
17+
$promise = $index->addDocuments(self::DOCUMENTS);
2218

23-
public function testGetDocuments()
24-
{
25-
$index = $this->client->createIndex('documents');
26-
$response = $index->addDocuments(self::DOCUMENTS);
27-
$index->waitForPendingUpdate($response['updateId']);
19+
$this->assertIsValidPromise($promise);
20+
21+
$index->waitForPendingUpdate($promise['updateId']);
2822

2923
$response = $index->getDocuments();
3024
$this->assertCount(count(self::DOCUMENTS), $response);
3125
}
3226

33-
public function testGetDocument()
27+
public function testGetSingleDocument()
3428
{
3529
$index = $this->client->createIndex('documents');
3630
$response = $index->addDocuments(self::DOCUMENTS);
3731
$index->waitForPendingUpdate($response['updateId']);
3832
$doc = $this->findDocumentWithId(self::DOCUMENTS, 4);
3933
$response = $index->getDocument($doc['id']);
34+
4035
$this->assertIsArray($response);
4136
$this->assertSame($doc['id'], $response['id']);
4237
$this->assertSame($doc['title'], $response['title']);
@@ -52,11 +47,12 @@ public function testReplaceDocuments()
5247
'title' => 'The Red And The Black',
5348
];
5449
$response = $index->addDocuments([$replacement]);
55-
$this->assertIsArray($response);
56-
$this->assertArrayHasKey('updateId', $response);
57-
$index->waitForPendingUpdate($response['updateId']);
5850

51+
$this->assertIsValidPromise($response);
52+
53+
$index->waitForPendingUpdate($response['updateId']);
5954
$response = $index->getDocument($replacement['id']);
55+
6056
$this->assertSame($replacement['id'], $response['id']);
6157
$this->assertSame($replacement['title'], $response['title']);
6258
$this->assertFalse(array_search('comment', $response));
@@ -67,22 +63,25 @@ public function testReplaceDocuments()
6763
public function testUpdateDocuments()
6864
{
6965
$index = $this->client->createIndex('documents');
70-
$response = $index->addDocuments(self::DOCUMENTS);
71-
$index->waitForPendingUpdate($response['updateId']);
66+
$promise = $index->addDocuments(self::DOCUMENTS);
67+
$index->waitForPendingUpdate($promise['updateId']);
7268
$replacement = [
7369
'id' => 456,
7470
'title' => 'The Little Prince',
7571
];
72+
$promise = $index->updateDocuments([$replacement]);
7673

77-
$response = $index->updateDocuments([$replacement]);
78-
$this->assertIsArray($response);
79-
$this->assertArrayHasKey('updateId', $response);
80-
$index->waitForPendingUpdate($response['updateId']);
74+
$this->assertIsValidPromise($promise);
75+
76+
$index->waitForPendingUpdate($promise['updateId']);
8177
$response = $index->getDocument($replacement['id']);
78+
8279
$this->assertSame($replacement['id'], $response['id']);
8380
$this->assertSame($replacement['title'], $response['title']);
8481
$this->assertArrayHasKey('comment', $response);
82+
8583
$response = $index->getDocuments();
84+
8685
$this->assertCount(count(self::DOCUMENTS), $response);
8786
}
8887

@@ -95,15 +94,19 @@ public function testAddWithUpdateDocuments()
9594
'id' => 9,
9695
'title' => '1984',
9796
];
98-
$response = $index->updateDocuments([$document]);
99-
$this->assertIsArray($response);
100-
$this->assertArrayHasKey('updateId', $response);
101-
$index->waitForPendingUpdate($response['updateId']);
97+
$promise = $index->updateDocuments([$document]);
98+
99+
$this->assertIsValidPromise($promise);
100+
101+
$index->waitForPendingUpdate($promise['updateId']);
102102
$response = $index->getDocument($document['id']);
103+
103104
$this->assertSame($document['id'], $response['id']);
104105
$this->assertSame($document['title'], $response['title']);
105106
$this->assertFalse(array_search('comment', $response));
107+
106108
$response = $index->getDocuments();
109+
107110
$this->assertCount(count(self::DOCUMENTS) + 1, $response);
108111
}
109112

@@ -112,14 +115,17 @@ public function testDeleteNonExistingDocument()
112115
$index = $this->client->createIndex('documents');
113116
$response = $index->addDocuments(self::DOCUMENTS);
114117
$index->waitForPendingUpdate($response['updateId']);
115-
$id = 9;
116-
$response = $index->deleteDocument($id);
117-
$this->assertIsArray($response);
118-
$this->assertArrayHasKey('updateId', $response);
119-
$index->waitForPendingUpdate($response['updateId']);
118+
119+
$documentId = 9;
120+
$promise = $index->deleteDocument($documentId);
121+
122+
$this->assertIsValidPromise($promise);
123+
124+
$index->waitForPendingUpdate($promise['updateId']);
120125
$response = $index->getDocuments();
126+
121127
$this->assertCount(count(self::DOCUMENTS), $response);
122-
$this->assertNull($this->findDocumentWithId($response, $id));
128+
$this->assertNull($this->findDocumentWithId($response, $documentId));
123129
}
124130

125131
public function testDeleteSingleExistingDocument()
@@ -128,49 +134,57 @@ public function testDeleteSingleExistingDocument()
128134
$response = $index->addDocuments(self::DOCUMENTS);
129135
$index->waitForPendingUpdate($response['updateId']);
130136

131-
$id = 123;
132-
$response = $index->deleteDocument($id);
133-
$this->assertIsArray($response);
134-
$this->assertArrayHasKey('updateId', $response);
135-
$index->waitForPendingUpdate($response['updateId']);
137+
$documentId = 123;
138+
$promise = $index->deleteDocument($documentId);
139+
140+
$this->assertIsValidPromise($promise);
141+
142+
$index->waitForPendingUpdate($promise['updateId']);
136143
$response = $index->getDocuments();
144+
137145
$this->assertCount(count(self::DOCUMENTS) - 1, $response);
138-
$this->assertNull($this->findDocumentWithId($response, $id));
146+
$this->assertNull($this->findDocumentWithId($response, $documentId));
139147
}
140148

141149
public function testDeleteMultipleDocuments()
142150
{
143151
$index = $this->client->createIndex('documents');
144152
$response = $index->addDocuments(self::DOCUMENTS);
145153
$index->waitForPendingUpdate($response['updateId']);
146-
$ids = [1, 2];
147-
$response = $index->deleteDocuments($ids);
148-
$this->assertIsArray($response);
149-
$this->assertArrayHasKey('updateId', $response);
150-
$index->waitForPendingUpdate($response['updateId']);
154+
$documentIds = [1, 2];
155+
$promise = $index->deleteDocuments($documentIds);
156+
157+
$this->assertIsValidPromise($promise);
158+
159+
$index->waitForPendingUpdate($promise['updateId']);
151160
$response = $index->getDocuments();
161+
152162
$this->assertCount(count(self::DOCUMENTS) - 2, $response);
153-
$this->assertNull($this->findDocumentWithId($response, $ids[0]));
154-
$this->assertNull($this->findDocumentWithId($response, $ids[1]));
163+
$this->assertNull($this->findDocumentWithId($response, $documentIds[0]));
164+
$this->assertNull($this->findDocumentWithId($response, $documentIds[1]));
155165
}
156166

157167
public function testDeleteAllDocuments()
158168
{
159169
$index = $this->client->createIndex('documents');
160170
$response = $index->addDocuments(self::DOCUMENTS);
161171
$index->waitForPendingUpdate($response['updateId']);
162-
$response = $index->deleteAllDocuments();
163-
$this->assertIsArray($response);
164-
$this->assertArrayHasKey('updateId', $response);
165-
$index->waitForPendingUpdate($response['updateId']);
172+
$promise = $index->deleteAllDocuments();
173+
174+
$this->assertIsValidPromise($promise);
175+
176+
$index->waitForPendingUpdate($promise['updateId']);
166177
$response = $index->getDocuments();
178+
167179
$this->assertCount(0, $response);
168180
}
169181

170182
public function testExceptionIfNoDocumentIdWhenGetting()
171183
{
172184
$index = $this->client->createIndex('new-index');
185+
173186
$this->expectException(HTTPRequestException::class);
187+
174188
$index->getDocument(1);
175189
}
176190

@@ -185,8 +199,10 @@ public function testAddDocumentWithPrimaryKey()
185199
];
186200
$index = $this->client->createIndex('an-index');
187201
$response = $index->addDocuments($documents, 'unique');
202+
188203
$this->assertArrayHasKey('updateId', $response);
189204
$index->waitForPendingUpdate($response['updateId']);
205+
190206
$this->assertSame('unique', $index->getPrimaryKey());
191207
$this->assertCount(1, $index->getDocuments());
192208
}
@@ -200,18 +216,21 @@ public function testUpdateDocumentWithPrimaryKey()
200216
'title' => 'Le Rouge et le Noir',
201217
],
202218
];
203-
$index = $this->client->createIndex('udpateUid');
204-
$response = $index->updateDocuments($documents, 'unique');
205-
$this->assertArrayHasKey('updateId', $response);
206-
$index->waitForPendingUpdate($response['updateId']);
219+
$index = $this->client->createIndex('index');
220+
$promise = $index->updateDocuments($documents, 'unique');
221+
222+
$this->assertIsValidPromise($promise);
223+
224+
$index->waitForPendingUpdate($promise['updateId']);
225+
207226
$this->assertSame('unique', $index->getPrimaryKey());
208227
$this->assertCount(1, $index->getDocuments());
209228
}
210229

211-
private function findDocumentWithId($documents, $id)
230+
private function findDocumentWithId($documents, $documentId)
212231
{
213232
foreach ($documents as $document) {
214-
if ($document['id'] == $id) {
233+
if ($document['id'] == $documentId) {
215234
return $document;
216235
}
217236
}

tests/Settings/AcceptNewFieldsTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public function testUpdateAcceptNewFields()
2323
$promise = $this->index->updateAcceptNewFields(false);
2424

2525
$this->assertIsValidPromise($promise);
26+
2627
$this->index->waitForPendingUpdate($promise['updateId']);
28+
2729
$this->assertFalse($this->index->getAcceptNewFields());
2830
}
2931
}

0 commit comments

Comments
 (0)