forked from clue/reactphp-socks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientTest.php
More file actions
623 lines (455 loc) · 23.9 KB
/
ClientTest.php
File metadata and controls
623 lines (455 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
<?php
namespace Clue\Tests\React\Socks;
use Clue\React\Socks\Client;
use Clue\React\Socks\Server;
use React\Promise\Deferred;
use React\Promise\Promise;
class ClientTest extends TestCase
{
private $connector;
/** @var Client */
private $client;
/**
* @before
*/
public function setUpMocks()
{
$this->connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$this->client = new Client('127.0.0.1:1080', $this->connector);
}
public function testConstructWithoutConnectorAssignsConnectorAutomatically()
{
$proxy = new Client('127.0.0.1:1080');
$ref = new \ReflectionProperty($proxy, 'connector');
$ref->setAccessible(true);
$connector = $ref->getValue($proxy);
$this->assertInstanceOf('React\Socket\ConnectorInterface', $connector);
}
public function testCtorAcceptsUriWithHostAndPort()
{
$client = new Client('127.0.0.1:9050', $this->connector);
$this->assertTrue(true);
}
public function testCtorAcceptsUriWithScheme()
{
$client = new Client('socks://127.0.0.1:9050', $this->connector);
$this->assertTrue(true);
}
public function testCtorAcceptsUriWithHostOnlyAssumesDefaultPort()
{
$client = new Client('127.0.0.1', $this->connector);
$this->assertTrue(true);
}
public function testCtorAcceptsUriWithSecureScheme()
{
$client = new Client('sockss://127.0.0.1:9050', $this->connector);
$this->assertTrue(true);
}
public function testCtorAcceptsUriWithSecureVersionScheme()
{
$client = new Client('socks5s://127.0.0.1:9050', $this->connector);
$this->assertTrue(true);
}
public function testCtorAcceptsUriWithSocksUnixScheme()
{
$client = new Client('socks+unix:///tmp/socks.socket', $this->connector);
$this->assertTrue(true);
}
public function testCtorAcceptsUriWithSocks5UnixScheme()
{
$client = new Client('socks5+unix:///tmp/socks.socket', $this->connector);
$this->assertTrue(true);
}
public function testCtorThrowsForInvalidUri()
{
$this->setExpectedException("InvalidArgumentException");
new Client('////', $this->connector);
}
public function testValidAuthFromUri()
{
$this->client = new Client('username:password@127.0.0.1', $this->connector);
$this->assertTrue(true);
}
public function testInvalidAuthInformation()
{
$this->setExpectedException("InvalidArgumentException");
new Client(str_repeat('a', 256) . ':test@127.0.0.1', $this->connector);
}
public function testValidAuthAndVersionFromUri()
{
$this->client = new Client('socks5://username:password@127.0.0.1:9050', $this->connector);
$this->assertTrue(true);
}
public function testInvalidCanNotSetAuthenticationForSocks4Uri()
{
$this->setExpectedException("InvalidArgumentException");
$this->client = new Client('socks4://username:password@127.0.0.1:9050', $this->connector);
}
public function testInvalidProtocolVersion()
{
$this->setExpectedException("InvalidArgumentException");
$this->client = new Client('socks3://127.0.0.1:9050', $this->connector);
}
public function testCtorThrowsForInvalidConnector()
{
$this->setExpectedException('InvalidArgumentException', 'Argument #2 ($connector) expected null|React\Socket\ConnectorInterface');
new Client('127.0.0.1:1080', 'connector');
}
public function testCreateWillConnectToProxy()
{
$promise = new Promise(function () { });
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=localhost')->willReturn($promise);
$promise = $this->client->connect('localhost:80');
$this->assertInstanceOf('\React\Promise\PromiseInterface', $promise);
}
public function testCreateWillConnectToProxyWithFullUri()
{
$promise = new Promise(function () { });
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080/?hostname=test#fragment')->willReturn($promise);
$promise = $this->client->connect('localhost:80/?hostname=test#fragment');
$this->assertInstanceOf('\React\Promise\PromiseInterface', $promise);
}
public function testCreateWithInvalidHostDoesNotConnect()
{
$promise = new Promise(function () { });
$this->connector->expects($this->never())->method('connect');
$promise = $this->client->connect(str_repeat('a', '256') . ':80');
$this->assertInstanceOf('\React\Promise\PromiseInterface', $promise);
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
}
public function testCreateWithInvalidPortDoesNotConnect()
{
$promise = new Promise(function () { });
$this->connector->expects($this->never())->method('connect');
$promise = $this->client->connect('some-random-site:some-random-port');
$this->assertInstanceOf('\React\Promise\PromiseInterface', $promise);
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
}
public function testConnectorRejectsWillRejectConnection()
{
$promise = \React\Promise\reject(new \RuntimeException());
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);
$promise = $this->client->connect('google.com:80');
$exception = null;
$promise->then(null, function ($reason) use (&$exception) {
$exception = $reason;
});
assert($exception instanceof \RuntimeException);
$this->assertInstanceOf('RuntimeException', $exception);
$this->assertEquals('Connection to tcp://google.com:80 failed because connection to proxy failed (ECONNREFUSED)', $exception->getMessage());
$this->assertEquals(defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111, $exception->getCode());
$this->assertInstanceOf('RuntimeException', $exception->getPrevious());
$this->assertNotEquals('', $exception->getTraceAsString());
}
public function testCancelConnectionDuringConnectionWillCancelConnection()
{
$promise = new Promise(function () { }, function () {
throw new \RuntimeException();
});
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);
$promise = $this->client->connect('google.com:80');
$promise->cancel();
$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
'Connection to tcp://google.com:80 cancelled while waiting for proxy (ECONNABORTED)',
defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103
));
}
public function testCancelConnectionDuringSessionWillCloseStream()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock();
$stream->expects($this->once())->method('close');
$promise = new Promise(function ($resolve) use ($stream) { $resolve($stream); });
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);
$promise = $this->client->connect('google.com:80');
$promise->cancel();
$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
'Connection to tcp://google.com:80 cancelled while waiting for proxy (ECONNABORTED)',
defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103
));
}
public function testCancelConnectionDuringDeferredSessionWillCloseStream()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->getMock();
$stream->expects($this->once())->method('close');
$deferred = new Deferred();
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($deferred->promise());
$promise = $this->client->connect('google.com:80');
$deferred->resolve($stream);
$promise->cancel();
$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
'Connection to tcp://google.com:80 cancelled while waiting for proxy (ECONNABORTED)',
defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103
));
}
public function testEmitConnectionCloseDuringSessionWillRejectConnection()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
$promise = \React\Promise\resolve($stream);
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);
$promise = $this->client->connect('google.com:80');
$stream->emit('close');
$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
'Connection to tcp://google.com:80 failed because connection to proxy was lost while waiting for response from proxy (ECONNRESET)',
defined('SOCKET_ECONNRESET') ? SOCKET_ECONNRESET : 104
));
}
public function testEmitConnectionErrorDuringSessionWillRejectConnection()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
$promise = \React\Promise\resolve($stream);
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);
$promise = $this->client->connect('google.com:80');
$stream->emit('error', array(new \RuntimeException()));
$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
'Connection to tcp://google.com:80 failed because connection to proxy caused a stream error (EIO)',
defined('SOCKET_EIO') ? SOCKET_EIO : 5
));
}
public function testEmitInvalidSocks4DataDuringSessionWillRejectConnection()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
$stream->expects($this->once())->method('close');
$promise = \React\Promise\resolve($stream);
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);
$promise = $this->client->connect('google.com:80');
$stream->emit('data', array("HTTP/1.1 400 Bad Request\r\n\r\n"));
$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
'Connection to tcp://google.com:80 failed because proxy returned invalid response (EBADMSG)',
defined('SOCKET_EBADMSG') ? SOCKET_EBADMSG: 71
));
}
public function testEmitInvalidSocks5DataDuringSessionWillRejectConnection()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
$stream->expects($this->once())->method('close');
$promise = \React\Promise\resolve($stream);
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);
$this->client = new Client('socks5://127.0.0.1:1080', $this->connector);
$promise = $this->client->connect('google.com:80');
$stream->emit('data', array("HTTP/1.1 400 Bad Request\r\n\r\n"));
$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
'Connection to tcp://google.com:80 failed because proxy returned invalid response (EBADMSG)',
defined('SOCKET_EBADMSG') ? SOCKET_EBADMSG: 71
));
}
public function testEmitSocks5DataErrorDuringSessionWillRejectConnection()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
$stream->expects($this->once())->method('close');
$promise = \React\Promise\resolve($stream);
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);
$this->client = new Client('socks5://127.0.0.1:1080', $this->connector);
$promise = $this->client->connect('google.com:80');
$stream->emit('data', array("\x05\x00" . "\x05\x01\x00\x00"));
$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
'Connection to tcp://google.com:80 failed because proxy refused connection with general server failure (ECONNREFUSED)',
defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111
));
}
public function testEmitSocks5DataInvalidAuthenticationMethodWillRejectConnection()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
$stream->expects($this->once())->method('close');
$promise = \React\Promise\resolve($stream);
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);
$this->client = new Client('socks5://127.0.0.1:1080', $this->connector);
$promise = $this->client->connect('google.com:80');
$stream->emit('data', array("\x05\x01"));
$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
'Connection to tcp://google.com:80 failed because proxy denied access due to unsupported authentication method (EACCES)',
defined('SOCKET_EACCES') ? SOCKET_EACCES : 13
));
}
public function testEmitSocks5DataInvalidAuthenticationDetailsWillRejectConnection()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
$stream->expects($this->once())->method('close');
$promise = \React\Promise\resolve($stream);
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);
$this->client = new Client('socks5://user:pass@127.0.0.1:1080', $this->connector);
$promise = $this->client->connect('google.com:80');
$stream->emit('data', array("\x05\x02" . "\x01\x01"));
$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
'Connection to tcp://google.com:80 failed because proxy denied access with given authentication details (EACCES)',
defined('SOCKET_EACCES') ? SOCKET_EACCES : 13
));
}
public function testEmitSocks5DataInvalidAddressTypeWillRejectConnection()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
$stream->expects($this->once())->method('close');
$promise = \React\Promise\resolve($stream);
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);
$this->client = new Client('socks5://127.0.0.1:1080', $this->connector);
$promise = $this->client->connect('google.com:80');
$stream->emit('data', array("\x05\x00" . "\x05\x00\x00\x00"));
$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
'Connection to tcp://google.com:80 failed because proxy returned invalid response (EBADMSG)',
defined('SOCKET_EBADMSG') ? SOCKET_EBADMSG: 71
));
}
public function testEmitSocks4DataInvalidResponseWillRejectConnection()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
$stream->expects($this->once())->method('close');
$promise = \React\Promise\resolve($stream);
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);
$this->client = new Client('socks4://127.0.0.1:1080', $this->connector);
$promise = $this->client->connect('google.com:80');
$stream->emit('data', array("\x00\x55" . "\x00\x00" . "\x00\x00\x00\x00"));
$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
'Connection to tcp://google.com:80 failed because proxy refused connection with error code 0x55 (ECONNREFUSED)',
defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111
));
}
public function testEmitSocks5DataIpv6AddressWillResolveConnection()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
$stream->expects($this->never())->method('close');
$promise = \React\Promise\resolve($stream);
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=%3A%3A1')->willReturn($promise);
$this->client = new Client('socks5://127.0.0.1:1080', $this->connector);
$promise = $this->client->connect('[::1]:80');
$stream->emit('data', array("\x05\x00" . "\x05\x00\x00\x04" . inet_pton('::1') . "\x00\x50"));
$promise->then($this->expectCallableOnce());
}
public function testEmitSocks5DataHostnameAddressWillResolveConnection()
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
$stream->expects($this->never())->method('close');
$promise = \React\Promise\resolve($stream);
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);
$this->client = new Client('socks5://127.0.0.1:1080', $this->connector);
$promise = $this->client->connect('google.com:80');
$stream->emit('data', array("\x05\x00" . "\x05\x00\x00\x03\x0Agoogle.com\x00\x50"));
$promise->then($this->expectCallableOnce());
}
public function provideConnectionErrors()
{
return array(
array(
Server::ERROR_GENERAL,
defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111,
'failed because proxy refused connection with general server failure (ECONNREFUSED)'
),
array(
Server::ERROR_NOT_ALLOWED_BY_RULESET,
defined('SOCKET_EACCES') ? SOCKET_EACCES : 13,
'failed because proxy denied access due to ruleset (EACCES)'
),
array(
Server::ERROR_NETWORK_UNREACHABLE,
defined('SOCKET_ENETUNREACH') ? SOCKET_ENETUNREACH : 101,
'failed because proxy reported network unreachable (ENETUNREACH)'
),
array(
Server::ERROR_HOST_UNREACHABLE,
defined('SOCKET_EHOSTUNREACH') ? SOCKET_EHOSTUNREACH : 113,
'failed because proxy reported host unreachable (EHOSTUNREACH)'
),
array(
Server::ERROR_CONNECTION_REFUSED,
defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111,
'failed because proxy reported connection refused (ECONNREFUSED)'
),
array(
Server::ERROR_TTL,
defined('SOCKET_ETIMEDOUT') ? SOCKET_ETIMEDOUT : 110,
'failed because proxy reported TTL/timeout expired (ETIMEDOUT)'
),
array(
Server::ERROR_COMMAND_UNSUPPORTED,
defined('SOCKET_EPROTO') ? SOCKET_EPROTO : 71,
'failed because proxy does not support the CONNECT command (EPROTO)'
),
array(
Server::ERROR_ADDRESS_UNSUPPORTED,
defined('SOCKET_EPROTO') ? SOCKET_EPROTO : 71,
'failed because proxy does not support this address type (EPROTO)'
),
array(
200,
defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111,
'failed because proxy server refused connection with unknown error code 0xC8 (ECONNREFUSED)'
)
);
}
/**
* @dataProvider provideConnectionErrors
* @param int $error
* @param int $expectedCode
* @param string $expectedMessage
*/
public function testEmitSocks5DataErrorMapsToExceptionCode($error, $expectedCode, $expectedMessage)
{
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
$stream->expects($this->once())->method('close');
$promise = \React\Promise\resolve($stream);
$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);
$this->client = new Client('socks5://127.0.0.1:1080', $this->connector);
$promise = $this->client->connect('google.com:80');
$stream->emit('data', array("\x05\x00" . "\x05" . chr($error) . "\x00\x00"));
$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
'Connection to tcp://google.com:80 ' . $expectedMessage,
$expectedCode
));
}
public function testConnectionErrorShouldNotCreateGarbageCycles()
{
if (class_exists('React\Promise\When')) {
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}
$deferred = new Deferred();
$this->connector->expects($this->once())->method('connect')->willReturn($deferred->promise());
gc_collect_cycles();
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
$promise = $this->client->connect('google.com:80');
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
$deferred->reject(new \RuntimeException());
unset($deferred, $promise);
$this->assertEquals(0, gc_collect_cycles());
}
public function testCancelConnectionDuringConnectionShouldNotCreateGarbageCycles()
{
if (class_exists('React\Promise\When')) {
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}
$promise = new Promise(function () { }, function () {
throw new \RuntimeException();
});
$this->connector->expects($this->once())->method('connect')->willReturn($promise);
gc_collect_cycles();
$promise = $this->client->connect('google.com:80');
$promise->cancel();
unset($promise);
$this->assertEquals(0, gc_collect_cycles());
}
public function testCancelConnectionDuringSessionShouldNotCreateGarbageCycles()
{
if (class_exists('React\Promise\When')) {
$this->markTestSkipped('Not supported on legacy Promise v1 API');
}
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
$stream->expects($this->once())->method('close');
$promise = new Promise(function ($resolve) use ($stream) { $resolve($stream); });
$this->connector->expects($this->once())->method('connect')->willReturn($promise);
gc_collect_cycles();
$promise = $this->client->connect('google.com:80');
$promise->cancel();
unset($promise);
$this->assertEquals(0, gc_collect_cycles());
}
}