Completed
Push — b/update_guzzle ( 16f2fc...3512e8 )
by
unknown
07:47
created

ClientTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 12
c 6
b 0
f 1
lcom 2
cbo 7
dl 0
loc 225
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 18 1
A testMultiServerConstructor() 0 16 1
A testNextServer() 0 16 1
A testDropServer() 0 16 1
A testDropLastServer() 0 19 1
A createResponse() 0 6 1
A testExecuteWithResponseFailure() 0 19 1
A testExecute() 0 20 1
A testGetServerInfo() 0 5 1
A testGetServerVersion() 0 5 1
A testSetTimeout() 0 9 1
A testSetDefaultSchema() 0 10 1
1
<?php
2
/**
3
 * @author Antoine Hedgcock
4
 */
5
6
namespace CrateTest\PDO\Http;
7
8
use Crate\PDO\Exception\RuntimeException;
9
use Crate\PDO\Exception\UnsupportedException;
10
use Crate\Stdlib\Collection;
11
use Crate\PDO\Http\Client;
12
use Crate\PDO\Http\Server;
13
use GuzzleHttp\Exception\ClientException;
14
use GuzzleHttp\Exception\ConnectException;
15
#use GuzzleHttp\Message\RequestInterface;
16
use Psr\Http\Message\RequestInterface;
17
#use GuzzleHttp\Message\Response;
18
use GuzzleHttp\Psr7\Response;
19
#use GuzzleHttp\Message\ResponseInterface;
20
use Psr\Http\Message\ResponseInterface;
21
#use GuzzleHttp\Stream\Stream;
22
use guzzlehttp\psr7;
23
use PHPUnit_Framework_TestCase;
24
use ReflectionClass;
25
26
/**
27
 * Class ClientTest
28
 *
29
 * @coversDefaultClass \Crate\PDO\Http\Client
30
 * @covers ::<!public>
31
 *
32
 * @group unit
33
 */
