Passed
Pull Request — master (#11)
by Michel
02:36
created

ConnectionTest.php ➔ random_int()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
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\ResponseInterface;
12
use TBolier\RethinkQL\Types\Query\QueryType;
13
use TBolier\RethinkQL\Types\Response\ResponseType;
14
15
function unpack($format = '', $data = '', $offset = 0)
16
{
17
    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...
18
}
19
20
function random_int($min = 0, $max = 0)
21
{
22
    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...
23
}
24
25
class ConnectionTest extends BaseTestCase
26
{
27
    /**
28
     * @var MockInterface
29
     */
30
    public static $functions;
31
32
    /**
33
     * @var MockInterface
34
     */
35
    private $optionsMock;
36
37
    /**
38
     * @return void
39
     */
40
    public function setUp(): void
41
    {
42
        parent::setUp();
43
44
        self::$functions = Mockery::mock();
45
46
        $this->useDefaultInternals();
47
48
        $this->optionsMock = Mockery::mock(OptionsInterface::class);
49
    }
50
51
    /**
52
     * @throws \Exception
53
     *
54
     * @return void
55
     */
56
    public function testConnect(): void
57
    {
58
        /** @var ConnectionInterface $connection */
59
        $connection = $this->createConnection('phpunit_default')->connect();
60
        $result = $connection->expr('foo');
61
62
        $this->assertInstanceOf(ResponseInterface::class, $result);
63
        $this->assertEquals('foo', $result->getData()[0]);
64
    }
65
66
    /**
67
     * @throws \Exception
68
     *
69
     * @return void
70
     */
71
    public function testConnectWithOpenConnection(): void
72
    {
73
        /** @var ConnectionInterface $connection */
74
        $connection = $this->createConnection('phpunit_default')->connect();
75
        $connection2 = $connection->connect();
76
77
        $this->assertSame($connection, $connection2);
78
    }
79
80
    /**
81
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
82
     * @expectedExceptionMessage Test exception
83
     * @return void
84
     */
85
    public function testConnectThrowsCorrectException(): void
86
    {
87
        $options = new Options(PHPUNIT_CONNECTIONS['phpunit_default']);
88
89
        $handshakeMock = Mockery::mock('\TBolier\RethinkQL\Connection\Socket\HandshakeInterface');
90
        $handshakeMock->shouldReceive('hello')->andThrow(new \Exception('Test exception', 404));
91
92
        $serializerMock = Mockery::mock('\Symfony\Component\Serializer\SerializerInterface');
93
94
        $connection = new Connection(
95
            function () use ($options) {
96
                return new Socket(
97
                    $options
98
                );
99
            },
100
            $handshakeMock,
101
            $options->getDbname(),
102
            $serializerMock,
103
            $serializerMock
104
        );
105
106
        $connection->connect();
107
    }
108
109
    /**
110
     * @return void
111
     */
112
    public function testServer(): void
113
    {
114
        /** @var ConnectionInterface $connection */
115
        $res = $this->createConnection('phpunit_default')->connect()->server();
116
117
        $this->assertEquals(QueryType::SERVER_INFO, $res->getType());
118
        $this->assertInternalType('string', $res->getData()[0]['name']);
119
    }
120
121
    /**
122
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
123
     * @expectedExceptionMessage Not connected
124
     */
125
    public function testServerThrowsNotConnectedException(): void
126
    {
127
        /** @var ConnectionInterface $connection */
128
        $this->createConnection('phpunit_default')->server();
129
    }
130
131
    /**
132
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
133
     */
134
    public function testServerThrowsExceptionOnConnectionException(): void
135
    {
136
        $options = new Options(PHPUNIT_CONNECTIONS['phpunit_default']);
137
138
        $handshakeMock = Mockery::mock('\TBolier\RethinkQL\Connection\Socket\HandshakeInterface');
139
        $handshakeMock->shouldReceive('hello');
140
141
        $serializerMock = Mockery::mock('\Symfony\Component\Serializer\SerializerInterface');
142
143
        $connection = new Connection(
144
            function () use ($options) {
145
                return new Socket(
146
                    $options
147
                );
148
            },
149
            $handshakeMock,
150
            $options->getDbname(),
151
            $serializerMock,
152
            $serializerMock
153
        );
154
155
        /** @var ConnectionInterface $connection */
156
        $connection->connect()->server();
157
    }
158
159
    /**
160
     * @return void
161
     */
162
    public function testClose(): void
163
    {
164
        $connection = $this->createConnection('phpunit_default')->connect();
165
166
        $this->assertTrue($connection->isStreamOpen());
167
168
        $connection->close(false);
169
170
        $this->assertFalse($connection->isStreamOpen());
171
    }
172
173
    /**
174
     * @return void
175
     */
176
    public function testCloseNoReplyWait(): void
177
    {
178
        $connection = $this->createConnection('phpunit_default')->connect();
179
180
        $this->assertTrue($connection->isStreamOpen());
181
182
        $connection->close(true);
183
184
        $this->assertFalse($connection->isStreamOpen());
185
    }
186
187
    /**
188
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
189
     * @expectedExceptionMessage Not connected
190
     * @return void
191
     */
192
    public function testCloseThrowsExceptionOnNotConnected(): void
193
    {
194
        $connection = $this->createConnection('phpunit_default');
195
        $connection->close();
196
    }
197
198
    /**
199
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
200
     * @expectedExceptionMessage Not connected
201
     * @return void
202
     */
203
    public function testRunThrowsExceptionOnNotConnected(): void
204
    {
205
        $connection = $this->createConnection('phpunit_default');
206
        $connection->run(Mockery::mock('\TBolier\RethinkQL\Query\MessageInterface'));
207
    }
208
209
    /**
210
     * @return void
211
     */
212
    public function testRunNoReply(): void
213
    {
214
        $message = $this->setUpMessageMock();
215
216
        $connection = $this->createConnection('phpunit_default')->connect();
217
218
        $reply = $connection->runNoReply($message);
219
220
        $connection->close(false);
221
222
        $this->assertEquals([], $reply);
223
    }
224
225
    /**
226
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
227
     * @expectedExceptionMessage Generating the token failed.
228
     * @return void
229
     */
230
    public function testNoReplyException(): void
231
    {
232
        self::$functions->shouldReceive('random_int')->andThrow(Exception::class, 'foo')->byDefault();
233
234
        $connection = $this->createConnection('phpunit_default')->connect();
235
236
        $connection->close(true);
237
    }
238
239
    /**
240
     * @return void
241
     */
242
    public function testRunPartialSuccess(): void
243
    {
244
        $expectedToken = [
245
            'token' => 1,
246
            'token2' => 0,
247
            'size' => 19
248
        ];
249
250
251
        self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken);
252
        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...
253
254
        $message = $this->setUpMessageMock();
255
256
        $responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface');
257
        $responseMock->shouldReceive('getType')->andReturn(ResponseType::SUCCESS_PARTIAL);
258
259
        $connection = $this->setUpMockConnection($responseMock);
260
261
        $connection = $connection->connect();
262
263
        $reply = $connection->run($message);
264
265
        $connection->close(false);
266
267
        $this->assertEquals($responseMock, $reply);
268
    }
269
270
271
    /**
272
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
273
     * @expectedExceptionMessage Serializing query message failed.
274
     * @return void
275
     */
276
    public function testIfRunThrowsException(): void
277
    {
278
        /** @var ConnectionInterface $connection */
279
        $connection = $this->createConnection('phpunit_default')->connect();
280
281
        $messageMock = Mockery::mock('\TBolier\RethinkQL\Query\Message');
282
        $messageMock->shouldReceive('setOptions');
283
284
        $connection->run($messageMock);
285
    }
286
287
    /**
288
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
289
     * @expectedExceptionMessage Generating the token failed.
290
     * @return void
291
     */
292
    public function testIfGenerateTokenThrowsException(): void
293
    {
294
        self::$functions = Mockery::mock();
295
296
        self::$functions->shouldReceive('random_int')->andThrow(Exception::class, 'foo')->byDefault();
297
298
        /** @var ConnectionInterface $connection */
299
        $connection = $this->createConnection('phpunit_default')->connect();
300
301
        $messageMock = Mockery::mock('\TBolier\RethinkQL\Query\Message');
302
        $messageMock->shouldReceive('setOptions');
303
304
        $connection->run($messageMock);
305
    }
306
307
    /**
308
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
309
     * @expectedExceptionMessage Response message has no type.
310
     * @return void
311
     */
312
    public function testConnectionExceptionThrownOnNoResponseType(): void
313
    {
314
        $expectedToken = [
315
            'token' => 1,
316
            'token2' => 0,
317
            'size' => 19
318
        ];
319
320
        self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken);
321
        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...
322
323
        $message = $this->setUpMessageMock();
324
325
        $responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface');
326
        $responseMock->shouldReceive('getType')->andReturn(0);
327
328
        $connection = $this->setUpMockConnection($responseMock);
329
        $connection = $connection->connect();
330
331
        $reply = $connection->run($message);
332
333
        $connection->close(false);
334
335
        $this->assertEquals($responseMock, $reply);
336
    }
