2
2
3
3
namespace Olekjs \Elasticsearch \Tests \Integration ;
4
4
5
+ use Illuminate \Support \Collection ;
5
6
use Illuminate \Support \Facades \Http ;
6
7
use Olekjs \Elasticsearch \Client ;
7
8
use Olekjs \Elasticsearch \Dto \PaginateResponseDto ;
9
+ use Olekjs \Elasticsearch \Dto \SearchHitDto ;
8
10
use Olekjs \Elasticsearch \Dto \SearchResponseDto ;
9
11
use Olekjs \Elasticsearch \Exceptions \ConflictResponseException ;
10
12
use Olekjs \Elasticsearch \Exceptions \DeleteResponseException ;
@@ -43,10 +45,10 @@ public function testSearchMethod(): void
43
45
44
46
$ this ->assertFalse ($ result ->getIsTimedOut ());
45
47
46
- $ this ->assertSame (['value ' => 5 , 'relation ' => 'eq ' ], $ result ->getResults ()->getTotal ());
47
- $ this ->assertSame (1.0 , $ result ->getResults ()->getMaxScore ());
48
+ $ this ->assertSame (['value ' => 5 , 'relation ' => 'eq ' ], $ result ->getResult ()->getTotal ());
49
+ $ this ->assertSame (1.0 , $ result ->getResult ()->getMaxScore ());
48
50
49
- foreach ($ result ->getResults ()->getHits () as $ hit ) {
51
+ foreach ($ result ->getResult ()->getHits () as $ hit ) {
50
52
$ this ->assertSame ('hello ' , $ hit ->getIndex ());
51
53
$ this ->assertSame ('183865906814918156 ' , $ hit ->getId ());
52
54
$ this ->assertSame (1.0 , $ hit ->getScore ());
@@ -447,9 +449,9 @@ public function testEmptyResponseInSearchMethod(): void
447
449
448
450
$ this ->assertFalse ($ result ->getIsTimedOut ());
449
451
450
- $ this ->assertSame (['value ' => 0 , 'relation ' => 'eq ' ], $ result ->getResults ()->getTotal ());
451
- $ this ->assertNull ($ result ->getResults ()->getMaxScore ());
452
- $ this ->assertEmpty ($ result ->getResults ()->getHits ());
452
+ $ this ->assertSame (['value ' => 0 , 'relation ' => 'eq ' ], $ result ->getResult ()->getTotal ());
453
+ $ this ->assertNull ($ result ->getResult ()->getMaxScore ());
454
+ $ this ->assertEmpty ($ result ->getResult ()->getHits ());
453
455
}
454
456
455
457
public function testSearchWhereInMethod (): void
@@ -473,10 +475,10 @@ public function testSearchWhereInMethod(): void
473
475
474
476
$ this ->assertFalse ($ result ->getIsTimedOut ());
475
477
476
- $ this ->assertSame (['value ' => 2 , 'relation ' => 'eq ' ], $ result ->getResults ()->getTotal ());
477
- $ this ->assertSame (1.0 , $ result ->getResults ()->getMaxScore ());
478
+ $ this ->assertSame (['value ' => 2 , 'relation ' => 'eq ' ], $ result ->getResult ()->getTotal ());
479
+ $ this ->assertSame (1.0 , $ result ->getResult ()->getMaxScore ());
478
480
479
- foreach ($ result ->getResults ()->getHits () as $ hit ) {
481
+ foreach ($ result ->getResult ()->getHits () as $ hit ) {
480
482
$ this ->assertSame ('hello ' , $ hit ->getIndex ());
481
483
$ this ->assertSame ('183865906814918156 ' , $ hit ->getId ());
482
484
$ this ->assertSame (1.0 , $ hit ->getScore ());
@@ -505,10 +507,10 @@ public function testSearchWhereKeywordMethod(): void
505
507
506
508
$ this ->assertFalse ($ result ->getIsTimedOut ());
507
509
508
- $ this ->assertSame (['value ' => 1 , 'relation ' => 'eq ' ], $ result ->getResults ()->getTotal ());
509
- $ this ->assertSame (1.6739764 , $ result ->getResults ()->getMaxScore ());
510
+ $ this ->assertSame (['value ' => 1 , 'relation ' => 'eq ' ], $ result ->getResult ()->getTotal ());
511
+ $ this ->assertSame (1.6739764 , $ result ->getResult ()->getMaxScore ());
510
512
511
- foreach ($ result ->getResults ()->getHits () as $ hit ) {
513
+ foreach ($ result ->getResult ()->getHits () as $ hit ) {
512
514
$ this ->assertSame ('hello ' , $ hit ->getIndex ());
513
515
$ this ->assertSame ('183865906814918156 ' , $ hit ->getId ());
514
516
$ this ->assertSame (1.6739764 , $ hit ->getScore ());
@@ -537,10 +539,10 @@ public function testSearchWhereLikeMethod(): void
537
539
538
540
$ this ->assertFalse ($ result ->getIsTimedOut ());
539
541
540
- $ this ->assertSame (['value ' => 1 , 'relation ' => 'eq ' ], $ result ->getResults ()->getTotal ());
541
- $ this ->assertSame (1.0 , $ result ->getResults ()->getMaxScore ());
542
+ $ this ->assertSame (['value ' => 1 , 'relation ' => 'eq ' ], $ result ->getResult ()->getTotal ());
543
+ $ this ->assertSame (1.0 , $ result ->getResult ()->getMaxScore ());
542
544
543
- foreach ($ result ->getResults ()->getHits () as $ hit ) {
545
+ foreach ($ result ->getResult ()->getHits () as $ hit ) {
544
546
$ this ->assertSame ('hello ' , $ hit ->getIndex ());
545
547
$ this ->assertSame ('183865906814918156 ' , $ hit ->getId ());
546
548
$ this ->assertSame (1.0 , $ hit ->getScore ());
@@ -657,4 +659,27 @@ public function testPaginateMethod(): void
657
659
658
660
$ this ->assertInstanceOf (SearchResponseDto::class, $ result ->getDocuments ());
659
661
}
662
+
663
+ public function testSearchResponseCanBeConvertedToCollection (): void
664
+ {
665
+ Http::fake (function () {
666
+ return Http::response (
667
+ file_get_contents ('tests/Responses/search_success_response.json ' )
668
+ );
669
+ });
670
+
671
+ $ client = new Client ();
672
+
673
+ $ result = $ client ->search ('hello ' , [
674
+ 'query ' => [
675
+ 'match_all ' => (object )[]
676
+ ]
677
+ ]);
678
+
679
+ $ this ->assertInstanceOf (Collection::class, $ result ->toCollect ());
680
+
681
+ foreach ($ result ->toCollect () as $ source ) {
682
+ $ this ->assertSame (['hello ' => 'world ' ], $ source );
683
+ }
684
+ }
660
685
}
0 commit comments