34
class ClientTest extends PHPUnit_Framework_TestCase
35
{
36
37
    /**
38
     * @var Client
39
     */
40
    private $client;
41
42
    /**
43
     * @var \PHPUnit_Framework_MockObject_MockObject
44
     */
45
    private $server;
46
47
    /**
48
     * @covers ::__construct
49
     */
50
    protected function setUp()
51
    {
52
        $server = 'localhost:4200';
53
        $this->client = new Client([$server], []);
54
        $this->server = $this->getMockBuilder(Server::class)
55
            ->disableOriginalConstructor()
56
            ->getMock();
57
58
        $clientReflection = new ReflectionClass($this->client);
59
60
        $availableServers = $clientReflection->getProperty('availableServers');
61
        $availableServers->setAccessible(true);
62
        $availableServers->setValue($this->client, [$server]);
63
64
        $serverPool = $clientReflection->getProperty('serverPool');
65
        $serverPool->setAccessible(true);
66
        $serverPool->setValue($this->client, [$server => $this->server]);
67
    }
68
69
    /**
70
     * @covers ::__construct
71
     */
72
    public function testMultiServerConstructor()
73
    {
74
        $servers = ['crate1:4200', 'crate2:4200', 'crate3:4200'];
75
        $client = new Client($servers, []);
76
        $clientReflection = new ReflectionClass($client);
77
78
        $p = $clientReflection->getProperty('availableServers');
79
        $p->setAccessible(true);
80
        $availableServers = $p->getValue($client);
81
        $this->assertEquals(3, count($availableServers));
82
83
        $p = $clientReflection->getProperty('serverPool');
84
        $p->setAccessible(true);
85
        $serverPool = $p->getValue($client);
86
        $this->assertEquals(3, count($serverPool));
87
    }
88
89
    /**
90
     * @covers ::nextServer
91
     */
92
    public function testNextServer()
93
    {
94
        $servers = ['crate1:4200', 'crate2:4200', 'crate3:4200'];
95
        $client = new Client($servers, []);
96
        $clientReflection = new ReflectionClass($client);
97
98
        $pAvailServers = $clientReflection->getProperty('availableServers');
99
        $pAvailServers->setAccessible(true);
100
        $pNextServer = $clientReflection->getMethod('nextServer');
101
        $pNextServer->setAccessible(true);
102
103
        $this->assertEquals('crate1:4200', $pNextServer->invoke($client));
104
        $this->assertEquals('crate2:4200', $pNextServer->invoke($client));
105
        $this->assertEquals('crate3:4200', $pNextServer->invoke($client));
106
        $this->assertEquals('crate1:4200', $pNextServer->invoke($client));
107
    }
108
109
    /**
110
     * @covers ::dropServer
111
     */
112
    public function testDropServer()
113
    {
114
        $servers = ['crate1:4200', 'crate2:4200'];
115
        $client = new Client($servers, []);
116
        $clientReflection = new ReflectionClass($client);
117
118
        $pAvailServers = $clientReflection->getProperty('availableServers');
119
        $pAvailServers->setAccessible(true);
120
        $pDropServer = $clientReflection->getMethod('dropServer');
121
        $pDropServer->setAccessible(true);
122
123
        $this->assertEquals(2, count($pAvailServers->getValue($client)));
124
        $pDropServer->invoke($client, 'crate2:4200', null);
125
        $this->assertEquals(1, count($pAvailServers->getValue($client)));
126
        $this->assertEquals(['crate1:4200'], $pAvailServers->getValue($client));
127
    }
128
129
    /**
130
     * @covers ::dropServer
131
     */
132
    public function testDropLastServer()
133
    {
134
        $servers = ['localhost:4200'];
135
        $client = new Client($servers, []);
136
        $clientReflection = new ReflectionClass($client);
137
138
139
        $this->setExpectedException(ConnectException::class, "No more servers available, exception from last server: Connection refused.");
140
        $ex = $this->getMock(ConnectException::class, null,
141
            ['Connection refused.', $this->getMock(RequestInterface::class), null]);
142
143
        $pDropServer = $clientReflection->getMethod('dropServer');
144
        $pDropServer->setAccessible(true);
145
        $pDropServer->invoke($client, 'localhost:4200');
146
147
        $pRaiseIfNoServers = $clientReflection->getMethod('raiseIfNoMoreServers');
148
        $pRaiseIfNoServers->setAccessible(true);
149
        $pRaiseIfNoServers->invoke($client, $ex);
150
    }
151
152
    /**
153
     * Create a response to be used
154
     *
155
     * @param int   $statusCode
156
     * @param array $body
157
     *
158
     * @return Response
159
     */
160
    private function createResponse($statusCode, array $body)
161
    {
162
        $body = psr7\stream_for(json_encode($body));
163
164
        return new Response($statusCode, [], $body);
165
    }
166
167
    /**
168
     * @covers ::execute
169
     */
170
    public function testExecuteWithResponseFailure()
171
    {
172
        $code    = 1337;
173
        $message = 'hello world';
174
175
        $this->setExpectedException(RuntimeException::class, $message, $code);
176
177
        $request = $this->getMock(RequestInterface::class);
178
        $response = $this->createResponse(400, ['error' => ['code' => $code, 'message' => $message]]);
179
180
        $exception = ClientException::create($request, $response);
181
182
        $this->server
183
            ->expects($this->once())
184
            ->method('doRequest')
185
            ->will($this->throwException($exception));
186
187
        $this->client->execute('SELECT ? FROM', ['foo']);
188
    }
189
190
    /**
191
     * @covers ::execute
192
     */
193
    public function testExecute()
194
    {
195
        $body = [
196
            'cols'     => ['name'],
197
            'rows'     => [['crate2'],['crate2']],
198
            'rowcount' => 2,
199
            'duration' => 0
200
        ];
201
202
        $response = $this->createResponse(200, $body);
203
204
        $this->server
205
            ->expects($this->once())
206
            ->method('doRequest')
207
            ->will($this->returnValue($response));
208
209
        $result = $this->client->execute('SELECT name FROM sys.nodes', []);
210
211
        $this->assertInstanceOf(Collection::class, $result);
212
    }
213
214
    /**
215
     * @covers ::getServerInfo
216
     */
217
    public function testGetServerInfo()
218
    {
219
        $this->setExpectedException(UnsupportedException::class);
220
        $this->client->getServerInfo();
221
    }
222
223
    /**
224
     * @covers ::getServerVersion
225
     */
226
    public function testGetServerVersion()
227
    {
228
        $this->setExpectedException(UnsupportedException::class);
229
        $this->client->getServerVersion();
230
    }
231
232
    /**
233
     * @covers ::setTimeout
234
     */
235
    public function testSetTimeout()
236
    {
237
        $this->server
238
            ->expects($this->once())
239
            ->method('setTimeout')
240
            ->with(4);
241
242
        $this->client->setTimeout('4');
243
    }
244
245
    /**
246
     * @covers ::setDefaultSchema
247
     */
248
    public function testSetDefaultSchema()
249
    {
250
        $schema = 'my_schema';
251
        $this->server
252
            ->expects($this->once())
253
            ->method('setHttpHeader')
254
            ->with('Default-Schema', $schema);
255
256
        $this->client->setDefaultSchema($schema);
257
    }
258
}
259