1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SlimX\Tests; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Based on WebTestClient from |
7
|
|
|
* [email protected]:there4/slim-unit-testing-example.git |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
use \Slim\Http\Body; |
11
|
|
|
use \Slim\Http\Environment; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Mocks the client request and response. |
15
|
|
|
*/ |
16
|
|
|
class WebTestClient |
17
|
|
|
{ |
18
|
|
|
private $app; |
19
|
|
|
private $request; |
20
|
|
|
private $response; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Abstract way to make a request to SlimPHP, this allows us to mock the |
24
|
|
|
* slim environment. |
25
|
|
|
* |
26
|
|
|
* @param string $method Method name. |
27
|
|
|
* @param string $path URI. |
28
|
|
|
* @param array $data Data request. |
29
|
|
|
* @param array $optionalHeaders Optional headers. |
30
|
|
|
* @return mixed. |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
private function request( |
33
|
|
|
string $method, |
34
|
|
|
string $path, |
35
|
|
|
$data = array(), |
36
|
|
|
array $optionalHeaders = array() |
37
|
|
|
) { |
38
|
|
|
// Capture STDOUT |
39
|
|
|
ob_start(); |
40
|
|
|
|
41
|
|
|
$options = array( |
42
|
|
|
'REQUEST_METHOD' => strtoupper($method), |
43
|
|
|
'REQUEST_URI' => $path, |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
if ($method === 'get') { |
47
|
|
|
$options['QUERY_STRING'] = http_build_query($data); |
48
|
|
|
} elseif (is_array($data)) { |
|
|
|
|
49
|
|
|
$options['slim.input'] = http_build_query($data); |
50
|
|
|
} else { |
51
|
|
|
$options['slim.input'] = $data; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Prepare a mock environment |
55
|
|
|
$env = Environment::mock($options); |
56
|
|
|
$request = \Slim\Http\Request::createFromEnvironment($env); |
57
|
|
|
|
58
|
|
|
foreach ($optionalHeaders as $key => $value) { |
59
|
|
|
$request = $request->withHeader($key, $value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ('get' !== $method && is_array($data)) { |
63
|
|
|
$request = $request->withParsedBody($data); |
64
|
|
|
} elseif ('get' !== $method && is_string($data)) { |
65
|
|
|
$body = new Body(fopen('php://temp', 'r+')); |
|
|
|
|
66
|
|
|
$body->write($data); |
67
|
|
|
$body->rewind(); |
68
|
|
|
$request = $request->withBody($body); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Execute our app |
72
|
|
|
$response = new \Slim\Http\Response(); |
73
|
|
|
$this->response = $this->app->process($request, $response); |
74
|
|
|
$this->request = $request; |
75
|
|
|
|
76
|
|
|
// Return the application output. Also available in `response->body()` |
77
|
|
|
return ob_get_clean(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
// We support these methods for testing. These are available via |
82
|
|
|
// `this->get()` and `$this->post()`. This is accomplished with the |
83
|
|
|
// `__call()` magic method below. |
84
|
|
|
private $testingMethods = array( |
85
|
|
|
'get', 'post', 'patch', 'put', 'delete', 'head', 'options' |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set the Slim app. |
90
|
|
|
* |
91
|
|
|
* @param \Slim\App $app Slim app instance. |
92
|
|
|
*/ |
93
|
|
|
public function __construct(\Slim\App $app) |
94
|
|
|
{ |
95
|
|
|
$this->app = $app; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Implement our `get`, `post`, and other http operations, as defined on |
100
|
|
|
* $testingMethods. |
101
|
|
|
* |
102
|
|
|
* @param string $method Method being called. |
103
|
|
|
* @param array $arguments List of arguments. |
104
|
|
|
* @return mixed. |
|
|
|
|
105
|
|
|
*/ |
106
|
|
|
public function __call(string $method, array $arguments) |
107
|
|
|
{ |
108
|
|
|
if (in_array(strtolower($method), $this->testingMethods)) { |
109
|
|
|
list($path, $data, $headers) = array_pad($arguments, 3, array()); |
110
|
|
|
return $this->request($method, $path, $data, $headers); |
111
|
|
|
} |
112
|
|
|
throw new \BadMethodCallException( |
113
|
|
|
strtoupper($method) . ' is not supported' |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the response objects. |
119
|
|
|
* |
120
|
|
|
* @return \Slim\Http\Response Response object. |
121
|
|
|
*/ |
122
|
|
|
public function getResponse() |
123
|
|
|
{ |
124
|
|
|
return $this->response; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the request object. |
129
|
|
|
* |
130
|
|
|
* @return \Slim\Http\Request Request object. |
131
|
|
|
*/ |
132
|
|
|
public function getRequest() |
133
|
|
|
{ |
134
|
|
|
return $this->request; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|