Completed
Push — master ( a656ee...9131a4 )
by Eugene
01:25
created

TestCase   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getClient() 0 5 1
A tarantoolVersionSatisfies() 0 4 1
A triggerUnexpectedResponse() 0 14 2
A getRawStream() 0 7 1
1
<?php
2
3
/**
4
 * This file is part of the tarantool/client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Client\Tests\Integration;
15
16
use Tarantool\Client\Client;
17
use Tarantool\Client\Connection\Connection;
18
use Tarantool\Client\Connection\StreamConnection;
19
use Tarantool\Client\Exception\CommunicationFailed;
20
use Tarantool\Client\Handler\Handler;
21
use Tarantool\Client\Request\Request;
22
use Tarantool\Client\Tests\PhpUnitCompat;
23
use Tarantool\PhpUnit\Annotation\Requirement\TarantoolVersionRequirement;
24
use Tarantool\PhpUnit\TestCase as BaseTestCase;
25
26
abstract class TestCase extends BaseTestCase
27
{
28
    use PhpUnitCompat;
29
30
    /** @var Client|null */
31
    protected $client;
32
33
    /**
34
     * @before
35
     */
36
    protected function getClient() : Client
37
    {
38
        return $this->client
39
            ?? $this->client = ClientBuilder::createFromEnv()->build();
40
    }
41
42
    final protected function tarantoolVersionSatisfies(string $constraints) : bool
43
    {
44
        return null === (new TarantoolVersionRequirement($this->client))->check($constraints);
0 ignored issues
show
Bug introduced by
It seems like $this->client can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
45
    }
46
47
    final protected static function triggerUnexpectedResponse(Handler $handler, Request $initialRequest, int $sync = 0) : Connection
48
    {
49
        $connection = $handler->getConnection();
50
        $packer = $handler->getPacker();
51
        $rawRequest = $packer->pack($initialRequest, $sync);
52
53
        // write a request without reading a response
54
        $connection->open();
55
        if (!\fwrite(self::getRawStream($connection), $rawRequest)) {
0 ignored issues
show
Compatibility introduced by
$connection of type object<Tarantool\Client\Connection\Connection> is not a sub-type of object<Tarantool\Client\...ction\StreamConnection>. It seems like you assume a concrete implementation of the interface Tarantool\Client\Connection\Connection to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
56
            throw new CommunicationFailed('Unable to write request');
57
        }
58
59
        return $connection;
60
    }
61
62
    final public static function getRawStream(StreamConnection $connection)
63
    {
64
        $prop = (new \ReflectionObject($connection))->getProperty('stream');
65
        $prop->setAccessible(true);
66
67
        return $prop->getValue($connection);
68
    }
69
}
70