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

Commit0c6c36b

Browse files
authored
Merge pull request#3132 from adoy/request-response-namedarg-strategy
Add new `RequestResponseNamedArgs` route strategy
2 parents6aa522d +65b1ad4 commit0c6c36b

File tree

3 files changed

+260
-1
lines changed

3 files changed

+260
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* Slim Framework (https://slimframework.com)
5+
*
6+
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespaceSlim\Handlers\Strategies;
12+
13+
usePsr\Http\Message\ResponseInterface;
14+
usePsr\Http\Message\ServerRequestInterface;
15+
useSlim\Interfaces\InvocationStrategyInterface;
16+
useRuntimeException;
17+
18+
/**
19+
* Route callback strategy with route parameters as individual arguments.
20+
*/
21+
class RequestResponseNamedArgsimplements InvocationStrategyInterface
22+
{
23+
publicfunction__construct()
24+
{
25+
if (PHP_VERSION_ID <80000) {
26+
thrownewRuntimeException('Named arguments are only available for PHP >= 8.0.0');
27+
}
28+
}
29+
30+
/**
31+
* Invoke a route callable with request, response and all route parameters
32+
* as individual arguments.
33+
*
34+
* @param callable $callable
35+
* @param ServerRequestInterface $request
36+
* @param ResponseInterface $response
37+
* @param array<string, string> $routeArguments
38+
*
39+
* @return ResponseInterface
40+
*/
41+
publicfunction__invoke(
42+
callable$callable,
43+
ServerRequestInterface$request,
44+
ResponseInterface$response,
45+
array$routeArguments
46+
):ResponseInterface {
47+
return$callable($request,$response, ...$routeArguments);
48+
}
49+
}

‎tests/AppTest.php‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
useSlim\Exception\HttpMethodNotAllowedException;
2929
useSlim\Exception\HttpNotFoundException;
3030
useSlim\Handlers\Strategies\RequestResponseArgs;
31+
useSlim\Handlers\Strategies\RequestResponseNamedArgs;
3132
useSlim\Interfaces\CallableResolverInterface;
3233
useSlim\Interfaces\MiddlewareDispatcherInterface;
3334
useSlim\Interfaces\RouteCollectorInterface;
@@ -1214,7 +1215,7 @@ public function testInvokeWithMatchingRouteWithSetArguments()
12141215
$this->assertEquals('Hello World', (string)$response->getBody());
12151216
}
12161217

1217-
publicfunctiontestInvokeWithMatchingRouteWithNamedParameter()
1218+
publicfunctiontestInvokeWithMatchingRouteWithNamedParameterRequestResponseStrategy()
12181219
{
12191220
$streamProphecy =$this->prophesize(StreamInterface::class);
12201221
$streamProphecy->__toString()->willReturn('');
@@ -1295,6 +1296,54 @@ public function testInvokeWithMatchingRouteWithNamedParameterRequestResponseArgS
12951296
$this->assertEquals('Hello World', (string)$response->getBody());
12961297
}
12971298

