Passed
Branch master (69e5ee)
by Timon
05:22
created

ConnectionTest::testConnectWithOpenConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TBolier\RethinkQL\Connection;
5
6
use Exception;
7
use Mockery;
8
use Mockery\MockInterface;
9
use TBolier\RethinkQL\Connection\Socket\Socket;
10
use TBolier\RethinkQL\IntegrationTest\BaseTestCase;
11
use TBolier\RethinkQL\Response\Cursor;
12
use TBolier\RethinkQL\Response\ResponseInterface;
13
use TBolier\RethinkQL\Types\Query\QueryType;
14
use TBolier\RethinkQL\Types\Response\ResponseType;
15
16
function unpack($format = '', $data = '', $offset = 0)
17
{
18
    return ConnectionTest::$functions->unpack($format, $data, $offset);
0 ignored issues
show
Bug introduced by
The method unpack() does not seem to exist on object<Mockery\MockInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
19
}
20
21
function random_int($min = 0, $max = 0)
22
{
23
    return ConnectionTest::$functions->random_int($min, $max);
0 ignored issues
show
Bug introduced by
The method random_int() does not seem to exist on object<Mockery\MockInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24
}
25
26
class ConnectionTest extends BaseTestCase
27
{
28
    /**
29
     * @var MockInterface
30
     */
31
    public static $functions;
32
33
    /**
34
     * @var MockInterface
35
     */
36
    private $optionsMock;
37
38
    /**
39
     * @return void
40
     */
41
    public function setUp(): void
42
    {
43
        parent::setUp();
44
45
        self::$functions = Mockery::mock();
46
47
        $this->useDefaultInternals();
48
49
        $this->optionsMock = Mockery::mock(OptionsInterface::class);
50
    }
51
52
    /**
53
     * @throws \Exception
54
     *
55
     * @return void
56
     */
57
    public function testConnect(): void
58
    {
59
        /** @var ConnectionInterface $connection */
60
        $connection = $this->createConnection('phpunit_default')->connect();
61
        $result = $connection->expr('foo');
62
63
        $this->assertInstanceOf(ResponseInterface::class, $result);
64
        $this->assertEquals('foo', $result->getData()[0]);
65
    }
66
67
    /**
68
     * @throws \Exception
69
     *
70
     * @return void
71
     */
72
    public function testConnectWithOpenConnection(): void
73
    {
74
        /** @var ConnectionInterface $connection */
75
        $connection = $this->createConnection('phpunit_default')->connect();
76
        $connection2 = $connection->connect();
77
78
        $this->assertSame($connection, $connection2);
79
    }
80
81
    /**
82
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
83
     * @expectedExceptionMessage Test exception
84
     * @return void
85
     */
86
    public function testConnectThrowsCorrectException(): void
87
    {
88
        $options = new Options(PHPUNIT_CONNECTIONS['phpunit_default']);
89
90
        $handshakeMock = Mockery::mock('\TBolier\RethinkQL\Connection\Socket\HandshakeInterface');
91
        $handshakeMock->shouldReceive('hello')->andThrow(new \Exception('Test exception', 404));
92
93
        $serializerMock = Mockery::mock('\Symfony\Component\Serializer\SerializerInterface');
94
95
        $connection = new Connection(
96
            function () use ($options) {
97
                return new Socket(
98
                    $options
99
                );
100
            },
101
            $handshakeMock,
102
            $options->getDbname(),
103
            $serializerMock,
104
            $serializerMock
105
        );
106
107
        $connection->connect();
108
    }
109
110
    /**
111
     * @return void
112
     */
113
    public function testServer(): void
114
    {
115
        /** @var ConnectionInterface $connection */
116
        $res = $this->createConnection('phpunit_default')->connect()->server();
117
118
        $this->assertEquals(QueryType::SERVER_INFO, $res->getType());
119
        $this->assertInternalType('string', $res->getData()[0]['name']);
120
    }
121
122
    /**
123
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
124
     * @expectedExceptionMessage Not connected
125
     */
126
    public function testServerThrowsNotConnectedException(): void
127
    {
128
        /** @var ConnectionInterface $connection */
129
        $this->createConnection('phpunit_default')->server();
130
    }
131
132
    /**
133
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
134
     */
135
    public function testServerThrowsExceptionOnConnectionException(): void
136
    {
137
        $options = new Options(PHPUNIT_CONNECTIONS['phpunit_default']);
138
139
        $handshakeMock = Mockery::mock('\TBolier\RethinkQL\Connection\Socket\HandshakeInterface');
140
        $handshakeMock->shouldReceive('hello');
141
142
        $serializerMock = Mockery::mock('\Symfony\Component\Serializer\SerializerInterface');
143
144
        $connection = new Connection(
145
            function () use ($options) {
146
                return new Socket(
147
                    $options
148
                );
149
            },
150
            $handshakeMock,
151
            $options->getDbname(),
152
            $serializerMock,
153
            $serializerMock
154
        );
155
156
        /** @var ConnectionInterface $connection */
157
        $connection->connect()->server();
158
    }
159
160
    /**
161
     * @return void
162
     */
163
    public function testClose(): void
164
    {
165
        $connection = $this->createConnection('phpunit_default')->connect();
166
167
        $this->assertTrue($connection->isStreamOpen());
168
169
        $connection->close(false);
170
171
        $this->assertFalse($connection->isStreamOpen());
172
    }
173
174
    /**
175
     * @return void
176
     */
177
    public function testCloseNoReplyWait(): void
178
    {
179
        $connection = $this->createConnection('phpunit_default')->connect();
180
181
        $this->assertTrue($connection->isStreamOpen());
182
183
        $connection->close(true);
184
185
        $this->assertFalse($connection->isStreamOpen());
186
    }
187
188
    /**
189
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
190
     * @expectedExceptionMessage Not connected
191
     * @return void
192
     */
193
    public function testCloseThrowsExceptionOnNotConnected(): void
194
    {
195
        $connection = $this->createConnection('phpunit_default');
196
        $connection->close();
197
    }
198
199
    /**
200
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
201
     * @expectedExceptionMessage Not connected
202
     * @return void
203
     */
204
    public function testRunThrowsExceptionOnNotConnected(): void
205
    {
206
        $connection = $this->createConnection('phpunit_default');
207
        $connection->run(Mockery::mock('\TBolier\RethinkQL\Query\MessageInterface'));
208
    }
209
210
    /**
211
     * @return void
212
     */
213
    public function testRunNoReply(): void
214
    {
215
        $message = $this->setUpMessageMock();
216
217
        $connection = $this->createConnection('phpunit_default')->connect();
218
219
        $reply = $connection->runNoReply($message);
220
221
        $connection->close(false);
222
223
        $this->assertNull($reply);
224
    }
225
226
    /**
227
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
228
     * @expectedExceptionMessage Generating the token failed.
229
     * @return void
230
     */
231
    public function testNoReplyException(): void
232
    {
233
        self::$functions->shouldReceive('random_int')->andThrow(Exception::class, 'foo')->byDefault();
234
235
        $connection = $this->createConnection('phpunit_default')->connect();
236
237
        $connection->close(true);
238
    }
239
240
    /**
241
     * @return void
242
     */
243
    public function testRunPartialSuccess(): void
244
    {
245
        $expectedToken = [
246
            'token' => 1,
247
            'token2' => 0,
248
            'size' => 19
249
        ];
250
251
252
        self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken);
253
        self::$functions->shouldReceive('random_int')->andReturn(1);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
254
255
        $message = $this->setUpMessageMock();
256
257
        $responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface');
258
        $responseMock->shouldReceive('getType')->andReturn(ResponseType::SUCCESS_PARTIAL);
259
        $responseMock->shouldReceive('getData')->andReturn([]);
260
261
        $connection = $this->setUpMockConnection($responseMock);
262
263
        $connection = $connection->connect();
264
265
        /** @var Cursor $reply */
266
        $reply = $connection->run($message);
267
268
        $connection->close(false);
269
270
        $this->assertInstanceOf(Cursor::class, $reply);
271
    }
