1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TBolier\RethinkQL\UnitTest\Connection\Socket; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use TBolier\RethinkQL\Connection\Socket\Handshake; |
8
|
|
|
|
9
|
|
|
class HandshakeTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Handshake |
13
|
|
|
*/ |
14
|
|
|
private $handshake; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
public function setUp(): void |
20
|
|
|
{ |
21
|
|
|
$this->handshake = new Handshake('foo', 'bar', 42); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\Socket\Exception |
26
|
|
|
* @expectedExceptionMessage Not connected |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
public function testExceptionThrownOnStreamNotWritable(): void |
30
|
|
|
{ |
31
|
|
|
$stream = \Mockery::mock('\Psr\Http\Message\StreamInterface'); |
32
|
|
|
$stream->shouldReceive('isWritable')->andReturn(false); |
|
|
|
|
33
|
|
|
$stream->shouldReceive('close'); |
34
|
|
|
|
35
|
|
|
$this->handshake->hello($stream); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\Socket\Exception |
40
|
|
|
* @expectedExceptionMessage Foobar |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
public function testExceptionThrownOnError(): void |
44
|
|
|
{ |
45
|
|
|
$stream = \Mockery::mock('\Psr\Http\Message\StreamInterface'); |
46
|
|
|
$stream->shouldReceive('isWritable')->andReturn(true); |
|
|
|
|
47
|
|
|
$stream->shouldReceive('close'); |
48
|
|
|
$stream->shouldReceive('write'); |
49
|
|
|
$stream->shouldReceive('getContents')->andReturn('ERROR: Foobar'); |
50
|
|
|
|
51
|
|
|
$this->handshake->hello($stream); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\Socket\Exception |
56
|
|
|
* @expectedExceptionMessage Foobar |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function testExceptionThrownOnVerifyProtocolWithError(): void |
60
|
|
|
{ |
61
|
|
|
$stream = \Mockery::mock('\Psr\Http\Message\StreamInterface'); |
62
|
|
|
$stream->shouldReceive('isWritable')->andReturn(true); |
|
|
|
|
63
|
|
|
$stream->shouldReceive('close'); |
64
|
|
|
$stream->shouldReceive('write'); |
65
|
|
|
$stream->shouldReceive('getContents')->andReturn('{"success":false, "error": "Foobar"}'); |
66
|
|
|
|
67
|
|
|
$this->handshake->hello($stream); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\Socket\Exception |
72
|
|
|
* @expectedExceptionMessage Unsupported protocol version. |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function testExceptionThrownOnInvalidProtocolVersion(): void |
76
|
|
|
{ |
77
|
|
|
$stream = \Mockery::mock('\Psr\Http\Message\StreamInterface'); |
78
|
|
|
$stream->shouldReceive('isWritable')->andReturn(true); |
|
|
|
|
79
|
|
|
$stream->shouldReceive('close'); |
80
|
|
|
$stream->shouldReceive('write'); |
81
|
|
|
$stream->shouldReceive('getContents')->andReturn('{"success":true, "max_protocol_version": 1, "min_protocol_version": 1}'); |
82
|
|
|
|
83
|
|
|
$this->handshake->hello($stream); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\Socket\Exception |
89
|
|
|
* @expectedExceptionMessage Woops! |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
public function testExceptionThrownOnProtocolError(): void |
93
|
|
|
{ |
94
|
|
|
$stream = \Mockery::mock('\Psr\Http\Message\StreamInterface'); |
95
|
|
|
$stream->shouldReceive('isWritable')->andReturn(true); |
|
|
|
|
96
|
|
|
$stream->shouldReceive('close'); |
97
|
|
|
$stream->shouldReceive('write'); |
98
|
|
|
$stream->shouldReceive('getContents')->andReturn('ERROR: Woops!'); |
99
|
|
|
|
100
|
|
|
$this->handshake->hello($stream); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: