Completed
Push — master ( d7a1ba...a587c5 )
by Neomerx
08:43
created

PhpUnitTestCase::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 4
crap 2
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
    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
        $headers['host'] = $host;
87
88
        $server = $this->applyHeadersToServer($headers, $server);
89
90
        $server['REQUEST_URI']    = $uri;
91
        $server['REQUEST_METHOD'] = $method;
92
93
        $sapi = $this->createSapi($server, $queryParams, $parsedBody, $cookies, $files, $content);
94
        $app  = $this->createApplication($sapi);
95
96
        $app->run();
97
        unset($app);
98
99
        $response = $sapi->getResponse();
100
        unset($sapi);
101
102
        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
    protected function get($uri, array $queryParams = [], array $headers = [], array $cookies = [])
114
    {
115
        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
    protected function post($uri, array $data = [], array $headers = [], array $cookies = [])
127
    {
128
        $headers[self::HEADER_CONTENT_TYPE] = 'application/x-www-form-urlencoded';
129
        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
    protected function put($uri, array $data = [], array $headers = [], array $cookies = [])
141
    {
142
        $headers[self::HEADER_CONTENT_TYPE] = 'application/x-www-form-urlencoded';
143
        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
    protected function patch($uri, array $data = [], array $headers = [], array $cookies = [])
155
    {
156
        $headers[self::HEADER_CONTENT_TYPE] = 'application/x-www-form-urlencoded';
157
        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
    protected function delete($uri, array $data = [], array $headers = [], array $cookies = [])
169
    {
170
        return $this->call('DELETE', $uri, [], $data, $headers, $cookies);
171
    }
172
173
    /**
174
     * @param string $content
175
     *
176
     * @return StreamInterface
177
     */
178
    protected function streamFromString($content)
179
    {
180
        $stream = new Stream('php://temp', 'wb+');
181
        $stream->write($content);
182
183
        return $stream;
184
    }
185
186
    /**
187
     * @param array $headers
188
     * @param array $server
189
     *
190
     * @return array
191
     */
192
    private function applyHeadersToServer(array $headers, array $server = [])
193
    {
194
        $prefix = 'HTTP_';
195
        foreach ($headers as $name => $value) {
196
            $name = strtr(strtoupper($name), '-', '_');
197
            if ($name !== self::HEADER_CONTENT_TYPE && strpos($name, $prefix) !== 0) {
198
                $name = $prefix . $name;
199
            }
200
            $server[$name] = $value;
201
        }
202
203
        return $server;
204
    }
205
}
206