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\Client\Response; |
26
|
|
|
use Tarantool\PhpUnit\Client\TestDoubleFactory; |
27
|
|
|
|
28
|
|
|
final class CustomErrorMiddlewareTest extends TestCase |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var Handler|MockObject |
32
|
|
|
*/ |
33
|
|
|
private $handler; |
34
|
|
|
|
35
|
|
|
protected function setUp() : void |
36
|
|
|
{ |
37
|
|
|
$this->handler = $this->createMock(Handler::class); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testFromNamespace() : void |
41
|
|
|
{ |
42
|
|
|
$errorMessage = 'Error message'; |
43
|
|
|
$errorCode = 42; |
44
|
|
|
|
45
|
|
|
$this->handler->expects($this->once())->method('handle') |
|
|
|
|
46
|
|
|
->willThrowException(RequestFailed::fromErrorResponse( |
47
|
|
|
TestDoubleFactory::createResponse([ |
48
|
|
|
Keys::ERROR_24 => $errorMessage, |
49
|
|
|
Keys::ERROR => [ |
50
|
|
|
Keys::ERROR_STACK => [ |
51
|
|
|
[ |
52
|
|
|
Keys::ERROR_TYPE => 'CustomError', |
53
|
|
|
Keys::ERROR_FILE => 'file.lua', |
54
|
|
|
Keys::ERROR_LINE => 1, |
55
|
|
|
Keys::ERROR_MESSAGE => $errorMessage, |
56
|
|
|
Keys::ERROR_NUMBER => 2, |
57
|
|
|
Keys::ERROR_CODE => $errorCode, |
58
|
|
|
Keys::ERROR_FIELDS => ['custom_type' => 'RequestDenied'], |
59
|
|
|
], |
60
|
|
|
], |
61
|
|
|
], |
62
|
|
|
], [ |
63
|
|
|
Keys::CODE => Response::TYPE_ERROR + $errorCode, |
64
|
|
|
]) |
65
|
|
|
)); |
66
|
|
|
|
67
|
|
|
$middleware = CustomErrorMiddleware::fromNamespace('Tarantool\Client\Exception'); |
68
|
|
|
|
69
|
|
|
$this->expectException(RequestDenied::class); |
70
|
|
|
$this->expectExceptionMessage($errorMessage); |
71
|
|
|
$this->expectExceptionCode($errorCode); |
72
|
|
|
|
73
|
|
|
$middleware->process(new PingRequest(), $this->handler); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testFromFactory() : void |
77
|
|
|
{ |
78
|
|
|
$errorMessage = 'Error message'; |
79
|
|
|
$errorCode = 42; |
80
|
|
|
|
81
|
|
|
$this->handler->expects($this->once())->method('handle') |
|
|
|
|
82
|
|
|
->willThrowException(RequestFailed::fromErrorResponse( |
83
|
|
|
TestDoubleFactory::createResponse([ |
84
|
|
|
Keys::ERROR_24 => $errorMessage, |
85
|
|
|
Keys::ERROR => [ |
86
|
|
|
Keys::ERROR_STACK => [ |
87
|
|
|
[ |
88
|
|
|
Keys::ERROR_TYPE => 'CustomError', |
89
|
|
|
Keys::ERROR_FILE => 'file.lua', |
90
|
|
|
Keys::ERROR_LINE => 1, |
91
|
|
|
Keys::ERROR_MESSAGE => $errorMessage, |
92
|
|
|
Keys::ERROR_NUMBER => 2, |
93
|
|
|
Keys::ERROR_CODE => $errorCode, |
94
|
|
|
Keys::ERROR_FIELDS => ['custom_type' => 'MyCustomErrorType'], |
95
|
|
|
], |
96
|
|
|
], |
97
|
|
|
], |
98
|
|
|
], [ |
99
|
|
|
Keys::CODE => Response::TYPE_ERROR + $errorCode, |
100
|
|
|
]) |
101
|
|
|
)); |
102
|
|
|
|
103
|
|
|
$middleware = CustomErrorMiddleware::fromFactory(static function (Error $err, RequestFailed $ex) { |
104
|
|
|
return 'MyCustomErrorType' === $err->tryGetField('custom_type') |
105
|
|
|
? new \RuntimeException('My custom exception', 3) |
106
|
|
|
: $ex; |
107
|
|
|
}); |
108
|
|
|
|
109
|
|
|
$this->expectException(\RuntimeException::class); |
110
|
|
|
$this->expectExceptionMessage('My custom exception'); |
111
|
|
|
$this->expectExceptionCode(3); |
112
|
|
|
|
113
|
|
|
$middleware->process(new PingRequest(), $this->handler); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testFromMapping() : void |
117
|
|
|
{ |
118
|
|
|
$errorMessage = 'Error message'; |
119
|
|
|
$errorCode = 42; |
120
|
|
|
|
121
|
|
|
$this->handler->expects($this->once())->method('handle') |
|
|
|
|
122
|
|
|
->willThrowException(RequestFailed::fromErrorResponse( |
123
|
|
|
TestDoubleFactory::createResponse([ |
124
|
|
|
Keys::ERROR_24 => $errorMessage, |
125
|
|
|
Keys::ERROR => [ |
126
|
|
|
Keys::ERROR_STACK => [ |
127
|
|
|
[ |
128
|
|
|
Keys::ERROR_TYPE => 'CustomError', |
129
|
|
|
Keys::ERROR_FILE => 'file.lua', |
130
|
|
|
Keys::ERROR_LINE => 1, |
131
|
|
|
Keys::ERROR_MESSAGE => $errorMessage, |
132
|
|
|
Keys::ERROR_NUMBER => 2, |
133
|
|
|
Keys::ERROR_CODE => $errorCode, |
134
|
|
|
Keys::ERROR_FIELDS => ['custom_type' => 'MyCustomErrorType'], |
135
|
|
|
], |
136
|
|
|
], |
137
|
|
|
], |
138
|
|
|
], [ |
139
|
|
|
Keys::CODE => Response::TYPE_ERROR + $errorCode, |
140
|
|
|
]) |
141
|
|
|
)); |
142
|
|
|
|
143
|
|
|
$middleware = CustomErrorMiddleware::fromMapping([ |
144
|
|
|
'MyCustomErrorType' => \RuntimeException::class, |
145
|
|
|
]); |
146
|
|
|
|
147
|
|
|
$this->expectException(\RuntimeException::class); |
148
|
|
|
$this->expectExceptionMessage($errorMessage); |
149
|
|
|
$this->expectExceptionCode($errorCode); |
150
|
|
|
|
151
|
|
|
$middleware->process(new PingRequest(), $this->handler); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testFromMappingDetectsNestedErrorType() : void |
155
|
|
|
{ |
156
|
|
|
$errorMessage = 'Error message'; |
157
|
|
|
$errorCode = 42; |
158
|
|
|
|
159
|
|
|
$this->handler->expects($this->once())->method('handle') |
|
|
|
|
160
|
|
|
->willThrowException(RequestFailed::fromErrorResponse( |
161
|
|
|
TestDoubleFactory::createResponse([ |
162
|
|
|
Keys::ERROR_24 => $errorMessage, |
163
|
|
|
Keys::ERROR => [ |
164
|
|
|
Keys::ERROR_STACK => [ |
165
|
|
|
[ |
166
|
|
|
Keys::ERROR_TYPE => 'CustomError', |
167
|
|
|
Keys::ERROR_FILE => 'file1.lua', |
168
|
|
|
Keys::ERROR_LINE => 1, |
169
|
|
|
Keys::ERROR_MESSAGE => $errorMessage, |
170
|
|
|
Keys::ERROR_NUMBER => 2, |
171
|
|
|
Keys::ERROR_CODE => $errorCode, |
172
|
|
|
Keys::ERROR_FIELDS => ['custom_type' => 'MyCustomErrorType1'], |
173
|
|
|
], |
174
|
|
|
[ |
175
|
|
|
Keys::ERROR_TYPE => 'CustomError', |
176
|
|
|
Keys::ERROR_FILE => 'file2.lua', |
177
|
|
|
Keys::ERROR_LINE => 11, |
178
|
|
|
Keys::ERROR_MESSAGE => 'Error Message 2', |
179
|
|
|
Keys::ERROR_NUMBER => 12, |
180
|
|
|
Keys::ERROR_CODE => 13, |
181
|
|
|
Keys::ERROR_FIELDS => ['custom_type' => 'MyCustomErrorType2'], |
182
|
|
|
], |
183
|
|
|
], |
184
|
|
|
], |
185
|
|
|
], [ |
186
|
|
|
Keys::CODE => Response::TYPE_ERROR + $errorCode, |
187
|
|
|
]) |
188
|
|
|
)); |
189
|
|
|
|
190
|
|
|
$middleware = CustomErrorMiddleware::fromMapping([ |
191
|
|
|
'MyCustomErrorType2' => \RuntimeException::class, |
192
|
|
|
]); |
193
|
|
|
|
194
|
|
|
$this->expectException(\RuntimeException::class); |
195
|
|
|
$this->expectExceptionMessage('Error Message 2'); |
196
|
|
|
$this->expectExceptionCode(13); |
197
|
|
|
|
198
|
|
|
$middleware->process(new PingRequest(), $this->handler); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: