1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Functional; |
4
|
|
|
|
5
|
|
|
use App\Executor; |
6
|
|
|
use Interop\Container\ContainerInterface; |
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
|
|
|
|
16
|
|
|
final protected function runAppMocked($requestData, array $values, $command) |
17
|
|
|
{ |
18
|
|
|
$request = $this->prepareRequest('alksjdljzcxl') |
19
|
|
|
->withParsedBody(json_decode($requestData, TRUE)); |
20
|
|
|
|
21
|
|
|
return $this->assertCommandEnvironment($request, $values, $command); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
final protected function runApp($requestData, array $settings = []) |
25
|
|
|
{ |
26
|
|
|
$request = $this->prepareRequest('alksjdljzcxl') |
27
|
|
|
->withParsedBody(json_decode($requestData, TRUE)); |
28
|
|
|
|
29
|
|
|
return $this->runRequest($request, $this->buildApp($settings)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
final protected function runInvalid() |
34
|
|
|
{ |
35
|
|
|
$request = $this->prepareRequest('alksjdljzcxl') |
36
|
|
|
->withParsedBody([]); |
37
|
|
|
|
38
|
|
|
return $this->runRequest($request, $this->buildApp()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
final protected function runUnsecured() |
43
|
|
|
{ |
44
|
|
|
$request = $this->prepareRequest() |
45
|
|
|
->withParsedBody([ |
46
|
|
|
'object_kind' => 'push' |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
return $this->runRequest($request, $this->buildApp()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
final protected function runNotHandled() |
54
|
|
|
{ |
55
|
|
|
$request = $this->prepareRequest('alksjdljzcxl') |
56
|
|
|
->withParsedBody([ |
57
|
|
|
'object_kind' => 'test' |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
return $this->runRequest($request, $this->buildApp()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param Request $request |
66
|
|
|
* @param array $values |
67
|
|
|
* @param string $command |
68
|
|
|
* @return ResponseInterface|Response |
69
|
|
|
*/ |
70
|
|
|
private function assertCommandEnvironment(Request $request, array $values, $command) |
71
|
|
|
{ |
72
|
|
|
|
73
|
|
|
$app = $this->buildApp(); |
74
|
|
|
|
75
|
|
|
if ($command !== NULL) { |
76
|
|
|
$app->getContainer()[Executor::class] = function (ContainerInterface $c) use ($values, $command) { |
|
|
|
|
77
|
|
|
$mock = $this->getMockBuilder(Executor::class) |
78
|
|
|
->setMethods(['executeCommand']) |
79
|
|
|
->getMock(); |
80
|
|
|
|
81
|
|
|
$mock->expects($this->once()) |
82
|
|
|
->method('executeCommand') |
83
|
|
|
->with($this->equalTo($command), $this->equalTo($values)); |
84
|
|
|
|
85
|
|
|
return $mock; |
86
|
|
|
}; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->runRequest($request, $app); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param array $settingsOverride |
95
|
|
|
* @return App |
96
|
|
|
*/ |
97
|
|
|
private function buildApp(array $settingsOverride = []) |
98
|
|
|
{ |
99
|
|
|
// Use the application settings |
100
|
|
|
if ( ! defined('CONFIG_DIR')) { |
101
|
|
|
define('CONFIG_DIR', __DIR__ . '/config'); |
102
|
|
|
} |
103
|
|
|
$settings = require __DIR__ . '/../../src/settings.php'; |
104
|
|
|
$settings = array_replace_recursive($settings, $settingsOverride); |
105
|
|
|
|
106
|
|
|
// Instantiate the application |
107
|
|
|
$app = new App($settings); |
108
|
|
|
unset($app->getContainer()['errorHandler']); |
109
|
|
|
|
110
|
|
|
// Set up dependencies |
111
|
|
|
require __DIR__ . '/../../src/dependencies.php'; |
112
|
|
|
|
113
|
|
|
return $app; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param Request $request |
119
|
|
|
* @param App $app |
120
|
|
|
* @return Response |
121
|
|
|
*/ |
122
|
|
|
private function runRequest(Request $request, App $app) |
123
|
|
|
{ |
124
|
|
|
// Register routes |
125
|
|
|
require __DIR__ . '/../../src/routes.php'; |
126
|
|
|
|
127
|
|
|
// Set up a response object |
128
|
|
|
$response = new Response(); |
129
|
|
|
|
130
|
|
|
// Process the application |
131
|
|
|
$response = $app->process($request, $response); |
132
|
|
|
|
133
|
|
|
// Return the response |
134
|
|
|
return $response; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string|NULL $secret |
140
|
|
|
* @return Request |
141
|
|
|
*/ |
142
|
|
|
private function prepareRequest($secret = NULL) |
143
|
|
|
{ |
144
|
|
|
return Request::createFromEnvironment($this->prepareEnvironment($secret)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string|NULL $secret |
150
|
|
|
* @return Environment |
151
|
|
|
*/ |
152
|
|
|
private function prepareEnvironment($secret = NULL) |
153
|
|
|
{ |
154
|
|
|
$data = [ |
155
|
|
|
'REQUEST_METHOD' => 'POST', |
156
|
|
|
'REQUEST_URI' => '/', |
157
|
|
|
'HTTP_CONTENT_TYPE' => 'application/json' |
158
|
|
|
]; |
159
|
|
|
|
160
|
|
|
if ($secret !== NULL) { |
161
|
|
|
$data['HTTP_X-Gitlab-Token'] = $secret; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return Environment::mock($data); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.