CustomErrorMiddlewareTest::testFromNamespace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 27
rs 9.7
c 2
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\Error;
19
use Tarantool\Client\Exception\RequestDenied;
20
use Tarantool\Client\Exception\RequestFailed;
21
use Tarantool\Client\Handler\Handler;
22
use Tarantool\Client\Keys;
23
use Tarantool\Client\Middleware\CustomErrorMiddleware;
24
use Tarantool\Client\Request\PingRequest;
25
use Tarantool\PhpUnit\Client\TestDoubleFactory;
26
27
final class CustomErrorMiddlewareTest extends TestCase
28
{
29
    /**
30
     * @var Handler|MockObject
31
     */
32
    private $handler;
33
34
    protected function setUp() : void
35
    {
36
        $this->handler = $this->createMock(Handler::class);
37
    }
38
39
    public function testFromNamespace() : void
40
    {
41
        $errorMessage = 'Error message';
42
        $errorCode = 42;
43
44
        $this->handler->expects(self::once())->method('handle')
45
            ->willThrowException(RequestFailed::fromErrorResponse(
46
                TestDoubleFactory::createErrorResponseFromStack([
47
                    [
48
                        Keys::ERROR_TYPE => 'CustomError',
49
                        Keys::ERROR_FILE => 'file.lua',
50
                        Keys::ERROR_LINE => 1,
51
                        Keys::ERROR_MESSAGE => $errorMessage,
52
                        Keys::ERROR_NUMBER => 2,
53
                        Keys::ERROR_CODE => $errorCode,
54
                        Keys::ERROR_FIELDS => ['custom_type' => 'RequestDenied'],
55
                    ],
56
                ])
57
            ));
58
59
        $middleware = CustomErrorMiddleware::fromNamespace('Tarantool\Client\Exception');
60
61
        $this->expectException(RequestDenied::class);
62
        $this->expectExceptionMessage($errorMessage);
63
        $this->expectExceptionCode($errorCode);
64
65
        $middleware->process(new PingRequest(), $this->handler);
66
    }
67
68
    public function testFromFactory() : void
69
    {
70
        $errorMessage = 'Error message';
71
        $errorCode = 42;
72
73
        $this->handler->expects(self::once())->method('handle')
74
            ->willThrowException(RequestFailed::fromErrorResponse(
75
                TestDoubleFactory::createErrorResponseFromStack([
76
                    [
77
                        Keys::ERROR_TYPE => 'CustomError',
78
                        Keys::ERROR_FILE => 'file.lua',
79
                        Keys::ERROR_LINE => 1,
80
                        Keys::ERROR_MESSAGE => $errorMessage,
81
                        Keys::ERROR_NUMBER => 2,
82
                        Keys::ERROR_CODE => $errorCode,
83
                        Keys::ERROR_FIELDS => ['custom_type' => 'MyCustomErrorType'],
84
                    ],
85
                ])
86
            ));
87
88
        $middleware = CustomErrorMiddleware::fromFactory(static function (Error $err, RequestFailed $ex) {
89
            return 'MyCustomErrorType' === $err->tryGetField('custom_type')
90
                ? new \RuntimeException('My custom exception', 3)
91
                : $ex;
92
        });
93
94
        $this->expectException(\RuntimeException::class);
95
        $this->expectExceptionMessage('My custom exception');
96
        $this->expectExceptionCode(3);
97
98
        $middleware->process(new PingRequest(), $this->handler);
99
    }
100
101
    public function testFromMapping() : void
102
    {
103
        $errorMessage = 'Error message';
104
        $errorCode = 42;
105
106
        $this->handler->expects(self::once())->method('handle')
107
            ->willThrowException(RequestFailed::fromErrorResponse(
108
                TestDoubleFactory::createErrorResponseFromStack([
109
                    [
110
                        Keys::ERROR_TYPE => 'CustomError',
111
                        Keys::ERROR_FILE => 'file.lua',
112
                        Keys::ERROR_LINE => 1,
113
                        Keys::ERROR_MESSAGE => $errorMessage,
114
                        Keys::ERROR_NUMBER => 2,
115
                        Keys::ERROR_CODE => $errorCode,
116
                        Keys::ERROR_FIELDS => ['custom_type' => 'MyCustomErrorType'],
117
                    ],
118
                ])
119
            ));
120
121
        $middleware = CustomErrorMiddleware::fromMapping([
122
            'MyCustomErrorType' => \RuntimeException::class,
123
        ]);
124
125
        $this->expectException(\RuntimeException::class);
126
        $this->expectExceptionMessage($errorMessage);
127
        $this->expectExceptionCode($errorCode);
128
129
        $middleware->process(new PingRequest(), $this->handler);
130
    }
131
132
    public function testFromMappingDetectsNestedErrorType() : void
133
    {
134
        $errorMessage = 'Error message';
135
        $errorCode = 42;
136
137
        $this->handler->expects(self::once())->method('handle')
138
            ->willThrowException(RequestFailed::fromErrorResponse(
139
                TestDoubleFactory::createErrorResponseFromStack([
140
                    [
141
                        Keys::ERROR_TYPE => 'CustomError',
142
                        Keys::ERROR_FILE => 'file1.lua',
143
                        Keys::ERROR_LINE => 1,
144
                        Keys::ERROR_MESSAGE => $errorMessage,
145
                        Keys::ERROR_NUMBER => 2,
146
                        Keys::ERROR_CODE => $errorCode,
147
                        Keys::ERROR_FIELDS => ['custom_type' => 'MyCustomErrorType1'],
148
                    ],
149
                    [
150
                        Keys::ERROR_TYPE => 'CustomError',
151
                        Keys::ERROR_FILE => 'file2.lua',
152
                        Keys::ERROR_LINE => 11,
153
                        Keys::ERROR_MESSAGE => 'Error Message 2',
154
                        Keys::ERROR_NUMBER => 12,
155
                        Keys::ERROR_CODE => 13,
156
                        Keys::ERROR_FIELDS => ['custom_type' => 'MyCustomErrorType2'],
157
                    ],
158
                ])
159
            ));
160
161
        $middleware = CustomErrorMiddleware::fromMapping([
162
            'MyCustomErrorType2' => \RuntimeException::class,
163
        ]);
164
165
        $this->expectException(\RuntimeException::class);
166
        $this->expectExceptionMessage('Error Message 2');
167
        $this->expectExceptionCode(13);
168
169
        $middleware->process(new PingRequest(), $this->handler);
170
    }
171
}
172