1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Functional; |
4
|
|
|
|
5
|
|
|
use App\Executor; |
6
|
|
|
use PHPUnit_Framework_MockObject_Stub_ReturnCallback; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Slim\App; |
9
|
|
|
use Slim\Http\Request; |
10
|
|
|
use Slim\Http\RequestBody; |
11
|
|
|
use Slim\Http\Response; |
12
|
|
|
use Slim\Http\Environment; |
13
|
|
|
|
14
|
|
|
abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase |
15
|
|
|
{ |
16
|
|
|
private static $path; |
17
|
|
|
private static $securityHeader; |
18
|
|
|
|
19
|
|
|
const SECRET = 'alksjdljzcxl'; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $path |
24
|
|
|
*/ |
25
|
|
|
public static function setPath($path) |
26
|
|
|
{ |
27
|
|
|
self::$path = $path; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $securityHeader |
33
|
|
|
*/ |
34
|
|
|
public static function setSecurityHeader($securityHeader) |
35
|
|
|
{ |
36
|
|
|
self::$securityHeader = $securityHeader; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return Response |
42
|
|
|
*/ |
43
|
|
|
final protected function runAppMocked($requestData, array $values, $command) |
44
|
|
|
{ |
45
|
|
|
$request = $this->prepareRequest(self::SECRET, $requestData); |
46
|
|
|
return $this->assertCommandEnvironment($request, $values, $command); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return Response |
52
|
|
|
*/ |
53
|
|
|
final protected function runApp($requestData, array $settings = []) |
54
|
|
|
{ |
55
|
|
|
$request = $this->prepareRequest(self::SECRET, $requestData); |
56
|
|
|
return $this->runRequest($request, $this->buildApp($settings)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return Response |
62
|
|
|
*/ |
63
|
|
|
final protected function runInvalid() |
64
|
|
|
{ |
65
|
|
|
$request = $this->prepareRequest(self::SECRET, []); |
66
|
|
|
return $this->runRequest($request, $this->buildApp()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return Response |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
final protected function runUnsecured() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$request = $this->prepareRequest(NULL, [ |
76
|
|
|
'object_kind' => 'push' |
77
|
|
|
]); |
78
|
|
|
return $this->runRequest($request, $this->buildApp()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return Response |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
final protected function runNotHandled() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$request = $this->prepareRequest(self::SECRET, [ |
88
|
|
|
'object_kind' => 'test' |
89
|
|
|
]); |
90
|
|
|
return $this->runRequest($request, $this->buildApp()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Request $request |
96
|
|
|
* @param array $values |
97
|
|
|
* @param string $command |
98
|
|
|
* @return ResponseInterface|Response |
99
|
|
|
*/ |
100
|
|
|
final protected function assertCommandEnvironment(Request $request, array $values, $command) |
101
|
|
|
{ |
102
|
|
|
$app = $this->buildApp(); |
103
|
|
|
|
104
|
|
|
$app->getContainer()[Executor::class] = function () use ($values, $command) { |
105
|
|
|
$mock = $this->getMockBuilder(Executor::class) |
106
|
|
|
->setMethods(['executeCommand']) |
107
|
|
|
->getMock(); |
108
|
|
|
|
109
|
|
|
if ($command !== NULL) { |
110
|
|
|
$mock->expects($this->once()) |
111
|
|
|
->method('executeCommand') |
112
|
|
|
->with($this->equalTo($command), $this->equalTo($values)) |
113
|
|
|
->will(new PHPUnit_Framework_MockObject_Stub_ReturnCallback(function ($command, $values) { |
114
|
|
|
asort($values); |
115
|
|
|
return json_encode($values); |
116
|
|
|
})); |
117
|
|
|
} else { |
118
|
|
|
$mock->expects($this->never()) |
119
|
|
|
->method('executeCommand'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $mock; |
123
|
|
|
}; |
124
|
|
|
|
125
|
|
|
return $this->runRequest($request, $app); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param Request $request |
131
|
|
|
* @param App $app |
132
|
|
|
* @return Response |
133
|
|
|
*/ |
134
|
|
|
final protected function runRequest(Request $request, App $app) |
135
|
|
|
{ |
136
|
|
|
// Register routes |
137
|
|
|
require __DIR__ . '/../../src/routes.php'; |
138
|
|
|
|
139
|
|
|
// Set up a response object |
140
|
|
|
$response = new Response(); |
141
|
|
|
|
142
|
|
|
// Process the application |
143
|
|
|
$response = $app->process($request, $response); |
144
|
|
|
|
145
|
|
|
// Return the response |
146
|
|
|
return $response; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param array $settingsOverride |
152
|
|
|
* @return App |
153
|
|
|
*/ |
154
|
|
|
final protected function buildApp(array $settingsOverride = []) |
155
|
|
|
{ |
156
|
|
|
// Use the application settings |
157
|
|
|
if ( ! defined('CONFIG_DIR')) { |
158
|
|
|
define('CONFIG_DIR', __DIR__ . '/config'); |
159
|
|
|
} |
160
|
|
|
$settings = require __DIR__ . '/../../src/settings.php'; |
161
|
|
|
$settings = array_replace_recursive($settings, $settingsOverride); |
162
|
|
|
|
163
|
|
|
// Instantiate the application |
164
|
|
|
$app = new App($settings); |
165
|
|
|
unset($app->getContainer()['errorHandler']); |
166
|
|
|
|
167
|
|
|
// Set up dependencies |
168
|
|
|
require __DIR__ . '/../../src/dependencies.php'; |
169
|
|
|
|
170
|
|
|
return $app; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string|NULL $secret |
176
|
|
|
* @param array|string $data |
177
|
|
|
* @return Request |
178
|
|
|
*/ |
179
|
|
|
protected function prepareRequest($secret = NULL, $data = '') |
180
|
|
|
{ |
181
|
|
|
$body = new RequestBody(); |
182
|
|
|
$body->write(is_array($data) ? json_encode($data) : $data); |
183
|
|
|
|
184
|
|
|
return Request::createFromEnvironment($this->prepareEnvironment($secret)) |
185
|
|
|
->withBody($body); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string|NULL $secret |
191
|
|
|
* @return Environment |
192
|
|
|
*/ |
193
|
|
|
private function prepareEnvironment($secret = NULL) |
194
|
|
|
{ |
195
|
|
|
$data = [ |
196
|
|
|
'REQUEST_METHOD' => 'POST', |
197
|
|
|
'REQUEST_URI' => self::$path, |
198
|
|
|
'HTTP_CONTENT_TYPE' => 'application/json' |
199
|
|
|
]; |
200
|
|
|
|
201
|
|
|
if ($secret !== NULL) { |
202
|
|
|
$data['HTTP_' . self::$securityHeader] = $secret; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return Environment::mock($data); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.