Passed
Push — master ( c833e6...2cc74e )
by Felipe
02:42 queued 19s
created

RequestMethodMiddlewareTest::testDiffMethodReturnNextResponse()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 1
nop 1
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of coisa/http.
4
 *
5
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace CoiSA\Http\Test\Handler;
12
13
use CoiSA\Http\Middleware\RequestMethodMiddleware;
14
use Fig\Http\Message\RequestMethodInterface;
15
16
/**
17
 * Class RequestMethodMiddlewareTest
18
 *
19
 * @package CoiSA\Http\Test\Handler
20
 */
21
final class RequestMethodMiddlewareTest extends AbstractMiddlewareTest
22
{
23
    public function setUp(): void
24
    {
25
        parent::setUp();
26
27
        $this->middleware = new RequestMethodMiddleware(
28
            RequestMethodInterface::METHOD_GET,
29
            $this->handler->reveal()
30
        );
31
    }
32
33
    public function testInvalidMethodThrowException(): void
34
    {
35
        $this->expectException(\UnexpectedValueException::class);
36
37
        new RequestMethodMiddleware(
38
            \uniqid('test', false),
39
            $this->handler->reveal()
40
        );
41
    }
42
43
    public function provideMethods()
44
    {
45
        $reflection     = new \ReflectionClass(RequestMethodInterface::class);
46
        $allowedMethods = $reflection->getConstants();
47
48
        return \array_chunk($allowedMethods, 1);
49
    }
50
51
    /**
52
     * @dataProvider provideMethods
53
     *
54
     * @param string $method
55
     */
56
    public function testGivenMethodReturnHandlerResponse(string $method): void
57
    {
58
        $this->serverRequest->getMethod()->willReturn($method);
59
60
        $middleware = new RequestMethodMiddleware(
61
            $method,
62
            $this->handler->reveal()
63
        );
64
65
        $response = $middleware->process($this->serverRequest->reveal(), $this->nextHandler->reveal());
66
67
        $this->assertSame($this->response->reveal(), $response);
68
    }
69
70
    /**
71
     * @dataProvider provideMethods
72
     *
73
     * @param string $method
74
     */
75
    public function testDiffMethodReturnNextResponse(string $method): void
76
    {
77
        $this->serverRequest->getMethod()->willReturn(
78
            $method === RequestMethodInterface::METHOD_GET ?
79
                RequestMethodInterface::METHOD_POST :
80
                RequestMethodInterface::METHOD_GET
81
        );
82
83
        $middleware = new RequestMethodMiddleware(
84
            $method,
85
            $this->handler->reveal()
86
        );
87
88
        $response = $middleware->process($this->serverRequest->reveal(), $this->nextHandler->reveal());
89
90
        $this->assertSame($this->nextResponse->reveal(), $response);
91
    }
92
}
93