272
273
274
    /**
275
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
276
     * @expectedExceptionMessage Serializing query message failed.
277
     * @return void
278
     */
279
    public function testIfRunThrowsException(): void
280
    {
281
        /** @var ConnectionInterface $connection */
282
        $connection = $this->createConnection('phpunit_default')->connect();
283
284
        $messageMock = Mockery::mock('\TBolier\RethinkQL\Query\Message');
285
        $messageMock->shouldReceive('setOptions');
286
287
        $connection->run($messageMock);
288
    }
289
290
    /**
291
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
292
     * @expectedExceptionMessage Generating the token failed.
293
     * @return void
294
     */
295
    public function testIfGenerateTokenThrowsException(): void
296
    {
297
        self::$functions = Mockery::mock();
298
299
        self::$functions->shouldReceive('random_int')->andThrow(Exception::class, 'foo')->byDefault();
300
301
        /** @var ConnectionInterface $connection */
302
        $connection = $this->createConnection('phpunit_default')->connect();
303
304
        $messageMock = Mockery::mock('\TBolier\RethinkQL\Query\Message');
305
        $messageMock->shouldReceive('setOptions');
306
307
        $connection->run($messageMock);
308
    }
309
310
    /**
311
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
312
     * @expectedExceptionMessage Response message has no type.
313
     * @return void
314
     */
