Completed
Push — develop ( d426db...c7f15b )
by Alejandro
22s queued 11s
created

Rest/test/Middleware/BodyParserMiddlewareTest.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Rest\Middleware;
6
7
use Laminas\Diactoros\Response;
8
use Laminas\Diactoros\ServerRequest;
9
use Laminas\Diactoros\Stream;
10
use PHPUnit\Framework\TestCase;
11
use Prophecy\Argument;
12
use Prophecy\Prophecy\ProphecyInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Server\RequestHandlerInterface;
15
use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware;
16
17
use function array_shift;
18
19
class BodyParserMiddlewareTest extends TestCase
20
{
21
    private BodyParserMiddleware $middleware;
22
23
    public function setUp(): void
24
    {
25
        $this->middleware = new BodyParserMiddleware();
26
    }
27
28
    /**
29
     * @test
30
     * @dataProvider provideIgnoredRequestMethods
31
     */
32
    public function requestsFromOtherMethodsJustFallbackToNextMiddleware(string $method): void
33
    {
34
        $request = $this->prophesize(ServerRequestInterface::class);
35
        $request->getMethod()->willReturn($method);
36
        $request->getParsedBody()->willReturn([]);
37
38
        self::assertHandlingRequestJustFallsBackToNext($request);
0 ignored issues
show
Bug Best Practice introduced by
The method ShlinkioTest\Shlink\Rest...stJustFallsBackToNext() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        self::/** @scrutinizer ignore-call */ 
39
              assertHandlingRequestJustFallsBackToNext($request);
Loading history...
39
    }
40
41
    public function provideIgnoredRequestMethods(): iterable
42
    {
43
        yield 'GET' => ['GET'];
44
        yield 'HEAD' => ['HEAD'];
45
        yield 'OPTIONS' => ['OPTIONS'];
46
    }
47
48
    /** @test */
49
    public function requestsWithNonEmptyBodyJustFallbackToNextMiddleware(): void
50
    {
51
        $request = $this->prophesize(ServerRequestInterface::class);
52
        $request->getMethod()->willReturn('POST');
53
        $request->getParsedBody()->willReturn(['foo' => 'bar']);
54
55
        self::assertHandlingRequestJustFallsBackToNext($request);
0 ignored issues
show
Bug Best Practice introduced by
The method ShlinkioTest\Shlink\Rest...stJustFallsBackToNext() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        self::/** @scrutinizer ignore-call */ 
56
              assertHandlingRequestJustFallsBackToNext($request);
Loading history...
56
    }
57
58
    private function assertHandlingRequestJustFallsBackToNext(ProphecyInterface $requestMock): void
59
    {
60
        $getContentType = $requestMock->getHeaderLine('Content-type')->willReturn('');
61
        $request = $requestMock->reveal();
62
63
        $nextHandler = $this->prophesize(RequestHandlerInterface::class);
64
        $handle = $nextHandler->handle($request)->willReturn(new Response());
65
66
        $this->middleware->process($request, $nextHandler->reveal());
67
68
        $handle->shouldHaveBeenCalledOnce();
69
        $getContentType->shouldNotHaveBeenCalled();
70
    }
71
72
    /** @test */
73
    public function jsonRequestsAreJsonDecoded(): void
74
    {
75
        $test = $this;
76
        $body = new Stream('php://temp', 'wr');
77
        $body->write('{"foo": "bar", "bar": ["one", 5]}');
78
        $request = (new ServerRequest())->withMethod('PUT')
79
                                        ->withBody($body)
80
                                        ->withHeader('content-type', 'application/json');
81
        $delegate = $this->prophesize(RequestHandlerInterface::class);
82
        $process = $delegate->handle(Argument::type(ServerRequestInterface::class))->will(
83
            function (array $args) use ($test) {
84
                /** @var ServerRequestInterface $req */
85
                $req = array_shift($args);
86
87
                $test->assertEquals([
88
                    'foo' => 'bar',
89
                    'bar' => ['one', 5],
90
                ], $req->getParsedBody());
91
92
                return new Response();
93
            },
94
        );
95
96
        $this->middleware->process($request, $delegate->reveal());
97
98
        $process->shouldHaveBeenCalledOnce();
99
    }
100
101
    /** @test */
102
    public function regularRequestsAreUrlDecoded(): void
103
    {
104
        $test = $this;
105
        $body = new Stream('php://temp', 'wr');
106
        $body->write('foo=bar&bar[]=one&bar[]=5');
107
        $request = (new ServerRequest())->withMethod('PUT')
108
                                        ->withBody($body);
109
        $delegate = $this->prophesize(RequestHandlerInterface::class);
110
        $process = $delegate->handle(Argument::type(ServerRequestInterface::class))->will(
111
            function (array $args) use ($test) {
112
                /** @var ServerRequestInterface $req */
113
                $req = array_shift($args);
114
115
                $test->assertEquals([
116
                    'foo' => 'bar',
117
                    'bar' => ['one', 5],
118
                ], $req->getParsedBody());
119
120
                return new Response();
121
            },
122
        );
123
124
        $this->middleware->process($request, $delegate->reveal());
125
126
        $process->shouldHaveBeenCalledOnce();
127
    }
128
}
129