Completed
Push — master ( bc1208...c4da36 )
by Neomerx
05:13
created

TestCaseTrait::call()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 38
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 17
cts 17
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 29
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 namespace Limoncello\Testing;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Contracts\Core\ApplicationInterface;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\StreamInterface;
22
23
/**
24
 * @package Limoncello\Testing
25
 */
26
trait TestCaseTrait
27
{
28
    /**
29
     * @param array|null                      $server
30
     * @param array|null                      $queryParams
31
     * @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...
32
     * @param array|null                      $cookies
33
     * @param array|null                      $files
34
     * @param string|resource|StreamInterface $messageBody
35
     * @param string                          $protocolVersion
36
     *
37
     * @return Sapi
38
     */
39
    abstract protected function createSapi(
40
        array $server = null,
41
        array $queryParams = null,
42
        array $parsedBody = null,
43
        array $cookies = null,
44
        array $files = null,
45
        $messageBody = 'php://input',
46
        string $protocolVersion = '1.1'
47
    ): Sapi;
48
49
    /**
50
     * @param Sapi $sapi
51
     *
52
     * @return ApplicationInterface
53
     */
54
    abstract protected function createApplication(Sapi $sapi): ApplicationInterface;
55
56
    /** @noinspection PhpTooManyParametersInspection
57
     * @param string                          $method
58
     * @param string                          $uri
59
     * @param array                           $queryParams
60
     * @param array                           $parsedBody
61
     * @param array                           $headers
62
     * @param array                           $cookies
63
     * @param array                           $files
64
     * @param array                           $server
65
     * @param string|resource|StreamInterface $content
66
     * @param string                          $host
67
     * @param string                          $protocolVersion
68
     *
69
     * @return ResponseInterface
70
     *
71
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
72
     */
73 8
    protected function call(
74
        string $method,
75
        string $uri,
76
        array $queryParams = [],
77
        array $parsedBody = [],
78
        array $headers = [],
79
        array $cookies = [],
80
        array $files = [],
81
        array $server = [],
82
        $content = 'php://input',
83
        string $host = 'localhost',
84
        string $protocolVersion = '1.1'
85
    ): ResponseInterface {
86 8
        $headers['host'] = $host;
87
88 8
        $prefix = 'HTTP_';
89 8
        foreach ($headers as $name => $value) {
90 8
            $name = strtr(strtoupper($name), '-', '_');
91 8
            if ($name !== 'CONTENT_TYPE' && strpos($name, $prefix) !== 0) {
92 8
                $name = $prefix . $name;
93
            }
94 8
            $server[$name] = $value;
95
        }
96
97 8
        $server['REQUEST_URI']    = $uri;
98 8
        $server['REQUEST_METHOD'] = $method;
99
100 8
        $sapi = $this->createSapi($server, $queryParams, $parsedBody, $cookies, $files, $content, $protocolVersion);
101 8
        $app  = $this->createApplication($sapi);
102
103 8
        $app->run();
104 8
        unset($app);
105
106 8
        $response = $sapi->getResponse();
107 8
        unset($sapi);
108
109 8
        return $response;
110
    }
111
}
112