Completed
Push — master ( ee6bc9...3f15df )
by Eugene
01:30
created

testCreateMiddlewareExecutesInFifoOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
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\Unit\Handler;
15
16
use PHPUnit\Framework\MockObject\MockObject;
17
use PHPUnit\Framework\TestCase;
18
use Tarantool\Client\Handler\Handler;
19
use Tarantool\Client\Handler\MiddlewareHandler;
20
use Tarantool\Client\Request\Request;
21
use Tarantool\Client\Tests\SpyMiddleware;
22
use Tarantool\PhpUnit\Client\TestDoubleClient;
23
24
final class MiddlewareHandlerTest extends TestCase
25
{
26
    use TestDoubleClient;
27
28
    /**
29
     * @var Request|MockObject
30
     */
31
    private $request;
32
33
    /**
34
     * @var Handler|MockObject
35
     */
36
    private $handler;
37
38
    protected function setUp() : void
39
    {
40
        $this->request = $this->createMock(Request::class);
41
        $this->handler = $this->createDummyClient()->getHandler();
42
    }
43
44
    public function testCreateMiddlewareExecutesInFifoOrder() : void
45
    {
46
        $trace = new \ArrayObject();
47
        $middleware1 = SpyMiddleware::fromTraceId(1, $trace);
48
        $middleware2 = SpyMiddleware::fromTraceId(2, $trace);
49
50
        $handler = MiddlewareHandler::create($this->handler, [$middleware1, $middleware2]);
0 ignored issues
show
Bug introduced by
It seems like $this->handler can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Tarantool\Client\Handler...lewareHandler::create() does only seem to accept object<Tarantool\Client\Handler\Handler>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
51
        $handler->handle($this->request);
0 ignored issues
show
Bug introduced by
It seems like $this->request can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Tarantool\Client\Handler\Handler::handle() does only seem to accept object<Tarantool\Client\Request\Request>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
52
53
        self::assertSame([1, 2], $trace->getArrayCopy());
54
    }
55
56
    public function testCreateAppendsMiddleware() : void
57
    {
58
        $trace = new \ArrayObject();
59
        $middleware1 = SpyMiddleware::fromTraceId(1, $trace);
60
        $middleware2 = SpyMiddleware::fromTraceId(2, $trace);
61
62
        $handler = MiddlewareHandler::create($this->handler, [$middleware1]);
0 ignored issues
show
Bug introduced by
It seems like $this->handler can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Tarantool\Client\Handler...lewareHandler::create() does only seem to accept object<Tarantool\Client\Handler\Handler>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
63
        $handler = MiddlewareHandler::create($handler, [$middleware2]);
64
        $handler->handle($this->request);
0 ignored issues
show
Bug introduced by
It seems like $this->request can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Tarantool\Client\Handler\Handler::handle() does only seem to accept object<Tarantool\Client\Request\Request>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
65
66
        self::assertSame([1, 2], $trace->getArrayCopy());
67
    }
68
69
    public function testCreatePrependsMiddleware() : void
70
    {
71
        $trace = new \ArrayObject();
72
        $middleware1 = SpyMiddleware::fromTraceId(1, $trace);
73
        $middleware2 = SpyMiddleware::fromTraceId(2, $trace);
74
75
        $handler = MiddlewareHandler::create($this->handler, [$middleware1]);
0 ignored issues
show
Bug introduced by
It seems like $this->handler can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Tarantool\Client\Handler...lewareHandler::create() does only seem to accept object<Tarantool\Client\Handler\Handler>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
76
        $handler = MiddlewareHandler::create($handler, [$middleware2], true);
77
        $handler->handle($this->request);
0 ignored issues
show
Bug introduced by
It seems like $this->request can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Tarantool\Client\Handler\Handler::handle() does only seem to accept object<Tarantool\Client\Request\Request>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
78
79
        self::assertSame([2, 1], $trace->getArrayCopy());
80
    }
81
82
    public function testMiddlewareRemainsAfterExecution() : void
83
    {
84
        $middleware = SpyMiddleware::fromTraceId(1);
85
86
        $handler = MiddlewareHandler::create($this->handler, [$middleware]);
0 ignored issues
show
Bug introduced by
It seems like $this->handler can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Tarantool\Client\Handler...lewareHandler::create() does only seem to accept object<Tarantool\Client\Handler\Handler>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
87
        $handler->handle($this->request);
0 ignored issues
show
Bug introduced by
It seems like $this->request can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Tarantool\Client\Handler\Handler::handle() does only seem to accept object<Tarantool\Client\Request\Request>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
88
        $handler->handle($this->request);
0 ignored issues
show
Bug introduced by
It seems like $this->request can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Tarantool\Client\Handler\Handler::handle() does only seem to accept object<Tarantool\Client\Request\Request>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
89
90
        self::assertSame([1, 1], $middleware->getTraceLogArray());
91
    }
92
}
93