-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathBase.php
More file actions
383 lines (330 loc) · 14.5 KB
/
Base.php
File metadata and controls
383 lines (330 loc) · 14.5 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
namespace Tests;
use Appwrite\SDK\Language;
use Appwrite\SDK\SDK;
use Appwrite\Spec\Swagger2;
use PHPUnit\Framework\TestCase;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
abstract class Base extends TestCase
{
protected const PING_RESPONSE = [
'GET:/v1/ping:passed',
];
protected const FOO_RESPONSES = [
'GET:/v1/mock/tests/foo:passed',
'POST:/v1/mock/tests/foo:passed',
'PUT:/v1/mock/tests/foo:passed',
'PATCH:/v1/mock/tests/foo:passed',
'DELETE:/v1/mock/tests/foo:passed',
];
protected const BAR_RESPONSES = [
'GET:/v1/mock/tests/bar:passed',
'POST:/v1/mock/tests/bar:passed',
'PUT:/v1/mock/tests/bar:passed',
'PATCH:/v1/mock/tests/bar:passed',
'DELETE:/v1/mock/tests/bar:passed',
];
protected const GENERAL_RESPONSES = [
'GET:/v1/mock/tests/general/redirect/done:passed',
];
protected const OAUTH_RESPONSES = [
'https://localhost?code=abcdef&state=123456',
];
protected const DOWNLOAD_RESPONSES = [
'GET:/v1/mock/tests/general/download:passed',
];
protected const COOKIE_RESPONSES = [
'GET:/v1/mock/tests/general/set-cookie:passed',
'GET:/v1/mock/tests/general/get-cookie:passed',
];
protected const ENUM_RESPONSES = [
'POST:/v1/mock/tests/general/enum:passed',
];
protected const MODEL_RESPONSES = [
'POST:/v1/mock/tests/general/models:passed',
'POST:/v1/mock/tests/general/models/array:passed',
];
protected const UNION_RESPONSES = [
'GET:/v1/mock/tests/union:passed',
'test-data',
'stub',
];
protected const UPLOAD_RESPONSE = [
'POST:/v1/mock/tests/general/upload:passed',
];
protected const UPLOAD_RESPONSES = [
'POST:/v1/mock/tests/general/upload:passed',
'POST:/v1/mock/tests/general/upload:passed',
'POST:/v1/mock/tests/general/upload:passed',
'POST:/v1/mock/tests/general/upload:passed',
];
protected const LARGE_FILE_RESPONSES = [
'POST:/v1/mock/tests/general/upload:passed',
];
/**
* 'Mock 400 error' -> message
* '{"message":"Mock 400 error","code":400}' -> response
*/
protected const EXCEPTION_RESPONSES = [
'Mock 400 error',
'{"message":"Mock 400 error","code":400}',
'Mock 500 error',
'{"message":"Mock 500 error","code":500}',
'This is a text error',
'This is a text error',
'Invalid endpoint URL: htp://cloud.appwrite.io/v1',
];
protected const REALTIME_RESPONSES = [
'WS:/v1/realtime:passed',
];
protected const QUERY_HELPER_RESPONSES = [
'{"method":"equal","attribute":"released","values":[true]}',
'{"method":"equal","attribute":"title","values":["Spiderman","Dr. Strange"]}',
'{"method":"notEqual","attribute":"title","values":["Spiderman"]}',
'{"method":"lessThan","attribute":"releasedYear","values":[1990]}',
'{"method":"greaterThan","attribute":"releasedYear","values":[1990]}',
'{"method":"search","attribute":"name","values":["john"]}',
'{"method":"isNull","attribute":"name"}',
'{"method":"isNotNull","attribute":"name"}',
'{"method":"between","attribute":"age","values":[50,100]}',
'{"method":"between","attribute":"age","values":[50.5,100.5]}',
'{"method":"between","attribute":"name","values":["Anna","Brad"]}',
'{"method":"startsWith","attribute":"name","values":["Ann"]}',
'{"method":"endsWith","attribute":"name","values":["nne"]}',
'{"method":"select","values":["name","age"]}',
'{"method":"orderAsc","attribute":"title"}',
'{"method":"orderDesc","attribute":"title"}',
'{"method":"orderRandom"}',
'{"method":"cursorAfter","values":["my_movie_id"]}',
'{"method":"cursorBefore","values":["my_movie_id"]}',
'{"method":"limit","values":[50]}',
'{"method":"offset","values":[20]}',
'{"method":"contains","attribute":"title","values":["Spider"]}',
'{"method":"contains","attribute":"labels","values":["first"]}',
'{"method":"notContains","attribute":"title","values":["Spider"]}',
'{"method":"notSearch","attribute":"name","values":["john"]}',
'{"method":"notBetween","attribute":"age","values":[50,100]}',
'{"method":"notStartsWith","attribute":"name","values":["Ann"]}',
'{"method":"notEndsWith","attribute":"name","values":["nne"]}',
'{"method":"lessThan","attribute":"$createdAt","values":["2023-01-01"]}',
'{"method":"greaterThan","attribute":"$createdAt","values":["2023-01-01"]}',
'{"method":"between","attribute":"$createdAt","values":["2023-01-01","2023-12-31"]}',
'{"method":"lessThan","attribute":"$updatedAt","values":["2023-01-01"]}',
'{"method":"greaterThan","attribute":"$updatedAt","values":["2023-01-01"]}',
'{"method":"between","attribute":"$updatedAt","values":["2023-01-01","2023-12-31"]}',
'{"method":"distanceEqual","attribute":"location","values":[[[[40.7128,-74],[40.7128,-74]],1000,true]]}',
'{"method":"distanceEqual","attribute":"location","values":[[[40.7128,-74],1000,true]]}',
'{"method":"distanceNotEqual","attribute":"location","values":[[[40.7128,-74],1000,true]]}',
'{"method":"distanceNotEqual","attribute":"location","values":[[[40.7128,-74],1000,true]]}',
'{"method":"distanceGreaterThan","attribute":"location","values":[[[40.7128,-74],1000,true]]}',
'{"method":"distanceGreaterThan","attribute":"location","values":[[[40.7128,-74],1000,true]]}',
'{"method":"distanceLessThan","attribute":"location","values":[[[40.7128,-74],1000,true]]}',
'{"method":"distanceLessThan","attribute":"location","values":[[[40.7128,-74],1000,true]]}',
'{"method":"intersects","attribute":"location","values":[[40.7128,-74]]}',
'{"method":"notIntersects","attribute":"location","values":[[40.7128,-74]]}',
'{"method":"crosses","attribute":"location","values":[[40.7128,-74]]}',
'{"method":"notCrosses","attribute":"location","values":[[40.7128,-74]]}',
'{"method":"overlaps","attribute":"location","values":[[40.7128,-74]]}',
'{"method":"notOverlaps","attribute":"location","values":[[40.7128,-74]]}',
'{"method":"touches","attribute":"location","values":[[40.7128,-74]]}',
'{"method":"notTouches","attribute":"location","values":[[40.7128,-74]]}',
'{"method":"contains","attribute":"location","values":[[40.7128,-74],[40.7128,-74]]}',
'{"method":"notContains","attribute":"location","values":[[40.7128,-74],[40.7128,-74]]}',
'{"method":"equal","attribute":"location","values":[[40.7128,-74],[40.7128,-74]]}',
'{"method":"notEqual","attribute":"location","values":[[40.7128,-74],[40.7128,-74]]}',
'{"method":"or","values":[{"method":"equal","attribute":"released","values":[true]},{"method":"lessThan","attribute":"releasedYear","values":[1990]}]}',
'{"method":"and","values":[{"method":"equal","attribute":"released","values":[false]},{"method":"greaterThan","attribute":"releasedYear","values":[2015]}]}'
];
protected const PERMISSION_HELPER_RESPONSES = [
'read("any")',
'write("user:userid")',
'create("users")',
'update("guests")',
'delete("team:teamId/owner")',
'delete("team:teamId")',
'create("member:memberId")',
'update("users/verified")',
'update("user:userid/unverified")',
'create("label:admin")',
];
protected const ID_HELPER_RESPONSES = [
'unique()',
'custom_id'
];
protected const OPERATOR_HELPER_RESPONSES = [
'{"method":"increment","values":[1]}',
'{"method":"increment","values":[5,100]}',
'{"method":"decrement","values":[1]}',
'{"method":"decrement","values":[3,0]}',
'{"method":"multiply","values":[2]}',
'{"method":"multiply","values":[3,1000]}',
'{"method":"divide","values":[2]}',
'{"method":"divide","values":[4,1]}',
'{"method":"modulo","values":[5]}',
'{"method":"power","values":[2]}',
'{"method":"power","values":[3,100]}',
'{"method":"arrayAppend","values":["item1","item2"]}',
'{"method":"arrayPrepend","values":["first","second"]}',
'{"method":"arrayInsert","values":[0,"newItem"]}',
'{"method":"arrayRemove","values":["oldItem"]}',
'{"method":"arrayUnique","values":[]}',
'{"method":"arrayIntersect","values":["a","b","c"]}',
'{"method":"arrayDiff","values":["x","y"]}',
'{"method":"arrayFilter","values":["equal","test"]}',
'{"method":"stringConcat","values":["suffix"]}',
'{"method":"stringReplace","values":["old","new"]}',
'{"method":"toggle","values":[]}',
'{"method":"dateAddDays","values":[7]}',
'{"method":"dateSubDays","values":[3]}',
'{"method":"dateSetNow","values":[]}',
];
protected string $class = '';
protected string $language = '';
protected array $build = [];
protected string $command = '';
protected array $expectedOutput = [];
protected string $sdkName;
protected string $sdkPlatform;
protected string $sdkLanguage;
protected string $version;
public function setUp(): void
{
$headers = "x-sdk-name: {$this->sdkName}; x-sdk-platform: {$this->sdkPlatform}; x-sdk-language: {$this->sdkLanguage}; x-sdk-version: {$this->version}";
$this->expectedOutput[] = $headers;
\exec('
cd ./mock-server && \
docker compose build && \
docker compose up -d --force-recreate
');
}
public function tearDown(): void
{
}
/**
* @throws SyntaxError
* @throws \Throwable
* @throws RuntimeError
* @throws LoaderError
*/
public function testHTTPSuccess(): void
{
$spec = file_get_contents(realpath(__DIR__ . '/resources/spec.json'));
if (empty($spec)) {
throw new \Exception('Failed to parse spec.');
}
$sdk = new SDK($this->getLanguage(), new Swagger2($spec));
$sdk
->setName($this->sdkName)
->setVersion($this->version)
->setPlatform($this->sdkPlatform)
->setDescription('Repo description goes here')
->setShortDescription('Repo short description goes here')
->setLogo('https://appwrite.io/v1/images/console.png')
->setWarning('**WORK IN PROGRESS - THIS IS JUST A TEST SDK**')
->setExamples('**EXAMPLES** <HTML>')
->setGitUserName('repoowner')
->setGitRepoName('reponame')
->setLicense('BSD-3-Clause')
->setLicenseContent('demo license')
->setChangelog('--changelog--')
->setDefaultHeaders([
'X-Appwrite-Response-Format' => '0.8.0',
])
->setTest("true");
if ($this->language === 'android' || $this->language === 'kotlin') {
$sdk->setNamespace("io.appwrite");
} else {
$sdk->setNamespace("appwrite");
}
$dir = __DIR__ . '/sdks/' . $this->language;
$this->rmdirRecursive($dir);
$sdk->generate(__DIR__ . '/sdks/' . $this->language);
/**
* Build SDK
*/
foreach ($this->build as $command) {
echo "Build Executing: {$command}\n";
exec($command);
}
$output = [];
echo "Env Executing: {$this->command}\n";
exec($this->command, $output);
$this->assertIsArray($output);
do {
$removed = \array_shift($output);
} while ($removed != 'Test Started' && sizeof($output) != 0);
echo \implode("\n", $output);
foreach ($this->expectedOutput as $index => $expected) {
// HACK: Swift does not guarantee the order of the JSON parameters
if (\str_starts_with($expected, '{')) {
$this->assertEquals(
\json_decode($expected, true),
\json_decode($output[$index], true)
);
} elseif ($expected == 'unique()') {
$this->assertNotEmpty($output[$index]);
$this->assertIsString($output[$index]);
$this->assertEquals(20, strlen($output[$index]));
$this->assertNotEquals($output[$index], 'unique()');
} else {
$this->assertEquals($expected, $output[$index]);
}
}
}
private function rmdirRecursive($dir): void
{
if (!\is_dir($dir)) {
return;
}
foreach (\scandir($dir) as $file) {
if ('.' === $file || '..' === $file) {
continue;
}
if (\is_dir("$dir/$file")) {
$this->rmdirRecursive("$dir/$file");
} else {
\unlink("$dir/$file");
}
}
\rmdir($dir);
}
public function getLanguage(): Language
{
return new $this->class();
}
/**
* Test that x-sdk-enums are properly parsed and included in response enums
*/
public function testSDKEnums(): void
{
$spec = file_get_contents(realpath(__DIR__ . '/resources/spec.json'));
if (empty($spec)) {
throw new \Exception('Failed to parse spec.');
}
$swagger = new Swagger2($spec);
$enums = $swagger->getResponseEnums();
$enumNames = array_column($enums, 'name');
// Find MockPlan enum from x-sdk-enums
$mockPlanEnum = null;
foreach ($enums as $enum) {
if ($enum['name'] === 'MockPlan') {
$mockPlanEnum = $enum;
break;
}
}
// x-sdk-enums are included
$this->assertNotNull($mockPlanEnum, 'x-sdk-enums should be included in response enums');
$this->assertArrayHasKey('enum', $mockPlanEnum);
$this->assertEquals(['free', 'pro', 'scale', 'enterprise'], $mockPlanEnum['enum']);
// x-sdk-enums don't duplicate
$mockPlanCount = count(array_filter($enums, fn($e) => $e['name'] === 'MockPlan'));
$this->assertEquals(1, $mockPlanCount, 'x-sdk-enums should not duplicate');
// regular property-level enums still work
$this->assertGreaterThan(1, count($enumNames), 'Both x-sdk-enums and property-level enums should be included');
}
}