Passed
Push — master ( d5342d...ace0e3 )
by Felipe
02:01
created

testInvalidMethodThrowException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
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
use Psr\Http\Server\RequestHandlerInterface;
16
17
/**
18
 * Class RequestMethodMiddlewareTest
19
 *
20
 * @package CoiSA\Http\Test\Handler
21
 */
22
final class RequestMethodMiddlewareTest extends AbstractMiddlewareTest
23
{
24
    public function setUp(): void
25
    {
26
        parent::setUp();
27
28
        $this->middleware = new RequestMethodMiddleware(
29
            RequestMethodInterface::METHOD_GET,
30
            $this->handler->reveal()
31
        );
32
    }
33
34
    public function testInvalidMethodThrowException()
35
    {
36
        $this->expectException(\UnexpectedValueException::class);
37
38
        new RequestMethodMiddleware(
39
            uniqid('test', false),
40
            $this->handler->reveal()
41
        );
42
    }
43
44
    public function provideMethods()
45
    {
46
        $reflection    = new \ReflectionClass(RequestMethodInterface::class);
47
        $allowedMethods = $reflection->getConstants();
48
49
        return \array_chunk($allowedMethods, 1);
50
    }
51
52
    /**
53
     * @dataProvider provideMethods
54
     *
55
     * @param string $method
56
     */
57
    public function testGivenMethodReturnHandlerResponse(string $method)
58
    {
59
        $this->serverRequest->getMethod()->willReturn($method);
60
61
        $middleware = new RequestMethodMiddleware(
62
            $method,
63
            $this->handler->reveal()
64
        );
65
66
        $response = $middleware->process($this->serverRequest->reveal(), $this->nextHandler->reveal());
67
68
        $this->assertSame($this->response->reveal(), $response);
69
    }
70
71
    /**
72
     * @dataProvider provideMethods
73
     *
74
     * @param string $method
75
     */
76
    public function testDiffMethodReturnNextResponse(string $method)
77
    {
78
        $this->serverRequest->getMethod()->willReturn(
79
            $method === RequestMethodInterface::METHOD_GET ?
80
                RequestMethodInterface::METHOD_POST :
81
                RequestMethodInterface::METHOD_GET
82
        );
83
84
        $middleware = new RequestMethodMiddleware(
85
            $method,
86
            $this->handler->reveal()
87
        );
88
89
        $response = $middleware->process($this->serverRequest->reveal(), $this->nextHandler->reveal());
90
91
        $this->assertSame($this->nextResponse->reveal(), $response);
92
    }
93
}
94