Completed
Push — master ( 8f6ce8...083c36 )
by Neomerx
08:10
created

PhpUnitTestCase::streamFromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
191
     */
192 1
    protected function streamFromString($content)
193
    {
194 1
        $stream = new Stream('php://temp', 'wb+');
195 1
        $stream->write($content);
196
197 1
        return $stream;
198
    }
199
200
    /**
201
     * @param array $headers
202
     * @param array $server
203
     *
204
     * @return array
205
     */
206 5
    private function applyHeadersToServer(array $headers, array $server = [])
207
    {
208 5
        $prefix = 'HTTP_';
209 5
        foreach ($headers as $name => $value) {
210 5
            $name = strtr(strtoupper($name), '-', '_');
211 5
            if ($name !== self::HEADER_CONTENT_TYPE && strpos($name, $prefix) !== 0) {
212 5
                $name = $prefix . $name;
213 5
            }
214 5
            $server[$name] = $value;
215 5
        }
216
217 5
        return $server;
218
    }
219
}
220