337
338
    /**
339
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
340
     * @expectedExceptionMessage Compile error: foo, jsonQuery: "{}"
341
     * @return void
342
     */
343
    public function testConnectionExceptionThrownOnCompileError(): void
344
    {
345
        $expectedToken = [
346
            'token' => 1,
347
            'token2' => 0,
348
            'size' => 19
349
        ];
350
351
        self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken);
352
        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...
353
354
        $message = $this->setUpMessageMock();
355
356
        $responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface');
357
        $responseMock->shouldReceive('getType')->andReturn(17);
358
        $responseMock->shouldReceive('getData')->andReturn([0=>'foo']);
359
360
        $connection = $this->setUpMockConnection($responseMock);
361
        $connection = $connection->connect();
362
363
        $reply = $connection->run($message);
364
365
        $connection->close(false);
366
367
        $this->assertEquals($responseMock, $reply);
368
    }
369
370
371
    /**
372
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
373
     * @expectedExceptionMessage Runtime error: foo, jsonQuery: "{}"
374
     * @return void
375
     */
376
    public function testConnectionExceptionThrownOnRuntimeError(): void
377
    {
378
        $expectedToken = [
379
            'token' => 1,
380
            'token2' => 0,
381
            'size' => 19
382
        ];
383
384
        self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken);
