From 81265ab458cd1d82db756b9aade3d12ad112721a Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 Jul 2022 17:12:12 +1200 Subject: [PATCH] Fix custom entity tests --- src/Appwrite/GraphQL/Builder.php | 45 +- tests/e2e/Services/GraphQL/GraphQLBase.php | 8 +- .../GraphQL/GraphQLDatabaseServerTest.php | 1711 +++++++++-------- 3 files changed, 896 insertions(+), 868 deletions(-) diff --git a/src/Appwrite/GraphQL/Builder.php b/src/Appwrite/GraphQL/Builder.php index be007cbab8..2132577335 100644 --- a/src/Appwrite/GraphQL/Builder.php +++ b/src/Appwrite/GraphQL/Builder.php @@ -473,6 +473,7 @@ class Builder $count += count($attrs); go(function () use ($utopia, $request, $response, $dbForProject, &$collections, &$queryFields, &$mutationFields, $limit, &$offset, $attrs, $userId, $wg) { foreach ($attrs as $attr) { + $databaseId = $attr->getAttribute('databaseId'); $collectionId = $attr->getAttribute('collectionId'); if ($attr->getAttribute('status') !== 'available') { @@ -502,27 +503,27 @@ class Builder $queryFields[$collectionId . 'Get'] = [ 'type' => $objectType, 'args' => self::$defaultDocumentArgs['id'], - 'resolve' => self::resolveDocumentGet($utopia, $request, $response, $dbForProject, $collectionId) + 'resolve' => self::resolveDocumentGet($utopia, $request, $response, $dbForProject, $databaseId, $collectionId) ]; $queryFields[$collectionId . 'List'] = [ 'type' => $objectType, 'args' => self::$defaultDocumentArgs['list'], - 'resolve' => self::resolveDocumentList($utopia, $request, $response, $dbForProject, $collectionId) + 'resolve' => self::resolveDocumentList($utopia, $request, $response, $dbForProject, $databaseId, $collectionId) ]; $mutationFields[$collectionId . 'Create'] = [ 'type' => $objectType, 'args' => $attributes, - 'resolve' => self::resolveDocumentMutate($utopia, $request, $response, $dbForProject, $collectionId, 'POST') + 'resolve' => self::resolveDocumentMutate($utopia, $request, $response, $dbForProject, $databaseId, $collectionId, 'POST') ]; $mutationFields[$collectionId . 'Update'] = [ 'type' => $objectType, 'args' => $attributes, - 'resolve' => self::resolveDocumentMutate($utopia, $request, $response, $dbForProject, $collectionId, 'PATCH') + 'resolve' => self::resolveDocumentMutate($utopia, $request, $response, $dbForProject, $databaseId, $collectionId, 'PATCH') ]; $mutationFields[$collectionId . 'Delete'] = [ 'type' => $objectType, 'args' => self::$defaultDocumentArgs['id'], - 'resolve' => self::resolveDocumentDelete($utopia, $request, $response, $dbForProject, $collectionId) + 'resolve' => self::resolveDocumentDelete($utopia, $request, $response, $dbForProject, $databaseId, $collectionId) ]; } $wg->done(); @@ -545,19 +546,21 @@ class Builder Request $request, Response $response, Database $dbForProject, + string $databaseId, string $collectionId ): callable { return fn($type, $args, $context, $info) => new CoroutinePromise( - function (callable $resolve, callable $reject) use ($utopia, $request, $response, $dbForProject, $collectionId, $type, $args) { + function (callable $resolve, callable $reject) use ($utopia, $request, $response, $dbForProject, $databaseId, $collectionId, $type, $args) { try { $swoole = $request->getSwoole(); $swoole->post = [ + 'databaseId' => $databaseId, 'collectionId' => $collectionId, 'documentId' => $args['id'], ]; $swoole->server['request_method'] = 'GET'; - $swoole->server['request_uri'] = "/v1/database/collections/$collectionId/documents/{$args['id']}"; - $swoole->server['path_info'] = "/v1/database/collections/$collectionId/documents/{$args['id']}"; + $swoole->server['request_uri'] = "/v1/databases/$databaseId/collections/$collectionId/documents/{$args['id']}"; + $swoole->server['path_info'] = "/v1/databases/$databaseId/collections/$collectionId/documents/{$args['id']}"; self::resolve($utopia, $swoole, $response, $resolve, $reject); } catch (\Throwable $e) { @@ -573,12 +576,14 @@ class Builder Request $request, Response $response, Database $dbForProject, - string $collectionId + string $databaseId, + string $collectionId, ): callable { return fn($type, $args, $context, $info) => new CoroutinePromise( - function (callable $resolve, callable $reject) use ($utopia, $request, $response, $dbForProject, $collectionId, $type, $args) { + function (callable $resolve, callable $reject) use ($utopia, $request, $response, $dbForProject, $databaseId, $collectionId, $type, $args) { $swoole = $request->getSwoole(); $swoole->post = [ + 'databaseId' => $databaseId, 'collectionId' => $collectionId, 'limit' => $args['limit'], 'offset' => $args['offset'], @@ -588,8 +593,8 @@ class Builder 'orderType' => $args['orderType'], ]; $swoole->server['request_method'] = 'GET'; - $swoole->server['request_uri'] = "/v1/database/collections/$collectionId/documents"; - $swoole->server['path_info'] = "/v1/database/collections/$collectionId/documents"; + $swoole->server['request_uri'] = "/v1/databases/$databaseId/collections/$collectionId/documents"; + $swoole->server['path_info'] = "/v1/databases/$databaseId/collections/$collectionId/documents"; self::resolve($utopia, $swoole, $response, $resolve, $reject); } @@ -601,11 +606,12 @@ class Builder Request $request, Response $response, Database $dbForProject, + string $databaseId, string $collectionId, string $method, ): callable { return fn($type, $args, $context, $info) => new CoroutinePromise( - function (callable $resolve, callable $reject) use ($utopia, $request, $response, $dbForProject, $collectionId, $method, $type, $args) { + function (callable $resolve, callable $reject) use ($utopia, $request, $response, $dbForProject, $databaseId, $collectionId, $method, $type, $args) { $swoole = $request->getSwoole(); $id = $args['id'] ?? 'unique()'; @@ -618,6 +624,7 @@ class Builder // Order must be the same as the route params $swoole->post = [ + 'databaseId' => $databaseId, 'documentId' => $id, 'collectionId' => $collectionId, 'data' => $args, @@ -625,8 +632,8 @@ class Builder 'write' => $write, ]; $swoole->server['request_method'] = $method; - $swoole->server['request_uri'] = "/v1/database/collections/$collectionId/documents"; - $swoole->server['path_info'] = "/v1/database/collections/$collectionId/documents"; + $swoole->server['request_uri'] = "/v1/databases/$databaseId/collections/$collectionId/documents"; + $swoole->server['path_info'] = "/v1/databases/$databaseId/collections/$collectionId/documents"; self::resolve($utopia, $swoole, $response, $resolve, $reject); } @@ -638,18 +645,20 @@ class Builder Request $request, Response $response, Database $dbForProject, + string $databaseId, string $collectionId ): callable { return fn($type, $args, $context, $info) => new CoroutinePromise( - function (callable $resolve, callable $reject) use ($utopia, $request, $response, $dbForProject, $collectionId, $type, $args) { + function (callable $resolve, callable $reject) use ($utopia, $request, $response, $dbForProject, $databaseId, $collectionId, $type, $args) { $swoole = $request->getSwoole(); $swoole->post = [ + 'databaseId' => $databaseId, 'collectionId' => $collectionId, 'documentId' => $args['id'], ]; $swoole->server['request_method'] = 'DELETE'; - $swoole->server['request_uri'] = "/v1/database/collections/$collectionId/documents/{$args['id']}"; - $swoole->server['path_info'] = "/v1/database/collections/$collectionId/documents/{$args['id']}"; + $swoole->server['request_uri'] = "/v1/databases/$databaseId/collections/$collectionId/documents/{$args['id']}"; + $swoole->server['path_info'] = "/v1/databases/$databaseId/collections/$collectionId/documents/{$args['id']}"; self::resolve($utopia, $swoole, $response, $resolve, $reject); } diff --git a/tests/e2e/Services/GraphQL/GraphQLBase.php b/tests/e2e/Services/GraphQL/GraphQLBase.php index 21825b65df..3376a6a154 100644 --- a/tests/e2e/Services/GraphQL/GraphQLBase.php +++ b/tests/e2e/Services/GraphQL/GraphQLBase.php @@ -412,12 +412,14 @@ trait GraphQLBase } }'; case self::$CREATE_CUSTOM_ENTITY: - return 'mutation createActor($name: String!, $age: Int!, $alive: Boolean!, $salary: Float) { - actorsCreate(name: $name, age: $age, alive: $alive, salary: $salary) { - _id + return 'mutation createActor($name: String!, $age: Int!, $alive: Boolean!, $salary: Float, $email: String!, $role: String!, $ip: String, $url: String){ + actorsCreate(name: $name, age: $age, alive: $alive, salary: $salary, email: $email, role: $role, ip: $ip, url: $url) { name age alive + salary + email + role } }'; case self::$UPDATE_DOCUMENT: diff --git a/tests/e2e/Services/GraphQL/GraphQLDatabaseServerTest.php b/tests/e2e/Services/GraphQL/GraphQLDatabaseServerTest.php index fc52b51a06..fff27dc3d7 100644 --- a/tests/e2e/Services/GraphQL/GraphQLDatabaseServerTest.php +++ b/tests/e2e/Services/GraphQL/GraphQLDatabaseServerTest.php @@ -14,869 +14,811 @@ class GraphQLDatabaseServerTest extends Scope use SideServer; use GraphQLBase; -// public function testCreateDatabase(): array -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$CREATE_DATABASE); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => 'actors', -// 'name' => 'Actors', -// ] -// ]; -// -// $database = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertIsArray($database['body']['data']); -// $this->assertArrayNotHasKey('errors', $database['body']); -// $database = $database['body']['data']['databasesCreate']; -// $this->assertEquals('Actors', $database['name']); -// -// return $database; -// } -// -// /** -// * @depends testCreateDatabase -// */ -// public function testCreateCollection($database): array -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$CREATE_COLLECTION); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $database['_id'], -// 'collectionId' => 'actors', -// 'name' => 'Actors', -// 'permission' => 'collection', -// 'read' => ['role:all'], -// 'write' => ['role:member'], -// ] -// ]; -// -// $collection = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertIsArray($collection['body']['data']); -// $this->assertArrayNotHasKey('errors', $collection['body']); -// $collection = $collection['body']['data']['databasesCreateCollection']; -// $this->assertEquals('Actors', $collection['name']); -// -// return [ -// 'database' => $database, -// 'collection' => $collection, -// ]; -// } -// -// /** -// * @depends testCreateCollection -// * @throws Exception -// */ -// public function testCreateStringAttribute($data): array -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$CREATE_STRING_ATTRIBUTE); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// 'key' => 'name', -// 'size' => 256, -// 'required' => true, -// ] -// ]; -// -// $attribute = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $attribute['body']); -// $this->assertIsArray($attribute['body']['data']); -// $this->assertIsArray($attribute['body']['data']['databasesCreateStringAttribute']); -// -// return $data; -// } -// -// /** -// * @depends testCreateCollection -// * @throws Exception -// */ -// public function testCreateIntegerAttribute($data): array -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$CREATE_INTEGER_ATTRIBUTE); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// 'key' => 'age', -// 'min' => 18, -// 'max' => 150, -// 'required' => true, -// ] -// ]; -// -// $attribute = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $attribute['body']); -// $this->assertIsArray($attribute['body']['data']); -// $this->assertIsArray($attribute['body']['data']['databasesCreateIntegerAttribute']); -// -// return $data; -// } -// -// /** -// * @depends testCreateCollection -// * @throws Exception -// */ -// public function testCreateBooleanAttribute($data): array -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$CREATE_BOOLEAN_ATTRIBUTE); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// 'key' => 'alive', -// 'required' => true, -// ] -// ]; -// -// $attribute = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $attribute['body']); -// $this->assertIsArray($attribute['body']['data']); -// $this->assertIsArray($attribute['body']['data']['databasesCreateBooleanAttribute']); -// -// return $data; -// } -// -// /** -// * @depends testCreateCollection -// * @throws Exception -// */ -// public function testCreateFloatAttribute($data): array -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$CREATE_FLOAT_ATTRIBUTE); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// 'key' => 'salary', -// 'min' => 1000.0, -// 'max' => 999999.99, -// 'default' => 1000.0, -// 'required' => false, -// ] -// ]; -// -// $attribute = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $attribute['body']); -// $this->assertIsArray($attribute['body']['data']); -// $this->assertIsArray($attribute['body']['data']['databasesCreateFloatAttribute']); -// -// return $data; -// } -// -// /** -// * @depends testCreateCollection -// * @throws Exception -// */ -// public function testCreateEmailAttribute($data): array -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$CREATE_EMAIL_ATTRIBUTE); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// 'key' => 'email', -// 'required' => true, -// ] -// ]; -// -// $attribute = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $attribute['body']); -// $this->assertIsArray($attribute['body']['data']); -// $this->assertIsArray($attribute['body']['data']['databasesCreateEmailAttribute']); -// -// return $data; -// } -// -// /** -// * @depends testCreateCollection -// * @throws Exception -// */ -// public function testCreateEnumAttribute($data): array -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$CREATE_ENUM_ATTRIBUTE); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// 'key' => 'role', -// 'elements' => [ -// 'crew', -// 'actor', -// 'guest', -// ], -// 'required' => true, -// ] -// ]; -// -// $attribute = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $attribute['body']); -// $this->assertIsArray($attribute['body']['data']); -// $this->assertIsArray($attribute['body']['data']['databasesCreateEnumAttribute']); -// -// return $data; -// } -// -// /** -// * @depends testCreateCollection -// * @throws Exception -// */ -// public function testCreateIPAttribute($data): array -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$CREATE_IP_ATTRIBUTE); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// 'key' => 'ip', -// 'required' => false, -// 'default' => '::1', -// ] -// ]; -// -// $attribute = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $attribute['body']); -// $this->assertIsArray($attribute['body']['data']); -// $this->assertIsArray($attribute['body']['data']['databasesCreateIpAttribute']); -// -// return $data; -// } -// -// /** -// * @throws Exception -// */ -// public function testCreateURLAttribute($data): array -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$CREATE_URL_ATTRIBUTE); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// 'key' => 'url', -// 'required' => false, -// 'default' => 'https://appwrite.io', -// ] -// ]; -// -// $attribute = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $attribute['body']); -// $this->assertIsArray($attribute['body']['data']); -// $this->assertIsArray($attribute['body']['data']['databasesCreateUrlAttribute']); -// -// return $data; -// } -// -// /** -// * @depends testCreateStringAttribute -// * @depends testCreateIntegerAttribute -// * @throws Exception -// */ -// public function testCreateIndex($data1, $data2): array -// { -// // Wait for attributes to be available -// sleep(10); -// -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$CREATE_INDEX); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data2['database']['_id'], -// 'collectionId' => $data2['collection']['_id'], -// 'key' => 'index', -// 'type' => 'key', -// 'attributes' => [ -// 'name', -// 'age', -// ], -// ] -// ]; -// -// $index = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// \var_dump($index); -// -// $this->assertArrayNotHasKey('errors', $index['body']); -// $this->assertIsArray($index['body']['data']); -// $this->assertIsArray($index['body']['data']['databasesCreateIndex']); -// -// return $data2; -// } -// -// /** -// * @depends testCreateStringAttribute -// * @throws \Exception -// */ -// public function testCreateDocument($data): array -// { -// // Wait for attributes to be available -// sleep(3); -// -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$CREATE_DOCUMENT); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// 'documentId' => 'unique()', -// 'data' => [ -// 'name' => 'John Doe', -// 'email' => 'example@appwrite.io', -// 'age' => 30, -// 'alive' => true, -// 'salary' => 9999.9, -// 'role' => 'crew', -// ], -// 'read' => ['role:all'], -// 'write' => ['role:all'], -// ] -// ]; -// -// $document = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $document['body']); -// $this->assertIsArray($document['body']['data']); -// -// $document = $document['body']['data']['databasesCreateDocument']; -// $this->assertIsArray($document); -// -// return [ -// 'database' => $data['database'], -// 'collection' => $data['collection'], -// 'document' => $document, -// ]; -// } -// -// /** -// * @depends testCreateCollection -// * @throws Exception -// */ -// public function testCreateCustomEntity($data): void -// { -// // Wait for attributes to be available -// sleep(3); -// -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$CREATE_CUSTOM_ENTITY); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'name' => 'John Doe', -// 'age' => 35, -// 'alive' => true, -// 'salary' => 9999.9, -// ] -// ]; -// -// $actor = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// //\var_dump($actor); -// -// $this->assertArrayNotHasKey('errors', $actor['body']); -// $this->assertIsArray($actor['body']['data']); -// $this->assertIsArray($actor['body']['data']['actorCreate']); -// } -// -// public function testGetDatabases(): void -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$GET_DATABASES); -// $gqlPayload = [ -// 'query' => $query, -// ]; -// -// $databases = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $databases['body']); -// $this->assertIsArray($databases['body']['data']); -// $this->assertIsArray($databases['body']['data']['databasesList']); -// } -// -// /** -// * @depends testCreateDatabase -// * @throws Exception -// */ -// public function testGetDatabase($database): void -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$GET_DATABASE); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $database['_id'], -// ] -// ]; -// -// $database = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $database['body']); -// $this->assertIsArray($database['body']['data']); -// $this->assertIsArray($database['body']['data']['databasesGet']); -// } -// -// /** -// * @depends testCreateCollection -// * @throws Exception -// */ -// public function testGetCollections($data): void -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$GET_COLLECTIONS); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// ] -// ]; -// -// $collections = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $collections['body']); -// $this->assertIsArray($collections['body']['data']); -// $this->assertIsArray($collections['body']['data']['databasesListCollections']); -// } -// -// /** -// * @depends testCreateCollection -// * @throws Exception -// */ -// public function testGetCollection($data): void -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$GET_COLLECTION); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// ] -// ]; -// -// $collection = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $collection['body']); -// $this->assertIsArray($collection['body']['data']); -// $this->assertIsArray($collection['body']['data']['databasesGetCollection']); -// } -// -// /** -// * @depends testCreateStringAttribute -// * @depends testCreateIntegerAttribute -// * @throws Exception -// */ -// public function testGetAttributes($data): void -// { -// // Wait for attributes to be available -// sleep(5); -// -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$GET_ATTRIBUTES); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// ] -// ]; -// -// $attributes = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// \var_dump($attributes); -// -// $this->assertArrayNotHasKey('errors', $attributes['body']); -// $this->assertIsArray($attributes['body']['data']); -// $this->assertIsArray($attributes['body']['data']['databasesListAttributes']); -// } -// -// /** -// * @depends testCreateCollection -// * @throws Exception -// */ -// public function testGetAttribute($data): void -// { -// // Wait for attributes to be available -// sleep(3); -// -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$GET_ATTRIBUTE); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// 'key' => 'name', -// ] -// ]; -// -// $attribute = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $attribute['body']); -// $this->assertIsArray($attribute['body']['data']); -// $this->assertIsArray($attribute['body']['data']['databasesGetAttribute']); -// } -// -// /** -// * @depends testCreateCollection -// * @throws Exception -// */ -// public function testGetIndexes($data): void -// { -// // Wait for attributes to be available -// sleep(3); -// -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$GET_INDEXES); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// ] -// ]; -// -// $indices = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $indices['body']); -// $this->assertIsArray($indices['body']['data']); -// $this->assertIsArray($indices['body']['data']['databasesGetIndices']); -// } -// -// /** -// * @depends testCreateCollection -// * @throws Exception -// */ -// public function testGetIndex($data): void -// { -// // Wait for attributes to be available -// sleep(3); -// -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$GET_INDEX); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// 'key' => 'index', -// ] -// ]; -// -// $index = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $index['body']); -// $this->assertIsArray($index['body']['data']); -// $this->assertIsArray($index['body']['data']['databasesGetIndex']); -// } -// -// /** -// * @depends testCreateCollection -// * @throws Exception -// */ -// public function testGetDocuments($data): void -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$GET_DOCUMENTS); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// ] -// ]; -// -// $documents = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $documents['body']); -// $this->assertIsArray($documents['body']['data']); -// $this->assertIsArray($documents['body']['data']['databasesListDocuments']); -// } -// -// /** -// * @depends testCreateDocument -// * @throws Exception -// */ -// public function testGetDocument($data): void -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$GET_DOCUMENT); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// 'documentId' => $data['document']['_id'], -// ] -// ]; -// -// $document = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $document['body']); -// $this->assertIsArray($document['body']['data']); -// $this->assertIsArray($document['body']['data']['databasesGetDocument']); -// } -// -// /** -// * @depends testCreateDatabase -// * @throws Exception -// */ -// public function testUpdateDatabase($database) -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$UPDATE_DATABASE); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $database['_id'], -// 'name' => 'New Database Name', -// ] -// ]; -// -// $database = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $database['body']); -// $this->assertIsArray($database['body']['data']); -// $this->assertIsArray($database['body']['data']['databasesUpdate']); -// } -// -// /** -// * @depends testCreateCollection -// * @throws Exception -// */ -// public function testUpdateCollection($data) -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$UPDATE_COLLECTION); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// 'name' => 'New Collection Name', -// 'permission' => 'collection', -// ] -// ]; -// -// $collection = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $collection['body']); -// $this->assertIsArray($collection['body']['data']); -// $this->assertIsArray($collection['body']['data']['databasesUpdateCollection']); -// } -// -// /** -// * @depends testCreateDocument -// * @throws Exception -// */ -// public function testUpdateDocument($data): void -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$UPDATE_DOCUMENT); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// 'documentId' => $data['document']['_id'], -// 'data' => [ -// 'name' => 'New Document Name', -// ], -// ] -// ]; -// -// $document = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertArrayNotHasKey('errors', $document['body']); -// $this->assertIsArray($document['body']['data']); -// $document = $document['body']['data']['databasesUpdateDocument']; -// $this->assertIsArray($document); -// $this->assertEquals('New Document Name', $document['name']); -// } + public function testCreateDatabase(): array + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$CREATE_DATABASE); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => 'actors', + 'name' => 'Actors', + ] + ]; + + $database = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertIsArray($database['body']['data']); + $this->assertArrayNotHasKey('errors', $database['body']); + $database = $database['body']['data']['databasesCreate']; + $this->assertEquals('Actors', $database['name']); + + return $database; + } + + /** + * @depends testCreateDatabase + */ + public function testCreateCollection($database): array + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$CREATE_COLLECTION); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $database['_id'], + 'collectionId' => 'actors', + 'name' => 'Actors', + 'permission' => 'collection', + 'read' => ['role:all'], + 'write' => ['role:member'], + ] + ]; + + $collection = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertIsArray($collection['body']['data']); + $this->assertArrayNotHasKey('errors', $collection['body']); + $collection = $collection['body']['data']['databasesCreateCollection']; + $this->assertEquals('Actors', $collection['name']); + + return [ + 'database' => $database, + 'collection' => $collection, + ]; + } + + /** + * @depends testCreateCollection + * @throws Exception + */ + public function testCreateStringAttribute($data): array + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$CREATE_STRING_ATTRIBUTE); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + 'key' => 'name', + 'size' => 256, + 'required' => true, + ] + ]; + + $attribute = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $attribute['body']); + $this->assertIsArray($attribute['body']['data']); + $this->assertIsArray($attribute['body']['data']['databasesCreateStringAttribute']); + + return $data; + } + + /** + * @depends testCreateCollection + * @throws Exception + */ + public function testCreateIntegerAttribute($data): array + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$CREATE_INTEGER_ATTRIBUTE); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + 'key' => 'age', + 'min' => 18, + 'max' => 150, + 'required' => true, + ] + ]; + + $attribute = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $attribute['body']); + $this->assertIsArray($attribute['body']['data']); + $this->assertIsArray($attribute['body']['data']['databasesCreateIntegerAttribute']); + + return $data; + } + + /** + * @depends testCreateCollection + * @throws Exception + */ + public function testCreateBooleanAttribute($data): array + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$CREATE_BOOLEAN_ATTRIBUTE); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + 'key' => 'alive', + 'required' => true, + ] + ]; + + $attribute = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $attribute['body']); + $this->assertIsArray($attribute['body']['data']); + $this->assertIsArray($attribute['body']['data']['databasesCreateBooleanAttribute']); + + return $data; + } + + /** + * @depends testCreateCollection + * @throws Exception + */ + public function testCreateFloatAttribute($data): array + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$CREATE_FLOAT_ATTRIBUTE); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + 'key' => 'salary', + 'min' => 1000.0, + 'max' => 999999.99, + 'default' => 1000.0, + 'required' => false, + ] + ]; + + $attribute = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $attribute['body']); + $this->assertIsArray($attribute['body']['data']); + $this->assertIsArray($attribute['body']['data']['databasesCreateFloatAttribute']); + + return $data; + } + + /** + * @depends testCreateCollection + * @throws Exception + */ + public function testCreateEmailAttribute($data): array + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$CREATE_EMAIL_ATTRIBUTE); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + 'key' => 'email', + 'required' => true, + ] + ]; + + $attribute = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $attribute['body']); + $this->assertIsArray($attribute['body']['data']); + $this->assertIsArray($attribute['body']['data']['databasesCreateEmailAttribute']); + + return $data; + } + + /** + * @depends testCreateCollection + * @throws Exception + */ + public function testCreateEnumAttribute($data): array + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$CREATE_ENUM_ATTRIBUTE); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + 'key' => 'role', + 'elements' => [ + 'crew', + 'actor', + 'guest', + ], + 'required' => true, + ] + ]; + + $attribute = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $attribute['body']); + $this->assertIsArray($attribute['body']['data']); + $this->assertIsArray($attribute['body']['data']['databasesCreateEnumAttribute']); + + return $data; + } + + /** + * @depends testCreateCollection + * @throws Exception + */ + public function testCreateIPAttribute($data): array + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$CREATE_IP_ATTRIBUTE); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + 'key' => 'ip', + 'required' => false, + 'default' => '::1', + ] + ]; + + $attribute = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $attribute['body']); + $this->assertIsArray($attribute['body']['data']); + $this->assertIsArray($attribute['body']['data']['databasesCreateIpAttribute']); + + return $data; + } + + /** + * @depends testCreateCollection + * @throws Exception + */ + public function testCreateURLAttribute($data): array + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$CREATE_URL_ATTRIBUTE); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + 'key' => 'url', + 'required' => false, + 'default' => 'https://appwrite.io', + ] + ]; + + $attribute = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $attribute['body']); + $this->assertIsArray($attribute['body']['data']); + $this->assertIsArray($attribute['body']['data']['databasesCreateUrlAttribute']); + + return $data; + } + + /** + * @depends testCreateStringAttribute + * @depends testCreateIntegerAttribute + * @throws Exception + */ + public function testCreateIndex($data): array + { + // Wait for attributes to be available + sleep(3); + + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$CREATE_INDEX); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + 'key' => 'index', + 'type' => 'key', + 'attributes' => [ + 'name', + 'age', + ], + ] + ]; + + $index = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $index['body']); + $this->assertIsArray($index['body']['data']); + $this->assertIsArray($index['body']['data']['databasesCreateIndex']); + + return [ + 'database' => $data['database'], + 'collection' => $data['collection'], + 'index' => $index['body']['data']['databasesCreateIndex'], + ]; + } + + /** + * @depends testCreateStringAttribute + * @throws Exception + */ + public function testCreateDocument($data): array + { + // Wait for attributes to be available + sleep(3); + + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$CREATE_DOCUMENT); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + 'documentId' => 'unique()', + 'data' => [ + 'name' => 'John Doe', + 'email' => 'example@appwrite.io', + 'age' => 30, + 'alive' => true, + 'salary' => 9999.9, + 'role' => 'crew', + ], + 'read' => ['role:all'], + 'write' => ['role:all'], + ] + ]; + + $document = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $document['body']); + $this->assertIsArray($document['body']['data']); + + $document = $document['body']['data']['databasesCreateDocument']; + $this->assertIsArray($document); + + return [ + 'database' => $data['database'], + 'collection' => $data['collection'], + 'document' => $document, + ]; + } + + /** + * @depends testCreateStringAttribute + * @depends testCreateIntegerAttribute + * @depends testCreateBooleanAttribute + * @depends testCreateFloatAttribute + * @depends testCreateEmailAttribute + * @depends testCreateEnumAttribute + * @throws Exception + */ + public function testCreateCustomEntity(): void + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$CREATE_CUSTOM_ENTITY); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'name' => 'John Doe', + 'age' => 35, + 'alive' => true, + 'salary' => 9999.9, + 'email' => 'johndoe@appwrite.io', + 'role' => 'crew', + ] + ]; + + $actor = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + \var_dump($actor); + + $this->assertArrayNotHasKey('errors', $actor['body']); + $this->assertIsArray($actor['body']['data']); + $this->assertIsArray($actor['body']['data']['actorsCreate']); + } + + public function testGetDatabases(): void + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$GET_DATABASES); + $gqlPayload = [ + 'query' => $query, + ]; + + $databases = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $databases['body']); + $this->assertIsArray($databases['body']['data']); + $this->assertIsArray($databases['body']['data']['databasesList']); + } + + /** + * @depends testCreateDatabase + * @throws Exception + */ + public function testGetDatabase($database): void + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$GET_DATABASE); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $database['_id'], + ] + ]; + + $database = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $database['body']); + $this->assertIsArray($database['body']['data']); + $this->assertIsArray($database['body']['data']['databasesGet']); + } + + /** + * @depends testCreateCollection + * @throws Exception + */ + public function testGetCollections($data): void + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$GET_COLLECTIONS); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + ] + ]; + + $collections = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $collections['body']); + $this->assertIsArray($collections['body']['data']); + $this->assertIsArray($collections['body']['data']['databasesListCollections']); + } + + /** + * @depends testCreateCollection + * @throws Exception + */ + public function testGetCollection($data): void + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$GET_COLLECTION); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + ] + ]; + + $collection = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $collection['body']); + $this->assertIsArray($collection['body']['data']); + $this->assertIsArray($collection['body']['data']['databasesGetCollection']); + } + + /** + * @depends testCreateStringAttribute + * @depends testCreateIntegerAttribute + * @throws Exception + */ + public function testGetAttributes($data): void + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$GET_ATTRIBUTES); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + ] + ]; + + $attributes = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $attributes['body']); + $this->assertIsArray($attributes['body']['data']); + $this->assertIsArray($attributes['body']['data']['databasesListAttributes']); + } + + /** + * @depends testCreateCollection + * @throws Exception + */ + public function testGetAttribute($data): void + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$GET_ATTRIBUTE); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + 'key' => 'name', + ] + ]; + + $attribute = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $attribute['body']); + $this->assertIsArray($attribute['body']['data']); + $this->assertIsArray($attribute['body']['data']['databasesGetAttribute']); + } + + /** + * @depends testCreateIndex + * @throws Exception + */ + public function testGetIndexes($data): void + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$GET_INDEXES); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + ] + ]; + + $indices = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $indices['body']); + $this->assertIsArray($indices['body']['data']); + $this->assertIsArray($indices['body']['data']['databasesListIndexes']); + } + + /** + * @depends testCreateIndex + * @throws Exception + */ + public function testGetIndex($data): void + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$GET_INDEX); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + 'key' => $data['index']['key'], + ] + ]; + + $index = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $index['body']); + $this->assertIsArray($index['body']['data']); + $this->assertIsArray($index['body']['data']['databasesGetIndex']); + } + + /** + * @depends testCreateCollection + * @throws Exception + */ + public function testGetDocuments($data): void + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$GET_DOCUMENTS); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + ] + ]; + + $documents = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $documents['body']); + $this->assertIsArray($documents['body']['data']); + $this->assertIsArray($documents['body']['data']['databasesListDocuments']); + } + + /** + * @depends testCreateDocument + * @throws Exception + */ + public function testGetDocument($data): void + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$GET_DOCUMENT); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + 'documentId' => $data['document']['_id'], + ] + ]; + + $document = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $document['body']); + $this->assertIsArray($document['body']['data']); + $this->assertIsArray($document['body']['data']['databasesGetDocument']); + } // /** -// * @depends testCreateDatabase +// * @depends testCreateCustomEntity // * @throws Exception // */ -// public function testDeleteDatabase($database) +// public function testGetCustomEntity($data) // { // $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$DELETE_DATABASE); +// $query = $this->getQuery(self::$GET_CUSTOM_ENTITY); // $gqlPayload = [ // 'query' => $query, // 'variables' => [ -// 'databaseId' => $database['_id'], +// 'id' => $data['entity']['_id'], // ] // ]; // -// $database = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ +// $entity = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ // 'content-type' => 'application/json', // 'x-appwrite-project' => $projectId, // ], $this->getHeaders()), $gqlPayload); // -// $this->assertEquals(204, $database['headers']['status-code']); +// $this->assertArrayNotHasKey('errors', $entity['body']); +// $this->assertIsArray($entity['body']['data']); +// $this->assertIsArray($entity['body']['data']['actorGet']); // } -// -// /** -// * @depends testCreateDatabase -// * @depends testCreateCollection -// * @throws Exception -// */ -// public function testDeleteCollection($database, $collection) -// { -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$DELETE_COLLECTION); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $database['_id'], -// 'collectionId' => $collection['_id'], -// ] -// ]; -// -// $collection = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertEquals(204, $collection['headers']['status-code']); -// } -// -// /** -// * @depends testCreateStringAttribute -// * @throws Exception -// */ -// public function testDeleteAttribute($datab): void -// { -// // Wait for attributes to be available -// sleep(3); -// -// $projectId = $this->getProject()['$id']; -// $query = $this->getQuery(self::$DELETE_ATTRIBUTE); -// $gqlPayload = [ -// 'query' => $query, -// 'variables' => [ -// 'databaseId' => $data['database']['_id'], -// 'collectionId' => $data['collection']['_id'], -// 'key' => 'name', -// ] -// ]; -// -// $attribute = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ -// 'content-type' => 'application/json', -// 'x-appwrite-project' => $projectId, -// ], $this->getHeaders()), $gqlPayload); -// -// $this->assertEquals(204, $attribute['headers']['status-code']); -// } -// + + /** + * @depends testCreateDatabase + * @throws Exception + */ + public function testUpdateDatabase($database) + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$UPDATE_DATABASE); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $database['_id'], + 'name' => 'New Database Name', + ] + ]; + + $database = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $database['body']); + $this->assertIsArray($database['body']['data']); + $this->assertIsArray($database['body']['data']['databasesUpdate']); + } + + /** + * @depends testCreateCollection + * @throws Exception + */ + public function testUpdateCollection($data) + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$UPDATE_COLLECTION); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + 'name' => 'New Collection Name', + 'permission' => 'collection', + ] + ]; + + $collection = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $collection['body']); + $this->assertIsArray($collection['body']['data']); + $this->assertIsArray($collection['body']['data']['databasesUpdateCollection']); + } + + /** + * @depends testCreateDocument + * @throws Exception + */ + public function testUpdateDocument($data): void + { + $projectId = $this->getProject()['$id']; + $query = $this->getQuery(self::$UPDATE_DOCUMENT); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'databaseId' => $data['database']['_id'], + 'collectionId' => $data['collection']['_id'], + 'documentId' => $data['document']['_id'], + 'data' => [ + 'name' => 'New Document Name', + ], + ] + ]; + + $document = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertArrayNotHasKey('errors', $document['body']); + $this->assertIsArray($document['body']['data']); + $document = $document['body']['data']['databasesUpdateDocument']; + $this->assertIsArray($document); + $this->assertStringContainsString('New Document Name', $document['data']); + } + // /** // * @depends testCreateDocument // * @throws Exception @@ -894,11 +836,86 @@ class GraphQLDatabaseServerTest extends Scope // ] // ]; // -// $document = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ +// $document = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ // 'content-type' => 'application/json', // 'x-appwrite-project' => $projectId, // ], $this->getHeaders()), $gqlPayload); // // $this->assertEquals(204, $document['headers']['status-code']); // } +// +// /** +// * @depends testCreateStringAttribute +// * @throws Exception +// */ +// public function testDeleteAttribute($data): void +// { +// // Wait for attributes to be available +// sleep(3); +// +// $projectId = $this->getProject()['$id']; +// $query = $this->getQuery(self::$DELETE_ATTRIBUTE); +// $gqlPayload = [ +// 'query' => $query, +// 'variables' => [ +// 'databaseId' => $data['database']['_id'], +// 'collectionId' => $data['collection']['_id'], +// 'key' => 'name', +// ] +// ]; +// +// $attribute = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ +// 'content-type' => 'application/json', +// 'x-appwrite-project' => $projectId, +// ], $this->getHeaders()), $gqlPayload); +// +// $this->assertEquals(204, $attribute['headers']['status-code']); +// } +// +// /** +// * @depends testCreateCollection +// * @throws Exception +// */ +// public function testDeleteCollection($data) +// { +// $projectId = $this->getProject()['$id']; +// $query = $this->getQuery(self::$DELETE_COLLECTION); +// $gqlPayload = [ +// 'query' => $query, +// 'variables' => [ +// 'databaseId' => $data['database']['_id'], +// 'collectionId' => $data['collection']['_id'], +// ] +// ]; +// +// $collection = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ +// 'content-type' => 'application/json', +// 'x-appwrite-project' => $projectId, +// ], $this->getHeaders()), $gqlPayload); +// +// $this->assertEquals(204, $collection['headers']['status-code']); +// } +// +// /** +// * @depends testCreateDatabase +// * @throws Exception +// */ +// public function testDeleteDatabase($database) +// { +// $projectId = $this->getProject()['$id']; +// $query = $this->getQuery(self::$DELETE_DATABASE); +// $gqlPayload = [ +// 'query' => $query, +// 'variables' => [ +// 'databaseId' => $database['_id'], +// ] +// ]; +// +// $database = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ +// 'content-type' => 'application/json', +// 'x-appwrite-project' => $projectId, +// ], $this->getHeaders()), $gqlPayload); +// +// $this->assertEquals(204, $database['headers']['status-code']); +// } }