|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Nyholm\Psr7\Response; |
|
6
|
|
|
use Nyholm\Psr7\ServerRequest; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
11
|
|
|
use Yiisoft\Http\Method; |
|
12
|
|
|
use Yiisoft\Yii\Web\Middleware\Callback; |
|
13
|
|
|
|
|
14
|
|
|
final class CallbackTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
public function testHandlerIsPassedToCallback(): void |
|
17
|
|
|
{ |
|
18
|
|
|
$middleware = new Callback(function (ServerRequestInterface $request, RequestHandlerInterface $handler) { |
|
19
|
|
|
return $handler->handle($request); |
|
20
|
|
|
}, $this->createContainer()); |
|
21
|
|
|
|
|
22
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
23
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function testCallbackResultReturned(): void |
|
27
|
|
|
{ |
|
28
|
|
|
$middleware = new Callback(function () { |
|
29
|
|
|
return new Response(400); |
|
30
|
|
|
}, $this->createContainer()); |
|
31
|
|
|
|
|
32
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
33
|
|
|
$this->assertEquals(400, $response->getStatusCode()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testRequestIsPassedToCallback(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$requestMethod = Method::PUT; |
|
39
|
|
|
$requestUri = '/test/request/uri'; |
|
40
|
|
|
$middleware = new Callback(function (ServerRequestInterface $request, RequestHandlerInterface $handler) use ($requestMethod, $requestUri) { |
|
41
|
|
|
$this->assertEquals($request->getMethod(), $requestMethod); |
|
42
|
|
|
$this->assertEquals($request->getUri(), $requestUri); |
|
43
|
|
|
return $handler->handle($request); |
|
44
|
|
|
}, $this->createContainer()); |
|
45
|
|
|
|
|
46
|
|
|
$middleware->process($this->createRequest($requestMethod, $requestUri), $this->createRequestHandler()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testCheckDiContainerCalled(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$middleware = new Callback(function (Response $response) { |
|
52
|
|
|
return $response; |
|
53
|
|
|
}, $this->createContainer()); |
|
54
|
|
|
|
|
55
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
56
|
|
|
$this->assertEquals(404, $response->getStatusCode()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function createContainer(): ContainerInterface |
|
60
|
|
|
{ |
|
61
|
|
|
$container = $this->createMock(ContainerInterface::class); |
|
62
|
|
|
$container |
|
63
|
|
|
->method('get') |
|
64
|
|
|
->willReturn(new Response(404)); |
|
65
|
|
|
|
|
66
|
|
|
return $container; |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function createRequestHandler(): RequestHandlerInterface |
|
70
|
|
|
{ |
|
71
|
|
|
$requestHandler = $this->createMock(RequestHandlerInterface::class); |
|
72
|
|
|
$requestHandler |
|
73
|
|
|
->method('handle') |
|
74
|
|
|
->willReturn(new Response(200)); |
|
75
|
|
|
|
|
76
|
|
|
return $requestHandler; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function createRequest(string $method = Method::GET, string $uri = '/'): ServerRequestInterface |
|
80
|
|
|
{ |
|
81
|
|
|
return new ServerRequest($method, $uri); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|