385
        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...
386
387
        $message = $this->setUpMessageMock();
388
389
        $responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface');
390
        $responseMock->shouldReceive('getType')->andReturn(18);
391
        $responseMock->shouldReceive('getData')->andReturn([0=>'foo']);
392
393
        $connection = $this->setUpMockConnection($responseMock);
394
        $connection = $connection->connect();
395
396
        $reply = $connection->run($message);
397
398
        $connection->close(false);
399
400
        $this->assertEquals($responseMock, $reply);
401
    }
402
403
    /**
404
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
405
     * @expectedExceptionMessage Received wrong token. Response does not match the request. Expected 1, received 11
406
     * @return void
407
     */
408
    public function testConnectionExceptionThrownOnInvalidResponseToken(): void
409
    {
410
        $expectedToken = [
411
            'token' => 11,
412
            'token2' => 0,
413
            'size' => 19
414
        ];
415
416
        self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken);
417
        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...
418
419
        $message = $this->setUpMessageMock();
420
421
        $responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface');
422
        $responseMock->shouldReceive('getType')->andReturn(18);
423
        $responseMock->shouldReceive('getData')->andReturn([0=>'foo']);
424
425
        $connection = $this->setUpMockConnection($responseMock);
426
        $connection = $connection->connect();
427
428
        $reply = $connection->run($message);
429
430
        $connection->close(false);
431
432
        $this->assertEquals($responseMock, $reply);
433
    }
434
435
    /**
436
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
437
     * @expectedExceptionMessage Invalid response from server: Invalid token.
438
     * @return void
439
     */
440
    public function testConnectionExceptionThrownOnInvalidResponseToken2(): void
441
    {
442
        $expectedToken = [
443
            'token' => 1,
444
            'token2' => 42,
445
            'size' => 19
446
        ];
447
448
        self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken);
449
        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...
450
451
        $message = $this->setUpMessageMock();
452
453
        $responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface');
454
        $responseMock->shouldReceive('getType')->andReturn(18);
455
        $responseMock->shouldReceive('getData')->andReturn([0=>'foo']);
