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\Response; |
11
|
|
|
use Slim\Http\Environment; |
12
|
|
|
|
13
|
|
|
abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
private static $path; |
16
|
|
|
private static $securityHeader; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $path |
21
|
|
|
*/ |
22
|
|
|
public static function setPath($path) |
23
|
|
|
{ |
24
|
|
|
self::$path = $path; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $securityHeader |
30
|
|
|
*/ |
31
|
|
|
public static function setSecurityHeader($securityHeader) |
32
|
|
|
{ |
33
|
|
|
self::$securityHeader = $securityHeader; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
final protected function runAppMocked($requestData, array $values, $command) |
38
|
|
|
{ |
39
|
|
|
$request = $this->prepareRequest('alksjdljzcxl') |
40
|
|
|
->withParsedBody(json_decode($requestData, TRUE)); |
41
|
|
|
|
42
|
|
|
return $this->assertCommandEnvironment($request, $values, $command); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
final protected function runApp($requestData, array $settings = []) |
46
|
|
|
{ |
47
|
|
|
$request = $this->prepareRequest('alksjdljzcxl') |
48
|
|
|
->withParsedBody(json_decode($requestData, TRUE)); |
49
|
|
|
|
50
|
|
|
return $this->runRequest($request, $this->buildApp($settings)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
final protected function runInvalid() |
55
|
|
|
{ |
56
|
|
|
$request = $this->prepareRequest('alksjdljzcxl') |
57
|
|
|
->withParsedBody([]); |
58
|
|
|
|
59
|
|
|
return $this->runRequest($request, $this->buildApp()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
final protected function runUnsecured() |
64
|
|
|
{ |
65
|
|
|
$request = $this->prepareRequest() |
66
|
|
|
->withParsedBody([ |
67
|
|
|
'object_kind' => 'push' |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
return $this->runRequest($request, $this->buildApp()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
final protected function runNotHandled() |
75
|
|
|
{ |
76
|
|
|
$request = $this->prepareRequest('alksjdljzcxl') |
77
|
|
|
->withParsedBody([ |
78
|
|
|
'object_kind' => 'test' |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
return $this->runRequest($request, $this->buildApp()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param Request $request |
87
|
|
|
* @param array $values |
88
|
|
|
* @param string $command |
89
|
|
|
* @return ResponseInterface|Response |
90
|
|
|
*/ |
91
|
|
|
private function assertCommandEnvironment(Request $request, array $values, $command) |
92
|
|
|
{ |
93
|
|
|
|
94
|
|
|
$app = $this->buildApp(); |
95
|
|
|
|
96
|
|
|
if ($command !== NULL) { |
97
|
|
|
$app->getContainer()[Executor::class] = function () use ($values, $command) { |
98
|
|
|
$mock = $this->getMockBuilder(Executor::class) |
99
|
|
|
->setMethods(['executeCommand']) |
100
|
|
|
->getMock(); |
101
|
|
|
|
102
|
|
|
if ($command !== NULL) { |
103
|
|
|
$mock->expects($this->once()) |
104
|
|
|
->method('executeCommand') |
105
|
|
|
->with($this->equalTo($command), $this->equalTo($values)) |
106
|
|
|
->will(new PHPUnit_Framework_MockObject_Stub_ReturnCallback(function ($command, $values) { |
107
|
|
|
return $values; |
108
|
|
|
})); |
109
|
|
|
} else { |
110
|
|
|
$mock->expects($this->never()) |
111
|
|
|
->method('executeCommand'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $mock; |
115
|
|
|
}; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->runRequest($request, $app); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array $settingsOverride |
124
|
|
|
* @return App |
125
|
|
|
*/ |
126
|
|
|
private function buildApp(array $settingsOverride = []) |
127
|
|
|
{ |
128
|
|
|
// Use the application settings |
129
|
|
|
if ( ! defined('CONFIG_DIR')) { |
130
|
|
|
define('CONFIG_DIR', __DIR__ . '/config'); |
131
|
|
|
} |
132
|
|
|
$settings = require __DIR__ . '/../../src/settings.php'; |
133
|
|
|
$settings = array_replace_recursive($settings, $settingsOverride); |
134
|
|
|
|
135
|
|
|
// Instantiate the application |
136
|
|
|
$app = new App($settings); |
137
|
|
|
unset($app->getContainer()['errorHandler']); |
138
|
|
|
|
139
|
|
|
// Set up dependencies |
140
|
|
|
require __DIR__ . '/../../src/dependencies.php'; |
141
|
|
|
|
142
|
|
|
return $app; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param Request $request |
148
|
|
|
* @param App $app |
149
|
|
|
* @return Response |
150
|
|
|
*/ |
151
|
|
|
private function runRequest(Request $request, App $app) |
152
|
|
|
{ |
153
|
|
|
// Register routes |
154
|
|
|
require __DIR__ . '/../../src/routes.php'; |
155
|
|
|
|
156
|
|
|
// Set up a response object |
157
|
|
|
$response = new Response(); |
158
|
|
|
|
159
|
|
|
// Process the application |
160
|
|
|
$response = $app->process($request, $response); |
161
|
|
|
|
162
|
|
|
// Return the response |
163
|
|
|
return $response; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string|NULL $secret |
169
|
|
|
* @return Request |
170
|
|
|
*/ |
171
|
|
|
private function prepareRequest($secret = NULL) |
172
|
|
|
{ |
173
|
|
|
return Request::createFromEnvironment($this->prepareEnvironment($secret)); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string|NULL $secret |
179
|
|
|
* @return Environment |
180
|
|
|
*/ |
181
|
|
|
private function prepareEnvironment($secret = NULL) |
182
|
|
|
{ |
183
|
|
|
$data = [ |
184
|
|
|
'REQUEST_METHOD' => 'POST', |
185
|
|
|
'REQUEST_URI' => self::$path, |
186
|
|
|
'HTTP_CONTENT_TYPE' => 'application/json' |
187
|
|
|
]; |
188
|
|
|
|
189
|
|
|
if ($secret !== NULL) { |
190
|
|
|
$data['HTTP_' . self::$securityHeader] = $secret; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return Environment::mock($data); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
} |
197
|
|
|
|