client->call(Client::METHOD_POST, '/database/collections', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ]), [ 'name' => 'Test 1', 'collectionId' => 'first', 'read' => ['role:all'], 'write' => ['role:all'], ]); $test2 = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ]), [ 'name' => 'Test 2', 'collectionId' => 'second', 'read' => ['role:all'], 'write' => ['role:all'], ]); $collections = $this->client->call(Client::METHOD_GET, '/database/collections', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders())); $this->assertEquals(2, $collections['body']['sum']); $this->assertEquals($test1['body']['$id'], $collections['body']['collections'][0]['$id']); $this->assertEquals($test2['body']['$id'], $collections['body']['collections'][1]['$id']); /** * Test for Order */ $base = array_reverse($collections['body']['collections']); $collections = $this->client->call(Client::METHOD_GET, '/database/collections', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'orderType' => 'DESC' ]); $this->assertEquals(2, $collections['body']['sum']); $this->assertEquals($base[0]['$id'], $collections['body']['collections'][0]['$id']); $this->assertEquals($base[1]['$id'], $collections['body']['collections'][1]['$id']); /** * Test for After */ $base = $this->client->call(Client::METHOD_GET, '/database/collections', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders())); $collections = $this->client->call(Client::METHOD_GET, '/database/collections', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'after' => $base['body']['collections'][0]['$id'] ]); $this->assertCount(1, $collections['body']['collections']); $this->assertEquals($base['body']['collections'][1]['$id'], $collections['body']['collections'][0]['$id']); $collections = $this->client->call(Client::METHOD_GET, '/database/collections', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'after' => $base['body']['collections'][1]['$id'] ]); $this->assertCount(0, $collections['body']['collections']); $this->assertEmpty($collections['body']['collections']); } public function testDeleteAttribute(): array { /** * Test for SUCCESS */ // Create collection $actors = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ]), [ 'collectionId' => 'unique()', 'name' => 'Actors', 'read' => ['role:all'], 'write' => ['role:all'], ]); $this->assertEquals($actors['headers']['status-code'], 201); $this->assertEquals($actors['body']['name'], 'Actors'); $firstName = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/attributes/string', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ]), [ 'attributeId' => 'firstName', 'size' => 256, 'required' => true, ]); $lastName = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/attributes/string', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ]), [ 'attributeId' => 'lastName', 'size' => 256, 'required' => true, ]); $unneeded = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/attributes/string', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ]), [ 'attributeId' => 'unneeded', 'size' => 256, 'required' => true, ]); // Wait for database worker to finish creating attributes sleep(5); $index = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/indexes', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ]), [ 'indexId' => 'key_lastName', 'type' => 'key', 'attributes' => [ 'lastName', ], ]); // Wait for database worker to finish creating index sleep(5); $collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $actors['body']['$id'], array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ]), []); $unneededId = $unneeded['body']['$id']; $this->assertEquals($collection['body']['$id'], $firstName['body']['$collection']); $this->assertEquals($collection['body']['$id'], $lastName['body']['$collection']); $this->assertIsArray($collection['body']['attributes']); $this->assertCount(3, $collection['body']['attributes']); $this->assertEquals($collection['body']['attributes'][0]['$id'], $firstName['body']['$id']); $this->assertEquals($collection['body']['attributes'][1]['$id'], $lastName['body']['$id']); $this->assertEquals($collection['body']['attributes'][2]['$id'], $unneeded['body']['$id']); $this->assertCount(1, $collection['body']['indexes']); $this->assertEquals($collection['body']['indexes'][0]['$id'], $index['body']['$id']); // Delete attribute $this->client->call(Client::METHOD_DELETE, '/database/collections/' . $actors ['body']['$id'] . '/attributes/' . $unneededId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ])); sleep(5); $collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $actors['body']['$id'], array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ]), []); $this->assertEquals($collection['body']['$id'], $firstName['body']['$collection']); $this->assertEquals($collection['body']['$id'], $lastName['body']['$collection']); $this->assertIsArray($collection['body']['attributes']); $this->assertCount(2, $collection['body']['attributes']); $this->assertEquals($collection['body']['attributes'][0]['$id'], $firstName['body']['$id']); $this->assertEquals($collection['body']['attributes'][1]['$id'], $lastName['body']['$id']); return [ 'collectionId' => $actors['body']['$id'], 'indexId' => $index['body']['$id'], ]; } /** * @depends testDeleteAttribute */ public function testDeleteIndex($data): array { $index = $this->client->call(Client::METHOD_DELETE, '/database/collections/' . $data['collectionId'] . '/indexes/'. $data['indexId'], array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ])); // Wait for database worker to finish deleting index sleep(5); $collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['collectionId'], array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ]), []); $this->assertCount(0, $collection['body']['indexes']); return $data; } /** * @depends testDeleteIndex */ public function testDeleteCollection($data) { $collectionId = $data['collectionId']; // Add Documents to the collection $document1 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'documentId' => 'unique()', 'data' => [ 'firstName' => 'Tom', 'lastName' => 'Holland', ], 'read' => ['user:'.$this->getUser()['$id']], 'write' => ['user:'.$this->getUser()['$id']], ]); $document2 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'documentId' => 'unique()', 'data' => [ 'firstName' => 'Samuel', 'lastName' => 'Jackson', ], 'read' => ['user:'.$this->getUser()['$id']], 'write' => ['user:'.$this->getUser()['$id']], ]); $this->assertEquals($document1['headers']['status-code'], 201); $this->assertEquals($document1['body']['$collection'], $collectionId); $this->assertIsArray($document1['body']['$read']); $this->assertIsArray($document1['body']['$write']); $this->assertCount(1, $document1['body']['$read']); $this->assertCount(1, $document1['body']['$write']); $this->assertEquals($document1['body']['firstName'], 'Tom'); $this->assertEquals($document1['body']['lastName'], 'Holland'); $this->assertEquals($document2['headers']['status-code'], 201); $this->assertEquals($document2['body']['$collection'], $collectionId); $this->assertIsArray($document2['body']['$read']); $this->assertIsArray($document2['body']['$write']); $this->assertCount(1, $document2['body']['$read']); $this->assertCount(1, $document2['body']['$write']); $this->assertEquals($document2['body']['firstName'], 'Samuel'); $this->assertEquals($document2['body']['lastName'], 'Jackson'); // Delete the actors collection $response = $this->client->call(Client::METHOD_DELETE, '/database/collections/' . $collectionId , array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ], $this->getHeaders())); $this->assertEquals($response['headers']['status-code'], 204); $this->assertEquals($response['body'],""); // Try to get the collection and check if it has been deleted $response = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collectionId , array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'] ], $this->getHeaders())); $this->assertEquals($response['headers']['status-code'], 404); } }