HandshakeTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 32
c 1
b 0
f 0
dl 0
loc 94
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A testExceptionThrownOnInvalidProtocolVersion() 0 10 1
A testExceptionThrownOnError() 0 9 1
A testExceptionThrownOnVerifyProtocolWithError() 0 9 1
A testExceptionThrownOnProtocolError() 0 10 1
A testExceptionThrownOnStreamNotWritable() 0 7 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace TBolier\RethinkQL\UnitTest\Connection\Socket;
5
6
use Mockery\MockInterface;
7
use TBolier\RethinkQL\Connection\Socket\Handshake;
8
use TBolier\RethinkQL\UnitTest\BaseUnitTestCase;
9
use Psr\Http\Message\StreamInterface;
10
11
class HandshakeTest extends BaseUnitTestCase
12
{
13
    /**
14
     * @var Handshake
15
     */
16
    private $handshake;
17
18
    /**
19
     * @return void
20
     */
21
    public function setUp(): void
22
    {
23
        $this->handshake = new Handshake('foo', 'bar', 42);
24
    }
25
26
    /**
27
     * @expectedException \TBolier\RethinkQL\Connection\Socket\Exception
28
     * @expectedExceptionMessage Not connected
29
     * @return void
30
     */
31
    public function testExceptionThrownOnStreamNotWritable(): void
32
    {
33
        $stream = \Mockery::mock(StreamInterface::class);
34
        $stream->shouldReceive('isWritable')->atLeast()->andReturn(false);
35
        $stream->shouldReceive('close');
36
37
        $this->handshake->hello($stream);
0 ignored issues
show
Bug introduced by
$stream of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type Psr\Http\Message\StreamInterface expected by parameter $stream of TBolier\RethinkQL\Connec...cket\Handshake::hello(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        $this->handshake->hello(/** @scrutinizer ignore-type */ $stream);
Loading history...
38
    }
39
40
    /**
41
     * @expectedException \TBolier\RethinkQL\Connection\Socket\Exception
42
     * @expectedExceptionMessage Foobar
43
     * @return void
44
     */
45
    public function testExceptionThrownOnError(): void
46
    {
47
        $stream = \Mockery::mock(StreamInterface::class);
48
        $stream->shouldReceive('isWritable')->atLeast()->andReturn(true);
49
        $stream->shouldReceive('close');
50
        $stream->shouldReceive('write');
51
        $stream->shouldReceive('getContents')->atLeast()->andReturn('ERROR: Foobar');
52
53
        $this->handshake->hello($stream);
0 ignored issues
show
Bug introduced by
$stream of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type Psr\Http\Message\StreamInterface expected by parameter $stream of TBolier\RethinkQL\Connec...cket\Handshake::hello(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $this->handshake->hello(/** @scrutinizer ignore-type */ $stream);
Loading history...
54
    }
55
56
    /**
57
     * @expectedException \TBolier\RethinkQL\Connection\Socket\Exception
58
     * @expectedExceptionMessage Foobar
59
     * @return void
60
     */
61
    public function testExceptionThrownOnVerifyProtocolWithError(): void
62
    {
63
        $stream = \Mockery::mock(StreamInterface::class);
64
        $stream->shouldReceive('isWritable')->atLeast()->andReturn(true);
65
        $stream->shouldReceive('close');
66
        $stream->shouldReceive('write');
67
        $stream->shouldReceive('getContents')->atLeast()->andReturn('{"success":false, "error": "Foobar"}');
68
69
        $this->handshake->hello($stream);
0 ignored issues
show
Bug introduced by
$stream of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type Psr\Http\Message\StreamInterface expected by parameter $stream of TBolier\RethinkQL\Connec...cket\Handshake::hello(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
        $this->handshake->hello(/** @scrutinizer ignore-type */ $stream);
Loading history...
70
    }
71
72
    /**
73
     * @expectedException \TBolier\RethinkQL\Connection\Socket\Exception
74
     * @expectedExceptionMessage Unsupported protocol version.
75
     * @return void
76
     */
77
    public function testExceptionThrownOnInvalidProtocolVersion(): void
78
    {
79
        $stream = \Mockery::mock(StreamInterface::class);
80
        $stream->shouldReceive('isWritable')->atLeast()->andReturn(true);
81
        $stream->shouldReceive('close');
82
        $stream->shouldReceive('write');
83
        $stream->shouldReceive('getContents')->atLeast()
84
            ->andReturn('{"success":true, "max_protocol_version": 1, "min_protocol_version": 1}');
85
86
        $this->handshake->hello($stream);
0 ignored issues
show
Bug introduced by
$stream of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type Psr\Http\Message\StreamInterface expected by parameter $stream of TBolier\RethinkQL\Connec...cket\Handshake::hello(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        $this->handshake->hello(/** @scrutinizer ignore-type */ $stream);
Loading history...
87
    }
88
89
90
    /**
91
     * @expectedException \TBolier\RethinkQL\Connection\Socket\Exception
92
     * @expectedExceptionMessage Woops!
93
     * @return void
94
     */
95
    public function testExceptionThrownOnProtocolError(): void
96
    {
97
        /** @var MockInterface $stream */
98
        $stream = \Mockery::mock(StreamInterface::class);
99
        $stream->shouldReceive('isWritable')->atLeast()->andReturn(true);
100
        $stream->shouldReceive('close');
101
        $stream->shouldReceive('write');
102
        $stream->shouldReceive('getContents')->atLeast()->andReturn('ERROR: Woops!');
103
104
        $this->handshake->hello($stream);
105
    }
106
}
107