1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TBolier\RethinkQL\UnitTest\Connection\Socket; |
5
|
|
|
|
6
|
|
|
use Mockery\MockInterface; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use TBolier\RethinkQL\Connection\Socket\Handshake; |
9
|
|
|
|
10
|
|
|
class HandshakeTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Handshake |
14
|
|
|
*/ |
15
|
|
|
private $handshake; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
public function setUp(): void |
21
|
|
|
{ |
22
|
|
|
$this->handshake = new Handshake('foo', 'bar', 42); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\Socket\Exception |
27
|
|
|
* @expectedExceptionMessage Not connected |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function testExceptionThrownOnStreamNotWritable(): void |
31
|
|
|
{ |
32
|
|
|
$stream = \Mockery::mock('\Psr\Http\Message\StreamInterface'); |
33
|
|
|
$stream->shouldReceive('isWritable')->andReturn(false); |
|
|
|
|
34
|
|
|
$stream->shouldReceive('close'); |
35
|
|
|
|
36
|
|
|
$this->handshake->hello($stream); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\Socket\Exception |
41
|
|
|
* @expectedExceptionMessage Foobar |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function testExceptionThrownOnError(): void |
45
|
|
|
{ |
46
|
|
|
$stream = \Mockery::mock('\Psr\Http\Message\StreamInterface'); |
47
|
|
|
$stream->shouldReceive('isWritable')->andReturn(true); |
|
|
|
|
48
|
|
|
$stream->shouldReceive('close'); |
49
|
|
|
$stream->shouldReceive('write'); |
50
|
|
|
$stream->shouldReceive('getContents')->andReturn('ERROR: Foobar'); |
51
|
|
|
|
52
|
|
|
$this->handshake->hello($stream); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\Socket\Exception |
57
|
|
|
* @expectedExceptionMessage Foobar |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function testExceptionThrownOnVerifyProtocolWithError(): void |
61
|
|
|
{ |
62
|
|
|
$stream = \Mockery::mock('\Psr\Http\Message\StreamInterface'); |
63
|
|
|
$stream->shouldReceive('isWritable')->andReturn(true); |
|
|
|
|
64
|
|
|
$stream->shouldReceive('close'); |
65
|
|
|
$stream->shouldReceive('write'); |
66
|
|
|
$stream->shouldReceive('getContents')->andReturn('{"success":false, "error": "Foobar"}'); |
67
|
|
|
|
68
|
|
|
$this->handshake->hello($stream); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\Socket\Exception |
73
|
|
|
* @expectedExceptionMessage Unsupported protocol version. |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function testExceptionThrownOnInvalidProtocolVersion(): void |
77
|
|
|
{ |
78
|
|
|
$stream = \Mockery::mock('\Psr\Http\Message\StreamInterface'); |
79
|
|
|
$stream->shouldReceive('isWritable')->andReturn(true); |
|
|
|
|
80
|
|
|
$stream->shouldReceive('close'); |
81
|
|
|
$stream->shouldReceive('write'); |
82
|
|
|
$stream->shouldReceive('getContents')->andReturn('{"success":true, "max_protocol_version": 1, "min_protocol_version": 1}'); |
83
|
|
|
|
84
|
|
|
$this->handshake->hello($stream); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\Socket\Exception |
90
|
|
|
* @expectedExceptionMessage Woops! |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public function testExceptionThrownOnProtocolError(): void |
94
|
|
|
{ |
95
|
|
|
/** @var MockInterface $stream */ |
96
|
|
|
$stream = \Mockery::mock('\Psr\Http\Message\StreamInterface'); |
97
|
|
|
$stream->shouldReceive('isWritable')->andReturn(true); |
|
|
|
|
98
|
|
|
$stream->shouldReceive('close'); |
99
|
|
|
$stream->shouldReceive('write'); |
100
|
|
|
$stream->shouldReceive('getContents')->andReturn('ERROR: Woops!'); |
101
|
|
|
|
102
|
|
|
$this->handshake->hello($stream); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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: