2
2
3
3
namespace Sti3bas \ScoutArray \Tests \Engines ;
4
4
5
- use Illuminate \Support \Collection ;
6
- use Illuminate \Support \Facades \Config ;
7
- use Laravel \Scout \Builder ;
8
5
use Mockery ;
6
+ use Laravel \Scout \Builder ;
9
7
use PHPUnit \Framework \TestCase ;
8
+ use Illuminate \Support \Collection ;
10
9
use Sti3bas \ScoutArray \ArrayStore ;
10
+ use Illuminate \Support \Facades \Config ;
11
+ use Illuminate \Support \LazyCollection ;
11
12
use Sti3bas \ScoutArray \Engines \ArrayEngine ;
12
- use Sti3bas \ScoutArray \Tests \Fixtures \EmptySearchableModel ;
13
13
use Sti3bas \ScoutArray \Tests \Fixtures \SearchableModel ;
14
+ use Sti3bas \ScoutArray \Tests \Fixtures \EmptySearchableModel ;
14
15
use Sti3bas \ScoutArray \Tests \Fixtures \SoftDeletableSearchableModel ;
15
16
16
17
class ArrayEngineTest extends TestCase
@@ -20,6 +21,15 @@ protected function setUp(): void
20
21
Config::shouldReceive ('get ' )->with ('scout.after_commit ' , Mockery::any ())->andReturn (false );
21
22
}
22
23
24
+ protected function tearDown (): void
25
+ {
26
+ $ this ->addToAssertionCount (
27
+ \Mockery::getContainer ()->mockery_getExpectationCount ()
28
+ );
29
+
30
+ \Mockery::close ();
31
+ }
32
+
23
33
/** @test */
24
34
public function it_can_search_for_the_records ()
25
35
{
@@ -346,6 +356,45 @@ public function it_returns_empty_collection_if_no_results_when_mapping()
346
356
$ this ->assertEquals (0 , count ($ results ));
347
357
}
348
358
359
+ /** @test */
360
+ public function it_can_lazy_map_records_to_models ()
361
+ {
362
+ $ engine = new ArrayEngine (new ArrayStore ());
363
+
364
+ $ model = Mockery::mock (stdClass::class);
365
+ $ model ->shouldReceive ('queryScoutModelsByIds->cursor ' )->andReturn ($ models = LazyCollection::make ([
366
+ $ model3 = new SearchableModel (['scoutKey ' => 3 ]),
367
+ $ model1 = new SearchableModel (['scoutKey ' => 1 ]),
368
+ $ model2 = new SearchableModel (['scoutKey ' => 2 ]),
369
+ ]));
370
+
371
+ $ builder = Mockery::mock (Builder::class);
372
+
373
+ $ results = $ engine ->lazyMap ($ builder , ['nbHits ' => 1 , 'hits ' => [
374
+ ['objectID ' => 2 ],
375
+ ['objectID ' => 1 ],
376
+ ['objectID ' => 3 ],
377
+ ]], $ model );
378
+
379
+ $ this ->assertEquals (3 , count ($ results ));
380
+ $ this ->assertInstanceOf (LazyCollection::class, $ results );
381
+
382
+ $ this ->assertTrue ($ results ->all ()[0 ]->is ($ model1 ));
383
+ $ this ->assertTrue ($ results ->all ()[1 ]->is ($ model2 ));
384
+ $ this ->assertTrue ($ results ->all ()[2 ]->is ($ model3 ));
385
+ }
386
+
387
+ /** @test */
388
+ public function it_returns_empty_lazy_collection_if_no_results_when_lazy_mapping ()
389
+ {
390
+ $ engine = new ArrayEngine (new ArrayStore ());
391
+
392
+ $ results = $ engine ->lazyMap (new Builder (new SearchableModel , '' ), ['hits ' => []], new SearchableModel );
393
+
394
+ $ this ->assertInstanceOf (LazyCollection::class, $ results );
395
+ $ this ->assertEquals (0 , count ($ results ));
396
+ }
397
+
349
398
/** @test */
350
399
public function it_knows_total_count ()
351
400
{
@@ -396,4 +445,28 @@ public function it_can_be_filtered()
396
445
$ this ->assertCount (1 , $ results ['hits ' ]);
397
446
$ this ->assertEquals (2 , $ results ['hits ' ][0 ]['scoutKey ' ]);
398
447
}
448
+
449
+ /** @test */
450
+ public function it_can_create_search_index ()
451
+ {
452
+ $ store = Mockery::spy (ArrayStore::class);
453
+
454
+ $ engine = new ArrayEngine ($ store );
455
+
456
+ $ engine ->createIndex ('test ' );
457
+
458
+ $ store ->shouldHaveReceived ('createIndex ' )->with ('test ' )->once ();
459
+ }
460
+
461
+ /** @test */
462
+ public function it_can_delete_search_index ()
463
+ {
464
+ $ store = Mockery::spy (ArrayStore::class);
465
+
466
+ $ engine = new ArrayEngine ($ store );
467
+
468
+ $ engine ->deleteIndex ('test ' );
469
+
470
+ $ store ->shouldHaveReceived ('deleteIndex ' )->with ('test ' )->once ();
471
+ }
399
472
}
0 commit comments