Completed
Push — master ( 954764...295bdd )
by Eugene
06:25
created

RetryMiddlewareTest::testInfiniteRetriesBreak()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 3
nop 0
dl 0
loc 20
rs 9.6
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\Middleware;
15
16
use PHPUnit\Framework\MockObject\MockObject;
17
use PHPUnit\Framework\TestCase;
18
use Tarantool\Client\Exception\ConnectionFailed;
19
use Tarantool\Client\Handler\Handler;
20
use Tarantool\Client\Middleware\RetryMiddleware;
21
use Tarantool\Client\Request\Request;
22
use Tarantool\PhpUnit\Client\TestDoubleFactory;
23
24
final class RetryMiddlewareTest extends TestCase
25
{
26
    /**
27
     * @var Request|MockObject
28
     */
29
    private $request;
30
31
    /**
32
     * @var Handler|MockObject
33
     */
34
    private $handler;
35
36
    protected function setUp() : void
37
    {
38
        $this->request = $this->createMock(Request::class);
39
        $this->handler = $this->createMock(Handler::class);
40
    }
41
42
    public function testSuccessfulRetry() : void
43
    {
44
        $response = TestDoubleFactory::createEmptyResponse();
45
46
        $this->handler->expects(self::exactly(3))->method('handle')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Tarantool\Client\Handler\Handler.

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...
47
            ->will(self::onConsecutiveCalls(
48
                self::throwException(new \RuntimeException()),
49
                self::throwException(new ConnectionFailed()),
50
                $response
51
            ));
52
53
        $totalRetries = 0;
54
        $middleware = RetryMiddleware::custom(static function (int $retries) use (&$totalRetries) : int {
55
            $totalRetries = $retries;
56
57
            return 0;
58
        });
59
60
        self::assertSame($response, $middleware->process($this->request, $this->handler));
61
        self::assertSame(2, $totalRetries);
62
    }
63
64
    public function testUnsuccessfulRetries() : void
65
    {
66
        $this->handler->expects(self::exactly(4))->method('handle')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Tarantool\Client\Handler\Handler.

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...
67
            ->willThrowException(new ConnectionFailed());
68
69
        $middleware = RetryMiddleware::custom(static function (int $retries) : ?int {
70
            return $retries <= 3 ? 0 : null;
71
        });
72
73
        $this->expectException(ConnectionFailed::class);
74
        $middleware->process($this->request, $this->handler);
75
    }
76
77
    public function testInfiniteRetriesBreak() : void
78
    {
79
        $this->handler->method('handle')
80
            ->willThrowException(new ConnectionFailed());
81
82
        $totalRetries = 0;
83
        $middleware = RetryMiddleware::custom(static function (int $retries) use (&$totalRetries) : int {
84
            $totalRetries = $retries;
85
            // always returning a value other than null
86
            // leads to an infinite retry loop
87
            return 0;
88
        });
89
90
        try {
91
            $middleware->process($this->request, $this->handler);
92
            self::fail(sprintf('"%s" exception was not thrown', ConnectionFailed::class));
93
        } catch (ConnectionFailed $e) {
94
            self::assertSame(100, $totalRetries);
95
        }
96
    }
97
}
98