1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Isswp101\Persimmon\Test; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Elasticsearch\Common\Exceptions\Missing404Exception; |
7
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Isswp101\Persimmon\Collection\ECollection; |
10
|
|
|
use Isswp101\Persimmon\ElasticsearchModel; |
11
|
|
|
use Isswp101\Persimmon\Exceptions\InvalidModelEndpointException; |
12
|
|
|
use Isswp101\Persimmon\Model; |
13
|
|
|
use Isswp101\Persimmon\QueryBuilder\Aggregations\TermsAggregation; |
14
|
|
|
use Isswp101\Persimmon\QueryBuilder\Filters\IdsFilter; |
15
|
|
|
use Isswp101\Persimmon\QueryBuilder\Filters\RangeOrExistFilter; |
16
|
|
|
use Isswp101\Persimmon\QueryBuilder\Filters\TermFilter; |
17
|
|
|
use Isswp101\Persimmon\QueryBuilder\QueryBuilder; |
18
|
|
|
use Isswp101\Persimmon\Test\Models\InvalidModel; |
19
|
|
|
use Isswp101\Persimmon\Test\Models\Product; |
20
|
|
|
|
21
|
|
|
class BasicFeaturesTest extends BaseTestCase |
22
|
|
|
{ |
23
|
|
|
public static function setUpBeforeClass() |
24
|
|
|
{ |
25
|
|
|
Product::$_index = 'travis_ci_test_' . time() . rand(1, 1000); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testValidateModel() |
29
|
|
|
{ |
30
|
|
|
$this->expectException(InvalidModelEndpointException::class); |
31
|
|
|
new InvalidModel(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testPrepareIndex() |
35
|
|
|
{ |
36
|
|
|
$index = Product::getIndex(); |
37
|
|
|
$type = Product::getType(); |
38
|
|
|
|
39
|
|
|
try { |
40
|
|
|
$this->es->indices()->delete(['index' => $index]); |
41
|
|
|
} catch (Missing404Exception $e) { |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->sleep(3); |
45
|
|
|
|
46
|
|
|
$this->es->indices()->create(['index' => $index]); |
47
|
|
|
|
48
|
|
|
$this->sleep(3); |
49
|
|
|
|
50
|
|
|
$query = ['index' => $index, 'type' => $type, 'body' => ['query' => ['match_all' => []]]]; |
51
|
|
|
$res = $this->es->search($query); |
52
|
|
|
$this->assertEquals(0, $res['hits']['total']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testFill() |
56
|
|
|
{ |
57
|
|
|
$p1 = new Product(); |
58
|
|
|
$p1->id = 1; |
59
|
|
|
$p1->name = 'name'; |
60
|
|
|
|
61
|
|
|
$p2 = new Product(['id' => 1, 'name' => 'name']); |
62
|
|
|
|
63
|
|
|
$this->assertSame($p1->toArray(), $p2->toArray()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testSave() |
67
|
|
|
{ |
68
|
|
|
$product = new Product(); |
69
|
|
|
$product->id = 1; |
70
|
|
|
$product->name = 'Product 1'; |
71
|
|
|
$product->price = 20; |
72
|
|
|
|
73
|
|
|
$this->assertFalse($product->_exist); |
74
|
|
|
|
75
|
|
|
$product->save(); |
76
|
|
|
|
77
|
|
|
$this->assertTrue($product->_exist); |
78
|
|
|
|
79
|
|
|
$this->assertInstanceOf(Model::class, $product); |
80
|
|
|
$this->assertInstanceOf(ElasticsearchModel::class, $product); |
81
|
|
|
|
82
|
|
|
$res = $this->es->get($product->getPath()->toArray()); |
83
|
|
|
|
84
|
|
|
$this->assertEquals($product->getIndex(), $res['_index']); |
85
|
|
|
$this->assertEquals($product->getType(), $res['_type']); |
86
|
|
|
$this->assertEquals($product->getId(), $res['_id']); |
87
|
|
|
|
88
|
|
|
$this->assertEquals(1, $res['_id']); |
89
|
|
|
$this->assertEquals('Product 1', $res['_source']['name']); |
90
|
|
|
$this->assertEquals(20, $res['_source']['price']); |
91
|
|
|
|
92
|
|
|
$this->assertNotNull($res['_source']['created_at']); |
93
|
|
|
$this->assertNotNull($res['_source']['updated_at']); |
94
|
|
|
$this->assertInstanceOf(Carbon::class, $product->getCreatedAt()); |
95
|
|
|
$this->assertInstanceOf(Carbon::class, $product->getUpdatedAt()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testFindBySpecifiedColumns() |
99
|
|
|
{ |
100
|
|
|
$product = Product::find(1, ['name']); |
101
|
|
|
$this->assertEquals('Product 1', $product->name); |
102
|
|
|
$this->assertEquals(0, $product->price); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testFind() |
106
|
|
|
{ |
107
|
|
|
$product = Product::find(1); |
108
|
|
|
|
109
|
|
|
$this->assertTrue($product->_exist); |
110
|
|
|
|
111
|
|
|
$this->assertEquals('Product 1', $product->name); |
112
|
|
|
$this->assertEquals('20', $product->price); |
113
|
|
|
$this->assertEquals(1, $product->getId()); |
114
|
|
|
|
115
|
|
|
$this->assertInstanceOf(Model::class, $product); |
116
|
|
|
$this->assertInstanceOf(ElasticsearchModel::class, $product); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
View Code Duplication |
public function testUpdate() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$product = Product::find(1); |
122
|
|
|
$product->name = 'Product 2'; |
123
|
|
|
$this->sleep(3); |
124
|
|
|
$product->save(); |
125
|
|
|
|
126
|
|
|
$res = $this->es->get($product->getPath()->toArray()); |
127
|
|
|
$this->assertEquals('Product 2', $res['_source']['name']); |
128
|
|
|
$this->assertNotSame($res['_source']['created_at'], $res['_source']['updated_at']); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testFindOrFail() |
132
|
|
|
{ |
133
|
|
|
$this->expectException(ModelNotFoundException::class); |
134
|
|
|
Product::findOrFail(2); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testFindOrNew() |
138
|
|
|
{ |
139
|
|
|
$product = Product::findOrNew(10); |
140
|
|
|
$this->assertFalse($product->_exist); |
141
|
|
|
$this->assertEquals(10, $product->getId()); |
142
|
|
|
$this->assertEmpty($product->name); |
143
|
|
|
$this->assertEquals(0, $product->price); |
144
|
|
|
$this->assertInstanceOf(Model::class, $product); |
145
|
|
|
$this->assertInstanceOf(ElasticsearchModel::class, $product); |
146
|
|
|
|
147
|
|
|
$this->expectException(Missing404Exception::class); |
148
|
|
|
$this->es->get($product->getPath()->toArray()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testDelete() |
152
|
|
|
{ |
153
|
|
|
Product::create(['id' => 6, 'name' => 'Product 6', 'price' => 66]); |
154
|
|
|
$product = Product::find(6); |
155
|
|
|
$this->assertNotNull($product); |
156
|
|
|
$this->assertTrue($product->_exist); |
157
|
|
|
$product->delete(); |
158
|
|
|
$this->assertFalse($product->_exist); |
159
|
|
|
|
160
|
|
|
$this->expectException(Missing404Exception::class); |
161
|
|
|
$this->es->get($product->getPath()->toArray()); |
162
|
|
|
|
163
|
|
|
$product = Product::find(6); |
164
|
|
|
$this->assertNull($product); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testDestroy() |
168
|
|
|
{ |
169
|
|
|
Product::create(['id' => 5, 'name' => 'Product 5', 'price' => 55]); |
170
|
|
|
$product = Product::find(5); |
171
|
|
|
$this->assertNotNull($product); |
172
|
|
|
|
173
|
|
|
Product::destroy(5); |
174
|
|
|
|
175
|
|
|
$this->expectException(Missing404Exception::class); |
176
|
|
|
$this->es->get($product->getPath()->toArray()); |
177
|
|
|
|
178
|
|
|
$product = Product::find(5); |
179
|
|
|
$this->assertNull($product); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
View Code Duplication |
public function testPartialUpdate() |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
$product = Product::find(1, ['name']); |
185
|
|
|
$product->name = 'Product 3'; |
186
|
|
|
$product->save('name'); |
187
|
|
|
|
188
|
|
|
$res = $this->es->get($product->getPath()->toArray()); |
189
|
|
|
|
190
|
|
|
$this->assertEquals('Product 3', $res['_source']['name']); |
191
|
|
|
$this->assertEquals(20, $res['_source']['price']); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testBasicSearch() |
195
|
|
|
{ |
196
|
|
|
$this->sleep(3); |
197
|
|
|
$products = Product::search(); |
198
|
|
|
$product = $products->first(); |
199
|
|
|
|
200
|
|
|
$this->assertInstanceOf(ECollection::class, $products); |
201
|
|
|
$this->assertInstanceOf(Product::class, $product); |
202
|
|
|
$this->assertEquals(1, $products->count()); |
203
|
|
|
$this->assertEquals(1, $product->getId()); |
204
|
|
|
$this->assertEquals(0, $product->_position); |
205
|
|
|
$this->assertEquals($products->count(), $products->getTotal()); |
206
|
|
|
$this->assertNotNull($product->_score); |
207
|
|
|
$this->assertTrue($product->_exist); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
View Code Duplication |
public function testFirst() |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
$product = Product::first(); |
213
|
|
|
$this->assertInstanceOf(Product::class, $product); |
214
|
|
|
$this->assertEquals(1, $product->getId()); |
215
|
|
|
$this->assertEquals(0, $product->_position); |
216
|
|
|
$this->assertTrue($product->_exist); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function testAll() |
220
|
|
|
{ |
221
|
|
|
$products = Product::all(); |
222
|
|
|
$this->assertInstanceOf(Collection::class, $products); |
223
|
|
|
$this->assertEquals(1, $products->count()); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
View Code Duplication |
public function testMap() |
|
|
|
|
227
|
|
|
{ |
228
|
|
|
$total = Product::map([], function (Product $product) { |
229
|
|
|
$this->assertInstanceOf(Product::class, $product); |
230
|
|
|
$this->assertEquals(1, $product->getId()); |
231
|
|
|
$this->assertEquals(0, $product->_position); |
232
|
|
|
$this->assertTrue($product->_exist); |
233
|
|
|
}); |
234
|
|
|
$this->assertEquals(1, $total); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function testCreate() |
238
|
|
|
{ |
239
|
|
|
$product = Product::create(['id' => 3, 'name' => 'Product 3', 'price' => 30]); |
240
|
|
|
|
241
|
|
|
$this->assertInstanceOf(Product::class, $product); |
242
|
|
|
$this->assertTrue($product->_exist); |
243
|
|
|
|
244
|
|
|
$res = $this->es->get($product->getPath()->toArray()); |
245
|
|
|
|
246
|
|
|
$this->assertEquals($product->getIndex(), $res['_index']); |
247
|
|
|
$this->assertEquals($product->getType(), $res['_type']); |
248
|
|
|
$this->assertEquals($product->getId(), $res['_id']); |
249
|
|
|
|
250
|
|
|
$this->assertEquals(3, $res['_id']); |
251
|
|
|
$this->assertEquals('Product 3', $res['_source']['name']); |
252
|
|
|
$this->assertEquals(30, $res['_source']['price']); |
253
|
|
|
|
254
|
|
|
$this->assertNotNull($res['_source']['created_at']); |
255
|
|
|
$this->assertNotNull($res['_source']['updated_at']); |
256
|
|
|
$this->assertInstanceOf(Carbon::class, $product->getCreatedAt()); |
257
|
|
|
$this->assertInstanceOf(Carbon::class, $product->getUpdatedAt()); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function testBasicFilters() |
261
|
|
|
{ |
262
|
|
|
Product::create(['id' => 1, 'name' => 'Product 1', 'price' => 10]); |
263
|
|
|
Product::create(['id' => 2, 'name' => 'Product 2', 'price' => 20]); |
264
|
|
|
Product::create(['id' => 3, 'name' => 'Product 3', 'price' => 30]); |
265
|
|
|
$this->sleep(3); |
266
|
|
|
|
267
|
|
|
$query = new QueryBuilder(); |
268
|
|
|
$query->match('name', 'Product'); |
269
|
|
|
$products = Product::search($query); |
270
|
|
|
$this->assertEquals(3, $products->count()); |
271
|
|
|
|
272
|
|
|
$query = new QueryBuilder(['query' => ['match' => ['name' => 'Product']]]); |
273
|
|
|
$products = Product::search($query); |
274
|
|
|
$this->assertEquals(3, $products->count()); |
275
|
|
|
|
276
|
|
|
$query = new QueryBuilder(); |
277
|
|
|
$query->betweenOrEquals('price', 20, 30)->greaterThan('price', 15); |
278
|
|
|
$products = Product::search($query); |
279
|
|
|
$this->assertEquals(2, $products->count()); |
280
|
|
|
|
281
|
|
|
$query = new QueryBuilder(); |
282
|
|
|
$query->orMatch('name', '1')->orMatch('name', '2'); |
283
|
|
|
$products = Product::search($query); |
284
|
|
|
$this->assertEquals(2, $products->count()); |
285
|
|
|
|
286
|
|
|
$query = new QueryBuilder(); |
287
|
|
|
$query->filter(new TermFilter('name', '2')); |
288
|
|
|
$products = Product::search($query); |
289
|
|
|
$this->assertEquals(1, $products->count()); |
290
|
|
|
|
291
|
|
|
$query = new QueryBuilder(); |
292
|
|
|
$query->filter(new TermFilter('name', ['2', '3'])); |
293
|
|
|
$products = Product::search($query); |
294
|
|
|
$this->assertEquals(2, $products->count()); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function testAggregation() |
298
|
|
|
{ |
299
|
|
|
$query = new QueryBuilder(); |
300
|
|
|
$query->aggregation(new TermsAggregation('name'))->size(0); |
301
|
|
|
$products = Product::search($query); |
302
|
|
|
$buckets = $products->getAggregation('name'); |
303
|
|
|
$this->assertEquals('product', $buckets[0]->getKey()); |
304
|
|
|
$this->assertEquals(3, $buckets[0]->getCount()); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function testPagination() |
308
|
|
|
{ |
309
|
|
|
$product = Product::find(1); |
310
|
|
|
$product->_position = 0; |
311
|
|
|
$product->makePagination(); |
312
|
|
|
$this->assertEquals(3, $product->getPrevious()->getId()); |
313
|
|
|
$this->assertEquals(2, $product->getNext()->getId()); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public function testIdsFilter() |
317
|
|
|
{ |
318
|
|
|
$query = new QueryBuilder(); |
319
|
|
|
$query->filter(new IdsFilter([1, 3])); |
320
|
|
|
$products = Product::search($query); |
321
|
|
|
|
322
|
|
|
$this->assertEquals(2, $products->count()); |
323
|
|
|
$this->assertEquals(1, $products->first()->getId()); |
324
|
|
|
$this->assertEquals(3, $products->last()->getId()); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
public function testRangeOrExistFilter() |
328
|
|
|
{ |
329
|
|
|
$query = new QueryBuilder(); |
330
|
|
|
$query->filter(new RangeOrExistFilter('price', ['gte' => 20])); |
331
|
|
|
$products = Product::search($query); |
332
|
|
|
$this->assertEquals(2, $products->count()); |
333
|
|
|
|
334
|
|
|
$query = new QueryBuilder(); |
335
|
|
|
$query->filter(new RangeOrExistFilter('price')); |
336
|
|
|
$products = Product::search($query); |
337
|
|
|
$this->assertEquals(3, $products->count()); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
public function testTearDown() |
341
|
|
|
{ |
342
|
|
|
$this->deleteIndex(Product::$_index); |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|