315
    public function testConnectionExceptionThrownOnNoResponseType(): void
316
    {
317
        $expectedToken = [
318
            'token' => 1,
319
            'token2' => 0,
320
            'size' => 19
321
        ];
322
323
        self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken);
324
        self::$functions->shouldReceive('random_int')->andReturn(1);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
325
326
        $message = $this->setUpMessageMock();
327
328
        $responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface');
329
        $responseMock->shouldReceive('getType')->andReturn(0);
330
331
        $connection = $this->setUpMockConnection($responseMock);
332
        $connection = $connection->connect();
333
334
        $reply = $connection->run($message);
335
336
        $connection->close(false);
337
338
        $this->assertEquals($responseMock, $reply);
339
    }
340
341
    /**
342
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
343
     * @expectedExceptionMessage Compile error: foo, jsonQuery: "{}"
344
     * @return void
345
     */
346
    public function testConnectionExceptionThrownOnCompileError(): void
347
    {
348
        $expectedToken = [
349
            'token' => 1,
350
            'token2' => 0,
351
            'size' => 19
352
        ];
353
354
        self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken);
355
        self::$functions->shouldReceive('random_int')->andReturn(1);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
356
357
        $message = $this->setUpMessageMock();
358
359
        $responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface');
360
        $responseMock->shouldReceive('getType')->andReturn(17);
361
        $responseMock->shouldReceive('getData')->andReturn([0=>'foo']);
362
363
        $connection = $this->setUpMockConnection($responseMock);
364
        $connection = $connection->connect();
365
366
        $reply = $connection->run($message);
367
368
        $connection->close(false);
369
370
        $this->assertEquals($responseMock, $reply);
371
    }
372
373
374
    /**
375
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
376
     * @expectedExceptionMessage Runtime error: foo, jsonQuery: "{}"
377
     * @return void
378
     */
379
    public function testConnectionExceptionThrownOnRuntimeError(): void
380
    {
381
        $expectedToken = [
382
            'token' => 1,
383
            'token2' => 0,
384
            'size' => 19
385
        ];
386
387
        self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken);
388
        self::$functions->shouldReceive('random_int')->andReturn(1);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
389
390
        $message = $this->setUpMessageMock();
391
392
        $responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface');
393
        $responseMock->shouldReceive('getType')->andReturn(18);
394
        $responseMock->shouldReceive('getData')->andReturn([0=>'foo']);
395
396
        $connection = $this->setUpMockConnection($responseMock);
397
        $connection = $connection->connect();
398
399
        $reply = $connection->run($message);
400
401
        $connection->close(false);
402
403
        $this->assertEquals($responseMock, $reply);
404
    }
405
406
    /**
407
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
408
     * @expectedExceptionMessage Received wrong token. Response does not match the request. Expected 1, received 11
409
     * @return void
410
     */
411
    public function testConnectionExceptionThrownOnInvalidResponseToken(): void
412
    {
413
        $expectedToken = [
414
            'token' => 11,
415
            'token2' => 0,
416
            'size' => 19
417
        ];
418
419
        self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken);
420
        self::$functions->shouldReceive('random_int')->andReturn(1);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
421
422
        $message = $this->setUpMessageMock();
423
424
        $responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface');
425
        $responseMock->shouldReceive('getType')->andReturn(18);
426
        $responseMock->shouldReceive('getData')->andReturn([0=>'foo']);
427
428
        $connection = $this->setUpMockConnection($responseMock);
429
        $connection = $connection->connect();
430
431
        $reply = $connection->run($message);
432
433
        $connection->close(false);
434
435
        $this->assertEquals($responseMock, $reply);
436
    }
437
438
    /**
439
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
440
     * @expectedExceptionMessage Invalid response from server: Invalid token.
441
     * @return void
442
     */
443
    public function testConnectionExceptionThrownOnInvalidResponseToken2(): void
444
    {
445
        $expectedToken = [
446
            'token' => 1,
447
            'token2' => 42,
448
            'size' => 19
449
        ];
450
451
        self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken);
452
        self::$functions->shouldReceive('random_int')->andReturn(1);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
453
454
        $message = $this->setUpMessageMock();
455
456
        $responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface');
