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 |
|
|
|
|
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
|
|
|
|