Passed
Pull Request — master (#63)
by Eugene
03:00
created

RetryMiddlewareTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 47
rs 10
c 2
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testUnsuccessfulRetries() 0 11 2
A setUp() 0 4 1
A testSuccessfulRetry() 0 16 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\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\Client\Tests\Unit\ResponseFactory;
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 = ResponseFactory::create();
45
46
        $this->handler->expects($this->exactly(3))->method('handle')
47
            ->will($this->onConsecutiveCalls(
48
                $this->throwException(new \RuntimeException()),
49
                $this->throwException(new ConnectionFailed()),
50
                $response
51
            ));
52
53
        $middleware = RetryMiddleware::custom(static function (int $retries) : ?int {
0 ignored issues
show
Unused Code introduced by
The parameter $retries is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

53
        $middleware = RetryMiddleware::custom(static function (/** @scrutinizer ignore-unused */ int $retries) : ?int {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
            return 0;
55
        });
56
57
        self::assertSame($response, $middleware->process($this->request, $this->handler));
58
    }
59
60
    public function testUnsuccessfulRetries() : void
61
    {
62
        $this->handler->expects($this->exactly(4))->method('handle')
63
            ->willThrowException(new ConnectionFailed());
64
65
        $middleware = RetryMiddleware::custom(static function (int $retries) : ?int {
66
            return $retries <= 3 ? 0 : null;
67
        });
68
69
        $this->expectException(ConnectionFailed::class);
70
        $middleware->process($this->request, $this->handler);
71
    }
72
}
73