456
457
        $connection = $this->setUpMockConnection($responseMock);
458
        $connection = $connection->connect();
459
460
        $reply = $connection->run($message);
461
462
        $connection->close(false);
463
464
        $this->assertEquals($responseMock, $reply);
465
    }
466
467
    /**
468
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
469
     * @expectedExceptionMessage Server says PHP-RQL is buggy:
470
     * @return void
471
     */
472
    public function testConnectionExceptionThrownOnClientErrorResponseType(): void
473
    {
474
        $expectedToken = [
475
            'token' => 1,
476
            'token2' => 0,
477
            'size' => 19
478
        ];
479
480
        self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken);
481
        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...
482
483
        $message = $this->setUpMessageMock();
484
485
        $responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface');
486
        $responseMock->shouldReceive('getType')->andReturn(16);
487
        $responseMock->shouldReceive('getData')->andReturn([0=>'foo']);
488
489
        $connection = $this->setUpMockConnection($responseMock);
490
        $connection = $connection->connect();
491
492
        $reply = $connection->run($message);
493
494
        $connection->close(false);
495
496
        $this->assertEquals($responseMock, $reply);
497
    }
498
499
    /**
500
     * @return void
501
     */
502
    public function testUse(): void
503
    {
504
        $connection = $this->createConnection('phpunit_default');
505
        $connection->use('foo');
506
507
        $reflection = new \ReflectionClass($connection);
508
        $dbName = $reflection->getProperty('dbName');
509
        $dbName->setAccessible(true);
510
        $this->assertEquals('foo', $dbName->getValue($connection));}
511
512
    /**
513
     * Resets the mocks to their PHP internal equivalents
514
     */
515
    private function useDefaultInternals(): void
516
    {
517
        // Reset expectations to use PHP built-in functions
518
        self::$functions = Mockery::mock();
519
520
        self::$functions->shouldReceive('random_int')->byDefault()->andReturnUsing(function ($min = 0, $max = 0) {
521
            return \random_int($min, $max);
522
        });
523
524
        self::$functions->shouldReceive('unpack')->byDefault()->andReturnUsing(function ($format = '', $data = '', $offset = 0) {
525
            return \unpack($format, $data, $offset);
526
        });
527
    }
528
529
    /**
530
     * @return MockInterface
531
     */
532
    private function setUpMessageMock(): MockInterface
533
    {
534
        $message = Mockery::mock('\TBolier\RethinkQL\Query\MessageInterface');
535
        $message->shouldReceive('setOptions')->andReturnSelf()->getMock();
536
        $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...
537
538
        return $message;
539
    }
540
541
    /**
542
     * @return ConnectionInterface
543
     */
544
    private function setUpMockConnection(MockInterface $responseMock): ConnectionInterface
545
    {
546
        $handshakeMock = Mockery::mock('\TBolier\RethinkQL\Connection\Socket\HandshakeInterface');
547
        $handshakeMock->shouldReceive('hello')->andReturnSelf();
548
549
        $serializerMock = Mockery::mock('\Symfony\Component\Serializer\SerializerInterface');
550
        $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...
551
552
        $serializerMock->shouldReceive('deserialize')->andReturn($responseMock);
553
554
        $streamMock = Mockery::mock('\TBolier\RethinkQL\Connection\Socket\Socket');
555
        $streamMock->shouldReceive('isWritable')->andReturn(true);
556
        $streamMock->shouldReceive('write')->andReturn(1);
557
        $streamMock->shouldReceive('close')->andReturn(1);
558
        $streamMock->shouldReceive('read')->andReturn('');
559
560
        $options = new Options(PHPUNIT_CONNECTIONS['phpunit_default']);
561
562
        $connection = new Connection(
563
            function () use ($options, $streamMock) {
564
                return $streamMock;
565
            },
566
            $handshakeMock,
567
            $options->getDbname(),
568
            $serializerMock,
569
            $serializerMock
570
        );
571
572
573
        return $connection;
574
    }
575
576
    /**
577
     * @return void
578
     */
579
    public function tearDown(): void
580
    {
581
        $this->useDefaultInternals();
582
583
        parent::tearDown(); // TODO: Change the autogenerated stub
584
    }
585
}
586