Passed
Push — master ( 5b49ac...957a79 )
by SignpostMarv
02:45
created

CookieMiddlewareTest::testCookieMiddleware()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 91
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 53
nc 2
nop 10
dl 0
loc 91
rs 8.518
c 0
b 0
f 0

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
* @author SignpostMarv
4
*/
5
declare(strict_types=1);
6
7
namespace SignpostMarv\DaftFramework\Tests;
8
9
use Generator;
10
use PHPUnit\Framework\TestCase as Base;
11
use SignpostMarv\DaftFramework\Http\CookieMiddleware;
12
use SignpostMarv\DaftFramework\HttpHandler;
13
use SignpostMarv\DaftRouter\DaftSource;
14
use Symfony\Component\HttpFoundation\Cookie;
15
use Symfony\Component\HttpFoundation\Request;
16
17
class CookieMiddlewareTest extends Base
18
{
19
    /**
20
    * @dataProvider DataProvderCookeMiddlewareTest
21
    */
22
    public function testCookieMiddleware(
23
        string $implementation,
24
        array $postConstructionCalls,
0 ignored issues
show
Unused Code introduced by
The parameter $postConstructionCalls is not used and could be removed. ( Ignorable by Annotation )

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

24
        /** @scrutinizer ignore-unused */ array $postConstructionCalls,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
        string $baseUrl,
26
        string $basePath,
27
        array $config,
28
        string $cookieName,
29
        string $cookieValue,
30
        string $secure,
31
        string $http,
32
        string $sameSite
33
    ) : void {
34
        $url = sprintf(
35
            'cookie-test/%s/%s/%s/%s/%s',
36
            rawurlencode($cookieName),
37
            rawurlencode($cookieValue),
38
            rawurlencode($secure),
39
            rawurlencode($http),
40
            rawurlencode($sameSite)
41
        );
42
43
        $config[CookieMiddleware::class] = [
44
            'secure' => '1' !== $secure,
45
            'httpOnly' => '1' !== $http,
46
            'sameSite' => (
47
                ('lax' === $sameSite)
48
                    ? 'strict'
49
                    : 'lax'
50
            ),
51
        ];
52
53
        $config[DaftSource::class]['sources'] = [
54
            fixtures\Routes\CookieTest::class,
55
        ];
56
        $config[DaftSource::class]['cacheFile'] = (__DIR__ . '/fixtures/cookie-test.fast-route.cache');
57
58
        $instance = Utilities::ObtainHttpHandlerInstance(
59
            $this,
60
            $implementation,
61
            $baseUrl,
62
            $basePath,
63
            $config
64
        );
65
66
        $request = Request::create($baseUrl . $url);
67
68
        $response = $instance->handle($request);
69
70
        $cookie = current(array_filter(
71
            $response->headers->getCookies(),
72
            function (Cookie $cookie) use ($cookieName) : bool {
73
                return $cookieName === $cookie->getName();
74
            }
75
        ));
76
77
        $this->assertInstanceOf(Cookie::class, $cookie);
78
79
        $this->assertSame('1' === $secure, $cookie->isSecure(), 'Secure must match without middleware');
80
        $this->assertSame('1' === $http, $cookie->isHttpOnly(), 'HttpOnly must match without middleware');
81
        $this->assertSame($sameSite, $cookie->getSameSite(), 'SameSite must match without middleware');
82
83
        $config[DaftSource::class]['sources'] = [
84
            fixtures\Routes\CookieTest::class,
85
            CookieMiddleware::class,
86
        ];
87
        $config[DaftSource::class]['cacheFile'] = (__DIR__ . '/fixtures/cookie-middleware.fast-route.cache');
88
89
        $instance = Utilities::ObtainHttpHandlerInstance(
90
            $this,
91
            $implementation,
92
            $baseUrl,
93
            $basePath,
94
            $config
95
        );
96
97
        $request = Request::create($baseUrl . $url);
98
99
        $response = $instance->handle($request);
100
101
        $cookie = current(array_filter(
102
            $response->headers->getCookies(),
103
            function (Cookie $cookie) use ($cookieName) : bool {
104
                return $cookieName === $cookie->getName();
105
            }
106
        ));
107
108
        $this->assertInstanceOf(Cookie::class, $cookie);
109
110
        $this->assertSame($config[CookieMiddleware::class]['secure'], $cookie->isSecure(), 'Secure must match flipped value with middleware');
111
        $this->assertSame($config[CookieMiddleware::class]['httpOnly'], $cookie->isHttpOnly(), 'HttpOnly must match flipped value with middleware');
112
        $this->assertSame($config[CookieMiddleware::class]['sameSite'], $cookie->getSameSite(), 'SameSite must match flipped value with middleware');
113
    }
114
115
    public function DataProvderCookeMiddlewareTest() : Generator
116
    {
117
        foreach ($this->DataProviderCookieNameValue() as $cookie) {
118
            foreach ($this->DataProviderCookieSecure() as $secure) {
119
                foreach ($this->DataProviderCookieHttp() as $http) {
120
                    foreach ($this->DataProviderCookieSameSite() as $sameSite) {
121
                        foreach ($this->DataProviderHttpHandlerInstances() as $handlerArgs) {
122
                            yield array_merge($handlerArgs, $cookie, [$secure, $http, $sameSite]);
123
                        }
124
                    }
125
                }
126
            }
127
        }
128
    }
129
130
    public function DataProviderHttpHandlerInstances() : Generator
131
    {
132
        yield from [
133
            [
134
                HttpHandler::class,
135
                [
136
                ],
137
                'https://example.com/',
138
                realpath(__DIR__ . '/fixtures'),
139
                [
140
                    DaftSource::class => [
141
                    ],
142
                ],
143
            ],
144
        ];
145
    }
146
147
    public function DataProviderCookieNameValue() : Generator
148
    {
149
        yield from [
150
            [
151
                'a',
152
                'b',
153
            ],
154
        ];
155
    }
156
157
    public function DataProviderCookieSecure() : Generator
158
    {
159
        yield from ['0', '1'];
160
    }
161
162
    public function DataProviderCookieHttp() : Generator
163
    {
164
        yield from ['0', '1'];
165
    }
166
167
    public function DataProviderCookieSameSite() : Generator
168
    {
169
        yield from ['lax', 'strict'];
170
    }
171
}
172