Completed
Push — m/note-default-schema ( 4c069f...201d93 )
by
unknown
16:20 queued 11:11
created

ClientTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

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