IsSafeHttpRequestTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 137
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testSafeMethods() 0 9 1
A httpMethodsProvider() 0 35 1
A testSafeMethodsWithDefaults() 0 9 1
B safeDefaultsMatchingProvider() 0 65 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PSR7CsrfTest\HttpMethod;
6
7
use PHPUnit\Framework\TestCase;
8
use Psr\Http\Message\RequestInterface;
9
use PSR7Csrf\HttpMethod\IsSafeHttpRequest;
10
11
/**
12
 * @covers \PSR7Csrf\HttpMethod\IsSafeHttpRequest
13
 */
14
final class IsSafeHttpRequestTest extends TestCase
15
{
16
    /**
17
     * @dataProvider httpMethodsProvider
18
     *
19
     * @param array  $safeMethods
20
     * @param string $httpMethod
21
     * @param bool   $expectedResult
22
     */
23
    public function testSafeMethods(array $safeMethods, string $httpMethod, bool $expectedResult)
24
    {
25
        /* @var $request RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
26
        $request = $this->createMock(RequestInterface::class);
27
28
        $request->expects(self::any())->method('getMethod')->willReturn($httpMethod);
29
30
        self::assertSame($expectedResult, (new IsSafeHttpRequest(...$safeMethods))->__invoke($request));
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->createMock(\Psr\H...equestInterface::class) on line 26 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PSR7Csrf\HttpMethod\IsSafeHttpRequest::__invoke() does only seem to accept object<Psr\Http\Message\RequestInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
31
    }
32
33
    public function httpMethodsProvider() : array
34
    {
35
        return [
36
            'empty' => [
37
                [],
38
                'GET',
39
                false,
40
            ],
41
            'GET only' => [
42
                ['GET'],
43
                'GET',
44
                true,
45
            ],
46
            'get only' => [
47
                ['get'],
48
                'GET',
49
                true,
50
            ],
51
            'GET only, matching lowercase get' => [
52
                ['GET'],
53
                'get',
54
                true,
55
            ],
56
            'GET only, non-matching method' => [
57
                ['GET'],
58
                'PUT',
59
                false,
60
            ],
61
            'GET, PUT only, matching method' => [
62
                ['GET', 'PUT'],
63
                'PUT',
64
                true,
65
            ],
66
        ];
67
    }
68
69
    /**
70
     * @dataProvider safeDefaultsMatchingProvider
71
     *
72
     * @param string $httpMethod
73
     * @param bool   $expectedResult
74
     */
75
    public function testSafeMethodsWithDefaults(string $httpMethod, bool $expectedResult)
76
    {
77
        /* @var $request RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
78
        $request = $this->createMock(RequestInterface::class);
79
80
        $request->expects(self::any())->method('getMethod')->willReturn($httpMethod);
81
82
        self::assertSame($expectedResult, IsSafeHttpRequest::fromDefaultSafeMethods()->__invoke($request));
83
    }
84
85
    public function safeDefaultsMatchingProvider() : array
86
    {
87
        return [
88
            'empty' => [
89
                '',
90
                false,
91
            ],
92
            'GET' => [
93
                'GET',
94
                true,
95
            ],
96
            'get' => [
97
                'get',
98
                true,
99
            ],
100
            'HEAD' => [
101
                'HEAD',
102
                true,
103
            ],
104
            'head' => [
105
                'head',
106
                true,
107
            ],
108
            'OPTIONS' => [
109
                'OPTIONS',
110
                true,
111
            ],
112
            'options' => [
113
                'options',
114
                true,
115
            ],
116
            'DELETE' => [
117
                'DELETE',
118
                false,
119
            ],
120
            'delete' => [
121
                'delete',
122
                false,
123
            ],
124
            'POST' => [
125
                'POST',
126
                false,
127
            ],
128
            'post' => [
129
                'post',
130
                false,
131
            ],
132
            'PUT' => [
133
                'PUT',
134
                false,
135
            ],
136
            'put' => [
137
                'put',
138
                false,
139
            ],
140
            'UNKNOWN' => [
141
                'UNKNOWN',
142
                false,
143
            ],
144
            'unknown' => [
145
                'unknown',
146
                false,
147
            ],
148
        ];
149
    }
150
}
151