Total Complexity | 3 |
Total Lines | 85 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 2 | Features | 2 |
1 | <?php |
||
15 | final class CallbackTest extends TestCase |
||
16 | { |
||
17 | /** |
||
18 | * @test |
||
19 | */ |
||
20 | public function handlerIsPassedToCallback() |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @test |
||
32 | */ |
||
33 | public function callbackResultReturned() |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @test |
||
45 | */ |
||
46 | public function requestIsPassedToCallback() |
||
47 | { |
||
48 | $requestMethod = Method::PUT; |
||
49 | $requestUri = '/test/request/uri'; |
||
50 | $middleware = new Callback(function (ServerRequestInterface $request, RequestHandlerInterface $handler) use ($requestMethod, $requestUri) { |
||
51 | $this->assertEquals($request->getMethod(), $requestMethod); |
||
52 | $this->assertEquals($request->getUri(), $requestUri); |
||
53 | return $handler->handle($request); |
||
54 | }, $this->createContainer()); |
||
55 | |||
56 | $middleware->process($this->createRequest($requestMethod, $requestUri), $this->createRequestHandler()); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @test |
||
61 | */ |
||
62 | public function checkDiContainerCalled() |
||
63 | { |
||
64 | $middleware = new Callback(function (Response $response) { |
||
65 | return $response; |
||
66 | }, $this->createContainer()); |
||
67 | |||
68 | $response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
||
69 | $this->assertEquals(404, $response->getStatusCode()); |
||
70 | } |
||
71 | |||
72 | private function createContainer(): ContainerInterface |
||
73 | { |
||
74 | return new class implements ContainerInterface { |
||
75 | public function get($id) |
||
76 | { |
||
77 | return new Response(404); |
||
78 | } |
||
79 | |||
80 | public function has($id) |
||
81 | { |
||
82 | // do nothing |
||
83 | } |
||
84 | }; |
||
85 | } |
||
86 | |||
87 | private function createRequestHandler(): RequestHandlerInterface |
||
88 | { |
||
89 | return new class implements RequestHandlerInterface { |
||
90 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
91 | { |
||
92 | return new Response(200); |
||
93 | } |
||
94 | }; |
||
95 | } |
||
96 | |||
97 | private function createRequest(string $method = Method::GET, string $uri = '/'): ServerRequestInterface |
||
100 | } |
||
101 | } |
||
102 |