TestCaseTrait::call()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 17
cts 17
cp 1
rs 9.328
c 0
b 0
f 0
cc 4
nc 3
nop 11
crap 4

How to fix   Many Parameters   

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 declare(strict_types=1);
2
3
namespace Limoncello\Testing;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Closure;
22
use Limoncello\Contracts\Core\ApplicationInterface;
23
use Psr\Http\Message\ResponseInterface;
24
use Psr\Http\Message\StreamInterface;
25
26
/**
27
 * @package Limoncello\Testing
28
 */
29
trait TestCaseTrait
30
{
31
    /**
32
     * @var array
33
     */
34
    private $eventHandlers = [];
35
36
    /**
37
     * @param array|null                      $server
38
     * @param array|null                      $queryParams
39
     * @param array|object|null               $parsedBody
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $parsedBody a bit more specific; maybe use null|array.
Loading history...
40
     * @param array|null                      $cookies
41
     * @param array|null                      $files
42
     * @param string|resource|StreamInterface $messageBody
43
     * @param string                          $protocolVersion
44
     *
45
     * @return Sapi
46
     */
47
    abstract protected function createSapi(
48
        array $server = null,
49
        array $queryParams = null,
50
        array $parsedBody = null,
51
        array $cookies = null,
52
        array $files = null,
53
        $messageBody = 'php://input',
54
        string $protocolVersion = '1.1'
55
    ): Sapi;
56
57
    /**
58
     * @return ApplicationInterface
59
     */
60
    abstract protected function createApplication(): ApplicationInterface;
61
62
    /** @noinspection PhpTooManyParametersInspection
63
     * @param string                          $method
64
     * @param string                          $uri
65
     * @param array                           $queryParams
66
     * @param array                           $parsedBody
67
     * @param array                           $headers
68
     * @param array                           $cookies
69
     * @param array                           $files
70
     * @param array                           $server
71
     * @param string|resource|StreamInterface $content
72
     * @param string                          $host
73
     * @param string                          $protocolVersion
74
     *
75
     * @return ResponseInterface
76
     *
77
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
78
     */
79 9
    protected function call(
80
        string $method,
81
        string $uri,
82
        array $queryParams = [],
83
        array $parsedBody = [],
84
        array $headers = [],
85
        array $cookies = [],
86
        array $files = [],
87
        array $server = [],
88
        $content = 'php://input',
89
        string $host = 'localhost',
90
        string $protocolVersion = '1.1'
91
    ): ResponseInterface {
92 9
        $headers['host'] = $host;
93
94 9
        $prefix = 'HTTP_';
95 9
        foreach ($headers as $name => $value) {
96 9
            $name = strtr(strtoupper($name), '-', '_');
97 9
            if ($name !== 'CONTENT_TYPE' && strpos($name, $prefix) !== 0) {
98 9
                $name = $prefix . $name;
99
            }
100 9
            $server[$name] = $value;
101
        }
102
103 9
        $server['REQUEST_URI']    = $uri;
104 9
        $server['REQUEST_METHOD'] = $method;
105
106 9
        $app  = $this->createApplication();
107 9
        $sapi = $this->createSapi($server, $queryParams, $parsedBody, $cookies, $files, $content, $protocolVersion);
108 9
        $app->setSapi($sapi)->run();
109 9
        unset($app);
110
111 9
        $response = $sapi->getResponse();
112 9
        unset($sapi);
113
114 9
        return $response;
115
    }
116
117
    /**
118
     * @return void
119
     */
120 1
    protected function resetEventHandlers(): void
121
    {
122 1
        $this->eventHandlers = [];
123
    }
124
125
    /**
126
     * @param Closure $handler
127
     *
128
     * @return void
129
     */
130 1
    protected function addOnHandleRequestEvent(Closure $handler): void
131
    {
132 1
        $this->eventHandlers[ApplicationWrapperInterface::EVENT_ON_HANDLE_REQUEST][] = $handler;
133
    }
134
135
    /**
136
     * @param Closure $handler
137
     *
138
     * @return void
139
     */
140 1
    protected function addOnHandleResponseEvent(Closure $handler): void
141
    {
142 1
        $this->eventHandlers[ApplicationWrapperInterface::EVENT_ON_HANDLE_RESPONSE][] = $handler;
143
    }
144
145
    /**
146
     * @param Closure $handler
147
     *
148
     * @return void
149
     */
150 1
    protected function addOnContainerCreatedEvent(Closure $handler): void
151
    {
152 1
        $this->eventHandlers[ApplicationWrapperInterface::EVENT_ON_CONTAINER_CREATED][] = $handler;
153
    }
154
155
    /**
156
     * @param Closure $handler
157
     *
158
     * @return void
159
     */
160 1
    protected function addOnContainerConfiguredEvent(Closure $handler): void
161
    {
162 1
        $this->eventHandlers[ApplicationWrapperInterface::EVENT_ON_CONTAINER_LAST_CONFIGURATOR][] = $handler;
163
    }
164
165
    /**
166
     * @return array
167
     */
168 1
    protected function getHandleRequestEvents(): array
169
    {
170 1
        return $this->eventHandlers[ApplicationWrapperInterface::EVENT_ON_HANDLE_REQUEST] ?? [];
171
    }
172
173
    /**
174
     * @return array
175
     */
176 1
    protected function getHandleResponseEvents(): array
177
    {
178 1
        return $this->eventHandlers[ApplicationWrapperInterface::EVENT_ON_HANDLE_RESPONSE] ?? [];
179
    }
180
181
    /**
182
     * @return array
183
     */
184 1
    protected function getContainerCreatedEvents(): array
185
    {
186 1
        return $this->eventHandlers[ApplicationWrapperInterface::EVENT_ON_CONTAINER_CREATED] ?? [];
187
    }
188
189
    /**
190
     * @return array
191
     */
192 1
    protected function getContainerConfiguredEvents(): array
193
    {
194 1
        return $this->eventHandlers[ApplicationWrapperInterface::EVENT_ON_CONTAINER_LAST_CONFIGURATOR] ?? [];
195
    }
196
}
197