457
        $responseMock->shouldReceive('getType')->andReturn(18);
458
        $responseMock->shouldReceive('getData')->andReturn([0=>'foo']);
459
460
        $connection = $this->setUpMockConnection($responseMock);
461
        $connection = $connection->connect();
462
463
        $reply = $connection->run($message);
464
465
        $connection->close(false);
466
467
        $this->assertEquals($responseMock, $reply);
468
    }
469
470
    /**
471
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
472
     * @expectedExceptionMessage Server says PHP-RQL is buggy:
473
     * @return void
474
     */
475
    public function testConnectionExceptionThrownOnClientErrorResponseType(): void
476
    {
477
        $expectedToken = [
478
            'token' => 1,
479
            'token2' => 0,
480
            'size' => 19
481
        ];
482
483
        self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken);
484
        self::$functions->shouldReceive('random_int')->andReturn(1);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
485
486
        $message = $this->setUpMessageMock();
487
488
        $responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface');
489
        $responseMock->shouldReceive('getType')->andReturn(16);
490
        $responseMock->shouldReceive('getData')->andReturn([0=>'foo']);
491
492
        $connection = $this->setUpMockConnection($responseMock);
493
        $connection = $connection->connect();
494
495
        $reply = $connection->run($message);
496
497
        $connection->close(false);
498
499
        $this->assertEquals($responseMock, $reply);
500
    }
501
502
    /**
503
     * @return void
504
     */
505
    public function testUse(): void
506
    {
507
        $connection = $this->createConnection('phpunit_default');
508
        $connection->use('foo');
509
510
        $reflection = new \ReflectionClass($connection);
511
        $dbName = $reflection->getProperty('dbName');
512
        $dbName->setAccessible(true);
513
        $this->assertEquals('foo', $dbName->getValue($connection));}
514
515
    /**
516
     * Resets the mocks to their PHP internal equivalents
517
     */
518
    private function useDefaultInternals(): void
519
    {
520
        // Reset expectations to use PHP built-in functions
521
        self::$functions = Mockery::mock();
522
523
        self::$functions->shouldReceive('random_int')->byDefault()->andReturnUsing(function ($min = 0, $max = 0) {
524
            return \random_int($min, $max);
525
        });
526
527
        self::$functions->shouldReceive('unpack')->byDefault()->andReturnUsing(function ($format = '', $data = '', $offset = 0) {
528
            return \unpack($format, $data, $offset);
529
        });
530
    }
531
532
    /**
533
     * @return MockInterface
534
     */
535
    private function setUpMessageMock(): MockInterface
536
    {
537
        $message = Mockery::mock('\TBolier\RethinkQL\Query\MessageInterface');
538
        $message->shouldReceive('setOptions')->andReturnSelf()->getMock();
539
        $message->shouldReceive('jsonSerialize')->andReturn('{}')->getMock();
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
540
541
        return $message;
542
    }
543
544
    /**
545
     * @return ConnectionInterface
546
     */
547
    private function setUpMockConnection(MockInterface $responseMock): ConnectionInterface
548
    {
549
        $handshakeMock = Mockery::mock('\TBolier\RethinkQL\Connection\Socket\HandshakeInterface');
550
        $handshakeMock->shouldReceive('hello')->andReturnSelf();
551
552
        $serializerMock = Mockery::mock('\Symfony\Component\Serializer\SerializerInterface');
553
        $serializerMock->shouldReceive('serialize')->andReturn('{}');
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
554
555
        $serializerMock->shouldReceive('deserialize')->andReturn($responseMock);
556
557
        $streamMock = Mockery::mock('\TBolier\RethinkQL\Connection\Socket\Socket');
558
        $streamMock->shouldReceive('isWritable')->andReturn(true);
559
        $streamMock->shouldReceive('write')->andReturn(1);
560
        $streamMock->shouldReceive('close')->andReturn(1);
561
        $streamMock->shouldReceive('read')->andReturn('');
562
563
        $options = new Options(PHPUNIT_CONNECTIONS['phpunit_default']);
564
565
        $connection = new Connection(
566
            function () use ($options, $streamMock) {
567
                return $streamMock;
568
            },
569
            $handshakeMock,
570
            $options->getDbname(),
571
            $serializerMock,
572
            $serializerMock
573
        );
574
575
576
        return $connection;
577
    }
578
579
    /**
580
     * @return void
581
     */
582
    public function tearDown(): void
583
    {
584
        $this->useDefaultInternals();
585
586
        parent::tearDown(); // TODO: Change the autogenerated stub
587
    }
588
}
589