-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTestCase.php
240 lines (211 loc) · 8.02 KB
/
TestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
namespace AC\WebServicesBundle;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
use \Mockery as m;
/**
* This is a base integration test case that you can use for your own tests.
* It contains a few convenience methods for making API calls.
**/
abstract class TestCase extends WebTestCase
{
private static $fixtures = [];
protected $fixtureData = [];
/**
* Override from child class to return a CachedFixture
*/
protected function getFixtureClass()
{
# TODO Maybe should use ac_web_services.default_fixture_class instead?
return null;
}
public function setUp()
{
parent::setUp();
$this->dieOnException("fixture loading", function() {
$c = $this->getFixtureClass();
if (is_null($c)) { return; }
if (!array_key_exists($c, self::$fixtures)) {
self::$fixtures[$c] = new $c;
}
if (!is_null(self::$fixtures[$c])) {
$client = $this->getClient();
$g = self::$fixtures[$c]->loadInto($client->getContainer());
$this->fixtureData = $g;
} else {
$this->fixtureData = [];
}
});
}
/**
* Shortcut to get a new client
*/
protected function getClient()
{
return static::createClient([
'environment' => 'test',
'debug' => true
]);
}
/**
* Shortcut to run a CLI command - returns a... ?
*/
protected function runCommand($string)
{
$command = sprintf('%s --quiet --env=test', $string);
$k = $this->createKernel();
$app = new Application($k);
$app->setAutoExit(false);
return $app->run(new StringInput($string), new NullOutput());
}
/**
* Shortcut to make a request and get the returned Response instance.
*
* Will fail unless the response's status code equals 200 (or a supplied expectedCode option).
*/
protected function callApi($method, $uri, $options = [])
{
$params = isset($options['params']) ? $options['params'] : [];
$files = isset($options['files']) ? $options['files'] : [];
$server = isset($options['server']) ? $options['server'] : [];
$content = isset($options['content']) ? $options['content'] : null;
$changeHist = isset($options['changeHistory']) ? $options['changeHistory'] : true;
$server['SERVER_NAME'] = '127.0.0.1';
$client = $this->getClient();
if (isset($options['containerConfig'])) {
call_user_func($options['containerConfig'], $client->getContainer());
}
if (isset($options['auth'])) {
$user = $options['auth']['user'];
$this->fakeUserAuth($client, $user);
}
$client->request($method, $uri, $params, $files, $server, $content, $changeHist);
$response = $client->getResponse();
if (!array_key_exists('expectedCode', $options)) {
$options['expectedCode'] = 200;
}
if (!is_null($options['expectedCode'])) {
if ($response->getStatusCode() != $options['expectedCode']) {
$msg = "Expected status code " . $options['expectedCode'] .
", got " . $response->getStatusCode() . ".\n";
if ($response->headers->get('Content-Type') == "application/json") {
$content = json_decode($response->getContent(), true);
if (is_null($content)) {
$msg .= "Response content (unparseable JSON):\n$result";
} else {
$msg .= "JSON content of invalid response:\n";
# Clean up the stack trace if there is one
if (isset($content['exception']) && isset($content['exception']['trace'])) {
$content['exception']['trace'] =
$this->cleanTrace($content['exception']['trace']);
}
$result = var_export($content, true);
if (strlen($result) > 20*1024) {
$result = substr($result, 0, 20*1024);
$result .= "\n.......\n.......";
}
$msg .= "$result\n";
}
} else {
$msg .= "Response content (first 16K):\n" . substr($result, 0, 1024*16);
}
$this->fail(trim($msg));
}
}
return $response;
}
/**
* Frontend to callApi that decodes JSON response content.
*
* Returns the JSON data.
*/
protected function callJsonApi($method, $uri, $options = [])
{
if (isset($options['content'])) {
if (
!isset($options['server']) ||
!isset($options['server']['CONTENT_TYPE'])
) {
$options['server']['CONTENT_TYPE'] = 'application/json';
}
if (
is_array($options['content']) &&
$options['server']['CONTENT_TYPE'] == 'application/json'
) {
$options['content'] = json_encode($options['content']);
}
}
$res = $this->callApi($method, $uri, $options);
$ctype = $res->headers->get('Content-Type');
if ($ctype != "application/json") {
$this->fail("Expecting JSON response, but instead got $ctype");
}
$json = json_decode($res->getContent(), true);
if (json_last_error() !== JSON_ERROR_NONE) {
$this->fail("Couldn't decode response, JSON error: " . json_last_error_msg());
}
return $json;
}
protected function dieOnException($desc, $fn)
{
try {
call_user_func($fn);
} catch (\Exception $e) {
print "\n\nFailure in $desc\n";
print $e;
print $e->getTraceAsString();
die(1);
}
}
protected function assertIdsMatch($ids, $data)
{
$dataIds = array_map(function($x) { return $x['id']; }, $data);
sort($dataIds);
$this->assertEquals($ids, $dataIds);
}
private function cleanTrace($trace)
{
if (!is_array($trace)) { return $trace; }
$cwd = getcwd();
$trace = array_map(function($line) use ($cwd) {
$line = preg_replace('/^\\d+ +/', '', $line);
$line = str_replace($cwd . '/', '', $line);
return $line;
}, $trace);
$trace = array_filter($trace, function ($line) {
return (
(FALSE === strpos($line, 'vendor/phpunit/phpunit'))
&& ($line != "{main}")
);
}
);
return $trace;
}
private function fakeUserAuth($client, $user)
{
$c = $client->getContainer();
// The user object may have been made in the context of a different instance of the
// object manager than the one being used by this client.
if ($fixtureCls = $this->getFixtureClass()) {
$fixture = new $fixtureCls;
$manager = null;
if (is_a($fixture, '\AC\WebServicesBundle\Fixture\CachedSqliteFixture')) {
$manager = $c->get('doctrine')->getManager();
$user = $manager->merge($user);
} else if (is_a($fixture, '\AC\WebServicesBundle\Fixture\CachedMongoFixture')) {
$manager = $c->get('doctrine_mongodb')->getManager();
$user = $manager->merge($user);
}
}
$token = new PreAuthenticatedToken($user, [], 'mock', $user->getRoles());
$c->set('security.context', m::mock(
$c->get('security.context'),
[
'getToken' => $token
]
));
}
}