1299+
publicfunctiontestInvokeWithMatchingRouteWithNamedParameterRequestResponseNamedArgsStrategy()
1300+
{
1301+
if (PHP_VERSION_ID <80000) {
1302+
$this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0');
1303+
}
1304+
1305+
$streamProphecy =$this->prophesize(StreamInterface::class);
1306+
$streamProphecy->__toString()->willReturn('');
1307+
$streamProphecy->write(Argument::type('string'))->will(function ($args) {
1308+
$body =$this->reveal()->__toString();
1309+
$body .=$args[0];
1310+
$this->__toString()->willReturn($body);
1311+
});
1312+
1313+
$responseProphecy =$this->prophesize(ResponseInterface::class);
1314+
$responseProphecy->getBody()->willReturn($streamProphecy->reveal());
1315+
1316+
$responseFactoryProphecy =$this->prophesize(ResponseFactoryInterface::class);
1317+
$responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal());
1318+
1319+
$app =newApp($responseFactoryProphecy->reveal());
1320+
$app->getRouteCollector()->setDefaultInvocationStrategy(newRequestResponseNamedArgs());
1321+
$app->get(
1322+
'/{greeting}/{name}',
1323+
function (ServerRequestInterface$request,ResponseInterface$response,$name,$greeting) {
1324+
$response->getBody()->write("{$greeting}{$name}");
1325+
return$response;
1326+
}
1327+
);
1328+
1329+
$uriProphecy =$this->prophesize(UriInterface::class);
1330+
$uriProphecy->getPath()->willReturn('/Hello/World');
1331+
1332+
$requestProphecy =$this->prophesize(ServerRequestInterface::class);
1333+
$requestProphecy->getMethod()->willReturn('GET');
1334+
$requestProphecy->getUri()->willReturn($uriProphecy->reveal());
1335+
$requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null);
1336+
$requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) {
1337+
$this->getAttribute($args[0])->willReturn($args[1]);
1338+
return$this;
1339+
});
1340+
1341+
$response =$app->handle($requestProphecy->reveal());
1342+
1343+
$this->assertInstanceOf(ResponseInterface::class,$response);
1344+
$this->assertEquals('Hello World', (string)$response->getBody());
1345+
}
1346+
12981347
publicfunctiontestInvokeWithMatchingRouteWithNamedParameterOverwritesSetArgument()
12991348
{
13001349
$streamProphecy =$this->prophesize(StreamInterface::class);
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
/**
4+
* Slim Framework (https://slimframework.com)
5+
*
6+
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespaceSlim\Tests\Handlers\Strategies;
12+
13+
usePsr\Http\Message\ResponseInterface;
14+
usePsr\Http\Message\ServerRequestInterface;
15+
useRuntimeException;
16+
useSlim\Handlers\Strategies\RequestResponseNamedArgs;
17+
useSlim\Tests\TestCase;
18+
19+
class RequestResponseNamedArgsTestextends TestCase
20+
{
21+
privateServerRequestInterface$request;
22+
privateResponseInterface$response;
23+
24+
publicfunctionsetUp():void
25+
{
26+
$this->request =$this->createMock(ServerRequestInterface::class);
27+
$this->response =$this->createMock(ResponseInterface::class);
28+
}
29+
30+
publicfunctiontestCreatingRequestResponseNamedArgsThrowsRuntimeExceptionForPHPOlderThan80()
31+
{
32+
if (PHP_VERSION_ID >=80000) {
33+
$this->markTestSkipped('Test only valid for PHP versions older than 8.0');
34+
}
35+
36+
$this->expectException(RuntimeException::class);
37+
newRequestResponseNamedArgs();
38+
}
39+
40+
publicfunctiontestCallingWithEmptyArguments()
41+
{
42+
if (PHP_VERSION_ID <80000) {
43+
$this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0');
44+
}
45+
46+
$args = [];
47+
$invocationStrategy =newRequestResponseNamedArgs();
48+
49+
$callback =function ($request,$response) {
50+
$this->assertSame($this->request,$request);
51+
$this->assertSame($this->response,$response);
52+
53+
return$response;
54+
};
55+
56+
$this->assertSame($this->response,$invocationStrategy($callback,$this->request,$this->response,$args));
57+
}
58+
59+
publicfunctiontestCallingWithKnownArguments()
60+
{
61+
if (PHP_VERSION_ID <80000) {
62+
$this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0');
63+
}
64+
65+
$args = [
66+
'name' =>'world',
67+
'greeting' =>'hello',
68+
];
69+
70+
$invocationStrategy =newRequestResponseNamedArgs();
71+
72+
$callback =function ($request,$response,$greeting,$name)use ($args) {
73+
$this->assertSame($this->request,$request);
74+
$this->assertSame($this->response,$response);
75+
$this->assertSame($greeting,$args['greeting']);
76+
$this->assertSame($name,$args['name']);
77+
78+
return$response;
79+
};
80+
81+
$this->assertSame($this->response,$invocationStrategy($callback,$this->request,$this->response,$args));
82+
}
83+
84+
publicfunctiontestCallingWithOptionalArguments()
85+
{
86+
if (PHP_VERSION_ID <80000) {
87+
$this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0');
88+
}
89+
90+
$args = [
91+
'name' =>'world',
92+
];
93+
94+
$invocationStrategy =newRequestResponseNamedArgs();
95+
96+
$callback =function ($request,$response,$greeting ='Hello',$name ='Rob')use ($args) {
97+
$this->assertSame($this->request,$request);
98+
$this->assertSame($this->response,$response);
99+
$this->assertSame($greeting,'Hello');
100+
$this->assertSame($name,$args['name']);
101+
102+
return$response;
103+
};
104+
105+
$this->assertSame($this->response,$invocationStrategy($callback,$this->request,$this->response,$args));
106+
}
107+
108+
publicfunctiontestCallingWithUnknownAndVariadic()
109+
{
110+
if (PHP_VERSION_ID <80000) {
111+
$this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0');
112+
}
113+
114+
$args = [
115+
'name' =>'world',
116+
'greeting' =>'hello',
117+
];
118+
119+
$invocationStrategy =newRequestResponseNamedArgs();
120+
121+
$callback =function ($request,$response, ...$arguments)use ($args) {
122+
$this->assertSame($this->request,$request);
123+
$this->assertSame($this->response,$response);
124+
$this->assertSame($args,$arguments);
125+
126+
return$response;
127+
};
128+
129+
$this->assertSame($this->response,$invocationStrategy($callback,$this->request,$this->response,$args));
130+
}
131+
132+
publicfunctiontestCallingWithMixedKnownAndUnknownParametersAndVariadic()
133+
{
134+
if (PHP_VERSION_ID <80000) {
135+
$this->markTestSkipped('Named arguments are not supported in PHP versions prior to 8.0');
136+
}
137+
138+
$known = [
139+
'name' =>'world',
140+
'greeting' =>'hello',
141+
];
142+
$unknown = [
143+
'foo' =>'foo',
144+
'bar' =>'bar',
145+
];
146+
$args =array_merge($known,$unknown);
147+
$invocationStrategy =newRequestResponseNamedArgs();
148+
149+
$callback =function ($request,$response,$name,$greeting, ...$arguments)use ($known,$unknown) {
150+
$this->assertSame($this->request,$request);
151+
$this->assertSame($this->response,$response);
152+
$this->assertSame($name,$known['name']);
153+
$this->assertSame($greeting,$known['greeting']);
154+
$this->assertSame($unknown,$arguments);
155+
156+
return$response;
157+
};
158+
159+
$this->assertSame($this->response,$invocationStrategy($callback,$this->request,$this->response,$args));
160+
}
161+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp