Skip to content

Commit d3f6326

Browse files
wdaglblimingxinleo
andauthored
Fixed bug that FormRequest could't get the changed data from Context. (#3351)
* Added test cases Co-authored-by: 李铭昕 <715557344@qq.com>
1 parent d924925 commit d3f6326

File tree

6 files changed

+57
-30
lines changed

6 files changed

+57
-30
lines changed

src/Middleware/ValidationMiddleware.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
5555
throw new ServerException(sprintf('The dispatched object is not a %s object.', Dispatched::class));
5656
}
5757

58+
Context::set(ServerRequestInterface::class, $request);
59+
5860
if ($this->shouldHandle($dispatched)) {
5961
try {
6062
[$requestHandler, $method] = $this->prepareHandler($dispatched->handler->callback);

tests/Cases/Stub/FooMiddleware.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\Validation\Cases\Stub;
13+
14+
use Psr\Http\Message\ResponseInterface;
15+
use Psr\Http\Message\ServerRequestInterface;
16+
use Psr\Http\Server\MiddlewareInterface;
17+
use Psr\Http\Server\RequestHandlerInterface;
18+
19+
class FooMiddleware implements MiddlewareInterface
20+
{
21+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
22+
{
23+
$parsedBody = $request->getParsedBody();
24+
if (! isset($parsedBody['password'])) {
25+
$parsedBody['password'] = 'Hyperf';
26+
}
27+
return $handler->handle($request->withParsedBody($parsedBody));
28+
}
29+
}

tests/Cases/ValidationMiddlewareTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Hyperf\Validation\ValidatorFactory;
3535
use HyperfTest\Validation\Cases\Stub\DemoController;
3636
use HyperfTest\Validation\Cases\Stub\DemoRequest;
37+
use HyperfTest\Validation\Cases\Stub\FooMiddleware;
3738
use Mockery;
3839
use PHPUnit\Framework\TestCase;
3940
use Psr\EventDispatcher\EventDispatcherInterface;
@@ -54,6 +55,31 @@ protected function tearDown(): void
5455
Context::set('http.request.parsedData', null);
5556
}
5657

58+
public function testValidationMiddlewareGetTheLatestInputData()
59+
{
60+
$container = $this->createContainer();
61+
$factory = $container->get(DispatcherFactory::class);
62+
63+
$router = $factory->getRouter('http');
64+
$router->addRoute('POST', '/sign-up', 'HyperfTest\Validation\Cases\Stub\DemoController@signUp');
65+
66+
$dispatcher = $factory->getDispatcher('http');
67+
$fooMiddleware = new FooMiddleware();
68+
$middleware = new ValidationMiddleware($container);
69+
$coreMiddleware = new CoreMiddleware($container, 'http');
70+
$handler = new HttpRequestHandler([$fooMiddleware, $middleware], $coreMiddleware, $container);
71+
Context::set(ResponseInterface::class, new Response());
72+
73+
$request = (new Request('POST', new Uri('/sign-up')))
74+
->withParsedBody(['username' => 'Hyperf']);
75+
$routes = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
76+
$request = Context::set(ServerRequestInterface::class, $request->withAttribute(Dispatched::class, new Dispatched($routes)));
77+
78+
$response = $handler->handle($request);
79+
80+
$this->assertEquals(200, $response->getStatusCode());
81+
}
82+
5783
public function testProcess()
5884
{
5985
$container = $this->createContainer();

tests/bootstrap.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/ci.ini

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/swoole.install.sh

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)