-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathBase.php
More file actions
565 lines (495 loc) · 21.5 KB
/
Copy pathBase.php
File metadata and controls
565 lines (495 loc) · 21.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
<?php
declare(strict_types=1);
namespace Tests\E2E;
use Exception;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use FilesystemIterator;
use Throwable;
use Appwrite\SDK\Language;
use Appwrite\SDK\SDK;
use Appwrite\Spec\OpenAPI3;
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 PATH_PARAM_RESPONSES = [
'GET:/v1/mock/tests/general/path/grant%2Fspecial%26id: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',
];
protected const EXCLUDED_FIXTURE_TOKENS = [
'zzexcludedservice',
'zzexcludedpayload',
'zzexcludedresult',
'zzexcludedstatus',
'zzexcludedchild',
'zzexcludedchildstatus',
'zzexcludedmethodpayload',
'zzexcludedmethodresult',
'zzexcludedmethodstatus',
];
/**
* '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',
'WS:/v1/realtime:passed',
'Realtime failed!',
'Realtime unsubscribe:passed',
'Realtime update:passed',
'Realtime presence:passed',
'Realtime disconnect: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":"containsAny","attribute":"labels","values":["first","second"]}',
'{"method":"containsAll","attribute":"labels","values":["first","second"]}',
'{"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]}]}',
'{"method":"regex","attribute":"name","values":["pattern.*"]}',
'{"method":"exists","values":["attr1","attr2"]}',
'{"method":"notExists","values":["attr1","attr2"]}',
'{"method":"elemMatch","attribute":"friends","values":[{"method":"equal","attribute":"name","values":["Alice"]},{"method":"greaterThan","attribute":"age","values":[18]}]}',
];
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 ADDITIONAL_PROPERTIES_RESPONSES = [
'{"theme":"dark","timezone":"UTC"}',
'{"$id":"row1","custom":"value","nested":{"enabled":true}}',
'{"data":{"enabled":true},"status":"ok","extra":"kept"}',
];
protected const CLI_CONSOLE_URL_RESPONSES = [
'https://cloud.appwrite.io/console/project-sgp-chirag-project-prod/sites/site-chirag-profile-website/deployments/deployment-123',
'https://cloud.appwrite.io/console/project-sgp-chirag-project-prod/functions/function-sample-function/deployment-123',
'https://abc.example.com/console/project-self-hosted-project/sites/site-docs/deployments/deployment-456',
];
protected const CLI_HEADERS_RESPONSES = [
'x-sdk-name: cli; x-sdk-platform: server; x-sdk-language: cli; '
. 'x-sdk-version: 0.0.1; accept: application/json, text/plain',
];
protected const CLI_FUNCTION_RESPONSES = [
'POST:/v1/functions/{functionId}/executions:passed',
];
protected const CLI_COMPLETION_RESPONSES = [
'compdef _appwrite appwrite',
'complete -F _appwrite_completion appwrite',
'complete -c \'appwrite\' -f -n \'__appwrite_using_command\' -a \'bar client completion foo functions general\'',
'\'foo:get\') context=\'foo get\' ;;',
];
protected const CLI_TYPEGEN_RESPONSES = [
'CLI_TYPEGEN:passed',
];
protected const AUTH_LOGIC_RESPONSES = [
'auth:endpoint-cloud-hostname:passed',
'auth:endpoint-regional:passed',
'auth:endpoint-localhost:passed',
'auth:endpoint-cloud-login:passed',
'auth:endpoint-dev-override:passed',
'auth:endpoint-normalize:passed',
'auth:console-slug-region:passed',
'auth:decode-id-token:passed',
'auth:authorization-pending-error:passed',
'auth:session-account-key:passed',
'auth:session-local-only:passed',
'auth:session-legacy:passed',
'auth:session-has-auth:passed',
'auth:plan-session-logout:passed',
'auth:logout-question-choices:passed',
'auth:restore-current-session-fallback:passed',
'auth:poll-device-token-success:passed',
'auth:poll-device-token-retry:passed',
'auth:poll-device-token-error:passed',
'auth:poll-device-token-timeout:passed',
'auth:poll-device-token-slow-down:passed',
'auth:poll-device-token-empty-error:passed',
'auth:poll-device-token-default-interval:passed',
'auth:valid-access-token-cached:passed',
'auth:valid-access-token-missing-expiry:passed',
'auth:oauth-login-flag:passed',
'auth:open-browser:passed',
];
protected const CLI_LOCAL_FUNCTION_EMULATION_RESPONSES = [
'CLI_LOCAL_FUNCTION_RUNNER_CONFIG:passed',
'CLI_LOCAL_SOURCE_PREFLIGHT:passed',
];
protected const HEADERS_RESPONSE = '__HEADERS_RESPONSE__';
protected const CLI_RUNTIME_RENDERING_RESPONSES = [
'CLI_RUNTIME_RENDERING:passed',
];
protected const CLI_QUERY_HELPER_RESPONSES = [
'[' .
'"{\"method\":\"orderDesc\",\"attribute\":\"rawName\"}",' .
'"{\"method\":\"equal\",\"attribute\":\"published\",\"values\":[true]}",' .
'"{\"method\":\"greaterThanEqual\",\"attribute\":\"score\",\"values\":[10]}",' .
'"{\"method\":\"equal\",\"attribute\":\"legacy\",\"values\":[true]}",' .
'"{\"method\":\"equal\",\"attribute\":\"status\",\"values\":[\"draft\",\"published\"]}",' .
'"{\"method\":\"orderAsc\",\"attribute\":\"title\"}",' .
'"{\"method\":\"orderDesc\",\"attribute\":\"$createdAt\"}",' .
'"{\"method\":\"limit\",\"values\":[25]}",' .
'"{\"method\":\"offset\",\"values\":[50]}",' .
'"{\"method\":\"cursorAfter\",\"values\":[\"row-before\"]}",' .
'"{\"method\":\"cursorBefore\",\"values\":[\"row-after\"]}",' .
'"{\"method\":\"select\",\"values\":[\"$id\",\"title\"]}"' .
']',
'CLI_QUERY_HELPERS:passed',
];
protected const CHANNEL_HELPER_RESPONSES = [
'databases.db1.collections.col1.documents',
'databases.db1.collections.col1.documents.doc1',
'databases.db1.collections.col1.documents.doc1.create',
'databases.db1.collections.col1.documents.doc1.upsert',
'tablesdb.db1.tables.table1.rows',
'tablesdb.db1.tables.table1.rows.row1',
'tablesdb.db1.tables.table1.rows.row1.update',
'account',
'buckets.bucket1.files',
'buckets.bucket1.files.file1',
'buckets.bucket1.files.file1.delete',
'functions.func2',
'functions.func1',
'executions.exec2',
'executions.exec1',
'documents',
'rows',
'files',
'executions',
'teams',
'teams.team2',
'teams.team1',
'teams.team1.create',
'memberships',
'memberships.membership2',
'memberships.membership1',
'memberships.membership1.update',
'presences',
'presences.presence2',
'presences.presence1',
'presences.presence1.upsert',
'presences.presence1.update',
'presences.presence1.delete',
];
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":[]}',
self::HEADERS_RESPONSE,
];
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
{
\array_unshift($this->expectedOutput, $this->getExpectedSdkHeaders());
\exec('
cd ./mock-server && \
docker compose build && \
docker compose up -d --force-recreate
');
}
protected function getExpectedSdkHeaders(): string
{
return "x-sdk-name: {$this->sdkName}; x-sdk-platform: {$this->sdkPlatform}; x-sdk-language: {$this->sdkLanguage}; x-sdk-version: {$this->version}";
}
public function tearDown(): void
{
// Remove the mock server so local test runs don't leave containers behind
\exec('
cd ./mock-server && \
docker compose down
');
}
/**
* @throws SyntaxError
* @throws Throwable
* @throws RuntimeError
* @throws LoaderError
*/
public function testHTTPSuccess(): void
{
$spec = file_get_contents(realpath(__DIR__ . '/../resources/spec-openapi3.json'));
if (empty($spec)) {
throw new Exception('Failed to parse spec.');
}
$sdk = new SDK($this->getLanguage(), new OpenAPI3($spec));
$sdk
->setName($this->sdkName)
->setVersion($this->version)
->setPlatform($this->sdkPlatform)
->setDescription('Repo description goes here')
->setShortDescription('Repo short description goes here')
->setCoverImage('https://github.com/appwrite/appwrite/raw/main/public/images/github.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',
])
->setExclude([
'services' => [
['name' => 'zzexcludedservice'],
],
'methods' => [
['name' => 'createExcludedGeneralFixture'],
],
])
->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);
$this->assertExcludedFixtureWasRemoved($dir);
/**
* 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' && count($output) !== 0);
echo \implode("\n", $output);
foreach ($this->expectedOutput as $index => $expected) {
if ($expected === self::HEADERS_RESPONSE) {
$expected = $this->getExpectedSdkHeaders() . '; accept: application/json, text/plain';
}
// HACK: Swift does not guarantee the order of the JSON parameters
if (\str_starts_with((string) $expected, '{')) {
$this->assertEquals(
\json_decode((string) $expected, true),
\json_decode($output[$index], true)
);
} elseif ($expected == 'unique()') {
$this->assertNotEmpty($output[$index]);
$this->assertIsString($output[$index]);
$this->assertSame(20, strlen($output[$index]));
$this->assertNotSame('unique()', $output[$index]);
} else {
$this->assertEquals($expected, $output[$index]);
}
}
}
private function rmdirRecursive(string $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);
}
private function assertExcludedFixtureWasRemoved(string $dir): void
{
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS)
);
foreach ($iterator as $file) {
$path = \strtolower((string) $file->getPathname());
foreach (self::EXCLUDED_FIXTURE_TOKENS as $token) {
$this->assertStringNotContainsString($token, $path, "Excluded fixture leaked into generated path: {$path}");
}
if (!$file->isFile()) {
continue;
}
$contents = \file_get_contents($file->getPathname());
if ($contents === false) {
continue;
}
$contents = \strtolower($contents);
foreach (self::EXCLUDED_FIXTURE_TOKENS as $token) {
$this->assertStringNotContainsString(
$token,
$contents,
"Excluded fixture leaked into generated file: {$file->getPathname()}"
);
}
}
}
public function getLanguage(): Language
{
return new $this->class();
}
}