@@ -14,29 +14,24 @@ public function setUp(): void
14
14
public function testAddDocuments ()
15
15
{
16
16
$ 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 );
22
18
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 ' ]);
28
22
29
23
$ response = $ index ->getDocuments ();
30
24
$ this ->assertCount (count (self ::DOCUMENTS ), $ response );
31
25
}
32
26
33
- public function testGetDocument ()
27
+ public function testGetSingleDocument ()
34
28
{
35
29
$ index = $ this ->client ->createIndex ('documents ' );
36
30
$ response = $ index ->addDocuments (self ::DOCUMENTS );
37
31
$ index ->waitForPendingUpdate ($ response ['updateId ' ]);
38
32
$ doc = $ this ->findDocumentWithId (self ::DOCUMENTS , 4 );
39
33
$ response = $ index ->getDocument ($ doc ['id ' ]);
34
+
40
35
$ this ->assertIsArray ($ response );
41
36
$ this ->assertSame ($ doc ['id ' ], $ response ['id ' ]);
42
37
$ this ->assertSame ($ doc ['title ' ], $ response ['title ' ]);
@@ -52,11 +47,12 @@ public function testReplaceDocuments()
52
47
'title ' => 'The Red And The Black ' ,
53
48
];
54
49
$ response = $ index ->addDocuments ([$ replacement ]);
55
- $ this ->assertIsArray ($ response );
56
- $ this ->assertArrayHasKey ('updateId ' , $ response );
57
- $ index ->waitForPendingUpdate ($ response ['updateId ' ]);
58
50
51
+ $ this ->assertIsValidPromise ($ response );
52
+
53
+ $ index ->waitForPendingUpdate ($ response ['updateId ' ]);
59
54
$ response = $ index ->getDocument ($ replacement ['id ' ]);
55
+
60
56
$ this ->assertSame ($ replacement ['id ' ], $ response ['id ' ]);
61
57
$ this ->assertSame ($ replacement ['title ' ], $ response ['title ' ]);
62
58
$ this ->assertFalse (array_search ('comment ' , $ response ));
@@ -67,22 +63,25 @@ public function testReplaceDocuments()
67
63
public function testUpdateDocuments ()
68
64
{
69
65
$ 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 ' ]);
72
68
$ replacement = [
73
69
'id ' => 456 ,
74
70
'title ' => 'The Little Prince ' ,
75
71
];
72
+ $ promise = $ index ->updateDocuments ([$ replacement ]);
76
73
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 ' ]);
81
77
$ response = $ index ->getDocument ($ replacement ['id ' ]);
78
+
82
79
$ this ->assertSame ($ replacement ['id ' ], $ response ['id ' ]);
83
80
$ this ->assertSame ($ replacement ['title ' ], $ response ['title ' ]);
84
81
$ this ->assertArrayHasKey ('comment ' , $ response );
82
+
85
83
$ response = $ index ->getDocuments ();
84
+
86
85
$ this ->assertCount (count (self ::DOCUMENTS ), $ response );
87
86
}
88
87
@@ -95,15 +94,19 @@ public function testAddWithUpdateDocuments()
95
94
'id ' => 9 ,
96
95
'title ' => '1984 ' ,
97
96
];
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 ' ]);
102
102
$ response = $ index ->getDocument ($ document ['id ' ]);
103
+
103
104
$ this ->assertSame ($ document ['id ' ], $ response ['id ' ]);
104
105
$ this ->assertSame ($ document ['title ' ], $ response ['title ' ]);
105
106
$ this ->assertFalse (array_search ('comment ' , $ response ));
107
+
106
108
$ response = $ index ->getDocuments ();
109
+
107
110
$ this ->assertCount (count (self ::DOCUMENTS ) + 1 , $ response );
108
111
}
109
112
@@ -112,14 +115,17 @@ public function testDeleteNonExistingDocument()
112
115
$ index = $ this ->client ->createIndex ('documents ' );
113
116
$ response = $ index ->addDocuments (self ::DOCUMENTS );
114
117
$ 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 ' ]);
120
125
$ response = $ index ->getDocuments ();
126
+
121
127
$ this ->assertCount (count (self ::DOCUMENTS ), $ response );
122
- $ this ->assertNull ($ this ->findDocumentWithId ($ response , $ id ));
128
+ $ this ->assertNull ($ this ->findDocumentWithId ($ response , $ documentId ));
123
129
}
124
130
125
131
public function testDeleteSingleExistingDocument ()
@@ -128,49 +134,57 @@ public function testDeleteSingleExistingDocument()
128
134
$ response = $ index ->addDocuments (self ::DOCUMENTS );
129
135
$ index ->waitForPendingUpdate ($ response ['updateId ' ]);
130
136
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 ' ]);
136
143
$ response = $ index ->getDocuments ();
144
+
137
145
$ this ->assertCount (count (self ::DOCUMENTS ) - 1 , $ response );
138
- $ this ->assertNull ($ this ->findDocumentWithId ($ response , $ id ));
146
+ $ this ->assertNull ($ this ->findDocumentWithId ($ response , $ documentId ));
139
147
}
140
148
141
149
public function testDeleteMultipleDocuments ()
142
150
{
143
151
$ index = $ this ->client ->createIndex ('documents ' );
144
152
$ response = $ index ->addDocuments (self ::DOCUMENTS );
145
153
$ 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 ' ]);
151
160
$ response = $ index ->getDocuments ();
161
+
152
162
$ 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 ]));
155
165
}
156
166
157
167
public function testDeleteAllDocuments ()
158
168
{
159
169
$ index = $ this ->client ->createIndex ('documents ' );
160
170
$ response = $ index ->addDocuments (self ::DOCUMENTS );
161
171
$ 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 ' ]);
166
177
$ response = $ index ->getDocuments ();
178
+
167
179
$ this ->assertCount (0 , $ response );
168
180
}
169
181
170
182
public function testExceptionIfNoDocumentIdWhenGetting ()
171
183
{
172
184
$ index = $ this ->client ->createIndex ('new-index ' );
185
+
173
186
$ this ->expectException (HTTPRequestException::class);
187
+
174
188
$ index ->getDocument (1 );
175
189
}
176
190
@@ -185,8 +199,10 @@ public function testAddDocumentWithPrimaryKey()
185
199
];
186
200
$ index = $ this ->client ->createIndex ('an-index ' );
187
201
$ response = $ index ->addDocuments ($ documents , 'unique ' );
202
+
188
203
$ this ->assertArrayHasKey ('updateId ' , $ response );
189
204
$ index ->waitForPendingUpdate ($ response ['updateId ' ]);
205
+
190
206
$ this ->assertSame ('unique ' , $ index ->getPrimaryKey ());
191
207
$ this ->assertCount (1 , $ index ->getDocuments ());
192
208
}
@@ -200,18 +216,21 @@ public function testUpdateDocumentWithPrimaryKey()
200
216
'title ' => 'Le Rouge et le Noir ' ,
201
217
],
202
218
];
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
+
207
226
$ this ->assertSame ('unique ' , $ index ->getPrimaryKey ());
208
227
$ this ->assertCount (1 , $ index ->getDocuments ());
209
228
}
210
229
211
- private function findDocumentWithId ($ documents , $ id )
230
+ private function findDocumentWithId ($ documents , $ documentId )
212
231
{
213
232
foreach ($ documents as $ document ) {
214
- if ($ document ['id ' ] == $ id ) {
233
+ if ($ document ['id ' ] == $ documentId ) {
215
234
return $ document ;
216
235
}
217
236
}
0 commit comments