1
0
Fork 0
mirror of synced 2024-08-26 23:51:39 +12:00
appwrite/tests/e2e/Services/GraphQL/FunctionsClientTest.php

246 lines
7.8 KiB
PHP
Raw Normal View History

2022-07-04 18:16:06 +12:00
<?php
namespace Tests\E2E\Services\GraphQL;
2022-07-06 20:56:58 +12:00
use CURLFile;
2022-07-04 18:16:06 +12:00
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideClient;
2023-01-16 22:25:40 +13:00
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Role;
2022-07-04 18:16:06 +12:00
2022-09-22 20:29:42 +12:00
class FunctionsClientTest extends Scope
2022-07-04 18:16:06 +12:00
{
use ProjectCustom;
use SideClient;
2022-09-22 20:29:42 +12:00
use Base;
2022-07-04 18:16:06 +12:00
public function testCreateFunction(): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$CREATE_FUNCTION);
$gqlPayload = [
'query' => $query,
'variables' => [
2022-09-22 13:53:41 +12:00
'functionId' => ID::unique(),
2022-07-04 18:16:06 +12:00
'name' => 'Test Function',
2022-09-22 14:16:13 +12:00
'runtime' => 'php-8.0',
2023-08-19 05:52:03 +12:00
'entrypoint' => 'index.php',
2022-09-22 14:16:13 +12:00
'execute' => [Role::any()->toString()],
2022-07-04 18:16:06 +12:00
]
];
$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']);
2022-09-22 14:16:13 +12:00
$function = $function['body']['data']['functionsCreate'];
2022-12-08 16:08:57 +13:00
$functionId = $function['_id'];
2022-09-22 14:16:13 +12:00
$query = '
mutation createVariables($functionId: String!) {
var1: functionsCreateVariable(functionId: $functionId, key: "name", value: "John Doe") {
2022-12-08 16:08:57 +13:00
_id
2022-09-22 14:16:13 +12:00
}
var2: functionsCreateVariable(functionId: $functionId, key: "age", value: "42") {
2022-12-08 16:08:57 +13:00
_id
2022-09-22 14:16:13 +12:00
}
}
';
$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']);
return $function;
2022-07-04 18:16:06 +12:00
}
/**
* @depends testCreateFunction
* @param $function
* @return array
* @throws \Exception
*/
public function testCreateDeployment($function): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$CREATE_DEPLOYMENT);
2023-10-27 03:50:35 +13:00
$folder = 'php';
$code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz";
$this->packageCode($folder);
2022-07-04 18:16:06 +12:00
$gqlPayload = [
2022-07-07 17:50:49 +12:00
'operations' => \json_encode([
'query' => $query,
'variables' => [
2022-12-08 16:08:57 +13:00
'functionId' => $function['_id'],
2022-07-07 17:50:49 +12:00
'activate' => true,
'code' => null,
]
]),
'map' => \json_encode([
'code' => ["variables.code"]
]),
'code' => new CURLFile($code, 'application/gzip', 'code.tar.gz'),
2022-07-04 18:16:06 +12:00
];
2022-07-07 17:51:20 +12:00
$deployment = $this->client->call(Client::METHOD_POST, '/graphql', [
2022-07-07 17:50:49 +12:00
'content-type' => 'multipart/form-data',
2022-07-04 18:16:06 +12:00
'x-appwrite-project' => $projectId,
2022-07-07 17:51:20 +12:00
'x-appwrite-key' => $this->getProject()['apiKey'],
], $gqlPayload);
2022-07-04 18:16:06 +12:00
$this->assertIsArray($deployment['body']['data']);
$this->assertArrayNotHasKey('errors', $deployment['body']);
2023-08-30 17:31:21 +12:00
// Poll get deployment until an error, or status is either 'ready' or 'failed'
$deployment = $deployment['body']['data']['functionsCreateDeployment'];
$deploymentId = $deployment['_id'];
2022-07-07 17:51:20 +12:00
2023-08-30 17:31:21 +12:00
$query = $this->getQuery(self::$GET_DEPLOYMENT);
$gqlPayload = [
'query' => $query,
'variables' => [
'functionId' => $function['_id'],
'deploymentId' => $deploymentId,
]
];
while (true) {
2023-08-30 18:10:50 +12:00
$deployment = $this->client->call(Client::METHOD_POST, '/graphql', [
2023-08-30 17:31:21 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
2023-08-30 18:10:50 +12:00
'x-appwrite-key' => $this->getProject()['apiKey'],
], $gqlPayload);
2023-08-30 17:31:21 +12:00
$this->assertIsArray($deployment['body']['data']);
$this->assertArrayNotHasKey('errors', $deployment['body']);
$deployment = $deployment['body']['data']['functionsGetDeployment'];
if (
$deployment['status'] === 'ready'
|| $deployment['status'] === 'failed'
) {
break;
}
\sleep(1);
}
$this->assertEquals('ready', $deployment['status']);
return $deployment;
2022-07-04 18:16:06 +12:00
}
/**
* @depends testCreateFunction
* @depends testCreateDeployment
* @param $function
* @param $deployment
* @return array
* @throws \Exception
*/
public function testCreateExecution($function, $deployment): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$CREATE_EXECUTION);
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 16:08:57 +13:00
'functionId' => $function['_id'],
2022-07-04 18:16:06 +12:00
]
];
$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']);
2022-07-07 17:51:20 +12:00
return $execution['body']['data']['functionsCreateExecution'];
2022-07-04 18:16:06 +12:00
}
/**
* @depends testCreateFunction
* @param $function
* @return array
* @throws \Exception
*/
public function testGetExecutions($function): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$GET_EXECUTIONS);
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 16:08:57 +13:00
'functionId' => $function['_id'],
2022-07-04 18:16:06 +12:00
]
];
$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;
}
/**
* @depends testCreateFunction
* @depends testCreateExecution
* @param $function
* @param $execution
* @return array
* @throws \Exception
*/
public function testGetExecution($function, $execution): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$GET_EXECUTION);
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 16:08:57 +13:00
'functionId' => $function['_id'],
'executionId' => $execution['_id'],
2022-07-04 18:16:06 +12:00
]
];
$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;
}
2022-07-06 15:51:37 +12:00
}