Completed
Push — master ( 80d207...26b26f )
by Neomerx
04:47
created

PhpUnitTestCase::put()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
nc 1
cc 1
eloc 2
nop 4
crap 1
1
<?php namespace Limoncello\Testing;
2
3
/**
4
 * Copyright 2015-2016 [email protected] (www.neomerx.com)
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\Core\Contracts\Application\ApplicationInterface;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\StreamInterface;
22
23
/**
24
 * @package Neomerx\Mimi
25
 */
26
abstract class PhpUnitTestCase extends \PHPUnit_Framework_TestCase
27
{
28
    use MeasureExecutionTimeTrait;
29
30
    /**
31
     * @param array|null                      $server
32
     * @param array|null                      $queryParams
33
     * @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...
34
     * @param array|null                      $cookies
35
     * @param array|null                      $files
36
     * @param string|resource|StreamInterface $messageBody
37
     *
38
     * @return Sapi
39
     */
40
    abstract protected function createSapi(
41
        array $server = null,
42
        array $queryParams = null,
43
        array $parsedBody = null,
44
        array $cookies = null,
45
        array $files = null,
46
        $messageBody = 'php://input'
47
    );
48
49
    /**
50
     * @param Sapi $sapi
51
     *
52
     * @return ApplicationInterface
53
     */
54
    abstract protected function createApplication(Sapi $sapi);
55
56
    /** @noinspection PhpTooManyParametersInspection
57
     * @param string $method
58
     * @param string $uri
59
     * @param array  $queryParams
60
     * @param array  $postData
61
     * @param array  $headers
62
     * @param array  $cookies
63
     * @param array  $files
64
     * @param array  $server
65
     * @param string $content
66
     * @param string $host
67
     *
68
     * @return ResponseInterface
69
     */
70 5
    protected function call(
71
        $method,
72
        $uri,
73
        array $queryParams = [],
74
        array $postData = [],
75
        array $headers = [],
76
        array $cookies = [],
77
        array $files = [],
78
        array $server = [],
79
        $content = 'php://input',
80
        $host = 'localhost'
81
    ) {
82 5
        $headers['host'] = $host;
83
84 5
        $server = $this->applyHeadersToServer($headers, $server);
85
86 5
        $server['REQUEST_URI']    = $uri;
87 5
        $server['REQUEST_METHOD'] = $method;
88
89 5
        $sapi = $this->createSapi($server, $queryParams, $postData, $cookies, $files, $content);
90 5
        $app  = $this->createApplication($sapi);
91
92 5
        $app->run();
93 5
        unset($app);
94
95 5
        $response = $sapi->getResponse();
96 5
        unset($sapi);
97
98 5
        return $response;
99
    }
100
101
    /**
102
     * @param string $uri
103
     * @param array $queryParams
104
     * @param array $headers
105
     * @param array $cookies
106
     *
107
     * @return ResponseInterface
108
     */
109 1
    protected function get($uri, array $queryParams = [], array $headers = [], array $cookies = [])
110
    {
111 1
        return $this->call('GET', $uri, $queryParams, [], $headers, $cookies);
112
    }
113
114
    /**
115
     * @param string $uri
116
     * @param array  $data
117
     * @param array  $headers
118
     * @param array  $cookies
119
     *
120
     * @return ResponseInterface
121
     */
122 1
    protected function post($uri, array $data = [], array $headers = [], array $cookies = [])
123
    {
124 1
        return $this->call('POST', $uri, [], $data, $headers, $cookies);
125
    }
126
127
    /**
128
     * @param string $uri
129
     * @param array  $data
130
     * @param array  $headers
131
     * @param array  $cookies
132
     *
133
     * @return ResponseInterface
134
     */
135 1
    protected function put($uri, array $data = [], array $headers = [], array $cookies = [])
136
    {
137 1
        return $this->call('PUT', $uri, [], $data, $headers, $cookies);
138
    }
139
140
    /**
141
     * @param string $uri
142
     * @param array  $data
143
     * @param array  $headers
144
     * @param array  $cookies
145
     *
146
     * @return ResponseInterface
147
     */
148 1
    protected function patch($uri, array $data = [], array $headers = [], array $cookies = [])
149
    {
150 1
        return $this->call('PATCH', $uri, [], $data, $headers, $cookies);
151
    }
152
153
    /**
154
     * @param string $uri
155
     * @param array  $data
156
     * @param array  $headers
157
     * @param array  $cookies
158
     *
159
     * @return ResponseInterface
160
     */
161 1
    protected function delete($uri, array $data = [], array $headers = [], array $cookies = [])
162
    {
163 1
        return $this->call('DELETE', $uri, [], $data, $headers, $cookies);
164
    }
165
166
    /**
167
     * @param ResponseInterface $response
168
     *
169
     * @return null|string
170
     */
171 2
    protected function readBody(ResponseInterface $response)
172
    {
173 2
        $body = $response->getBody();
174 2
        $size = $body->getSize();
175 2
        $text = $size > 0 ? $body->read($size) : null;
176
177 2
        return $text;
178
    }
179
180
    /**
181
     * @param array $headers
182
     * @param array $server
183
     *
184
     * @return array
185
     */
186 5
    private function applyHeadersToServer(array $headers, array $server = [])
187
    {
188 5
        $prefix = 'HTTP_';
189 5
        foreach ($headers as $name => $value) {
190 5
            $name = strtr(strtoupper($name), '-', '_');
191 5
            if ($name !== 'CONTENT_TYPE' && strpos($name, $prefix) !== 0) {
192 5
                $name = $prefix . $name;
193 5
            }
194 5
            $server[$name] = $value;
195 5
        }
196
197 5
        return $server;
198
    }
199
}
200