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 | abstract class ConnectionTestCase 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 callable |
||||
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 () { |
||||
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, |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
63 | 'test', |
||||
64 | $this->querySerializer, |
||||
0 ignored issues
–
show
$this->querySerializer of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type Symfony\Component\Serializer\SerializerInterface expected by parameter $querySerializer of TBolier\RethinkQL\Connec...nnection::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
65 | $this->responseSerializer |
||||
0 ignored issues
–
show
$this->responseSerializer of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type Symfony\Component\Serializer\SerializerInterface expected by parameter $responseSerializer of TBolier\RethinkQL\Connec...nnection::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
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 MockInterface $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 \stdClass |
||||
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 |