TestCase   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 1
Metric Value
eloc 16
c 9
b 0
f 1
dl 0
loc 42
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A triggerUnexpectedResponse() 0 13 2
A getRawStream() 0 6 1
A getClient() 0 4 1
A tarantoolVersionSatisfies() 0 3 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->getClient()))->check($constraints);
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)) {
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