Passed
Pull Request — master (#14)
by Timon
02:48
created

BaseConnectionTestCase::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TBolier\RethinkQL\UnitTest\Connection;
5
6
use Mockery;
7
use Mockery\MockInterface;
8
use Psr\Http\Message\StreamInterface;
9
use Symfony\Component\Serializer\SerializerInterface;
10
use TBolier\RethinkQL\Connection\Connection;
11
use TBolier\RethinkQL\Connection\ConnectionInterface;
12
use TBolier\RethinkQL\Connection\Socket\HandshakeInterface;
13
use TBolier\RethinkQL\UnitTest\BaseUnitTestCase;
14
15
class BaseConnectionTestCase extends BaseUnitTestCase
16
{
17
    /**
18
     * @var Connection
19
     */
20
    protected $connection;
21
22
    /**
23
     * @var MockInterface
24
     */
25
    protected $handshake;
26
27
    /**
28
     * @var MockInterface
29
     */
30
    protected $querySerializer;
31
32
    /**
33
     * @var MockInterface
34
     */
35
    protected $responseSerializer;
36
37
    /**
38
     * @var MockInterface
39
     */
40
    protected $stream;
41
42
    /**
43
     * @var MockInterface
44
     */
45
    protected $streamWrapper;
46
47
    protected function setUp()
48
    {
49
        parent::setUp();
50
51
        $this->stream = Mockery::mock(StreamInterface::class);
52
        $this->streamWrapper = function () {
0 ignored issues
show
Documentation Bug introduced by
It seems like function () { return $this->stream; } of type object<Closure> is incompatible with the declared type object<Mockery\MockInterface> of property $streamWrapper.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53
            return $this->stream;
54
        };
55
56
        $this->handshake = Mockery::mock(HandshakeInterface::class);
57
        $this->querySerializer = Mockery::mock(SerializerInterface::class);
58
        $this->responseSerializer = Mockery::mock(SerializerInterface::class);
59
60
        $this->connection = new Connection(
61
            $this->streamWrapper,
62
            $this->handshake,
63
            'test',
64
            $this->querySerializer,
65
            $this->responseSerializer
66
        );
67
    }
68
69
    /**
70
     * @return void
71
     */
72
    protected function connect(): void
73
    {
74
        try {
75
            $this->handshake->shouldReceive('hello')->once();
76
            /** @var ConnectionInterface $connection */
77
            $this->assertInstanceOf(ConnectionInterface::class, $this->connection->connect());
78
        } catch (\Exception $e) {
79
            $this->fail($e->getMessage());
80
        }
81
    }
82
83
    /**
84
     * @param mixed|null $response
85
     * @return void
86
     */
87
    protected function setExpectations($response = null): void
88
    {
89
        $this->querySerializer->shouldReceive('serialize')->atLeast()->andReturn("['serialized': true]");
90
91
        $buffer = new \stdClass();
92
        $this->catchStreamWrite($buffer);
93
94
        if ($response) {
95
            $this->catchStreamRead(4 + 8, $buffer);
96
            $this->catchStreamRead(20, $buffer);
97
98
            $this->responseSerializer->shouldReceive('deserialize')->atLeast()->andReturn($response);
99
        }
100
    }
101
102
    /**
103
     * @param \stdClass $buffer
104
     * @return mixed
105
     */
106
    protected function catchStreamWrite(\stdClass $buffer)
107
    {
108
        $this->stream->shouldReceive('write')->andReturnUsing(function ($string) use ($buffer) {
109
            $buffer->data = $string;
110
111
            return 20;
112
        });
113
114
        return $buffer;
115
    }
116
117
    protected function catchStreamRead(int $bytes, \stdClass $buffer): void
118
    {
119
        $this->stream->shouldReceive('read')->once()->with($bytes)->andReturnUsing(function ($bytes) use (&$buffer) {
120
            $data = '';
121
            if (isset($buffer->data)) {
122
                $data = substr($buffer->data, 0, $bytes);
123
            }
124
125
            return $data;
126
        });
127
    }
128
}
129