Completed
Push — master ( ea3502...a65de6 )
by Nicolas
02:31
created

ResponseSetTest::_getFixture()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 9.296
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
        list($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
        list($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
        list($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 View Code Duplication
        foreach ($bulkResponses as $i => $bulkResponse) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        list($responseData, $actions) = $this->_getFixture();
112
113
        $responseSet = $this->_createResponseSet($responseData, $actions);
114
115
        $this->assertCount(3, $responseSet);
116
117 View Code Duplication
        foreach ($responseSet as $i => $bulkResponse) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        $this->assertInstanceOf(Bulk\Response::class, $responseSet->current());
134
    }
135
136
    public function isOkDataProvider(): \Generator
137
    {
138
        list($responseData, $actions) = $this->_getFixture();
139
140
        yield [$responseData, $actions, true];
141
        $responseData['items'][2]['index']['ok'] = false;
142
        yield [$responseData, $actions, false];
143
    }
144
145
    protected function _createResponseSet(array $responseData, array $actions): ResponseSet
146
    {
147
        $client = $this->createMock(Client::class);
148
149
        $response = new Response($responseData, 200);
150
        $response->setQueryTime(12.3);
151
        $response->setTransferInfo([
152
            'url' => 'http://127.0.0.1:9200/_bulk',
153
            'http_code' => 200,
154
        ]);
155
156
        $client->expects($this->once())
157
            ->method('request')
158
            ->withAnyParameters()
159
            ->willReturn($response)
160
        ;
161
162
        $bulk = new Bulk($client);
163
        $bulk->addActions($actions);
164
165
        return $bulk->send();
166
    }
167
168
    protected function _getFixture(): array
169
    {
170
        $responseData = [
171
            'took' => 5,
172
            'items' => [
173
                [
174
                    'index' => [
175
                        '_index' => 'index',
176
                        '_id' => '1',
177
                        '_version' => 1,
178
                        'ok' => true,
179
                    ],
180
                ],
181
                [
182
                    'index' => [
183
                        '_index' => 'index',
184
                        '_id' => '2',
185
                        '_version' => 1,
186
                        'ok' => true,
187
                    ],
188
                ],
189
                [
190
                    'index' => [
191
                        '_index' => 'index',
192
                        '_id' => '3',
193
                        '_version' => 1,
194
                        'ok' => true,
195
                    ],
196
                ],
197
            ],
198
        ];
199
        $bulkResponses = [
200
            new Action(),
201
            new Action(),
202
            new Action(),
203
        ];
204
205
        return [$responseData, $bulkResponses];
206
    }
207
}
208