Test Setup Failed
Pull Request — master (#14)
by Timon
05:00
created

ConnectionTestCase::setExpectations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TBolier\RethinkQL\UnitTest\Connection;
5
6
use Mockery;
7
use Mockery\MockInterface;
8
use PHPUnit\Framework\TestCase;
9
use Psr\Http\Message\StreamInterface;
10
use Symfony\Component\Serializer\SerializerInterface;
11
use TBolier\RethinkQL\Connection\Connection;
12
use TBolier\RethinkQL\Connection\ConnectionInterface;
13
use TBolier\RethinkQL\Connection\Socket\HandshakeInterface;
14
15
class ConnectionTestCase extends TestCase
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->stream->shouldReceive('isWritable')->once()->andReturnTrue();
90
91
        $this->querySerializer->shouldReceive('serialize')->once()->andReturn("['serialized': true]");
92
93
        $buffer = new \stdClass();
94
        $this->catchStreamWrite($buffer);
95
        $this->catchStreamRead(4 + 8, $buffer);
96
        $this->catchStreamRead(20, $buffer);
97
98
        $this->responseSerializer->shouldReceive('deserialize')->once()->andReturn($response);
99
    }
100
101
    /**
102
     * @param \stdClass $buffer
103
     * @return mixed
104
     */
105
    private function catchStreamWrite(\stdClass $buffer)
106
    {
107
        $this->stream->shouldReceive('write')->once()->andReturnUsing(function ($string) use ($buffer) {
108
            $buffer->data = $string;
109
110
            return 20;
111
        });
112
113
        return $buffer;
114
    }
115
116
    private function catchStreamRead(int $bytes, \stdClass $buffer): void
117
    {
118
        $this->stream->shouldReceive('read')->once()->with($bytes)->andReturnUsing(function ($bytes) use (&$buffer) {
119
            $binaryHeader = substr($buffer->data, 0, $bytes);
120
121
            return $binaryHeader;
122
        });
123
    }
124
}
125