Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit75d5613

Browse files
authored
Merge pull request#3220 from amirkhodabande/refactor
Refactor
2 parentsb0f4ca3 +2739c2c commit75d5613

File tree

5 files changed

+111
-82
lines changed

5 files changed

+111
-82
lines changed

‎tests/Handlers/Strategies/RequestResponseNamedArgsTest.php‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class RequestResponseNamedArgsTest extends TestCase
2121
privateServerRequestInterface$request;
2222
privateResponseInterface$response;
2323

24+
privateconstPHP_8_0_VERSION_ID =80000;
25+
2426
publicfunctionsetUp():void
2527
{
2628
$this->request =$this->createMock(ServerRequestInterface::class);
@@ -29,7 +31,7 @@ public function setUp(): void
2931

3032
publicfunctiontestCreatingRequestResponseNamedArgsThrowsRuntimeExceptionForPHPOlderThan80()
3133
{
32-
if (PHP_VERSION_ID >=80000) {
34+
if (PHP_VERSION_ID >=self::PHP_8_0_VERSION_ID) {
3335
$this->markTestSkipped('Test only valid for PHP versions older than 8.0');
3436
}
3537

@@ -39,7 +41,7 @@ public function testCreatingRequestResponseNamedArgsThrowsRuntimeExceptionForPHP
3941

4042
publicfunctiontestCallingWithEmptyArguments()
4143
{
42-
if (PHP_VERSION_ID <80000) {
44+
if (PHP_VERSION_ID <self::PHP_8_0_VERSION_ID) {
4345
$this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0');
4446
}
4547

@@ -58,7 +60,7 @@ public function testCallingWithEmptyArguments()
5860

5961
publicfunctiontestCallingWithKnownArguments()
6062
{
61-
if (PHP_VERSION_ID <80000) {
63+
if (PHP_VERSION_ID <self::PHP_8_0_VERSION_ID) {
6264
$this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0');
6365
}
6466

@@ -107,7 +109,7 @@ public function testCallingWithOptionalArguments()
107109

108110
publicfunctiontestCallingWithUnknownAndVariadic()
109111
{
110-
if (PHP_VERSION_ID <80000) {
112+
if (PHP_VERSION_ID <self::PHP_8_0_VERSION_ID) {
111113
$this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0');
112114
}
113115

@@ -131,7 +133,7 @@ public function testCallingWithUnknownAndVariadic()
131133

132134
publicfunctiontestCallingWithMixedKnownAndUnknownParametersAndVariadic()
133135
{
134-
if (PHP_VERSION_ID <80000) {
136+
if (PHP_VERSION_ID <self::PHP_8_0_VERSION_ID) {
135137
$this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0');
136138
}
137139

‎tests/Middleware/ErrorMiddlewareTest.php‎

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ public function testSetErrorHandler()
3939
$callableResolver =$app->getCallableResolver();
4040
$logger =$this->getMockLogger();
4141

42-
$mw =newRoutingMiddleware($app->getRouteResolver(),$app->getRouteCollector()->getRouteParser());
43-
$app->add($mw);
42+
$routingMiddleware =newRoutingMiddleware(
43+
$app->getRouteResolver(),
44+
$app->getRouteCollector()->getRouteParser()
45+
);
46+
$app->add($routingMiddleware);
4447

4548
$exception = HttpNotFoundException::class;
4649
$handler = (function () {
@@ -49,9 +52,16 @@ public function testSetErrorHandler()
4952
return$response;
5053
})->bindTo($this);
5154

52-
$mw2 =newErrorMiddleware($callableResolver,$this->getResponseFactory(),false,false,false,$logger);
53-
$mw2->setErrorHandler($exception,$handler);
54-
$app->add($mw2);
55+
$errorMiddleware =newErrorMiddleware(
56+
$callableResolver,
57+
$this->getResponseFactory(),
58+
false,
59+
false,
60+
false,
61+
$logger
62+
);
63+
$errorMiddleware->setErrorHandler($exception,$handler);
64+
$app->add($errorMiddleware);
5565

5666
$request =$this->createServerRequest('/foo/baz/');
5767
$app->run($request);
@@ -66,18 +76,28 @@ public function testSetDefaultErrorHandler()
6676
$callableResolver =$app->getCallableResolver();
6777
$logger =$this->getMockLogger();
6878

69-
$mw =newRoutingMiddleware($app->getRouteResolver(),$app->getRouteCollector()->getRouteParser());
70-
$app->add($mw);
79+
$routingMiddleware =newRoutingMiddleware(
80+
$app->getRouteResolver(),
81+
$app->getRouteCollector()->getRouteParser()
82+
);
83+
$app->add($routingMiddleware);
7184

7285
$handler = (function () {
7386
$response =$this->createResponse();
7487
$response->getBody()->write('Oops..');
7588
return$response;
7689
})->bindTo($this);
7790

78-
$mw2 =newErrorMiddleware($callableResolver,$this->getResponseFactory(),false,false,false,$logger);
79-
$mw2->setDefaultErrorHandler($handler);
80-
$app->add($mw2);
91+
$errorMiddleware =newErrorMiddleware(
92+
$callableResolver,
93+
$this->getResponseFactory(),
94+
false,
95+
false,
96+
false,
97+
$logger
98+
);
99+
$errorMiddleware->setDefaultErrorHandler($handler);
100+
$app->add($errorMiddleware);
81101

82102
$request =$this->createServerRequest('/foo/baz/');
83103
$app->run($request);
@@ -94,9 +114,16 @@ public function testSetDefaultErrorHandlerThrowsException()
94114
$callableResolver =$app->getCallableResolver();
95115
$logger =$this->getMockLogger();
96116

97-
$mw =newErrorMiddleware($callableResolver,$this->getResponseFactory(),false,false,false,$logger);
98-
$mw->setDefaultErrorHandler('Uncallable');
99-
$mw->getDefaultErrorHandler();
117+
$errorMiddleware =newErrorMiddleware(
118+
$callableResolver,
119+
$this->getResponseFactory(),
120+
false,
121+
false,
122+
false,
123+
$logger
124+
);
125+
$errorMiddleware->setDefaultErrorHandler('Uncallable');
126+
$errorMiddleware->getDefaultErrorHandler();
100127
}
101128

102129
publicfunctiontestGetErrorHandlerWillReturnDefaultErrorHandlerForUnhandledExceptions()
@@ -118,21 +145,21 @@ public function testSuperclassExceptionHandlerHandlesExceptionWithSubclassExactM
118145
$app =newApp($responseFactory);
119146
$callableResolver =$app->getCallableResolver();
120147
$logger =$this->getMockLogger();
121-
$mw =newErrorMiddleware($callableResolver,$this->getResponseFactory(),false,false,false,$logger);
148+
$middleware =newErrorMiddleware($callableResolver,$this->getResponseFactory(),false,false,false,$logger);
122149
$app->add(function ($request,$handler) {
123150
thrownewLogicException('This is a LogicException...');
124151
});
125-
$mw->setErrorHandler(LogicException::class, (function (ServerRequestInterface$request,$exception) {
152+
$middleware->setErrorHandler(LogicException::class, (function (ServerRequestInterface$request,$exception) {
126153
$response =$this->createResponse();
127154
$response->getBody()->write($exception->getMessage());
128155
return$response;
129156
})->bindTo($this),true);// - true; handle subclass but also LogicException explicitly
130-
$mw->setDefaultErrorHandler((function () {
157+
$middleware->setDefaultErrorHandler((function () {
131158
$response =$this->createResponse();
132159
$response->getBody()->write('Oops..');
133160
return$response;
134161
})->bindTo($this));
135-
$app->add($mw);
162+
$app->add($middleware);
136163
$app->get('/foo',function (ServerRequestInterface$request,ResponseInterface$response) {
137164
$response->getBody()->write('...');
138165
return$response;
@@ -149,25 +176,25 @@ public function testSuperclassExceptionHandlerHandlesSubclassException()
149176
$callableResolver =$app->getCallableResolver();
150177
$logger =$this->getMockLogger();
151178

152-
$mw =newErrorMiddleware($callableResolver,$this->getResponseFactory(),false,false,false,$logger);
179+
$middleware =newErrorMiddleware($callableResolver,$this->getResponseFactory(),false,false,false,$logger);
153180

154181
$app->add(function ($request,$handler) {
155182
thrownewInvalidArgumentException('This is a subclass of LogicException...');
156183
});
157184

158-
$mw->setErrorHandler(LogicException::class, (function (ServerRequestInterface$request,$exception) {
185+
$middleware->setErrorHandler(LogicException::class, (function (ServerRequestInterface$request,$exception) {
159186
$response =$this->createResponse();
160187
$response->getBody()->write($exception->getMessage());
161188
return$response;
162189
})->bindTo($this),true);// - true; handle subclass
163190

164-
$mw->setDefaultErrorHandler((function () {
191+
$middleware->setDefaultErrorHandler((function () {
165192
$response =$this->createResponse();
166193
$response->getBody()->write('Oops..');
167194
return$response;
168195
})->bindTo($this));
169196

170-
$app->add($mw);
197+
$app->add($middleware);
171198

172199
$app->get('/foo',function (ServerRequestInterface$request,ResponseInterface$response) {
173200
$response->getBody()->write('...');
@@ -187,25 +214,25 @@ public function testSuperclassExceptionHandlerDoesNotHandleSubclassException()
187214
$callableResolver =$app->getCallableResolver();
188215
$logger =$this->getMockLogger();
189216

190-
$mw =newErrorMiddleware($callableResolver,$this->getResponseFactory(),false,false,false,$logger);
217+
$middleware =newErrorMiddleware($callableResolver,$this->getResponseFactory(),false,false,false,$logger);
191218

192219
$app->add(function ($request,$handler) {
193220
thrownewInvalidArgumentException('This is a subclass of LogicException...');
194221
});
195222

196-
$mw->setErrorHandler(LogicException::class, (function (ServerRequestInterface$request,$exception) {
223+
$middleware->setErrorHandler(LogicException::class, (function (ServerRequestInterface$request,$exception) {
197224
$response =$this->createResponse();
198225
$response->getBody()->write($exception->getMessage());
199226
return$response;
200227
})->bindTo($this),false);// - false; don't handle subclass
201228

202-
$mw->setDefaultErrorHandler((function () {
229+
$middleware->setDefaultErrorHandler((function () {
203230
$response =$this->createResponse();
204231
$response->getBody()->write('Oops..');
205232
return$response;
206233
})->bindTo($this));
207234

208-
$app->add($mw);
235+
$app->add($middleware);
209236

210237
$app->get('/foo',function (ServerRequestInterface$request,ResponseInterface$response) {
211238
$response->getBody()->write('...');
@@ -225,7 +252,7 @@ public function testHandleMultipleExceptionsAddedAsArray()
225252
$callableResolver =$app->getCallableResolver();
226253
$logger =$this->getMockLogger();
227254

228-
$mw =newErrorMiddleware($callableResolver,$this->getResponseFactory(),false,false,false,$logger);
255+
$middleware =newErrorMiddleware($callableResolver,$this->getResponseFactory(),false,false,false,$logger);
229256

230257
$app->add(function ($request,$handler) {
231258
thrownewInvalidArgumentException('This is an invalid argument exception...');
@@ -237,15 +264,15 @@ public function testHandleMultipleExceptionsAddedAsArray()
237264
return$response;
238265
});
239266

240-
$mw->setErrorHandler([LogicException::class, InvalidArgumentException::class],$handler->bindTo($this));
267+
$middleware->setErrorHandler([LogicException::class, InvalidArgumentException::class],$handler->bindTo($this));
241268

242-
$mw->setDefaultErrorHandler((function () {
269+
$middleware->setDefaultErrorHandler((function () {
243270
$response =$this->createResponse();
244271
$response->getBody()->write('Oops..');
245272
return$response;
246273
})->bindTo($this));
247274

248-
$app->add($mw);
275+
$app->add($middleware);
249276

250277
$app->get('/foo',function (ServerRequestInterface$request,ResponseInterface$response) {
251278
$response->getBody()->write('...');
@@ -265,19 +292,19 @@ public function testErrorHandlerHandlesThrowables()
265292
$callableResolver =$app->getCallableResolver();
266293
$logger =$this->getMockLogger();
267294

268-
$mw =newErrorMiddleware($callableResolver,$this->getResponseFactory(),false,false,false,$logger);
295+
$middleware =newErrorMiddleware($callableResolver,$this->getResponseFactory(),false,false,false,$logger);
269296

270297
$app->add(function ($request,$handler) {
271298
thrownewError('Oops..');
272299
});
273300

274-
$mw->setDefaultErrorHandler((function (ServerRequestInterface$request,$exception) {
301+
$middleware->setDefaultErrorHandler((function (ServerRequestInterface$request,$exception) {
275302
$response =$this->createResponse();
276303
$response->getBody()->write($exception->getMessage());
277304
return$response;
278305
})->bindTo($this));
279306

280-
$app->add($mw);
307+
$app->add($middleware);
281308

282309
$app->get('/foo',function (ServerRequestInterface$request,ResponseInterface$response) {
283310
$response->getBody()->write('...');

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp