mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
d2230f8fe7
Raises `phpstan.neon` level from 3 to 4 and fixes the 549 new errors
that level 4 surfaces across 157 files. Fixes are root-cause — no
`@phpstan-ignore`, no `@var` casts, no baseline entries, no widened
types. A handful of latent bugs were fixed along the way:
- `app/controllers/general.php`: path-traversal guard was negating
`\substr(...)` before the strict comparison (`!\substr(...) === $base`
was always `false === $base`). Rewritten as `\substr(...) !== $base`.
- `src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php`
and `.../TablesDB/Logs/XList.php`: were importing the raw Matomo
`DeviceDetector` (whose `getDevice()` returns `?int`) but treating the
result as an array with `deviceName/deviceBrand/deviceModel` keys.
Swapped to `Appwrite\Detector\Detector`, matching the wrapper already
used a few lines below for `$os`/`$client`.
- `src/Appwrite/Platform/Modules/Functions/Workers/Builds.php`: a match
key was checking `$resourceKey === 'functions'` when `$resourceKey`
is `'functionId'|'siteId'` — always false. Switched to the intended
`$resource->getCollection() === 'functions'` check.
- `src/Appwrite/OpenSSL/OpenSSL.php`: `encrypt()` return type tightened
to `string|false` to match `openssl_encrypt`; this lets callers'
`=== false` error handling remain meaningful.
- `app/controllers/api/messaging.php`: removed a dead
`array_key_exists('from', [])` branch in the Msg91 provider (empty
array literal; branch was unreachable).
Large cleanup categories across the 549 fixes:
- Removed redundant `?? default` on array offsets and expressions that
PHPStan now knows are non-nullable.
- Removed unreachable statements (mostly `return;` after `throw` or
`markTestSkipped()`).
- Removed redundant `is_array`/`is_string`/`is_bool`/`instanceof` checks
on already-narrowed types.
- Added `default =>` arms (or throwing arms) to non-exhaustive matches
on `string`/`mixed` input.
- Removed dead `$document === false` branches where method return types
were tightened to non-nullable `Document`.
- Removed unused properties (`$version` on Etsy/Zoom OAuth2, `$paths` on
Installer State, `$source` on MigrationsWorker, `$account2` on two
GraphQL auth tests), unused traits (`ApiVectorsDB`, `DatabaseFixture`),
and an unused `cleanupStaleExecutions` task method.
- Replaced `assertTrue(true)` and redundant `assertIsArray`/`assertIsString`/
`assertNotNull` assertions with `addToAssertionCount(1)` or
`assertNotEmpty` where the runtime type was already known.
272 lines
8.6 KiB
PHP
272 lines
8.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\E2E\Services\GraphQL;
|
|
|
|
use Appwrite\Tests\Async;
|
|
use Tests\E2E\Client;
|
|
use Tests\E2E\Scopes\ProjectCustom;
|
|
use Tests\E2E\Scopes\Scope;
|
|
use Tests\E2E\Scopes\SideClient;
|
|
use Utopia\Database\Helpers\ID;
|
|
use Utopia\Database\Helpers\Role;
|
|
|
|
class FunctionsClientTest extends Scope
|
|
{
|
|
use ProjectCustom;
|
|
use SideClient;
|
|
use Base;
|
|
use Async;
|
|
|
|
private static array $cachedFunction = [];
|
|
private static array $cachedDeployment = [];
|
|
private static array $cachedExecution = [];
|
|
|
|
protected function setupFunction(): array
|
|
{
|
|
$key = $this->getProject()['$id'];
|
|
if (!empty(self::$cachedFunction[$key])) {
|
|
return self::$cachedFunction[$key];
|
|
}
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
$query = $this->getQuery(self::CREATE_FUNCTION);
|
|
$gqlPayload = [
|
|
'query' => $query,
|
|
'variables' => [
|
|
'functionId' => ID::unique(),
|
|
'name' => 'Test Function',
|
|
'runtime' => 'node-22',
|
|
'entrypoint' => 'index.js',
|
|
'execute' => [Role::any()->toString()],
|
|
]
|
|
];
|
|
|
|
$function = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
'content-type' => 'application/json',
|
|
'x-appwrite-project' => $projectId,
|
|
'x-appwrite-key' => $this->getProject()['apiKey'],
|
|
], $gqlPayload);
|
|
|
|
$this->assertIsArray($function['body']['data']);
|
|
$this->assertArrayNotHasKey('errors', $function['body']);
|
|
|
|
$function = $function['body']['data']['functionsCreate'];
|
|
$functionId = $function['_id'];
|
|
|
|
$query = '
|
|
mutation createVariables($functionId: String!) {
|
|
var1: functionsCreateVariable(functionId: $functionId, key: "name", value: "John Doe") {
|
|
_id
|
|
}
|
|
var2: functionsCreateVariable(functionId: $functionId, key: "age", value: "42") {
|
|
_id
|
|
}
|
|
}
|
|
';
|
|
$gqlPayload = [
|
|
'query' => $query,
|
|
'variables' => [
|
|
'functionId' => $functionId,
|
|
]
|
|
];
|
|
|
|
$variables = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
'content-type' => 'application/json',
|
|
'x-appwrite-project' => $projectId,
|
|
'x-appwrite-key' => $this->getProject()['apiKey'],
|
|
], $gqlPayload);
|
|
|
|
$this->assertIsArray($variables['body']['data']);
|
|
$this->assertArrayNotHasKey('errors', $variables['body']);
|
|
|
|
self::$cachedFunction[$key] = $function;
|
|
return $function;
|
|
}
|
|
|
|
protected function setupDeployment(): array
|
|
{
|
|
$key = $this->getProject()['$id'];
|
|
if (!empty(self::$cachedDeployment[$key])) {
|
|
return self::$cachedDeployment[$key];
|
|
}
|
|
|
|
$function = $this->setupFunction();
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
$query = $this->getQuery(self::CREATE_DEPLOYMENT);
|
|
|
|
$gqlPayload = [
|
|
'operations' => \json_encode([
|
|
'query' => $query,
|
|
'variables' => [
|
|
'functionId' => $function['_id'],
|
|
'activate' => true,
|
|
'code' => null,
|
|
]
|
|
]),
|
|
'map' => \json_encode([
|
|
'code' => ["variables.code"]
|
|
]),
|
|
'code' => $this->packageFunction('basic')
|
|
];
|
|
|
|
$deployment = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
'content-type' => 'multipart/form-data',
|
|
'x-appwrite-project' => $projectId,
|
|
'x-appwrite-key' => $this->getProject()['apiKey'],
|
|
], $gqlPayload);
|
|
|
|
$this->assertIsArray($deployment['body']['data']);
|
|
$this->assertArrayNotHasKey('errors', $deployment['body']);
|
|
|
|
// Poll get deployment until an error, or status is either 'ready' or 'failed'
|
|
$deployment = $deployment['body']['data']['functionsCreateDeployment'];
|
|
$deploymentId = $deployment['_id'];
|
|
|
|
$query = $this->getQuery(self::GET_DEPLOYMENT);
|
|
$gqlPayload = [
|
|
'query' => $query,
|
|
'variables' => [
|
|
'functionId' => $function['_id'],
|
|
'deploymentId' => $deploymentId,
|
|
]
|
|
];
|
|
|
|
$this->assertEventually(function () use ($projectId, $gqlPayload, &$deployment) {
|
|
$deployment = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
'content-type' => 'application/json',
|
|
'x-appwrite-project' => $projectId,
|
|
'x-appwrite-key' => $this->getProject()['apiKey'],
|
|
], $gqlPayload);
|
|
|
|
$this->assertIsArray($deployment['body']['data']);
|
|
$this->assertArrayNotHasKey('errors', $deployment['body']);
|
|
|
|
$deployment = $deployment['body']['data']['functionsGetDeployment'];
|
|
$this->assertEquals('ready', $deployment['status']);
|
|
}, 60000);
|
|
|
|
self::$cachedDeployment[$key] = $deployment;
|
|
return $deployment;
|
|
}
|
|
|
|
protected function setupExecution(): array
|
|
{
|
|
$key = $this->getProject()['$id'];
|
|
if (!empty(self::$cachedExecution[$key])) {
|
|
return self::$cachedExecution[$key];
|
|
}
|
|
|
|
$function = $this->setupFunction();
|
|
$this->setupDeployment(); // Ensure deployment exists
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
$query = $this->getQuery(self::CREATE_EXECUTION);
|
|
$gqlPayload = [
|
|
'query' => $query,
|
|
'variables' => [
|
|
'functionId' => $function['_id'],
|
|
]
|
|
];
|
|
|
|
$execution = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
|
|
'content-type' => 'application/json',
|
|
'x-appwrite-project' => $projectId,
|
|
], $this->getHeaders()), $gqlPayload);
|
|
|
|
$this->assertIsArray($execution['body']['data']);
|
|
$this->assertArrayNotHasKey('errors', $execution['body']);
|
|
|
|
self::$cachedExecution[$key] = $execution['body']['data']['functionsCreateExecution'];
|
|
return self::$cachedExecution[$key];
|
|
}
|
|
|
|
public function testCreateFunction(): void
|
|
{
|
|
$function = $this->setupFunction();
|
|
$this->assertNotEmpty($function);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws \Exception
|
|
*/
|
|
public function testCreateDeployment(): void
|
|
{
|
|
$deployment = $this->setupDeployment();
|
|
$this->assertNotEmpty($deployment);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws \Exception
|
|
*/
|
|
public function testCreateExecution(): void
|
|
{
|
|
$execution = $this->setupExecution();
|
|
$this->assertNotEmpty($execution);
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @throws \Exception
|
|
*/
|
|
public function testGetExecutions(): array
|
|
{
|
|
$function = $this->setupFunction();
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
$query = $this->getQuery(self::GET_EXECUTIONS);
|
|
$gqlPayload = [
|
|
'query' => $query,
|
|
'variables' => [
|
|
'functionId' => $function['_id'],
|
|
]
|
|
];
|
|
|
|
$executions = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
|
|
'content-type' => 'application/json',
|
|
'x-appwrite-project' => $projectId,
|
|
], $this->getHeaders()), $gqlPayload);
|
|
|
|
$this->assertIsArray($executions['body']['data']);
|
|
$this->assertArrayNotHasKey('errors', $executions['body']);
|
|
$executions = $executions['body']['data']['functionsListExecutions'];
|
|
$this->assertIsArray($executions);
|
|
|
|
return $executions;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @throws \Exception
|
|
*/
|
|
public function testGetExecution(): array
|
|
{
|
|
$function = $this->setupFunction();
|
|
$execution = $this->setupExecution();
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
$query = $this->getQuery(self::GET_EXECUTION);
|
|
$gqlPayload = [
|
|
'query' => $query,
|
|
'variables' => [
|
|
'functionId' => $function['_id'],
|
|
'executionId' => $execution['_id'],
|
|
]
|
|
];
|
|
|
|
$execution = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
|
|
'content-type' => 'application/json',
|
|
'x-appwrite-project' => $projectId,
|
|
], $this->getHeaders()), $gqlPayload);
|
|
|
|
$this->assertIsArray($execution['body']['data']);
|
|
$this->assertArrayNotHasKey('errors', $execution['body']);
|
|
$execution = $execution['body']['data']['functionsGetExecution'];
|
|
$this->assertIsArray($execution);
|
|
|
|
return $execution;
|
|
}
|
|
}
|