These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Elastica\Test\Bulk; |
||
4 | |||
5 | use Elastica\Bulk; |
||
6 | use Elastica\Bulk\Action; |
||
7 | use Elastica\Bulk\ResponseSet; |
||
8 | use Elastica\Client; |
||
9 | use Elastica\Exception\Bulk\Response\ActionException; |
||
10 | use Elastica\Exception\Bulk\ResponseException; |
||
11 | use Elastica\Response; |
||
12 | use Elastica\Test\Base as BaseTest; |
||
13 | |||
14 | /** |
||
15 | * @internal |
||
16 | */ |
||
17 | class ResponseSetTest extends BaseTest |
||
18 | { |
||
19 | /** |
||
20 | * @group unit |
||
21 | */ |
||
22 | public function testConstructor(): void |
||
23 | { |
||
24 | [$responseData, $actions] = $this->_getFixture(); |
||
25 | |||
26 | $responseSet = $this->_createResponseSet($responseData, $actions); |
||
27 | |||
28 | $this->assertEquals(200, $responseSet->getStatus()); |
||
29 | $this->assertEquals(12.3, $responseSet->getQueryTime()); |
||
30 | $this->assertEquals([ |
||
31 | 'url' => 'http://127.0.0.1:9200/_bulk', |
||
32 | 'http_code' => 200, |
||
33 | ], $responseSet->getTransferInfo()); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @group unit |
||
38 | * @dataProvider isOkDataProvider |
||
39 | */ |
||
40 | public function testIsOk(array $responseData, array $actions, bool $expected): void |
||
41 | { |
||
42 | $responseSet = $this->_createResponseSet($responseData, $actions); |
||
43 | $this->assertEquals($expected, $responseSet->isOk()); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @group unit |
||
48 | */ |
||
49 | public function testGetError(): void |
||
50 | { |
||
51 | [$responseData, $actions] = $this->_getFixture(); |
||
52 | $responseData['items'][1]['index']['ok'] = false; |
||
53 | $responseData['items'][1]['index']['error'] = 'SomeExceptionMessage'; |
||
54 | $responseData['items'][2]['index']['ok'] = false; |
||
55 | $responseData['items'][2]['index']['error'] = 'AnotherExceptionMessage'; |
||
56 | |||
57 | try { |
||
58 | $this->_createResponseSet($responseData, $actions); |
||
59 | $this->fail('Bulk request should fail'); |
||
60 | } catch (ResponseException $e) { |
||
61 | $responseSet = $e->getResponseSet(); |
||
62 | |||
63 | $this->assertTrue($responseSet->hasError()); |
||
64 | $this->assertEquals('SomeExceptionMessage', $responseSet->getError()); |
||
65 | $this->assertNotEquals('AnotherExceptionMessage', $responseSet->getError()); |
||
66 | |||
67 | $actionExceptions = $e->getActionExceptions(); |
||
68 | $this->assertCount(2, $actionExceptions); |
||
69 | |||
70 | $this->assertInstanceOf(ActionException::class, $actionExceptions[0]); |
||
71 | $this->assertSame($actions[1], $actionExceptions[0]->getAction()); |
||
72 | $this->assertStringContainsString('SomeExceptionMessage', $actionExceptions[0]->getMessage()); |
||
73 | $this->assertTrue($actionExceptions[0]->getResponse()->hasError()); |
||
74 | |||
75 | $this->assertInstanceOf(ActionException::class, $actionExceptions[1]); |
||
76 | $this->assertSame($actions[2], $actionExceptions[1]->getAction()); |
||
77 | $this->assertStringContainsString('AnotherExceptionMessage', $actionExceptions[1]->getMessage()); |
||
78 | $this->assertTrue($actionExceptions[1]->getResponse()->hasError()); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @group unit |
||
84 | */ |
||
85 | public function testGetBulkResponses(): void |
||
86 | { |
||
87 | [$responseData, $actions] = $this->_getFixture(); |
||
88 | |||
89 | $responseSet = $this->_createResponseSet($responseData, $actions); |
||
90 | |||
91 | $bulkResponses = $responseSet->getBulkResponses(); |
||
92 | $this->assertIsArray($bulkResponses); |
||
93 | $this->assertCount(3, $bulkResponses); |
||
94 | |||
95 | foreach ($bulkResponses as $i => $bulkResponse) { |
||
96 | $this->assertInstanceOf(Bulk\Response::class, $bulkResponse); |
||
97 | $bulkResponseData = $bulkResponse->getData(); |
||
98 | $this->assertIsArray($bulkResponseData); |
||
99 | $this->assertArrayHasKey('_id', $bulkResponseData); |
||
100 | $this->assertEquals($responseData['items'][$i]['index']['_id'], $bulkResponseData['_id']); |
||
101 | $this->assertSame($actions[$i], $bulkResponse->getAction()); |
||
102 | $this->assertEquals('index', $bulkResponse->getOpType()); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @group unit |
||
108 | */ |
||
109 | public function testIterator(): void |
||
110 | { |
||
111 | [$responseData, $actions] = $this->_getFixture(); |
||
112 | |||
113 | $responseSet = $this->_createResponseSet($responseData, $actions); |
||
114 | |||
115 | $this->assertCount(3, $responseSet); |
||
116 | |||
117 | foreach ($responseSet as $i => $bulkResponse) { |
||
118 | $this->assertInstanceOf(Bulk\Response::class, $bulkResponse); |
||
119 | $bulkResponseData = $bulkResponse->getData(); |
||
120 | $this->assertIsArray($bulkResponseData); |
||
121 | $this->assertArrayHasKey('_id', $bulkResponseData); |
||
122 | $this->assertEquals($responseData['items'][$i]['index']['_id'], $bulkResponseData['_id']); |
||
123 | $this->assertSame($actions[$i], $bulkResponse->getAction()); |
||
124 | $this->assertEquals('index', $bulkResponse->getOpType()); |
||
125 | } |
||
126 | |||
127 | $this->assertFalse($responseSet->valid()); |
||
128 | |||
129 | $responseSet->rewind(); |
||
130 | |||
131 | $this->assertEquals(0, $responseSet->key()); |
||
132 | $this->assertTrue($responseSet->valid()); |
||
133 | } |
||
134 | |||
135 | public function isOkDataProvider(): \Generator |
||
136 | { |
||
137 | [$responseData, $actions] = $this->_getFixture(); |
||
138 | |||
139 | yield [$responseData, $actions, true]; |
||
140 | $responseData['items'][2]['index']['ok'] = false; |
||
141 | yield [$responseData, $actions, false]; |
||
142 | } |
||
143 | |||
144 | protected function _createResponseSet(array $responseData, array $actions): ResponseSet |
||
145 | { |
||
146 | $client = $this->createMock(Client::class); |
||
147 | |||
148 | $response = new Response($responseData, 200); |
||
149 | $response->setQueryTime(12.3); |
||
150 | $response->setTransferInfo([ |
||
151 | 'url' => 'http://127.0.0.1:9200/_bulk', |
||
152 | 'http_code' => 200, |
||
153 | ]); |
||
154 | |||
155 | $client->expects($this->once()) |
||
156 | ->method('request') |
||
157 | ->withAnyParameters() |
||
158 | ->willReturn($response) |
||
159 | ; |
||
160 | |||
161 | $bulk = new Bulk($client); |
||
0 ignored issues
–
show
|
|||
162 | $bulk->addActions($actions); |
||
163 | |||
164 | return $bulk->send(); |
||
165 | } |
||
166 | |||
167 | protected function _getFixture(): array |
||
168 | { |
||
169 | $responseData = [ |
||
170 | 'took' => 5, |
||
171 | 'items' => [ |
||
172 | [ |
||
173 | 'index' => [ |
||
174 | '_index' => 'index', |
||
175 | '_id' => '1', |
||
176 | '_version' => 1, |
||
177 | 'ok' => true, |
||
178 | ], |
||
179 | ], |
||
180 | [ |
||
181 | 'index' => [ |
||
182 | '_index' => 'index', |
||
183 | '_id' => '2', |
||
184 | '_version' => 1, |
||
185 | 'ok' => true, |
||
186 | ], |
||
187 | ], |
||
188 | [ |
||
189 | 'index' => [ |
||
190 | '_index' => 'index', |
||
191 | '_id' => '3', |
||
192 | '_version' => 1, |
||
193 | 'ok' => true, |
||
194 | ], |
||
195 | ], |
||
196 | ], |
||
197 | ]; |
||
198 | $bulkResponses = [ |
||
199 | new Action(), |
||
200 | new Action(), |
||
201 | new Action(), |
||
202 | ]; |
||
203 | |||
204 | return [$responseData, $bulkResponses]; |
||
205 | } |
||
206 | } |
||
207 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: