18
18
19
19
use Elastic \Elasticsearch \ClientInterface ;
20
20
use Elastic \Elasticsearch \Helper \Iterators \SearchResponseIterator ;
21
+ use Elastic \Elasticsearch \Response \Elasticsearch ;
21
22
use Mockery as m ;
23
+ use Nyholm \Psr7 \Factory \Psr17Factory ;
22
24
use PHPUnit \Framework \TestCase ;
23
25
24
26
/**
@@ -33,6 +35,18 @@ public function tearDown(): void
33
35
m::close ();
34
36
}
35
37
38
+ private function elasticsearchResponse ($ data ): Elasticsearch
39
+ {
40
+ $ elasticsearch = new Elasticsearch ();
41
+ $ body = (new Psr17Factory ())->createStream (json_encode ($ data ));
42
+ $ response = (new Psr17Factory ())->createResponse (200 )->withBody ($ body )
43
+ ->withHeader ('X-Elastic-Product ' , 'Elasticsearch ' )
44
+ ->withHeader ('Content-Type ' , 'application/json ' );
45
+ $ elasticsearch ->setResponse ($ response );
46
+
47
+ return $ elasticsearch ;
48
+ }
49
+
36
50
public function testWithNoResults (): void
37
51
{
38
52
$ search_params = [
@@ -51,7 +65,7 @@ public function testWithNoResults(): void
51
65
$ mock_client ->shouldReceive ('search ' )
52
66
->twice ()
53
67
->with ($ search_params )
54
- ->andReturn (['_scroll_id ' => 'scroll_id_01 ' ]);
68
+ ->andReturn ($ this -> elasticsearchResponse ( ['_scroll_id ' => 'scroll_id_01 ' ]) );
55
69
56
70
$ mock_client ->shouldReceive ('clearScroll ' )
57
71
->twice ()
@@ -81,8 +95,7 @@ public function testWithHits()
81
95
->once ()
82
96
->ordered ()
83
97
->with ($ search_params )
84
- ->andReturn (
85
- [
98
+ ->andReturn ($ this ->elasticsearchResponse ([
86
99
'_scroll_id ' => 'scroll_id_01 ' ,
87
100
'hits ' => [
88
101
'hits ' => [
@@ -91,7 +104,7 @@ public function testWithHits()
91
104
]
92
105
]
93
106
]
94
- ]
107
+ ])
95
108
);
96
109
97
110
$ mock_client ->shouldReceive ('scroll ' )
@@ -105,8 +118,7 @@ public function testWithHits()
105
118
]
106
119
]
107
120
)
108
- ->andReturn (
109
- [
121
+ ->andReturn ($ this ->elasticsearchResponse ([
110
122
'_scroll_id ' => 'scroll_id_02 ' ,
111
123
'hits ' => [
112
124
'hits ' => [
@@ -115,7 +127,7 @@ public function testWithHits()
115
127
]
116
128
]
117
129
]
118
- ]
130
+ ])
119
131
);
120
132
121
133
$ mock_client ->shouldReceive ('scroll ' )
@@ -129,8 +141,7 @@ public function testWithHits()
129
141
]
130
142
]
131
143
)
132
- ->andReturn (
133
- [
144
+ ->andReturn ($ this ->elasticsearchResponse ([
134
145
'_scroll_id ' => 'scroll_id_03 ' ,
135
146
'hits ' => [
136
147
'hits ' => [
@@ -139,7 +150,7 @@ public function testWithHits()
139
150
]
140
151
]
141
152
]
142
- ]
153
+ ])
143
154
);
144
155
145
156
$ mock_client ->shouldReceive ('scroll ' )
@@ -153,13 +164,13 @@ public function testWithHits()
153
164
]
154
165
]
155
166
)
156
- ->andReturn (
167
+ ->andReturn ($ this -> elasticsearchResponse (
157
168
[
158
169
'_scroll_id ' => 'scroll_id_04 ' ,
159
170
'hits ' => [
160
171
'hits ' => []
161
172
]
162
- ]
173
+ ])
163
174
);
164
175
165
176
$ mock_client ->shouldReceive ('scroll ' )
0 commit comments