getProject()['$id']; $key = ''; $query = $this->getQuery(self::$CREATE_COLLECTION); $collectionAttrs = [ 'collectionId' => 'actors', 'name' => 'Actors', 'permission' => 'collection', 'read' => ['role:all'], 'write' => ['role:member', 'role:admin'], ]; $gqlPayload = [ 'query' => $query, 'variables' => $collectionAttrs ]; $actors = $this->client->call(Client::METHOD_POST, '/graphql', [ 'origin' => 'http://localhost', 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, 'x-appwrite-key' => $key ], $gqlPayload); $errorMessage = 'User (role: guest) missing scope (collections.write)'; $this->assertEquals($actors['headers']['status-code'], 401); $this->assertEquals($actors['body']['errors'][0]['message'], $errorMessage); $this->assertIsArray($actors['body']['data']); $this->assertNull($actors['body']['data']['databaseCreateCollection']); $key = $this->createKey('test', ['collections.write']); $actors = $this->client->call(Client::METHOD_POST, '/graphql', [ 'origin' => 'http://localhost', 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, 'x-appwrite-key' => $key ], $gqlPayload); $this->assertEquals($actors['headers']['status-code'], 201); $this->assertNull($actors['body']['errors']); $this->assertIsArray($actors['body']['data']); $data = $actors['body']['data']['databaseCreateCollection']; $this->assertEquals('Actors', $data['name']); $this->assertArrayHasKey('id', $data); $this->assertArrayHasKey('permissions', $data); $this->assertContains('role:all', $data['read']); $this->assertContains('role:member', $data['write']); $this->assertContains('role:admin', $data['write']); return [ 'collectionId' => $data['id'], 'key' => $key ]; } /** * @depends testCreateCollection * @throws \Exception */ public function testCreateStringAttribute(array $data) { $projectId = $this->getProject()['$id']; $key = $data['key']; $query = $this->getQuery(self::$CREATE_STRING_ATTRIBUTE); $attributeAttrs = [ 'collectionId' => $data['collectionId'], 'key' => 'name', 'size' => 256, 'required' => true, ]; $gqlPayload = [ 'query' => $query, 'variables' => $attributeAttrs ]; $attribute = $this->client->call(Client::METHOD_POST, '/graphql', [ 'origin' => 'http://localhost', 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, 'x-appwrite-key' => $key ], $gqlPayload); $this->assertNull($attribute['body']['errors']); $this->assertIsArray($attribute['body']['data']); $this->assertIsArray($attribute['body']['data']['databaseCreateStringAttribute']); } /** * @depends testCreateCollection * @throws \Exception */ public function testCreateIntegerAttribute(array $data) { $projectId = $this->getProject()['$id']; $key = $data['key']; $query = $this->getQuery(self::$CREATE_INTEGER_ATTRIBUTE); $attributeAttrs = [ 'collectionId' => $data['collectionId'], 'key' => 'age', 'min' => 18, 'max' => 99, 'required' => true, ]; $gqlPayload = [ 'query' => $query, 'variables' => $attributeAttrs ]; $attribute = $this->client->call(Client::METHOD_POST, '/graphql', [ 'origin' => 'http://localhost', 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, 'x-appwrite-key' => $key ], $gqlPayload); $this->assertNull($attribute['body']['errors']); $this->assertIsArray($attribute['body']['data']); $this->assertIsArray($attribute['body']['data']['databaseCreateIntegerAttribute']); } /** * @depends testCreateCollection * @throws \Exception */ public function testCreateBooleanAttribute(array $data) { $projectId = $this->getProject()['$id']; $key = $data['key']; $query = $this->getQuery(self::$CREATE_BOOLEAN_ATTRIBUTE); $attributeAttrs = [ 'collectionId' => $data['collectionId'], 'key' => 'alive', 'required' => true, ]; $gqlPayload = [ 'query' => $query, 'variables' => $attributeAttrs ]; $attribute = $this->client->call(Client::METHOD_POST, '/graphql', [ 'origin' => 'http://localhost', 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, 'x-appwrite-key' => $key ], $gqlPayload); $this->assertNull($attribute['body']['errors']); $this->assertIsArray($attribute['body']['data']); $this->assertIsArray($attribute['body']['data']['databaseCreateBooleanAttribute']); } /** * @depends testCreateCollection * @throws \Exception */ public function testCreateFloatAttribute(array $data) { $projectId = $this->getProject()['$id']; $key = $data['key']; $query = $this->getQuery(self::$CREATE_FLOAT_ATTRIBUTE); $attributeAttrs = [ 'collectionId' => $data['collectionId'], 'key' => 'salary', 'min' => 1000.0, 'max' => 999999.99, 'default' => 1000.0, 'required' => false, ]; $gqlPayload = [ 'query' => $query, 'variables' => $attributeAttrs ]; $attribute = $this->client->call(Client::METHOD_POST, '/graphql', [ 'origin' => 'http://localhost', 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, 'x-appwrite-key' => $key ], $gqlPayload); $this->assertNull($attribute['body']['errors']); $this->assertIsArray($attribute['body']['data']); $this->assertIsArray($attribute['body']['data']['databaseCreateFloatAttribute']); } /** * @depends testCreateCollection * @depends testCreateStringAttribute * @depends testCreateIntegerAttribute * @depends testCreateBooleanAttribute * @depends testCreateFloatAttribute * @throws \Exception */ public function testCreateDocumentREST(array $data) { $projectId = $this->getProject()['$id']; $key = $data['key']; $query = $this->getQuery(self::$CREATE_DOCUMENT_REST); $documentAttrs = [ 'collectionId' => $data['collectionId'], 'data' => [ 'name' => 'John Doe', 'age' => 30, 'alive' => true, 'salary' => 9999.5 ] ]; $gqlPayload = [ 'query' => $query, 'variables' => $documentAttrs ]; $document = $this->client->call(Client::METHOD_POST, '/graphql', [ 'origin' => 'http://localhost', 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, 'x-appwrite-key' => $key ], $gqlPayload); $this->assertNull($document['body']['errors']); $this->assertIsArray($document['body']['data']); $this->assertIsArray($document['body']['data']['databaseCreateDocument']); } /** * @depends testCreateCollection * @depends testCreateStringAttribute * @depends testCreateIntegerAttribute * @depends testCreateBooleanAttribute * @depends testCreateFloatAttribute * @throws \Exception */ public function testCreateDocumentGQL(array $data) { $projectId = $this->getProject()['$id']; $key = ''; $query = $this->getQuery(self::$CREATE_DOCUMENT_GQL_HOOKS); $documentAttrs = [ 'name' => 'John Doe', 'age' => 30, 'alive' => true, 'salary' => 9999.5, ]; $gqlPayload = [ 'query' => $query, 'variables' => $documentAttrs ]; $document = $this->client->call(Client::METHOD_POST, '/graphql', [ 'origin' => 'http://localhost', 'content-type' => 'application/json', 'x-appwrite-project' => $projectId, 'x-appwrite-key' => $key ], $gqlPayload); $this->assertNull($document['body']['errors']); $this->assertIsArray($document['body']['data']); $this->assertIsArray($document['body']['data']['actorCreate']); } }