Completed
Pull Request — master (#113)
by
unknown
02:56 queued 01:21
created

CallbackTest::checkDiContainerCalled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 8
cc 1
rs 10
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\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Yiisoft\Router\Method;
13
use Yiisoft\Yii\Web\Middleware\Callback;
14
15
final class CallbackTest extends TestCase
16
{
17
    /**
18
     * @test
19
     */
20
    public function handlerIsPassedToCallback()
21
    {
22
        $middleware = new Callback(function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
23
            return $handler->handle($request);
24
        }, $this->createContainer());
25
26
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
27
        $this->assertEquals(200, $response->getStatusCode());
28
    }
29
30
    /**
31
     * @test
32
     */
33
    public function callbackResultReturned()
34
    {
35
        $middleware = new Callback(function () {
36
            return new Response(400);
37
        }, $this->createContainer());
38
39
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
40
        $this->assertEquals(400, $response->getStatusCode());
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
98
    {
99
        return new ServerRequest($method, $uri);
100
    }
101
}
102