|
| 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://doc.hyperf.io |
| 9 | + |
| 10 | + * @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE |
| 11 | + */ |
| 12 | + |
| 13 | +namespace HyperfTest\Validation\Cases; |
| 14 | + |
| 15 | +use Hyperf\Contract\NormalizerInterface; |
| 16 | +use Hyperf\Di\Container; |
| 17 | +use Hyperf\Di\MethodDefinitionCollector; |
| 18 | +use Hyperf\Di\MethodDefinitionCollectorInterface; |
| 19 | +use Hyperf\Dispatcher\HttpRequestHandler; |
| 20 | +use Hyperf\HttpMessage\Base\Response; |
| 21 | +use Hyperf\HttpMessage\Server\Request; |
| 22 | +use Hyperf\HttpMessage\Uri\Uri; |
| 23 | +use Hyperf\HttpServer\CoreMiddleware; |
| 24 | +use Hyperf\HttpServer\Router\Dispatched; |
| 25 | +use Hyperf\HttpServer\Router\DispatcherFactory; |
| 26 | +use Hyperf\Translation\ArrayLoader; |
| 27 | +use Hyperf\Translation\Translator; |
| 28 | +use Hyperf\Utils\ApplicationContext; |
| 29 | +use Hyperf\Utils\Context; |
| 30 | +use Hyperf\Utils\Serializer\SimpleNormalizer; |
| 31 | +use Hyperf\Validation\Contract\ValidatorFactoryInterface; |
| 32 | +use Hyperf\Validation\Middleware\ValidationMiddleware; |
| 33 | +use Hyperf\Validation\ValidatorFactory; |
| 34 | +use HyperfTest\Validation\Cases\Stub\DemoController; |
| 35 | +use HyperfTest\Validation\Cases\Stub\DemoRequest; |
| 36 | +use Mockery; |
| 37 | +use PHPUnit\Framework\TestCase; |
| 38 | +use Psr\EventDispatcher\EventDispatcherInterface; |
| 39 | +use Psr\Http\Message\ResponseInterface; |
| 40 | +use Psr\Http\Message\ServerRequestInterface; |
| 41 | + |
| 42 | +/** |
| 43 | + * @internal |
| 44 | + * @coversNothing |
| 45 | + */ |
| 46 | +class ValidationMiddlewareTest extends TestCase |
| 47 | +{ |
| 48 | + public function testProcess() |
| 49 | + { |
| 50 | + $container = $this->createContainer(); |
| 51 | + $factory = $container->get(DispatcherFactory::class); |
| 52 | + |
| 53 | + $router = $factory->getRouter('http'); |
| 54 | + $router->addRoute('POST', '/sign-up', 'HyperfTest\Validation\Cases\Stub\DemoController@signUp'); |
| 55 | + $router->addRoute('POST', '/sign-in', 'HyperfTest\Validation\Cases\Stub\DemoController::signIn'); |
| 56 | + $router->addRoute('POST', '/sign-out', [\HyperfTest\Validation\Cases\Stub\DemoController::class, 'signOut']); |
| 57 | + |
| 58 | + $dispatcher = $factory->getDispatcher('http'); |
| 59 | + $middleware = new ValidationMiddleware($container); |
| 60 | + $coreMiddleware = new CoreMiddleware($container, 'http'); |
| 61 | + $handler = new HttpRequestHandler([$middleware], $coreMiddleware, $container); |
| 62 | + Context::set(ResponseInterface::class, new Response()); |
| 63 | + |
| 64 | + $request = (new Request('POST', new Uri('/sign-up'))) |
| 65 | + ->withParsedBody(['username' => 'Hyperf', 'password' => 'Hyperf']); |
| 66 | + $routes = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath()); |
| 67 | + $request = Context::set(ServerRequestInterface::class, $request->withAttribute(Dispatched::class, new Dispatched($routes))); |
| 68 | + $response = $middleware->process($request, $handler); |
| 69 | + $this->assertEquals(200, $response->getStatusCode()); |
| 70 | + |
| 71 | + $request = (new Request('POST', new Uri('/sign-in'))) |
| 72 | + ->withParsedBody(['username' => 'Hyperf', 'password' => 'Hyperf']); |
| 73 | + $routes = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath()); |
| 74 | + $request = Context::set(ServerRequestInterface::class, $request->withAttribute(Dispatched::class, new Dispatched($routes))); |
| 75 | + $response = $middleware->process($request, $handler); |
| 76 | + $this->assertEquals(200, $response->getStatusCode()); |
| 77 | + |
| 78 | + $request = (new Request('POST', new Uri('/sign-out'))) |
| 79 | + ->withParsedBody(['username' => 'Hyperf', 'password' => 'Hyperf']); |
| 80 | + $routes = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath()); |
| 81 | + $request = Context::set(ServerRequestInterface::class, $request->withAttribute(Dispatched::class, new Dispatched($routes))); |
| 82 | + $response = $middleware->process($request, $handler); |
| 83 | + $this->assertEquals(200, $response->getStatusCode()); |
| 84 | + } |
| 85 | + |
| 86 | + public function createContainer() |
| 87 | + { |
| 88 | + $eventDispatcher = Mockery::mock(EventDispatcherInterface::class); |
| 89 | + $container = Mockery::mock(Container::class); |
| 90 | + |
| 91 | + $container->shouldReceive('get')->with(DispatcherFactory::class) |
| 92 | + ->andReturn(new DispatcherFactory()); |
| 93 | + $container->shouldReceive('get')->with(EventDispatcherInterface::class) |
| 94 | + ->andReturn($eventDispatcher); |
| 95 | + $container->shouldReceive('get')->with(ValidatorFactoryInterface::class) |
| 96 | + ->andReturn(new ValidatorFactory(new Translator(new ArrayLoader(), 'en'), $container)); |
| 97 | + $container->shouldReceive('get')->with(NormalizerInterface::class) |
| 98 | + ->andReturn(new SimpleNormalizer()); |
| 99 | + $container->shouldReceive('get')->with(MethodDefinitionCollectorInterface::class) |
| 100 | + ->andReturn(new MethodDefinitionCollector()); |
| 101 | + $container->shouldReceive('get')->with(DemoController::class) |
| 102 | + ->andReturn(new DemoController()); |
| 103 | + $container->shouldReceive('get')->with(DemoRequest::class) |
| 104 | + ->andReturn(new DemoRequest($container)); |
| 105 | + $container->shouldReceive('has')->with(DemoRequest::class) |
| 106 | + ->andReturn(true); |
| 107 | + |
| 108 | + ApplicationContext::setContainer($container); |
| 109 | + |
| 110 | + return $container; |
| 111 | + } |
| 112 | +} |
0 commit comments