diff --git a/README.md b/README.md index 1adf58143..cccfa1afa 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ A list of the utopia/php concepts and their relevant equivalent using the differ - **Document** - A simple JSON object that will be stored in one of the utopia/database collections. For SQL-based adapters, this will be equivalent to a row. For a No-SQL adapter, this will equivalent to a native document. - **Attribute** - A simple document attribute. For SQL-based adapters, this will be equivalent to a column. For a No-SQL adapter, this will equivalent to a native document field. - **Index** - A simple collection index used to improve the performance of your database queries. -- **Permissions** - Using permissions, you can decide which roles have read, create, update and delete access for a specific document. The special attribute `$permissions` is used to store permission metadata for each document in the collection. A permission role can be any string you want. You can use `Authorization::setRole()` to delegate new roles to your users, once obtained a new role a user would gain read, create, update or delete access to a relevant document. +- **Permissions** - Using permissions, you can decide which roles have read, create, update and delete access for a specific document. The special attribute `$permissions` is used to store permission metadata for each document in the collection. A permission role can be any string you want. You can use `$authorization->addRole()` to delegate new roles to your users, once obtained a new role a user would gain read, create, update or delete access to a relevant document. ### Filters diff --git a/bin/tasks/load.php b/bin/tasks/load.php index 5008c5fa4..6ed52761a 100644 --- a/bin/tasks/load.php +++ b/bin/tasks/load.php @@ -25,6 +25,8 @@ use Utopia\Http\Validator\Numeric; use Utopia\Http\Validator\Text; +$authorization = new Authorization(); + /** * @Example * docker compose exec tests bin/load --adapter=mariadb --limit=1000 --name=testing @@ -61,7 +63,7 @@ $database->setNamespace($namespace); // Outline collection schema - createSchema($database); + $createSchema($database); // reclaim resources $database = null; @@ -121,7 +123,7 @@ $database->setNamespace($namespace); // Outline collection schema - createSchema($database); + $createSchema($database); // reclaim resources $database = null; @@ -183,7 +185,7 @@ $database->setNamespace($namespace); // Outline collection schema - createSchema($database); + $createSchema($database); // Fill DB $faker = Factory::create(); @@ -226,14 +228,13 @@ }); -function createSchema(Database $database): void -{ +$createSchema = function (Database $database) use ($authorization): void { if ($database->exists($database->getDatabase())) { $database->delete($database->getDatabase()); } $database->create(); - Authorization::setRole(Role::any()->toString()); + $authorization->addRole(Role::any()->toString()); $database->createCollection('articles', permissions: [ Permission::create(Role::any()), @@ -247,7 +248,7 @@ function createSchema(Database $database): void $database->createAttribute('articles', 'views', Database::VAR_INTEGER, 0, true); $database->createAttribute('articles', 'tags', Database::VAR_STRING, 0, true, array: true); $database->createIndex('articles', 'text', Database::INDEX_FULLTEXT, ['text']); -} +}; function createDocument($database, Generator $faker): void { diff --git a/bin/tasks/query.php b/bin/tasks/query.php index 5019260a0..7eb04a996 100644 --- a/bin/tasks/query.php +++ b/bin/tasks/query.php @@ -15,7 +15,6 @@ use Utopia\Database\Query; use Utopia\Database\Adapter\Mongo; use Utopia\Database\Adapter\MariaDB; -use Utopia\Database\Validator\Authorization; use Utopia\Http\Validator\Numeric; use Utopia\Http\Validator\Text; @@ -84,35 +83,35 @@ $report = []; - $count = setRoles($faker, 1); + $count = $setRoles($faker, 1); Console::info("\n{$count} roles:"); $report[] = [ 'roles' => $count, 'results' => runQueries($database, $limit) ]; - $count = setRoles($faker, 100); + $count = $setRoles($faker, 100); Console::info("\n{$count} roles:"); $report[] = [ 'roles' => $count, 'results' => runQueries($database, $limit) ]; - $count = setRoles($faker, 400); + $count = $setRoles($faker, 400); Console::info("\n{$count} roles:"); $report[] = [ 'roles' => $count, 'results' => runQueries($database, $limit) ]; - $count = setRoles($faker, 500); + $count = $setRoles($faker, 500); Console::info("\n{$count} roles:"); $report[] = [ 'roles' => $count, 'results' => runQueries($database, $limit) ]; - $count = setRoles($faker, 1000); + $count = $setRoles($faker, 1000); Console::info("\n{$count} roles:"); $report[] = [ 'roles' => $count, @@ -136,13 +135,12 @@ Console::error($error->getMessage()); }); -function setRoles($faker, $count): int -{ +$setRoles = function ($faker, $count) use ($authorization): int { for ($i = 0; $i < $count; $i++) { - Authorization::setRole($faker->numerify('user####')); + $authorization->addRole($faker->numerify('user####')); } - return \count(Authorization::getRoles()); -} + return \count($authorization->getRoles()); +}; function runQueries(Database $database, int $limit): array { diff --git a/composer.lock b/composer.lock index 0d9341c64..2ab5f8dc0 100644 --- a/composer.lock +++ b/composer.lock @@ -838,16 +838,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.56", + "version": "1.10.57", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "27816a01aea996191ee14d010f325434c0ee76fa" + "reference": "1627b1d03446904aaa77593f370c5201d2ecc34e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/27816a01aea996191ee14d010f325434c0ee76fa", - "reference": "27816a01aea996191ee14d010f325434c0ee76fa", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/1627b1d03446904aaa77593f370c5201d2ecc34e", + "reference": "1627b1d03446904aaa77593f370c5201d2ecc34e", "shasum": "" }, "require": { @@ -896,7 +896,7 @@ "type": "tidelift" } ], - "time": "2024-01-15T10:43:00+00:00" + "time": "2024-01-24T11:51:34+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 764f41df6..536718d5b 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -4,6 +4,7 @@ use Exception; use Utopia\Database\Exception as DatabaseException; +use Utopia\Database\Validator\Authorization; abstract class Adapter { @@ -32,6 +33,23 @@ abstract class Adapter */ protected array $metadata = []; + /** + * @var Authorization + */ + protected Authorization $authorization; + + /** + * @param Authorization $authorization + * + * @return $this + */ + public function setAuthorization(Authorization $authorization): self + { + $this->authorization = $authorization; + + return $this; + } + /** * @param string $key * @param mixed $value diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 302c4ab28..80d12fcbb 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -11,7 +11,6 @@ use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\Timeout as TimeoutException; use Utopia\Database\Query; -use Utopia\Database\Validator\Authorization; class MariaDB extends SQL { @@ -1529,7 +1528,7 @@ public function deleteDocument(string $collection, string $id): bool public function find(string $collection, array $queries = [], ?int $limit = 25, ?int $offset = null, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER): array { $name = $this->filter($collection); - $roles = Authorization::getRoles(); + $roles = $this->authorization->getRoles(); $where = []; $orders = []; @@ -1613,7 +1612,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, $where[] = $conditions; } - if (Authorization::$status) { + if ($this->authorization->getStatus()) { $where[] = $this->getSQLPermissionsCondition($name, $roles); } @@ -1730,7 +1729,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, public function count(string $collection, array $queries = [], ?int $max = null): int { $name = $this->filter($collection); - $roles = Authorization::getRoles(); + $roles = $this->authorization->getRoles(); $where = []; $limit = \is_null($max) ? '' : 'LIMIT :max'; @@ -1739,7 +1738,7 @@ public function count(string $collection, array $queries = [], ?int $max = null) $where[] = $conditions; } - if (Authorization::$status) { + if ($this->authorization->getStatus()) { $where[] = $this->getSQLPermissionsCondition($name, $roles); } @@ -1801,7 +1800,7 @@ public function count(string $collection, array $queries = [], ?int $max = null) public function sum(string $collection, string $attribute, array $queries = [], ?int $max = null): int|float { $name = $this->filter($collection); - $roles = Authorization::getRoles(); + $roles = $this->authorization->getRoles(); $where = []; $limit = \is_null($max) ? '' : 'LIMIT :max'; @@ -1809,7 +1808,7 @@ public function sum(string $collection, string $attribute, array $queries = [], $where[] = $this->getSQLCondition($query); } - if (Authorization::$status) { + if ($this->authorization->getStatus()) { $where[] = $this->getSQLPermissionsCondition($name, $roles); } diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index f79a5a14a..1fd3966cd 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -927,8 +927,8 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, } // permissions - if (Authorization::$status) { // skip if authorization is disabled - $roles = \implode('|', Authorization::getRoles()); + if ($this->authorization->getStatus()) { // skip if authorization is disabled + $roles = \implode('|', $this->authorization->getRoles()); $filters['_permissions']['$in'] = [new Regex("read\(\".*(?:{$roles}).*\"\)", 'i')]; } @@ -1196,8 +1196,8 @@ public function count(string $collection, array $queries = [], ?int $max = null) $filters = $this->buildFilters($queries); // permissions - if (Authorization::$status) { // skip if authorization is disabled - $roles = \implode('|', Authorization::getRoles()); + if ($this->authorization->getStatus()) { // skip if authorization is disabled + $roles = \implode('|', $this->authorization->getRoles()); $filters['_permissions']['$in'] = [new Regex("read\(\".*(?:{$roles}).*\"\)", 'i')]; } @@ -1223,8 +1223,8 @@ public function sum(string $collection, string $attribute, array $queries = [], $filters = $this->buildFilters($queries); // permissions - if (Authorization::$status) { // skip if authorization is disabled - $roles = \implode('|', Authorization::getRoles()); + if ($this->authorization->getStatus()) { // skip if authorization is disabled + $roles = \implode('|', $this->authorization->getRoles()); $filters['_permissions']['$in'] = [new Regex("read\(\".*(?:{$roles}).*\"\)", 'i')]; } diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 83779ef49..22ff310c5 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -12,7 +12,6 @@ use Utopia\Database\Exception\Duplicate; use Utopia\Database\Exception\Timeout; use Utopia\Database\Query; -use Utopia\Database\Validator\Authorization; class Postgres extends SQL { @@ -1515,7 +1514,7 @@ public function deleteDocument(string $collection, string $id): bool public function find(string $collection, array $queries = [], ?int $limit = 25, ?int $offset = null, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER): array { $name = $this->filter($collection); - $roles = Authorization::getRoles(); + $roles = $this->authorization->getRoles(); $where = []; $orders = []; @@ -1597,7 +1596,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, $where[] = "table_main._tenant = :_tenant"; } - if (Authorization::$status) { + if ($this->authorization->getStatus()) { $where[] = $this->getSQLPermissionsCondition($name, $roles); } @@ -1710,7 +1709,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, public function count(string $collection, array $queries = [], ?int $max = null): int { $name = $this->filter($collection); - $roles = Authorization::getRoles(); + $roles = $this->authorization->getRoles(); $where = []; $limit = \is_null($max) ? '' : 'LIMIT :max'; @@ -1723,7 +1722,7 @@ public function count(string $collection, array $queries = [], ?int $max = null) $where[] = "table_main._tenant = :_tenant"; } - if (Authorization::$status) { + if ($this->authorization->getStatus()) { $where[] = $this->getSQLPermissionsCondition($name, $roles); } @@ -1774,7 +1773,7 @@ public function count(string $collection, array $queries = [], ?int $max = null) public function sum(string $collection, string $attribute, array $queries = [], ?int $max = null): int|float { $name = $this->filter($collection); - $roles = Authorization::getRoles(); + $roles = $this->authorization->getRoles(); $where = []; $limit = \is_null($max) ? '' : 'LIMIT :max'; @@ -1786,7 +1785,7 @@ public function sum(string $collection, string $attribute, array $queries = [], $where[] = "table_main._tenant = :_tenant"; } - if (Authorization::$status) { + if ($this->authorization->getStatus()) { $where[] = $this->getSQLPermissionsCondition($name, $roles); } diff --git a/src/Database/Database.php b/src/Database/Database.php index 72864e2cc..65e64bf56 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -17,6 +17,7 @@ use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; use Utopia\Database\Validator\Authorization; +use Utopia\Database\Validator\Authorization\Input; use Utopia\Database\Validator\Queries\Document as DocumentValidator; use Utopia\Database\Validator\Queries\Documents as DocumentsValidator; use Utopia\Database\Validator\Index as IndexValidator; @@ -337,6 +338,11 @@ class Database */ protected array $relationshipDeleteStack = []; + /** + * @var Authorization + */ + private Authorization $authorization; + /** * @param Adapter $adapter * @param Cache $cache @@ -348,6 +354,8 @@ public function __construct(Adapter $adapter, Cache $cache, array $filters = []) $this->cache = $cache; $this->instanceFilters = $filters; + $this->setAuthorization(new Authorization()); + self::addFilter( 'json', /** @@ -419,6 +427,19 @@ function (?string $value) { ); } + /** + * Sets instance of authorization for permission checks + * + * @param Authorization $authorization + * @return self + */ + public function setAuthorization(Authorization $authorization): self + { + $this->adapter->setAuthorization($authorization); + $this->authorization = $authorization; + return $this; + } + /** * Add listener to events * @@ -2671,7 +2692,6 @@ public function getDocument(string $collection, string $id, array $queries = []) $queries = \array_values($queries); - $validator = new Authorization(self::PERMISSION_READ); $documentSecurity = $collection->getAttribute('documentSecurity', false); $cacheKey = $this->cacheName . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':' . $collection->getId() . ':' . $id; @@ -2685,10 +2705,12 @@ public function getDocument(string $collection, string $id, array $queries = []) $document = new Document($cache); if ($collection->getId() !== self::METADATA) { - if (!$validator->isValid([ + $isValid = $this->authorization->isValid(new Input(self::PERMISSION_READ, [ ...$collection->getRead(), ...($documentSecurity ? $document->getRead() : []) - ])) { + ])); + + if (!$isValid) { return new Document(); } } @@ -2707,10 +2729,12 @@ public function getDocument(string $collection, string $id, array $queries = []) $document->setAttribute('$collection', $collection->getId()); if ($collection->getId() !== self::METADATA) { - if (!$validator->isValid([ + $isValid = $this->authorization->isValid(new Input(self::PERMISSION_READ, [ ...$collection->getRead(), ...($documentSecurity ? $document->getRead() : []) - ])) { + ])); + + if (!$isValid) { return new Document(); } } @@ -3033,9 +3057,9 @@ public function createDocument(string $collection, Document $document): Document $collection = $this->silent(fn () => $this->getCollection($collection)); if ($collection->getId() !== self::METADATA) { - $authorization = new Authorization(self::PERMISSION_CREATE); - if (!$authorization->isValid($collection->getCreate())) { - throw new AuthorizationException($authorization->getDescription()); + $isValid = $this->authorization->isValid(new Input(self::PERMISSION_CREATE, $collection->getCreate())); + if (!$isValid) { + throw new AuthorizationException($this->authorization->getDescription()); } } @@ -3445,7 +3469,7 @@ public function updateDocument(string $collection, string $id, Document $documen } $time = DateTime::now(); - $old = Authorization::skip(fn () => $this->silent(fn () => $this->getDocument($collection, $id))); // Skip ensures user does not need read permission for this + $old = $this->authorization->skip(fn () => $this->silent(fn () => $this->getDocument($collection, $id))); // Skip ensures user does not need read permission for this $document = \array_merge($old->getArrayCopy(), $document->getArrayCopy()); $document['$collection'] = $old->getAttribute('$collection'); // Make sure user doesn't switch collection ID $document['$tenant'] = $old->getAttribute('$tenant'); // Make sure user doesn't switch tenant @@ -3458,7 +3482,6 @@ public function updateDocument(string $collection, string $id, Document $documen return $attribute['type'] === Database::VAR_RELATIONSHIP; }); - $validator = new Authorization(self::PERMISSION_UPDATE); $shouldUpdate = false; if ($collection->getId() !== self::METADATA) { @@ -3549,11 +3572,14 @@ public function updateDocument(string $collection, string $id, Document $documen } } - if ($shouldUpdate && !$validator->isValid([ - ...$collection->getUpdate(), - ...($documentSecurity ? $old->getUpdate() : []) - ])) { - throw new AuthorizationException($validator->getDescription()); + if ($shouldUpdate) { + $isValid = $this->authorization->isValid(new Input(self::PERMISSION_UPDATE, [ + ...$collection->getUpdate(), + ...($documentSecurity ? $old->getUpdate() : []) + ])); + if (!$isValid) { + throw new AuthorizationException($this->authorization->getDescription()); + } } } @@ -3636,19 +3662,18 @@ public function updateDocuments(string $collection, array $documents, int $batch $document->setAttribute('$updatedAt', empty($updatedAt) || !$this->preserveDates ? $time : $updatedAt); $document = $this->encode($collection, $document); - $old = Authorization::skip(fn () => $this->silent( + $old = $this->authorization->skip(fn () => $this->silent( fn () => $this->getDocument( $collection->getId(), $document->getId() ) )); - $validator = new Authorization(self::PERMISSION_UPDATE); if ( $collection->getId() !== self::METADATA - && !$validator->isValid($old->getUpdate()) + && !$this->authorization->isValid(new Input(self::PERMISSION_UPDATE, $old->getUpdate())) ) { - throw new AuthorizationException($validator->getDescription()); + throw new AuthorizationException($this->authorization->getDescription()); } $validator = new Structure($collection); @@ -3858,12 +3883,12 @@ private function updateDocumentRelationships(Document $collection, Document $old $removedDocuments = \array_diff($oldIds, $newIds); foreach ($removedDocuments as $relation) { - $relation = Authorization::skip(fn () => $this->skipRelationships(fn () => $this->getDocument( + $relation = $this->authorization->skip(fn () => $this->skipRelationships(fn () => $this->getDocument( $relatedCollection->getId(), $relation ))); - Authorization::skip(fn () => $this->skipRelationships(fn () => $this->updateDocument( + $this->authorization->skip(fn () => $this->skipRelationships(fn () => $this->updateDocument( $relatedCollection->getId(), $relation->getId(), $relation->setAttribute($twoWayKey, null) @@ -3984,7 +4009,7 @@ private function updateDocumentRelationships(Document $collection, Document $old ]); foreach ($junctions as $junction) { - Authorization::skip(fn () => $this->deleteDocument($junction->getCollection(), $junction->getId())); + $this->authorization->skip(fn () => $this->deleteDocument($junction->getCollection(), $junction->getId())); } } @@ -4076,19 +4101,18 @@ public function increaseDocumentAttribute(string $collection, string $id, string throw new DatabaseException('Value must be numeric and greater than 0'); } - $validator = new Authorization(self::PERMISSION_UPDATE); - - $document = Authorization::skip(fn () => $this->silent(fn () => $this->getDocument($collection, $id))); // Skip ensures user does not need read permission for this + $document = $this->authorization->skip(fn () => $this->silent(fn () => $this->getDocument($collection, $id))); // Skip ensures user does not need read permission for this $collection = $this->silent(fn () => $this->getCollection($collection)); if ($collection->getId() !== self::METADATA) { $documentSecurity = $collection->getAttribute('documentSecurity', false); - if (!$validator->isValid([ + $isValid = $this->authorization->isValid(new Input(self::PERMISSION_UPDATE, [ ...$collection->getUpdate(), ...($documentSecurity ? $document->getUpdate() : []) - ])) { - throw new AuthorizationException($validator->getDescription()); + ])); + if (!$isValid) { + throw new AuthorizationException($this->authorization->getDescription()); } } @@ -4148,19 +4172,18 @@ public function decreaseDocumentAttribute(string $collection, string $id, string throw new DatabaseException('Value must be numeric and greater than 0'); } - $validator = new Authorization(self::PERMISSION_UPDATE); - - $document = Authorization::skip(fn () => $this->silent(fn () => $this->getDocument($collection, $id))); // Skip ensures user does not need read permission for this + $document = $this->authorization->skip(fn () => $this->silent(fn () => $this->getDocument($collection, $id))); // Skip ensures user does not need read permission for this $collection = $this->silent(fn () => $this->getCollection($collection)); if ($collection->getId() !== self::METADATA) { $documentSecurity = $collection->getAttribute('documentSecurity', false); - if (!$validator->isValid([ + $isValid = $this->authorization->isValid(new Input(self::PERMISSION_UPDATE, [ ...$collection->getUpdate(), ...($documentSecurity ? $document->getUpdate() : []) - ])) { - throw new AuthorizationException($validator->getDescription()); + ])); + if (!$isValid) { + throw new AuthorizationException($this->authorization->getDescription()); } } @@ -4217,19 +4240,18 @@ public function deleteDocument(string $collection, string $id): bool throw new DatabaseException('Missing tenant. Tenant must be set when table sharing is enabled.'); } - $document = Authorization::skip(fn () => $this->silent(fn () => $this->getDocument($collection, $id))); + $document = $this->authorization->skip(fn () => $this->silent(fn () => $this->getDocument($collection, $id))); $collection = $this->silent(fn () => $this->getCollection($collection)); - $validator = new Authorization(self::PERMISSION_DELETE); - if ($collection->getId() !== self::METADATA) { $documentSecurity = $collection->getAttribute('documentSecurity', false); - if (!$validator->isValid([ + $isValid = $this->authorization->isValid(new Input(self::PERMISSION_DELETE, [ ...$collection->getDelete(), ...($documentSecurity ? $document->getDelete() : []) - ])) { - throw new AuthorizationException($validator->getDescription()); + ])); + if (!$isValid) { + throw new AuthorizationException($this->authorization->getDescription()); } } @@ -4389,7 +4411,7 @@ private function deleteRestrict( && $side === Database::RELATION_SIDE_CHILD && !$twoWay ) { - Authorization::skip(function () use ($document, $relatedCollection, $twoWayKey) { + $this->authorization->skip(function () use ($document, $relatedCollection, $twoWayKey) { $related = $this->findOne($relatedCollection->getId(), [ Query::equal($twoWayKey, [$document->getId()]) ]); @@ -4411,7 +4433,7 @@ private function deleteRestrict( $relationType === Database::RELATION_MANY_TO_ONE && $side === Database::RELATION_SIDE_CHILD ) { - $related = Authorization::skip(fn () => $this->findOne($relatedCollection->getId(), [ + $related = $this->authorization->skip(fn () => $this->findOne($relatedCollection->getId(), [ Query::equal($twoWayKey, [$document->getId()]) ])); @@ -4446,7 +4468,7 @@ private function deleteSetNull(Document $collection, Document $relatedCollection } // Shouldn't need read or update permission to delete - Authorization::skip(function () use ($document, $value, $relatedCollection, $twoWay, $twoWayKey, $side) { + $this->authorization->skip(function () use ($document, $value, $relatedCollection, $twoWay, $twoWayKey, $side) { if (!$twoWay && $side === Database::RELATION_SIDE_CHILD) { $related = $this->findOne($relatedCollection->getId(), [ Query::equal($twoWayKey, [$document->getId()]) @@ -4474,7 +4496,7 @@ private function deleteSetNull(Document $collection, Document $relatedCollection break; } foreach ($value as $relation) { - Authorization::skip(function () use ($relatedCollection, $twoWayKey, $relation) { + $this->authorization->skip(function () use ($relatedCollection, $twoWayKey, $relation) { $related = $this->getDocument($relatedCollection->getId(), $relation->getId()); $this->skipRelationships(fn () => $this->updateDocument( @@ -4498,7 +4520,7 @@ private function deleteSetNull(Document $collection, Document $relatedCollection } foreach ($value as $relation) { - Authorization::skip(function () use ($relatedCollection, $twoWayKey, $relation) { + $this->authorization->skip(function () use ($relatedCollection, $twoWayKey, $relation) { $related = $this->getDocument($relatedCollection->getId(), $relation->getId()); $this->skipRelationships(fn () => $this->updateDocument( @@ -4687,12 +4709,12 @@ public function find(string $collection, array $queries = []): array } } - $authorization = new Authorization(self::PERMISSION_READ); $documentSecurity = $collection->getAttribute('documentSecurity', false); - $skipAuth = $authorization->isValid($collection->getRead()); + + $skipAuth = $this->authorization->isValid(new Input(self::PERMISSION_READ, $collection->getRead())); if (!$skipAuth && !$documentSecurity && $collection->getId() !== self::METADATA) { - throw new AuthorizationException($authorization->getDescription()); + throw new AuthorizationException($this->authorization->getDescription()); } $relationships = \array_filter( @@ -4779,7 +4801,7 @@ public function find(string $collection, array $queries = []): array $cursorDirection ?? Database::CURSOR_AFTER ); - $results = $skipAuth ? Authorization::skip($getResults) : $getResults(); + $results = $skipAuth ? $this->authorization->skip($getResults) : $getResults(); foreach ($results as &$node) { if ($this->resolveRelationships && (empty($selects) || !empty($nestedSelections))) { @@ -4862,16 +4884,13 @@ public function count(string $collection, array $queries = [], ?int $max = null) } } - $authorization = new Authorization(self::PERMISSION_READ); - if ($authorization->isValid($collection->getRead())) { - $skipAuth = true; - } + $skipAuth = $this->authorization->isValid(new Input(self::PERMISSION_READ, $collection->getRead())); $queries = Query::groupByType($queries)['filters']; $queries = self::convertQueries($collection, $queries); $getCount = fn () => $this->adapter->count($collection->getId(), $queries, $max); - $count = $skipAuth ?? false ? Authorization::skip($getCount) : $getCount(); + $count = $skipAuth ? $this->authorization->skip($getCount) : $getCount(); $this->trigger(self::EVENT_DOCUMENT_COUNT, $count); diff --git a/src/Database/Validator/Authorization.php b/src/Database/Validator/Authorization.php index f9b7b918b..41d904889 100644 --- a/src/Database/Validator/Authorization.php +++ b/src/Database/Validator/Authorization.php @@ -2,34 +2,35 @@ namespace Utopia\Database\Validator; +use Utopia\Database\Validator\Authorization\Input; use Utopia\Http\Validator; class Authorization extends Validator { /** - * @var array + * @var bool */ - private static array $roles = [ - 'any' => true - ]; + protected bool $status = true; /** - * @var string + * Default value in case we need + * to reset Authorization status + * + * @var bool */ - protected string $action = ''; + protected bool $statusDefault = true; /** - * @var string + * @var array */ - protected string $message = 'Authorization Error'; + private array $roles = [ + 'any' => true + ]; /** - * @param string $action + * @var string */ - public function __construct(string $action) - { - $this->action = $action; - } + protected string $message = 'Authorization Error'; /** * Get Description. @@ -43,36 +44,43 @@ public function getDescription(): string return $this->message; } - /** - * Is valid. + /* + * Validation * * Returns true if valid or false if not. - * - * @param mixed $permissions - * - * @return bool - */ - public function isValid($permissions): bool + */ + public function isValid(mixed $input): bool // any, CREATE { - if (!self::$status) { + if(!($input instanceof Input)) { + $this->message = 'Invalid input provided'; + return false; + } + + /** + * @var Input $input + */ + + $permissions = $input->getPermissions(); + $action = $input->getAction(); + + if (!$this->status) { return true; } if (empty($permissions)) { - $this->message = 'No permissions provided for action \''.$this->action.'\''; + $this->message = 'No permissions provided for action \''.$action.'\''; return false; } $permission = '-'; foreach ($permissions as $permission) { - if (\array_key_exists($permission, self::$roles)) { + if (\array_key_exists($permission, $this->roles)) { return true; } } - $this->message = 'Missing "'.$this->action.'" permission for role "'.$permission.'". Only "'.\json_encode(self::getRoles()).'" scopes are allowed and "'.\json_encode($permissions).'" was given.'; - + $this->message = 'Missing "'.$action.'" permission for role "'.$permission.'". Only "'.\json_encode($this->getRoles()).'" scopes are allowed and "'.\json_encode($permissions).'" was given.'; return false; } @@ -80,9 +88,9 @@ public function isValid($permissions): bool * @param string $role * @return void */ - public static function setRole(string $role): void + public function addRole(string $role): void // user:meldiron, users, any { - self::$roles[$role] = true; + $this->roles[$role] = true; } /** @@ -90,25 +98,25 @@ public static function setRole(string $role): void * * @return void */ - public static function unsetRole(string $role): void + public function removeRole(string $role): void { - unset(self::$roles[$role]); + unset($this->roles[$role]); } /** * @return array */ - public static function getRoles(): array + public function getRoles(): array { - return \array_keys(self::$roles); + return \array_keys($this->roles); } /** * @return void */ - public static function cleanRoles(): void + public function cleanRoles(): void { - self::$roles = []; + $this->roles = []; } /** @@ -116,36 +124,43 @@ public static function cleanRoles(): void * * @return bool */ - public static function isRole(string $role): bool + public function isRole(string $role): bool { - return (\array_key_exists($role, self::$roles)); + return (\array_key_exists($role, $this->roles)); } /** - * @var bool + * Change default status. + * This will be used for the + * value set on the $this->reset() method + * @param bool $status + * @return void */ - public static bool $status = true; + public function setDefaultStatus(bool $status): void + { + $this->statusDefault = $status; + $this->status = $status; + } /** - * Default value in case we need - * to reset Authorization status + * Change status * - * @var bool + * @param bool $status + * @return void */ - public static bool $statusDefault = true; + public function setStatus(bool $status): void + { + $this->status = $status; + } /** - * Change default status. - * This will be used for the - * value set on the self::reset() method + * Get status * - * @param bool $status - * @return void + * @return bool */ - public static function setDefaultStatus(bool $status): void + public function getStatus(): bool { - self::$statusDefault = $status; - self::$status = $status; + return $this->status; } /** @@ -157,15 +172,15 @@ public static function setDefaultStatus(bool $status): void * @param callable(): T $callback * @return T */ - public static function skip(callable $callback): mixed + public function skip(callable $callback): mixed { - $initialStatus = self::$status; - self::disable(); + $initialStatus = $this->status; + $this->disable(); try { return $callback(); } finally { - self::$status = $initialStatus; + $this->status = $initialStatus; } } @@ -174,9 +189,9 @@ public static function skip(callable $callback): mixed * * @return void */ - public static function enable(): void + public function enable(): void { - self::$status = true; + $this->status = true; } /** @@ -184,9 +199,9 @@ public static function enable(): void * * @return void */ - public static function disable(): void + public function disable(): void { - self::$status = false; + $this->status = false; } /** @@ -194,9 +209,9 @@ public static function disable(): void * * @return void */ - public static function reset(): void + public function reset(): void { - self::$status = self::$statusDefault; + $this->status = $this->statusDefault; } /** diff --git a/src/Database/Validator/Authorization/Input.php b/src/Database/Validator/Authorization/Input.php new file mode 100644 index 000000000..4c2387097 --- /dev/null +++ b/src/Database/Validator/Authorization/Input.php @@ -0,0 +1,49 @@ +permissions = $permissions; + $this->action = $action; + } + + /** + * @param string[] $permissions + */ + public function setPermissions(array $permissions): self + { + $this->permissions = $permissions; + return $this; + } + + public function setAction(string $action): self + { + $this->action = $action; + return $this; + } + + /** + * @return string[] + */ + public function getPermissions(): array + { + return $this->permissions; + } + + public function getAction(): string + { + return $this->action; + } +} diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index cc633ee38..fd02867ad 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -32,10 +32,15 @@ abstract class Base extends TestCase { protected static string $namespace; + /** + * @var Authorization + */ + protected static ?Authorization $authorization = null; + /** * @return Database */ - abstract protected static function getDatabase(): Database; + abstract protected function getDatabase(): Database; /** * @return string @@ -44,226 +49,230 @@ abstract protected static function getAdapterName(): string; public function setUp(): void { - Authorization::setRole('any'); + if (is_null(self::$authorization)) { + self::$authorization = new Authorization(); + } + + self::$authorization->addRole('any'); } public function tearDown(): void { - Authorization::setDefaultStatus(true); + self::$authorization->setDefaultStatus(true); } protected string $testDatabase = 'utopiaTests'; public function testPing(): void { - $this->assertEquals(true, static::getDatabase()->ping()); + $this->assertEquals(true, $this->getDatabase()->ping()); } public function testCreateExistsDelete(): void { $schemaSupport = $this->getDatabase()->getAdapter()->getSupportForSchemas(); if (!$schemaSupport) { - $this->assertEquals(static::getDatabase(), static::getDatabase()->setDatabase($this->testDatabase)); - $this->assertEquals(true, static::getDatabase()->create()); + $this->assertEquals($this->getDatabase(), $this->getDatabase()->setDatabase($this->testDatabase)); + $this->assertEquals(true, $this->getDatabase()->create()); return; } - if (!static::getDatabase()->exists($this->testDatabase)) { - $this->assertEquals(true, static::getDatabase()->create()); + if (!$this->getDatabase()->exists($this->testDatabase)) { + $this->assertEquals(true, $this->getDatabase()->create()); } - $this->assertEquals(true, static::getDatabase()->exists($this->testDatabase)); - $this->assertEquals(true, static::getDatabase()->delete($this->testDatabase)); - $this->assertEquals(false, static::getDatabase()->exists($this->testDatabase)); - $this->assertEquals(static::getDatabase(), static::getDatabase()->setDatabase($this->testDatabase)); - $this->assertEquals(true, static::getDatabase()->create()); + $this->assertEquals(true, $this->getDatabase()->exists($this->testDatabase)); + $this->assertEquals(true, $this->getDatabase()->delete($this->testDatabase)); + $this->assertEquals(false, $this->getDatabase()->exists($this->testDatabase)); + $this->assertEquals($this->getDatabase(), $this->getDatabase()->setDatabase($this->testDatabase)); + $this->assertEquals(true, $this->getDatabase()->create()); } public function testDeleteRelatedCollection(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('c1'); - static::getDatabase()->createCollection('c2'); + $this->getDatabase()->createCollection('c1'); + $this->getDatabase()->createCollection('c2'); // ONE_TO_ONE - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'c1', relatedCollection: 'c2', type: Database::RELATION_ONE_TO_ONE, ); - $this->assertEquals(true, static::getDatabase()->deleteCollection('c1')); - $collection = static::getDatabase()->getCollection('c2'); + $this->assertEquals(true, $this->getDatabase()->deleteCollection('c1')); + $collection = $this->getDatabase()->getCollection('c2'); $this->assertCount(0, $collection->getAttribute('attributes')); $this->assertCount(0, $collection->getAttribute('indexes')); - static::getDatabase()->createCollection('c1'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createCollection('c1'); + $this->getDatabase()->createRelationship( collection: 'c1', relatedCollection: 'c2', type: Database::RELATION_ONE_TO_ONE, ); - $this->assertEquals(true, static::getDatabase()->deleteCollection('c2')); - $collection = static::getDatabase()->getCollection('c1'); + $this->assertEquals(true, $this->getDatabase()->deleteCollection('c2')); + $collection = $this->getDatabase()->getCollection('c1'); $this->assertCount(0, $collection->getAttribute('attributes')); $this->assertCount(0, $collection->getAttribute('indexes')); - static::getDatabase()->createCollection('c2'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createCollection('c2'); + $this->getDatabase()->createRelationship( collection: 'c1', relatedCollection: 'c2', type: Database::RELATION_ONE_TO_ONE, twoWay: true ); - $this->assertEquals(true, static::getDatabase()->deleteCollection('c1')); - $collection = static::getDatabase()->getCollection('c2'); + $this->assertEquals(true, $this->getDatabase()->deleteCollection('c1')); + $collection = $this->getDatabase()->getCollection('c2'); $this->assertCount(0, $collection->getAttribute('attributes')); $this->assertCount(0, $collection->getAttribute('indexes')); - static::getDatabase()->createCollection('c1'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createCollection('c1'); + $this->getDatabase()->createRelationship( collection: 'c1', relatedCollection: 'c2', type: Database::RELATION_ONE_TO_ONE, twoWay: true ); - $this->assertEquals(true, static::getDatabase()->deleteCollection('c2')); - $collection = static::getDatabase()->getCollection('c1'); + $this->assertEquals(true, $this->getDatabase()->deleteCollection('c2')); + $collection = $this->getDatabase()->getCollection('c1'); $this->assertCount(0, $collection->getAttribute('attributes')); $this->assertCount(0, $collection->getAttribute('indexes')); // ONE_TO_MANY - static::getDatabase()->createCollection('c2'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createCollection('c2'); + $this->getDatabase()->createRelationship( collection: 'c1', relatedCollection: 'c2', type: Database::RELATION_ONE_TO_MANY, ); - $this->assertEquals(true, static::getDatabase()->deleteCollection('c1')); - $collection = static::getDatabase()->getCollection('c2'); + $this->assertEquals(true, $this->getDatabase()->deleteCollection('c1')); + $collection = $this->getDatabase()->getCollection('c2'); $this->assertCount(0, $collection->getAttribute('attributes')); $this->assertCount(0, $collection->getAttribute('indexes')); - static::getDatabase()->createCollection('c1'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createCollection('c1'); + $this->getDatabase()->createRelationship( collection: 'c1', relatedCollection: 'c2', type: Database::RELATION_ONE_TO_MANY, ); - $this->assertEquals(true, static::getDatabase()->deleteCollection('c2')); - $collection = static::getDatabase()->getCollection('c1'); + $this->assertEquals(true, $this->getDatabase()->deleteCollection('c2')); + $collection = $this->getDatabase()->getCollection('c1'); $this->assertCount(0, $collection->getAttribute('attributes')); $this->assertCount(0, $collection->getAttribute('indexes')); - static::getDatabase()->createCollection('c2'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createCollection('c2'); + $this->getDatabase()->createRelationship( collection: 'c1', relatedCollection: 'c2', type: Database::RELATION_ONE_TO_MANY, twoWay: true ); - $this->assertEquals(true, static::getDatabase()->deleteCollection('c1')); - $collection = static::getDatabase()->getCollection('c2'); + $this->assertEquals(true, $this->getDatabase()->deleteCollection('c1')); + $collection = $this->getDatabase()->getCollection('c2'); $this->assertCount(0, $collection->getAttribute('attributes')); $this->assertCount(0, $collection->getAttribute('indexes')); - static::getDatabase()->createCollection('c1'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createCollection('c1'); + $this->getDatabase()->createRelationship( collection: 'c1', relatedCollection: 'c2', type: Database::RELATION_ONE_TO_MANY, twoWay: true ); - $this->assertEquals(true, static::getDatabase()->deleteCollection('c2')); - $collection = static::getDatabase()->getCollection('c1'); + $this->assertEquals(true, $this->getDatabase()->deleteCollection('c2')); + $collection = $this->getDatabase()->getCollection('c1'); $this->assertCount(0, $collection->getAttribute('attributes')); $this->assertCount(0, $collection->getAttribute('indexes')); // RELATION_MANY_TO_ONE - static::getDatabase()->createCollection('c2'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createCollection('c2'); + $this->getDatabase()->createRelationship( collection: 'c1', relatedCollection: 'c2', type: Database::RELATION_MANY_TO_ONE, ); - $this->assertEquals(true, static::getDatabase()->deleteCollection('c1')); - $collection = static::getDatabase()->getCollection('c2'); + $this->assertEquals(true, $this->getDatabase()->deleteCollection('c1')); + $collection = $this->getDatabase()->getCollection('c2'); $this->assertCount(0, $collection->getAttribute('attributes')); $this->assertCount(0, $collection->getAttribute('indexes')); - static::getDatabase()->createCollection('c1'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createCollection('c1'); + $this->getDatabase()->createRelationship( collection: 'c1', relatedCollection: 'c2', type: Database::RELATION_MANY_TO_ONE, ); - $this->assertEquals(true, static::getDatabase()->deleteCollection('c2')); - $collection = static::getDatabase()->getCollection('c1'); + $this->assertEquals(true, $this->getDatabase()->deleteCollection('c2')); + $collection = $this->getDatabase()->getCollection('c1'); $this->assertCount(0, $collection->getAttribute('attributes')); $this->assertCount(0, $collection->getAttribute('indexes')); - static::getDatabase()->createCollection('c2'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createCollection('c2'); + $this->getDatabase()->createRelationship( collection: 'c1', relatedCollection: 'c2', type: Database::RELATION_MANY_TO_ONE, twoWay: true ); - $this->assertEquals(true, static::getDatabase()->deleteCollection('c1')); - $collection = static::getDatabase()->getCollection('c2'); + $this->assertEquals(true, $this->getDatabase()->deleteCollection('c1')); + $collection = $this->getDatabase()->getCollection('c2'); $this->assertCount(0, $collection->getAttribute('attributes')); $this->assertCount(0, $collection->getAttribute('indexes')); - static::getDatabase()->createCollection('c1'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createCollection('c1'); + $this->getDatabase()->createRelationship( collection: 'c1', relatedCollection: 'c2', type: Database::RELATION_MANY_TO_ONE, twoWay: true ); - $this->assertEquals(true, static::getDatabase()->deleteCollection('c2')); - $collection = static::getDatabase()->getCollection('c1'); + $this->assertEquals(true, $this->getDatabase()->deleteCollection('c2')); + $collection = $this->getDatabase()->getCollection('c1'); $this->assertCount(0, $collection->getAttribute('attributes')); $this->assertCount(0, $collection->getAttribute('indexes')); } public function testPreserveDatesUpdate(): void { - Authorization::disable(); + self::$authorization->disable(); - static::getDatabase()->setPreserveDates(true); + $this->getDatabase()->setPreserveDates(true); - static::getDatabase()->createCollection('preserve_update_dates'); + $this->getDatabase()->createCollection('preserve_update_dates'); - static::getDatabase()->createAttribute('preserve_update_dates', 'attr1', Database::VAR_STRING, 10, false); + $this->getDatabase()->createAttribute('preserve_update_dates', 'attr1', Database::VAR_STRING, 10, false); - $doc1 = static::getDatabase()->createDocument('preserve_update_dates', new Document([ + $doc1 = $this->getDatabase()->createDocument('preserve_update_dates', new Document([ '$id' => 'doc1', '$permissions' => [], 'attr1' => 'value1', ])); - $doc2 = static::getDatabase()->createDocument('preserve_update_dates', new Document([ + $doc2 = $this->getDatabase()->createDocument('preserve_update_dates', new Document([ '$id' => 'doc2', '$permissions' => [], 'attr1' => 'value2', ])); - $doc3 = static::getDatabase()->createDocument('preserve_update_dates', new Document([ + $doc3 = $this->getDatabase()->createDocument('preserve_update_dates', new Document([ '$id' => 'doc3', '$permissions' => [], 'attr1' => 'value3', @@ -272,46 +281,46 @@ public function testPreserveDatesUpdate(): void $newDate = '2000-01-01T10:00:00.000+00:00'; $doc1->setAttribute('$updatedAt', $newDate); - static::getDatabase()->updateDocument('preserve_update_dates', 'doc1', $doc1); - $doc1 = static::getDatabase()->getDocument('preserve_update_dates', 'doc1'); + $this->getDatabase()->updateDocument('preserve_update_dates', 'doc1', $doc1); + $doc1 = $this->getDatabase()->getDocument('preserve_update_dates', 'doc1'); $this->assertEquals($newDate, $doc1->getAttribute('$updatedAt')); $doc2->setAttribute('$updatedAt', $newDate); $doc3->setAttribute('$updatedAt', $newDate); - static::getDatabase()->updateDocuments('preserve_update_dates', [$doc2, $doc3], 2); + $this->getDatabase()->updateDocuments('preserve_update_dates', [$doc2, $doc3], 2); - $doc2 = static::getDatabase()->getDocument('preserve_update_dates', 'doc2'); - $doc3 = static::getDatabase()->getDocument('preserve_update_dates', 'doc3'); + $doc2 = $this->getDatabase()->getDocument('preserve_update_dates', 'doc2'); + $doc3 = $this->getDatabase()->getDocument('preserve_update_dates', 'doc3'); $this->assertEquals($newDate, $doc2->getAttribute('$updatedAt')); $this->assertEquals($newDate, $doc3->getAttribute('$updatedAt')); - static::getDatabase()->deleteCollection('preserve_update_dates'); + $this->getDatabase()->deleteCollection('preserve_update_dates'); - static::getDatabase()->setPreserveDates(false); + $this->getDatabase()->setPreserveDates(false); - Authorization::reset(); + self::$authorization->reset(); } public function testPreserveDatesCreate(): void { - Authorization::disable(); + self::$authorization->disable(); - static::getDatabase()->setPreserveDates(true); + $this->getDatabase()->setPreserveDates(true); - static::getDatabase()->createCollection('preserve_create_dates'); + $this->getDatabase()->createCollection('preserve_create_dates'); - static::getDatabase()->createAttribute('preserve_create_dates', 'attr1', Database::VAR_STRING, 10, false); + $this->getDatabase()->createAttribute('preserve_create_dates', 'attr1', Database::VAR_STRING, 10, false); $date = '2000-01-01T10:00:00.000+00:00'; - static::getDatabase()->createDocument('preserve_create_dates', new Document([ + $this->getDatabase()->createDocument('preserve_create_dates', new Document([ '$id' => 'doc1', '$permissions' => [], 'attr1' => 'value1', '$createdAt' => $date ])); - static::getDatabase()->createDocuments('preserve_create_dates', [ + $this->getDatabase()->createDocuments('preserve_create_dates', [ new Document([ '$id' => 'doc2', '$permissions' => [], @@ -326,18 +335,18 @@ public function testPreserveDatesCreate(): void ]), ], 2); - $doc1 = static::getDatabase()->getDocument('preserve_create_dates', 'doc1'); - $doc2 = static::getDatabase()->getDocument('preserve_create_dates', 'doc2'); - $doc3 = static::getDatabase()->getDocument('preserve_create_dates', 'doc3'); + $doc1 = $this->getDatabase()->getDocument('preserve_create_dates', 'doc1'); + $doc2 = $this->getDatabase()->getDocument('preserve_create_dates', 'doc2'); + $doc3 = $this->getDatabase()->getDocument('preserve_create_dates', 'doc3'); $this->assertEquals($date, $doc1->getAttribute('$createdAt')); $this->assertEquals($date, $doc2->getAttribute('$createdAt')); $this->assertEquals($date, $doc3->getAttribute('$createdAt')); - static::getDatabase()->deleteCollection('preserve_create_dates'); + $this->getDatabase()->deleteCollection('preserve_create_dates'); - static::getDatabase()->setPreserveDates(false); + $this->getDatabase()->setPreserveDates(false); - Authorization::reset(); + self::$authorization->reset(); } /** @@ -387,14 +396,14 @@ public function testIndexValidation(): void 'indexes' => $indexes ]); - $validator = new Index($attributes, static::getDatabase()->getAdapter()->getMaxIndexLength()); + $validator = new Index($attributes, $this->getDatabase()->getAdapter()->getMaxIndexLength()); $errorMessage = 'Index length 701 is larger than the size for title1: 700"'; $this->assertFalse($validator->isValid($indexes[0])); $this->assertEquals($errorMessage, $validator->getDescription()); try { - static::getDatabase()->createCollection($collection->getId(), $attributes, $indexes, [ + $this->getDatabase()->createCollection($collection->getId(), $attributes, $indexes, [ Permission::read(Role::any()), Permission::create(Role::any()), ]); @@ -415,13 +424,13 @@ public function testIndexValidation(): void $collection->setAttribute('indexes', $indexes); - if (static::getDatabase()->getAdapter()->getMaxIndexLength() > 0) { - $errorMessage = 'Index length is longer than the maximum: ' . static::getDatabase()->getAdapter()->getMaxIndexLength(); + if ($this->getDatabase()->getAdapter()->getMaxIndexLength() > 0) { + $errorMessage = 'Index length is longer than the maximum: ' . $this->getDatabase()->getAdapter()->getMaxIndexLength(); $this->assertFalse($validator->isValid($indexes[0])); $this->assertEquals($errorMessage, $validator->getDescription()); try { - static::getDatabase()->createCollection($collection->getId(), $attributes, $indexes); + $this->getDatabase()->createCollection($collection->getId(), $attributes, $indexes); $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertEquals($errorMessage, $e->getMessage()); @@ -457,13 +466,13 @@ public function testIndexValidation(): void 'indexes' => $indexes ]); - $validator = new Index($attributes, static::getDatabase()->getAdapter()->getMaxIndexLength()); + $validator = new Index($attributes, $this->getDatabase()->getAdapter()->getMaxIndexLength()); $errorMessage = 'Attribute "integer" cannot be part of a FULLTEXT index, must be of type string'; $this->assertFalse($validator->isValid($indexes[0])); $this->assertEquals($errorMessage, $validator->getDescription()); try { - static::getDatabase()->createCollection($collection->getId(), $attributes, $indexes); + $this->getDatabase()->createCollection($collection->getId(), $attributes, $indexes); $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertEquals($errorMessage, $e->getMessage()); @@ -472,9 +481,9 @@ public function testIndexValidation(): void public function testCreatedAtUpdatedAt(): void { - $this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createCollection('created_at')); - static::getDatabase()->createAttribute('created_at', 'title', Database::VAR_STRING, 100, false); - $document = static::getDatabase()->createDocument('created_at', new Document([ + $this->assertInstanceOf('Utopia\Database\Document', $this->getDatabase()->createCollection('created_at')); + $this->getDatabase()->createAttribute('created_at', 'title', Database::VAR_STRING, 100, false); + $document = $this->getDatabase()->createDocument('created_at', new Document([ '$id' => ID::custom('uid123'), '$permissions' => [ @@ -493,11 +502,11 @@ public function testCreatedAtUpdatedAt(): void public function testQueryTimeout(): void { if ($this->getDatabase()->getAdapter()->getSupportForTimeouts()) { - static::getDatabase()->createCollection('global-timeouts'); - $this->assertEquals(true, static::getDatabase()->createAttribute('global-timeouts', 'longtext', Database::VAR_STRING, 100000000, true)); + $this->getDatabase()->createCollection('global-timeouts'); + $this->assertEquals(true, $this->getDatabase()->createAttribute('global-timeouts', 'longtext', Database::VAR_STRING, 100000000, true)); for ($i = 0 ; $i <= 20 ; $i++) { - static::getDatabase()->createDocument('global-timeouts', new Document([ + $this->getDatabase()->createDocument('global-timeouts', new Document([ 'longtext' => file_get_contents(__DIR__ . '/../../resources/longtext.txt'), '$permissions' => [ Permission::read(Role::any()), @@ -509,15 +518,15 @@ public function testQueryTimeout(): void $this->expectException(TimeoutException::class); - static::getDatabase()->setTimeout(1); + $this->getDatabase()->setTimeout(1); try { - static::getDatabase()->find('global-timeouts', [ + $this->getDatabase()->find('global-timeouts', [ Query::notEqual('longtext', 'appwrite'), ]); } catch(TimeoutException $ex) { - static::getDatabase()->clearTimeout(); - static::getDatabase()->deleteCollection('global-timeouts'); + $this->getDatabase()->clearTimeout(); + $this->getDatabase()->deleteCollection('global-timeouts'); throw $ex; } } @@ -530,65 +539,65 @@ public function testQueryTimeout(): void */ public function testCreateListExistsDeleteCollection(): void { - $this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createCollection('actors', permissions: [ + $this->assertInstanceOf('Utopia\Database\Document', $this->getDatabase()->createCollection('actors', permissions: [ Permission::create(Role::any()), Permission::read(Role::any()), ])); - $this->assertCount(1, static::getDatabase()->listCollections()); - $this->assertEquals(true, static::getDatabase()->exists($this->testDatabase, 'actors')); + $this->assertCount(1, $this->getDatabase()->listCollections()); + $this->assertEquals(true, $this->getDatabase()->exists($this->testDatabase, 'actors')); // Collection names should not be unique - $this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createCollection('actors2', permissions: [ + $this->assertInstanceOf('Utopia\Database\Document', $this->getDatabase()->createCollection('actors2', permissions: [ Permission::create(Role::any()), Permission::read(Role::any()), ])); - $this->assertCount(2, static::getDatabase()->listCollections()); - $this->assertEquals(true, static::getDatabase()->exists($this->testDatabase, 'actors2')); - $collection = static::getDatabase()->getCollection('actors2'); + $this->assertCount(2, $this->getDatabase()->listCollections()); + $this->assertEquals(true, $this->getDatabase()->exists($this->testDatabase, 'actors2')); + $collection = $this->getDatabase()->getCollection('actors2'); $collection->setAttribute('name', 'actors'); // change name to one that exists - $this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->updateDocument( + $this->assertInstanceOf('Utopia\Database\Document', $this->getDatabase()->updateDocument( $collection->getCollection(), $collection->getId(), $collection )); - $this->assertEquals(true, static::getDatabase()->deleteCollection('actors2')); // Delete collection when finished - $this->assertCount(1, static::getDatabase()->listCollections()); + $this->assertEquals(true, $this->getDatabase()->deleteCollection('actors2')); // Delete collection when finished + $this->assertCount(1, $this->getDatabase()->listCollections()); - $this->assertEquals(false, static::getDatabase()->getCollection('actors')->isEmpty()); - $this->assertEquals(true, static::getDatabase()->deleteCollection('actors')); - $this->assertEquals(true, static::getDatabase()->getCollection('actors')->isEmpty()); - $this->assertEquals(false, static::getDatabase()->exists($this->testDatabase, 'actors')); + $this->assertEquals(false, $this->getDatabase()->getCollection('actors')->isEmpty()); + $this->assertEquals(true, $this->getDatabase()->deleteCollection('actors')); + $this->assertEquals(true, $this->getDatabase()->getCollection('actors')->isEmpty()); + $this->assertEquals(false, $this->getDatabase()->exists($this->testDatabase, 'actors')); } public function testSizeCollection(): void { - static::getDatabase()->createCollection('sizeTest1'); - static::getDatabase()->createCollection('sizeTest2'); + $this->getDatabase()->createCollection('sizeTest1'); + $this->getDatabase()->createCollection('sizeTest2'); - $size1 = static::getDatabase()->getSizeOfCollection('sizeTest1'); - $size2 = static::getDatabase()->getSizeOfCollection('sizeTest2'); + $size1 = $this->getDatabase()->getSizeOfCollection('sizeTest1'); + $size2 = $this->getDatabase()->getSizeOfCollection('sizeTest2'); $sizeDifference = abs($size1 - $size2); // Size of an empty collection returns either 172032 or 167936 bytes randomly // Therefore asserting with a tolerance of 5000 bytes $byteDifference = 5000; $this->assertLessThan($byteDifference, $sizeDifference); - static::getDatabase()->createAttribute('sizeTest2', 'string1', Database::VAR_STRING, 128, true); - static::getDatabase()->createAttribute('sizeTest2', 'string2', Database::VAR_STRING, 254 + 1, true); - static::getDatabase()->createAttribute('sizeTest2', 'string3', Database::VAR_STRING, 254 + 1, true); - static::getDatabase()->createIndex('sizeTest2', 'index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); + $this->getDatabase()->createAttribute('sizeTest2', 'string1', Database::VAR_STRING, 128, true); + $this->getDatabase()->createAttribute('sizeTest2', 'string2', Database::VAR_STRING, 254 + 1, true); + $this->getDatabase()->createAttribute('sizeTest2', 'string3', Database::VAR_STRING, 254 + 1, true); + $this->getDatabase()->createIndex('sizeTest2', 'index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); $loopCount = 40; for ($i = 0; $i < $loopCount; $i++) { - static::getDatabase()->createDocument('sizeTest2', new Document([ + $this->getDatabase()->createDocument('sizeTest2', new Document([ 'string1' => 'string1' . $i, 'string2' => 'string2' . $i, 'string3' => 'string3' . $i, ])); } - $size2 = static::getDatabase()->getSizeOfCollection('sizeTest2'); + $size2 = $this->getDatabase()->getSizeOfCollection('sizeTest2'); $this->assertGreaterThan($size1, $size2); } @@ -596,126 +605,126 @@ public function testSizeCollection(): void public function testSizeFullText(): void { // SQLite does not support fulltext indexes - if (!static::getDatabase()->getAdapter()->getSupportForFulltextIndex()) { + if (!$this->getDatabase()->getAdapter()->getSupportForFulltextIndex()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('fullTextSizeTest'); + $this->getDatabase()->createCollection('fullTextSizeTest'); - $size1 = static::getDatabase()->getSizeOfCollection('fullTextSizeTest'); + $size1 = $this->getDatabase()->getSizeOfCollection('fullTextSizeTest'); - static::getDatabase()->createAttribute('fullTextSizeTest', 'string1', Database::VAR_STRING, 128, true); - static::getDatabase()->createAttribute('fullTextSizeTest', 'string2', Database::VAR_STRING, 254, true); - static::getDatabase()->createAttribute('fullTextSizeTest', 'string3', Database::VAR_STRING, 254, true); - static::getDatabase()->createIndex('fullTextSizeTest', 'index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); + $this->getDatabase()->createAttribute('fullTextSizeTest', 'string1', Database::VAR_STRING, 128, true); + $this->getDatabase()->createAttribute('fullTextSizeTest', 'string2', Database::VAR_STRING, 254, true); + $this->getDatabase()->createAttribute('fullTextSizeTest', 'string3', Database::VAR_STRING, 254, true); + $this->getDatabase()->createIndex('fullTextSizeTest', 'index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128]); $loopCount = 10; for ($i = 0; $i < $loopCount; $i++) { - static::getDatabase()->createDocument('fullTextSizeTest', new Document([ + $this->getDatabase()->createDocument('fullTextSizeTest', new Document([ 'string1' => 'string1' . $i, 'string2' => 'string2' . $i, 'string3' => 'string3' . $i, ])); } - $size2 = static::getDatabase()->getSizeOfCollection('fullTextSizeTest'); + $size2 = $this->getDatabase()->getSizeOfCollection('fullTextSizeTest'); $this->assertGreaterThan($size1, $size2); - static::getDatabase()->createIndex('fullTextSizeTest', 'fulltext_index', Database::INDEX_FULLTEXT, ['string1']); + $this->getDatabase()->createIndex('fullTextSizeTest', 'fulltext_index', Database::INDEX_FULLTEXT, ['string1']); - $size3 = static::getDatabase()->getSizeOfCollection('fullTextSizeTest'); + $size3 = $this->getDatabase()->getSizeOfCollection('fullTextSizeTest'); $this->assertGreaterThan($size2, $size3); } public function testCreateDeleteAttribute(): void { - static::getDatabase()->createCollection('attributes'); + $this->getDatabase()->createCollection('attributes'); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string1', Database::VAR_STRING, 128, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string2', Database::VAR_STRING, 16382 + 1, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string3', Database::VAR_STRING, 65535 + 1, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string4', Database::VAR_STRING, 16777215 + 1, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'integer', Database::VAR_INTEGER, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'bigint', Database::VAR_INTEGER, 8, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'float', Database::VAR_FLOAT, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'boolean', Database::VAR_BOOLEAN, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'string1', Database::VAR_STRING, 128, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'string2', Database::VAR_STRING, 16382 + 1, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'string3', Database::VAR_STRING, 65535 + 1, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'string4', Database::VAR_STRING, 16777215 + 1, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'integer', Database::VAR_INTEGER, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'bigint', Database::VAR_INTEGER, 8, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'float', Database::VAR_FLOAT, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'boolean', Database::VAR_BOOLEAN, 0, true)); - $this->assertEquals(true, static::getDatabase()->createIndex('attributes', 'string1_index', Database::INDEX_KEY, ['string1'])); - $this->assertEquals(true, static::getDatabase()->createIndex('attributes', 'string2_index', Database::INDEX_KEY, ['string2'], [255])); - $this->assertEquals(true, static::getDatabase()->createIndex('attributes', 'multi_index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128])); + $this->assertEquals(true, $this->getDatabase()->createIndex('attributes', 'string1_index', Database::INDEX_KEY, ['string1'])); + $this->assertEquals(true, $this->getDatabase()->createIndex('attributes', 'string2_index', Database::INDEX_KEY, ['string2'], [255])); + $this->assertEquals(true, $this->getDatabase()->createIndex('attributes', 'multi_index', Database::INDEX_KEY, ['string1', 'string2', 'string3'], [128, 128, 128])); - $collection = static::getDatabase()->getCollection('attributes'); + $collection = $this->getDatabase()->getCollection('attributes'); $this->assertCount(8, $collection->getAttribute('attributes')); $this->assertCount(3, $collection->getAttribute('indexes')); // Array - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string_list', Database::VAR_STRING, 128, true, null, true, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'integer_list', Database::VAR_INTEGER, 0, true, null, true, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'float_list', Database::VAR_FLOAT, 0, true, null, true, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'boolean_list', Database::VAR_BOOLEAN, 0, true, null, true, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'string_list', Database::VAR_STRING, 128, true, null, true, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'integer_list', Database::VAR_INTEGER, 0, true, null, true, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'float_list', Database::VAR_FLOAT, 0, true, null, true, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'boolean_list', Database::VAR_BOOLEAN, 0, true, null, true, true)); - $collection = static::getDatabase()->getCollection('attributes'); + $collection = $this->getDatabase()->getCollection('attributes'); $this->assertCount(12, $collection->getAttribute('attributes')); // Default values - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string_default', Database::VAR_STRING, 256, false, 'test')); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'integer_default', Database::VAR_INTEGER, 0, false, 1)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'float_default', Database::VAR_FLOAT, 0, false, 1.5)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'boolean_default', Database::VAR_BOOLEAN, 0, false, false)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'datetime_default', Database::VAR_DATETIME, 0, false, '2000-06-12T14:12:55.000+00:00', true, false, null, [], ['datetime'])); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'string_default', Database::VAR_STRING, 256, false, 'test')); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'integer_default', Database::VAR_INTEGER, 0, false, 1)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'float_default', Database::VAR_FLOAT, 0, false, 1.5)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'boolean_default', Database::VAR_BOOLEAN, 0, false, false)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'datetime_default', Database::VAR_DATETIME, 0, false, '2000-06-12T14:12:55.000+00:00', true, false, null, [], ['datetime'])); - $collection = static::getDatabase()->getCollection('attributes'); + $collection = $this->getDatabase()->getCollection('attributes'); $this->assertCount(17, $collection->getAttribute('attributes')); // Delete - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'string1')); - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'string2')); - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'string3')); - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'string4')); - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'integer')); - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'bigint')); - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'float')); - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'boolean')); - - $collection = static::getDatabase()->getCollection('attributes'); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'string1')); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'string2')); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'string3')); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'string4')); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'integer')); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'bigint')); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'float')); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'boolean')); + + $collection = $this->getDatabase()->getCollection('attributes'); $this->assertCount(9, $collection->getAttribute('attributes')); $this->assertCount(0, $collection->getAttribute('indexes')); // Delete Array - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'string_list')); - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'integer_list')); - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'float_list')); - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'boolean_list')); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'string_list')); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'integer_list')); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'float_list')); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'boolean_list')); - $collection = static::getDatabase()->getCollection('attributes'); + $collection = $this->getDatabase()->getCollection('attributes'); $this->assertCount(5, $collection->getAttribute('attributes')); // Delete default - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'string_default')); - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'integer_default')); - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'float_default')); - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'boolean_default')); - $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'datetime_default')); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'string_default')); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'integer_default')); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'float_default')); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'boolean_default')); + $this->assertEquals(true, $this->getDatabase()->deleteAttribute('attributes', 'datetime_default')); - $collection = static::getDatabase()->getCollection('attributes'); + $collection = $this->getDatabase()->getCollection('attributes'); $this->assertCount(0, $collection->getAttribute('attributes')); // Test for custom chars in ID - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'as_5dasdasdas', Database::VAR_BOOLEAN, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'as5dasdasdas_', Database::VAR_BOOLEAN, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', '.as5dasdasdas', Database::VAR_BOOLEAN, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', '-as5dasdasdas', Database::VAR_BOOLEAN, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'as-5dasdasdas', Database::VAR_BOOLEAN, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'as5dasdasdas-', Database::VAR_BOOLEAN, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'socialAccountForYoutubeSubscribersss', Database::VAR_BOOLEAN, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', '5f058a89258075f058a89258075f058t9214', Database::VAR_BOOLEAN, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'as_5dasdasdas', Database::VAR_BOOLEAN, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'as5dasdasdas_', Database::VAR_BOOLEAN, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', '.as5dasdasdas', Database::VAR_BOOLEAN, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', '-as5dasdasdas', Database::VAR_BOOLEAN, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'as-5dasdasdas', Database::VAR_BOOLEAN, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'as5dasdasdas-', Database::VAR_BOOLEAN, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'socialAccountForYoutubeSubscribersss', Database::VAR_BOOLEAN, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', '5f058a89258075f058a89258075f058t9214', Database::VAR_BOOLEAN, 0, true)); // Using this collection to test invalid default values - // static::getDatabase()->deleteCollection('attributes'); + // $this->getDatabase()->deleteCollection('attributes'); } /** @@ -749,7 +758,7 @@ public function invalidDefaultValues(): array public function testInvalidDefaultValues(string $type, mixed $default): void { $this->expectException(\Exception::class); - $this->assertEquals(false, static::getDatabase()->createAttribute('attributes', 'bad_default', $type, 256, true, $default)); + $this->assertEquals(false, $this->getDatabase()->createAttribute('attributes', 'bad_default', $type, 256, true, $default)); } /** @@ -757,18 +766,18 @@ public function testInvalidDefaultValues(string $type, mixed $default): void */ public function testAttributeCaseInsensitivity(): void { - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'caseSensitive', Database::VAR_STRING, 128, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'caseSensitive', Database::VAR_STRING, 128, true)); $this->expectException(DuplicateException::class); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'CaseSensitive', Database::VAR_STRING, 128, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributes', 'CaseSensitive', Database::VAR_STRING, 128, true)); } public function testAttributeKeyWithSymbols(): void { - static::getDatabase()->createCollection('attributesWithKeys'); + $this->getDatabase()->createCollection('attributesWithKeys'); - $this->assertEquals(true, static::getDatabase()->createAttribute('attributesWithKeys', 'key_with.sym$bols', Database::VAR_STRING, 128, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('attributesWithKeys', 'key_with.sym$bols', Database::VAR_STRING, 128, true)); - $document = static::getDatabase()->createDocument('attributesWithKeys', new Document([ + $document = $this->getDatabase()->createDocument('attributesWithKeys', new Document([ 'key_with.sym$bols' => 'value', '$permissions' => [ Permission::read(Role::any()), @@ -777,7 +786,7 @@ public function testAttributeKeyWithSymbols(): void $this->assertEquals('value', $document->getAttribute('key_with.sym$bols')); - $document = static::getDatabase()->getDocument('attributesWithKeys', $document->getId()); + $document = $this->getDatabase()->getDocument('attributesWithKeys', $document->getId()); $this->assertEquals('value', $document->getAttribute('key_with.sym$bols')); } @@ -785,7 +794,7 @@ public function testAttributeKeyWithSymbols(): void public function testCollectionNotFound(): void { try { - static::getDatabase()->find('not_exist', []); + $this->getDatabase()->find('not_exist', []); $this->fail('Failed to throw Exception'); } catch (Exception $e) { $this->assertEquals('Collection not found', $e->getMessage()); @@ -794,9 +803,9 @@ public function testCollectionNotFound(): void public function testAttributeNamesWithDots(): void { - static::getDatabase()->createCollection('dots.parent'); + $this->getDatabase()->createCollection('dots.parent'); - $this->assertTrue(static::getDatabase()->createAttribute( + $this->assertTrue($this->getDatabase()->createAttribute( collection: 'dots.parent', id: 'dots.name', type: Database::VAR_STRING, @@ -804,14 +813,14 @@ public function testAttributeNamesWithDots(): void required: false )); - $document = static::getDatabase()->find('dots.parent', [ + $document = $this->getDatabase()->find('dots.parent', [ Query::select(['dots.name']), ]); $this->assertEmpty($document); - static::getDatabase()->createCollection('dots'); + $this->getDatabase()->createCollection('dots'); - $this->assertTrue(static::getDatabase()->createAttribute( + $this->assertTrue($this->getDatabase()->createAttribute( collection: 'dots', id: 'name', type: Database::VAR_STRING, @@ -819,13 +828,13 @@ public function testAttributeNamesWithDots(): void required: false )); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'dots.parent', relatedCollection: 'dots', type: Database::RELATION_ONE_TO_ONE ); - static::getDatabase()->createDocument('dots.parent', new Document([ + $this->getDatabase()->createDocument('dots.parent', new Document([ '$id' => ID::custom('father'), 'dots.name' => 'Bill clinton', '$permissions' => [ @@ -845,7 +854,7 @@ public function testAttributeNamesWithDots(): void ] ])); - $documents = static::getDatabase()->find('dots.parent', [ + $documents = $this->getDatabase()->find('dots.parent', [ Query::select(['*']), ]); @@ -857,9 +866,9 @@ public function testAttributeNamesWithDots(): void */ public function testIndexCaseInsensitivity(): void { - $this->assertEquals(true, static::getDatabase()->createIndex('attributes', 'key_caseSensitive', Database::INDEX_KEY, ['caseSensitive'], [128])); + $this->assertEquals(true, $this->getDatabase()->createIndex('attributes', 'key_caseSensitive', Database::INDEX_KEY, ['caseSensitive'], [128])); $this->expectException(DuplicateException::class); - $this->assertEquals(true, static::getDatabase()->createIndex('attributes', 'key_CaseSensitive', Database::INDEX_KEY, ['caseSensitive'], [128])); + $this->assertEquals(true, $this->getDatabase()->createIndex('attributes', 'key_CaseSensitive', Database::INDEX_KEY, ['caseSensitive'], [128])); } /** @@ -869,7 +878,7 @@ public function testIndexCaseInsensitivity(): void */ public function testCleanupAttributeTests(): void { - static::getDatabase()->deleteCollection('attributes'); + $this->getDatabase()->deleteCollection('attributes'); $this->assertEquals(1, 1); } @@ -880,42 +889,42 @@ public function testCleanupAttributeTests(): void public function testUnknownFormat(): void { $this->expectException(\Exception::class); - $this->assertEquals(false, static::getDatabase()->createAttribute('attributes', 'bad_format', Database::VAR_STRING, 256, true, null, true, false, 'url')); + $this->assertEquals(false, $this->getDatabase()->createAttribute('attributes', 'bad_format', Database::VAR_STRING, 256, true, null, true, false, 'url')); } public function testCreateDeleteIndex(): void { - static::getDatabase()->createCollection('indexes'); + $this->getDatabase()->createCollection('indexes'); - $this->assertEquals(true, static::getDatabase()->createAttribute('indexes', 'string', Database::VAR_STRING, 128, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('indexes', 'order', Database::VAR_STRING, 128, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('indexes', 'integer', Database::VAR_INTEGER, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('indexes', 'float', Database::VAR_FLOAT, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('indexes', 'boolean', Database::VAR_BOOLEAN, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('indexes', 'string', Database::VAR_STRING, 128, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('indexes', 'order', Database::VAR_STRING, 128, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('indexes', 'integer', Database::VAR_INTEGER, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('indexes', 'float', Database::VAR_FLOAT, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('indexes', 'boolean', Database::VAR_BOOLEAN, 0, true)); // Indexes - $this->assertEquals(true, static::getDatabase()->createIndex('indexes', 'index1', Database::INDEX_KEY, ['string', 'integer'], [128], [Database::ORDER_ASC])); - $this->assertEquals(true, static::getDatabase()->createIndex('indexes', 'index2', Database::INDEX_KEY, ['float', 'integer'], [], [Database::ORDER_ASC, Database::ORDER_DESC])); - $this->assertEquals(true, static::getDatabase()->createIndex('indexes', 'index3', Database::INDEX_KEY, ['integer', 'boolean'], [], [Database::ORDER_ASC, Database::ORDER_DESC, Database::ORDER_DESC])); - $this->assertEquals(true, static::getDatabase()->createIndex('indexes', 'index4', Database::INDEX_UNIQUE, ['string'], [128], [Database::ORDER_ASC])); - $this->assertEquals(true, static::getDatabase()->createIndex('indexes', 'index5', Database::INDEX_UNIQUE, ['$id', 'string'], [128], [Database::ORDER_ASC])); - $this->assertEquals(true, static::getDatabase()->createIndex('indexes', 'order', Database::INDEX_UNIQUE, ['order'], [128], [Database::ORDER_ASC])); - - $collection = static::getDatabase()->getCollection('indexes'); + $this->assertEquals(true, $this->getDatabase()->createIndex('indexes', 'index1', Database::INDEX_KEY, ['string', 'integer'], [128], [Database::ORDER_ASC])); + $this->assertEquals(true, $this->getDatabase()->createIndex('indexes', 'index2', Database::INDEX_KEY, ['float', 'integer'], [], [Database::ORDER_ASC, Database::ORDER_DESC])); + $this->assertEquals(true, $this->getDatabase()->createIndex('indexes', 'index3', Database::INDEX_KEY, ['integer', 'boolean'], [], [Database::ORDER_ASC, Database::ORDER_DESC, Database::ORDER_DESC])); + $this->assertEquals(true, $this->getDatabase()->createIndex('indexes', 'index4', Database::INDEX_UNIQUE, ['string'], [128], [Database::ORDER_ASC])); + $this->assertEquals(true, $this->getDatabase()->createIndex('indexes', 'index5', Database::INDEX_UNIQUE, ['$id', 'string'], [128], [Database::ORDER_ASC])); + $this->assertEquals(true, $this->getDatabase()->createIndex('indexes', 'order', Database::INDEX_UNIQUE, ['order'], [128], [Database::ORDER_ASC])); + + $collection = $this->getDatabase()->getCollection('indexes'); $this->assertCount(6, $collection->getAttribute('indexes')); // Delete Indexes - $this->assertEquals(true, static::getDatabase()->deleteIndex('indexes', 'index1')); - $this->assertEquals(true, static::getDatabase()->deleteIndex('indexes', 'index2')); - $this->assertEquals(true, static::getDatabase()->deleteIndex('indexes', 'index3')); - $this->assertEquals(true, static::getDatabase()->deleteIndex('indexes', 'index4')); - $this->assertEquals(true, static::getDatabase()->deleteIndex('indexes', 'index5')); - $this->assertEquals(true, static::getDatabase()->deleteIndex('indexes', 'order')); - - $collection = static::getDatabase()->getCollection('indexes'); + $this->assertEquals(true, $this->getDatabase()->deleteIndex('indexes', 'index1')); + $this->assertEquals(true, $this->getDatabase()->deleteIndex('indexes', 'index2')); + $this->assertEquals(true, $this->getDatabase()->deleteIndex('indexes', 'index3')); + $this->assertEquals(true, $this->getDatabase()->deleteIndex('indexes', 'index4')); + $this->assertEquals(true, $this->getDatabase()->deleteIndex('indexes', 'index5')); + $this->assertEquals(true, $this->getDatabase()->deleteIndex('indexes', 'order')); + + $collection = $this->getDatabase()->getCollection('indexes'); $this->assertCount(0, $collection->getAttribute('indexes')); - static::getDatabase()->deleteCollection('indexes'); + $this->getDatabase()->deleteCollection('indexes'); } public function testCreateCollectionWithSchema(): void @@ -974,7 +983,7 @@ public function testCreateCollectionWithSchema(): void ]), ]; - $collection = static::getDatabase()->createCollection('withSchema', $attributes, $indexes); + $collection = $this->getDatabase()->createCollection('withSchema', $attributes, $indexes); $this->assertEquals(false, $collection->isEmpty()); $this->assertEquals('withSchema', $collection->getId()); @@ -997,10 +1006,10 @@ public function testCreateCollectionWithSchema(): void $this->assertEquals('index3', $collection->getAttribute('indexes')[2]['$id']); $this->assertEquals(Database::INDEX_KEY, $collection->getAttribute('indexes')[2]['type']); - static::getDatabase()->deleteCollection('withSchema'); + $this->getDatabase()->deleteCollection('withSchema'); // Test collection with dash (+attribute +index) - $collection2 = static::getDatabase()->createCollection('with-dash', [ + $collection2 = $this->getDatabase()->createCollection('with-dash', [ new Document([ '$id' => ID::custom('attribute-one'), 'type' => Database::VAR_STRING, @@ -1030,7 +1039,7 @@ public function testCreateCollectionWithSchema(): void $this->assertCount(1, $collection2->getAttribute('indexes')); $this->assertEquals('index-one', $collection2->getAttribute('indexes')[0]['$id']); $this->assertEquals(Database::INDEX_KEY, $collection2->getAttribute('indexes')[0]['type']); - static::getDatabase()->deleteCollection('with-dash'); + $this->getDatabase()->deleteCollection('with-dash'); } public function testCreateCollectionValidator(): void @@ -1129,7 +1138,7 @@ public function testCreateCollectionValidator(): void ]; foreach ($collections as $id) { - $collection = static::getDatabase()->createCollection($id, $attributes, $indexes); + $collection = $this->getDatabase()->createCollection($id, $attributes, $indexes); $this->assertEquals(false, $collection->isEmpty()); $this->assertEquals($id, $collection->getId()); @@ -1156,24 +1165,24 @@ public function testCreateCollectionValidator(): void $this->assertEquals('index.4', $collection->getAttribute('indexes')[3]['$id']); $this->assertEquals(Database::INDEX_KEY, $collection->getAttribute('indexes')[3]['type']); - static::getDatabase()->deleteCollection($id); + $this->getDatabase()->deleteCollection($id); } } public function testCreateDocument(): Document { - static::getDatabase()->createCollection('documents'); + $this->getDatabase()->createCollection('documents'); - $this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'string', Database::VAR_STRING, 128, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'integer', Database::VAR_INTEGER, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'bigint', Database::VAR_INTEGER, 8, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'float', Database::VAR_FLOAT, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'boolean', Database::VAR_BOOLEAN, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'colors', Database::VAR_STRING, 32, true, null, true, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'empty', Database::VAR_STRING, 32, false, null, true, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'with-dash', Database::VAR_STRING, 128, false, null)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('documents', 'string', Database::VAR_STRING, 128, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('documents', 'integer', Database::VAR_INTEGER, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('documents', 'bigint', Database::VAR_INTEGER, 8, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('documents', 'float', Database::VAR_FLOAT, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('documents', 'boolean', Database::VAR_BOOLEAN, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('documents', 'colors', Database::VAR_STRING, 32, true, null, true, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('documents', 'empty', Database::VAR_STRING, 32, false, null, true, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('documents', 'with-dash', Database::VAR_STRING, 128, false, null)); - $document = static::getDatabase()->createDocument('documents', new Document([ + $document = $this->getDatabase()->createDocument('documents', new Document([ '$permissions' => [ Permission::read(Role::any()), Permission::read(Role::user(ID::custom('1'))), @@ -1215,7 +1224,7 @@ public function testCreateDocument(): Document $this->assertEquals('Works', $document->getAttribute('with-dash')); // Test create document with manual internal id - $manualIdDocument = static::getDatabase()->createDocument('documents', new Document([ + $manualIdDocument = $this->getDatabase()->createDocument('documents', new Document([ '$id' => '56000', '$internalId' => '56000', '$permissions' => [ @@ -1259,7 +1268,7 @@ public function testCreateDocument(): Document $this->assertEquals([], $manualIdDocument->getAttribute('empty')); $this->assertEquals('Works', $manualIdDocument->getAttribute('with-dash')); - $manualIdDocument = static::getDatabase()->getDocument('documents', '56000'); + $manualIdDocument = $this->getDatabase()->getDocument('documents', '56000'); $this->assertEquals('56000', $manualIdDocument->getInternalId()); $this->assertNotEmpty(true, $manualIdDocument->getId()); @@ -1289,11 +1298,11 @@ public function testCreateDocuments(): array $count = 3; $collection = 'testCreateDocuments'; - static::getDatabase()->createCollection($collection); + $this->getDatabase()->createCollection($collection); - $this->assertEquals(true, static::getDatabase()->createAttribute($collection, 'string', Database::VAR_STRING, 128, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute($collection, 'integer', Database::VAR_INTEGER, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute($collection, 'bigint', Database::VAR_INTEGER, 8, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute($collection, 'string', Database::VAR_STRING, 128, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute($collection, 'integer', Database::VAR_INTEGER, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute($collection, 'bigint', Database::VAR_INTEGER, 8, true)); // Create an array of documents with random attributes. Don't use the createDocument function $documents = []; @@ -1312,7 +1321,7 @@ public function testCreateDocuments(): array ]); } - $documents = static::getDatabase()->createDocuments($collection, $documents, 3); + $documents = $this->getDatabase()->createDocuments($collection, $documents, 3); $this->assertEquals($count, count($documents)); @@ -1331,15 +1340,15 @@ public function testCreateDocuments(): array public function testRespectNulls(): Document { - static::getDatabase()->createCollection('documents_nulls'); + $this->getDatabase()->createCollection('documents_nulls'); - $this->assertEquals(true, static::getDatabase()->createAttribute('documents_nulls', 'string', Database::VAR_STRING, 128, false)); - $this->assertEquals(true, static::getDatabase()->createAttribute('documents_nulls', 'integer', Database::VAR_INTEGER, 0, false)); - $this->assertEquals(true, static::getDatabase()->createAttribute('documents_nulls', 'bigint', Database::VAR_INTEGER, 8, false)); - $this->assertEquals(true, static::getDatabase()->createAttribute('documents_nulls', 'float', Database::VAR_FLOAT, 0, false)); - $this->assertEquals(true, static::getDatabase()->createAttribute('documents_nulls', 'boolean', Database::VAR_BOOLEAN, 0, false)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('documents_nulls', 'string', Database::VAR_STRING, 128, false)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('documents_nulls', 'integer', Database::VAR_INTEGER, 0, false)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('documents_nulls', 'bigint', Database::VAR_INTEGER, 8, false)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('documents_nulls', 'float', Database::VAR_FLOAT, 0, false)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('documents_nulls', 'boolean', Database::VAR_BOOLEAN, 0, false)); - $document = static::getDatabase()->createDocument('documents_nulls', new Document([ + $document = $this->getDatabase()->createDocument('documents_nulls', new Document([ '$permissions' => [ Permission::read(Role::any()), Permission::read(Role::user('1')), @@ -1367,16 +1376,16 @@ public function testRespectNulls(): Document public function testCreateDocumentDefaults(): void { - static::getDatabase()->createCollection('defaults'); + $this->getDatabase()->createCollection('defaults'); - $this->assertEquals(true, static::getDatabase()->createAttribute('defaults', 'string', Database::VAR_STRING, 128, false, 'default')); - $this->assertEquals(true, static::getDatabase()->createAttribute('defaults', 'integer', Database::VAR_INTEGER, 0, false, 1)); - $this->assertEquals(true, static::getDatabase()->createAttribute('defaults', 'float', Database::VAR_FLOAT, 0, false, 1.5)); - $this->assertEquals(true, static::getDatabase()->createAttribute('defaults', 'boolean', Database::VAR_BOOLEAN, 0, false, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('defaults', 'colors', Database::VAR_STRING, 32, false, ['red', 'green', 'blue'], true, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('defaults', 'datetime', Database::VAR_DATETIME, 0, false, '2000-06-12T14:12:55.000+00:00', true, false, null, [], ['datetime'])); + $this->assertEquals(true, $this->getDatabase()->createAttribute('defaults', 'string', Database::VAR_STRING, 128, false, 'default')); + $this->assertEquals(true, $this->getDatabase()->createAttribute('defaults', 'integer', Database::VAR_INTEGER, 0, false, 1)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('defaults', 'float', Database::VAR_FLOAT, 0, false, 1.5)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('defaults', 'boolean', Database::VAR_BOOLEAN, 0, false, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('defaults', 'colors', Database::VAR_STRING, 32, false, ['red', 'green', 'blue'], true, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('defaults', 'datetime', Database::VAR_DATETIME, 0, false, '2000-06-12T14:12:55.000+00:00', true, false, null, [], ['datetime'])); - $document = static::getDatabase()->createDocument('defaults', new Document([ + $document = $this->getDatabase()->createDocument('defaults', new Document([ '$permissions' => [ Permission::read(Role::any()), Permission::create(Role::any()), @@ -1385,7 +1394,7 @@ public function testCreateDocumentDefaults(): void ], ])); - $document2 = static::getDatabase()->getDocument('defaults', $document->getId()); + $document2 = $this->getDatabase()->getDocument('defaults', $document->getId()); $this->assertCount(4, $document2->getPermissions()); $this->assertEquals('read("any")', $document2->getPermissions()[0]); $this->assertEquals('create("any")', $document2->getPermissions()[1]); @@ -1407,7 +1416,7 @@ public function testCreateDocumentDefaults(): void $this->assertEquals('2000-06-12T14:12:55.000+00:00', $document->getAttribute('datetime')); // cleanup collection - static::getDatabase()->deleteCollection('defaults'); + $this->getDatabase()->deleteCollection('defaults'); } /** @@ -1416,14 +1425,14 @@ public function testCreateDocumentDefaults(): void public function testIncreaseDecrease(): Document { $collection = 'increase_decrease'; - static::getDatabase()->createCollection($collection); + $this->getDatabase()->createCollection($collection); - $this->assertEquals(true, static::getDatabase()->createAttribute($collection, 'increase', Database::VAR_INTEGER, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute($collection, 'decrease', Database::VAR_INTEGER, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute($collection, 'increase_text', Database::VAR_STRING, 255, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute($collection, 'increase_float', Database::VAR_FLOAT, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute($collection, 'increase', Database::VAR_INTEGER, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute($collection, 'decrease', Database::VAR_INTEGER, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute($collection, 'increase_text', Database::VAR_STRING, 255, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute($collection, 'increase_float', Database::VAR_FLOAT, 0, true)); - $document = static::getDatabase()->createDocument($collection, new Document([ + $document = $this->getDatabase()->createDocument($collection, new Document([ 'increase' => 100, 'decrease' => 100, 'increase_float' => 100, @@ -1436,21 +1445,21 @@ public function testIncreaseDecrease(): Document ] ])); - $this->assertEquals(true, static::getDatabase()->increaseDocumentAttribute($collection, $document->getId(), 'increase', 1, 101)); + $this->assertEquals(true, $this->getDatabase()->increaseDocumentAttribute($collection, $document->getId(), 'increase', 1, 101)); - $document = static::getDatabase()->getDocument($collection, $document->getId()); + $document = $this->getDatabase()->getDocument($collection, $document->getId()); $this->assertEquals(101, $document->getAttribute('increase')); - $this->assertEquals(true, static::getDatabase()->decreaseDocumentAttribute($collection, $document->getId(), 'decrease', 1, 98)); - $document = static::getDatabase()->getDocument($collection, $document->getId()); + $this->assertEquals(true, $this->getDatabase()->decreaseDocumentAttribute($collection, $document->getId(), 'decrease', 1, 98)); + $document = $this->getDatabase()->getDocument($collection, $document->getId()); $this->assertEquals(99, $document->getAttribute('decrease')); - $this->assertEquals(true, static::getDatabase()->increaseDocumentAttribute($collection, $document->getId(), 'increase_float', 5.5, 110)); - $document = static::getDatabase()->getDocument($collection, $document->getId()); + $this->assertEquals(true, $this->getDatabase()->increaseDocumentAttribute($collection, $document->getId(), 'increase_float', 5.5, 110)); + $document = $this->getDatabase()->getDocument($collection, $document->getId()); $this->assertEquals(105.5, $document->getAttribute('increase_float')); - $this->assertEquals(true, static::getDatabase()->decreaseDocumentAttribute($collection, $document->getId(), 'increase_float', 1.1, 100)); - $document = static::getDatabase()->getDocument($collection, $document->getId()); + $this->assertEquals(true, $this->getDatabase()->decreaseDocumentAttribute($collection, $document->getId(), 'increase_float', 1.1, 100)); + $document = $this->getDatabase()->getDocument($collection, $document->getId()); $this->assertEquals(104.4, $document->getAttribute('increase_float')); return $document; @@ -1462,7 +1471,7 @@ public function testIncreaseDecrease(): Document public function testIncreaseLimitMax(Document $document): void { $this->expectException(Exception::class); - $this->assertEquals(true, static::getDatabase()->increaseDocumentAttribute('increase_decrease', $document->getId(), 'increase', 10.5, 102.4)); + $this->assertEquals(true, $this->getDatabase()->increaseDocumentAttribute('increase_decrease', $document->getId(), 'increase', 10.5, 102.4)); } /** @@ -1471,7 +1480,7 @@ public function testIncreaseLimitMax(Document $document): void public function testDecreaseLimitMin(Document $document): void { $this->expectException(Exception::class); - $this->assertEquals(false, static::getDatabase()->decreaseDocumentAttribute('increase_decrease', $document->getId(), 'decrease', 10, 99)); + $this->assertEquals(false, $this->getDatabase()->decreaseDocumentAttribute('increase_decrease', $document->getId(), 'decrease', 10, 99)); } /** @@ -1480,7 +1489,7 @@ public function testDecreaseLimitMin(Document $document): void public function testIncreaseTextAttribute(Document $document): void { $this->expectException(Exception::class); - $this->assertEquals(false, static::getDatabase()->increaseDocumentAttribute('increase_decrease', $document->getId(), 'increase_text')); + $this->assertEquals(false, $this->getDatabase()->increaseDocumentAttribute('increase_decrease', $document->getId(), 'increase_text')); } /** @@ -1488,7 +1497,7 @@ public function testIncreaseTextAttribute(Document $document): void */ public function testGetDocument(Document $document): Document { - $document = static::getDatabase()->getDocument('documents', $document->getId()); + $document = $this->getDatabase()->getDocument('documents', $document->getId()); $this->assertNotEmpty(true, $document->getId()); $this->assertIsString($document->getAttribute('string')); @@ -1513,7 +1522,7 @@ public function testGetDocumentSelect(Document $document): Document { $documentId = $document->getId(); - $document = static::getDatabase()->getDocument('documents', $documentId, [ + $document = $this->getDatabase()->getDocument('documents', $documentId, [ Query::select(['string', 'integer']), ]); @@ -1534,7 +1543,7 @@ public function testGetDocumentSelect(Document $document): Document $this->assertArrayNotHasKey('$permissions', $document); $this->assertArrayNotHasKey('$collection', $document); - $document = static::getDatabase()->getDocument('documents', $documentId, [ + $document = $this->getDatabase()->getDocument('documents', $documentId, [ Query::select(['string', 'integer', '$id']), ]); @@ -1545,7 +1554,7 @@ public function testGetDocumentSelect(Document $document): Document $this->assertArrayNotHasKey('$permissions', $document); $this->assertArrayNotHasKey('$collection', $document); - $document = static::getDatabase()->getDocument('documents', $documentId, [ + $document = $this->getDatabase()->getDocument('documents', $documentId, [ Query::select(['string', 'integer', '$permissions']), ]); @@ -1556,7 +1565,7 @@ public function testGetDocumentSelect(Document $document): Document $this->assertArrayHasKey('$permissions', $document); $this->assertArrayNotHasKey('$collection', $document); - $document = static::getDatabase()->getDocument('documents', $documentId, [ + $document = $this->getDatabase()->getDocument('documents', $documentId, [ Query::select(['string', 'integer', '$internalId']), ]); @@ -1567,7 +1576,7 @@ public function testGetDocumentSelect(Document $document): Document $this->assertArrayNotHasKey('$permissions', $document); $this->assertArrayNotHasKey('$collection', $document); - $document = static::getDatabase()->getDocument('documents', $documentId, [ + $document = $this->getDatabase()->getDocument('documents', $documentId, [ Query::select(['string', 'integer', '$collection']), ]); @@ -1578,7 +1587,7 @@ public function testGetDocumentSelect(Document $document): Document $this->assertArrayNotHasKey('$permissions', $document); $this->assertArrayHasKey('$collection', $document); - $document = static::getDatabase()->getDocument('documents', $documentId, [ + $document = $this->getDatabase()->getDocument('documents', $documentId, [ Query::select(['string', 'integer', '$createdAt']), ]); @@ -1589,7 +1598,7 @@ public function testGetDocumentSelect(Document $document): Document $this->assertArrayNotHasKey('$permissions', $document); $this->assertArrayNotHasKey('$collection', $document); - $document = static::getDatabase()->getDocument('documents', $documentId, [ + $document = $this->getDatabase()->getDocument('documents', $documentId, [ Query::select(['string', 'integer', '$updatedAt']), ]); @@ -1616,7 +1625,7 @@ public function testFulltextIndexWithInteger(): void $this->expectExceptionMessage('Attribute "integer" cannot be part of a FULLTEXT index, must be of type string'); } - static::getDatabase()->createIndex('documents', 'fulltext_integer', Database::INDEX_FULLTEXT, ['string','integer']); + $this->getDatabase()->createIndex('documents', 'fulltext_integer', Database::INDEX_FULLTEXT, ['string','integer']); } public function testListDocumentSearch(): void @@ -1627,8 +1636,8 @@ public function testListDocumentSearch(): void return; } - static::getDatabase()->createIndex('documents', 'string', Database::INDEX_FULLTEXT, ['string']); - static::getDatabase()->createDocument('documents', new Document([ + $this->getDatabase()->createIndex('documents', 'string', Database::INDEX_FULLTEXT, ['string']); + $this->getDatabase()->createDocument('documents', new Document([ '$permissions' => [ Permission::read(Role::any()), Permission::create(Role::any()), @@ -1647,7 +1656,7 @@ public function testListDocumentSearch(): void /** * Allow reserved keywords for search */ - $documents = static::getDatabase()->find('documents', [ + $documents = $this->getDatabase()->find('documents', [ Query::search('string', '*test+alias@email-provider.com'), ]); @@ -1662,17 +1671,17 @@ public function testEmptySearch(): void return; } - $documents = static::getDatabase()->find('documents', [ + $documents = $this->getDatabase()->find('documents', [ Query::search('string', ''), ]); $this->assertEquals(0, count($documents)); - $documents = static::getDatabase()->find('documents', [ + $documents = $this->getDatabase()->find('documents', [ Query::search('string', '*'), ]); $this->assertEquals(0, count($documents)); - $documents = static::getDatabase()->find('documents', [ + $documents = $this->getDatabase()->find('documents', [ Query::search('string', '<>'), ]); $this->assertEquals(0, count($documents)); @@ -1759,7 +1768,7 @@ public function testUpdateDocuments(array $documents): void ]); } - $documents = static::getDatabase()->updateDocuments( + $documents = $this->getDatabase()->updateDocuments( $collection, $documents, \count($documents) @@ -1770,7 +1779,7 @@ public function testUpdateDocuments(array $documents): void $this->assertEquals(6, $document->getAttribute('integer')); } - $documents = static::getDatabase()->find($collection, [ + $documents = $this->getDatabase()->find($collection, [ Query::limit(\count($documents)) ]); @@ -1862,9 +1871,9 @@ public function testDeleteDocument(Document $document): void */ public function testArrayAttribute(): void { - Authorization::setRole(Role::any()->toString()); + self::$authorization->addRole(Role::any()->toString()); - $database = static::getDatabase(); + $database = $this->getDatabase(); $collection = 'json'; $permissions = [Permission::read(Role::any())]; @@ -2093,23 +2102,23 @@ public function testArrayAttribute(): void */ public function testFind(): array { - Authorization::setRole(Role::any()->toString()); + self::$authorization->addRole(Role::any()->toString()); - static::getDatabase()->createCollection('movies', permissions: [ + $this->getDatabase()->createCollection('movies', permissions: [ Permission::create(Role::any()), Permission::update(Role::users()) ]); - $this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'name', Database::VAR_STRING, 128, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'director', Database::VAR_STRING, 128, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'year', Database::VAR_INTEGER, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'price', Database::VAR_FLOAT, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'active', Database::VAR_BOOLEAN, 0, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'genres', Database::VAR_STRING, 32, true, null, true, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'with-dash', Database::VAR_STRING, 128, true)); - $this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'nullable', Database::VAR_STRING, 128, false)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('movies', 'name', Database::VAR_STRING, 128, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('movies', 'director', Database::VAR_STRING, 128, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('movies', 'year', Database::VAR_INTEGER, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('movies', 'price', Database::VAR_FLOAT, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('movies', 'active', Database::VAR_BOOLEAN, 0, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('movies', 'genres', Database::VAR_STRING, 32, true, null, true, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('movies', 'with-dash', Database::VAR_STRING, 128, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('movies', 'nullable', Database::VAR_STRING, 128, false)); - $document = static::getDatabase()->createDocument('movies', new Document([ + $document = $this->getDatabase()->createDocument('movies', new Document([ '$id' => ID::custom('frozen'), '$permissions' => [ Permission::read(Role::any()), @@ -2134,7 +2143,7 @@ public function testFind(): array 'with-dash' => 'Works' ])); - static::getDatabase()->createDocument('movies', new Document([ + $this->getDatabase()->createDocument('movies', new Document([ '$permissions' => [ Permission::read(Role::any()), Permission::read(Role::user('1')), @@ -2158,7 +2167,7 @@ public function testFind(): array 'with-dash' => 'Works' ])); - static::getDatabase()->createDocument('movies', new Document([ + $this->getDatabase()->createDocument('movies', new Document([ '$permissions' => [ Permission::read(Role::any()), Permission::read(Role::user('1')), @@ -2182,7 +2191,7 @@ public function testFind(): array 'with-dash' => 'Works2' ])); - static::getDatabase()->createDocument('movies', new Document([ + $this->getDatabase()->createDocument('movies', new Document([ '$permissions' => [ Permission::read(Role::any()), Permission::read(Role::user('1')), @@ -2206,7 +2215,7 @@ public function testFind(): array 'with-dash' => 'Works2' ])); - static::getDatabase()->createDocument('movies', new Document([ + $this->getDatabase()->createDocument('movies', new Document([ '$permissions' => [ Permission::read(Role::any()), Permission::read(Role::user('1')), @@ -2230,7 +2239,7 @@ public function testFind(): array 'with-dash' => 'Works3' ])); - static::getDatabase()->createDocument('movies', new Document([ + $this->getDatabase()->createDocument('movies', new Document([ '$permissions' => [ Permission::read(Role::user('x')), Permission::create(Role::any()), @@ -2260,7 +2269,7 @@ public function testFind(): array public function testFindBasicChecks(): void { - $documents = static::getDatabase()->find('movies'); + $documents = $this->getDatabase()->find('movies'); $movieDocuments = $documents; $this->assertEquals(5, count($documents)); @@ -2293,13 +2302,13 @@ public function testFindBasicChecks(): void /** * Check $id: Notice, this orders ID names alphabetically, not by internal numeric ID */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(25), Query::offset(0), Query::orderDesc('$id'), ]); $this->assertEquals($lastDocumentId, $documents[0]->getId()); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(25), Query::offset(0), Query::orderAsc('$id'), @@ -2309,13 +2318,13 @@ public function testFindBasicChecks(): void /** * Check internal numeric ID sorting */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(25), Query::offset(0), Query::orderDesc(''), ]); $this->assertEquals($movieDocuments[\count($movieDocuments) - 1]->getId(), $documents[0]->getId()); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(25), Query::offset(0), Query::orderAsc(''), @@ -2328,8 +2337,8 @@ public function testFindCheckPermissions(): void /** * Check Permissions */ - Authorization::setRole('user:x'); - $documents = static::getDatabase()->find('movies'); + self::$authorization->addRole('user:x'); + $documents = $this->getDatabase()->find('movies'); $this->assertEquals(6, count($documents)); } @@ -2339,13 +2348,13 @@ public function testFindCheckInteger(): void /** * Query with dash attribute */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::equal('with-dash', ['Works']), ]); $this->assertEquals(2, count($documents)); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::equal('with-dash', ['Works2', 'Works3']), ]); @@ -2354,7 +2363,7 @@ public function testFindCheckInteger(): void /** * Check an Integer condition */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::equal('year', [2019]), ]); @@ -2368,7 +2377,7 @@ public function testFindBoolean(): void /** * Boolean condition */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::equal('active', [true]), ]); @@ -2380,7 +2389,7 @@ public function testFindStringQueryEqual(): void /** * String condition */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::equal('director', ['TBD']), ]); @@ -2392,7 +2401,7 @@ public function testFindNotEqual(): void /** * Not Equal query */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::notEqual('director', 'TBD'), ]); @@ -2406,22 +2415,22 @@ public function testFindNotEqual(): void public function testFindBetween(): void { - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::between('price', 25.94, 25.99), ]); $this->assertEquals(2, count($documents)); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::between('price', 30, 35), ]); $this->assertEquals(0, count($documents)); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::between('$createdAt', '1975-12-06', '2050-12-06'), ]); $this->assertEquals(6, count($documents)); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::between('$updatedAt', '1975-12-06T07:08:49.733+02:00', '2050-02-05T10:15:21.825+00:00'), ]); $this->assertEquals(6, count($documents)); @@ -2432,7 +2441,7 @@ public function testFindFloat(): void /** * Float condition */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::lessThan('price', 26.00), Query::greaterThan('price', 25.98), ]); @@ -2447,7 +2456,7 @@ public function testFindContains(): void return; } - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::contains('genres', ['comics']) ]); @@ -2456,20 +2465,20 @@ public function testFindContains(): void /** * Array contains OR condition */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::contains('genres', ['comics', 'kids']), ]); $this->assertEquals(4, count($documents)); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::contains('genres', ['non-existent']), ]); $this->assertEquals(0, count($documents)); try { - static::getDatabase()->find('movies', [ + $this->getDatabase()->find('movies', [ Query::contains('price', [10.5]), ]); $this->fail('Failed to throw exception'); @@ -2485,10 +2494,10 @@ public function testFindFulltext(): void * Fulltext search */ if ($this->getDatabase()->getAdapter()->getSupportForFulltextIndex()) { - $success = static::getDatabase()->createIndex('movies', 'name', Database::INDEX_FULLTEXT, ['name']); + $success = $this->getDatabase()->createIndex('movies', 'name', Database::INDEX_FULLTEXT, ['name']); $this->assertEquals(true, $success); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::search('name', 'captain'), ]); @@ -2502,7 +2511,7 @@ public function testFindFulltext(): void // TODO: I think this needs a changes? how do we distinguish between regular full text and wildcard? if ($this->getDatabase()->getAdapter()->getSupportForFulltextWildCardIndex()) { - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::search('name', 'cap'), ]); @@ -2515,62 +2524,62 @@ public function testFindFulltext(): void public function testFindFulltextSpecialChars(): void { - if (!static::getDatabase()->getAdapter()->getSupportForFulltextIndex()) { + if (!$this->getDatabase()->getAdapter()->getSupportForFulltextIndex()) { $this->expectNotToPerformAssertions(); return; } $collection = 'full_text'; - static::getDatabase()->createCollection($collection, permissions: [ + $this->getDatabase()->createCollection($collection, permissions: [ Permission::create(Role::any()), Permission::update(Role::users()) ]); - $this->assertTrue(static::getDatabase()->createAttribute($collection, 'ft', Database::VAR_STRING, 128, true)); - $this->assertTrue(static::getDatabase()->createIndex($collection, 'ft-index', Database::INDEX_FULLTEXT, ['ft'])); + $this->assertTrue($this->getDatabase()->createAttribute($collection, 'ft', Database::VAR_STRING, 128, true)); + $this->assertTrue($this->getDatabase()->createIndex($collection, 'ft-index', Database::INDEX_FULLTEXT, ['ft'])); - static::getDatabase()->createDocument($collection, new Document([ + $this->getDatabase()->createDocument($collection, new Document([ '$permissions' => [Permission::read(Role::any())], 'ft' => 'Alf: chapter_4@nasa.com' ])); - $documents = static::getDatabase()->find($collection, [ + $documents = $this->getDatabase()->find($collection, [ Query::search('ft', 'chapter_4'), ]); $this->assertEquals(1, count($documents)); - static::getDatabase()->createDocument($collection, new Document([ + $this->getDatabase()->createDocument($collection, new Document([ '$permissions' => [Permission::read(Role::any())], 'ft' => 'al@ba.io +-*)(<>~' ])); - $documents = static::getDatabase()->find($collection, [ + $documents = $this->getDatabase()->find($collection, [ Query::search('ft', 'al@ba.io'), // === al ba io* ]); - if (static::getDatabase()->getAdapter()->getSupportForFulltextWildcardIndex()) { + if ($this->getDatabase()->getAdapter()->getSupportForFulltextWildcardIndex()) { $this->assertEquals(0, count($documents)); } else { $this->assertEquals(1, count($documents)); } - static::getDatabase()->createDocument($collection, new Document([ + $this->getDatabase()->createDocument($collection, new Document([ '$permissions' => [Permission::read(Role::any())], 'ft' => 'donald duck' ])); - static::getDatabase()->createDocument($collection, new Document([ + $this->getDatabase()->createDocument($collection, new Document([ '$permissions' => [Permission::read(Role::any())], 'ft' => 'donald trump' ])); - $documents = static::getDatabase()->find($collection, [ + $documents = $this->getDatabase()->find($collection, [ Query::search('ft', 'donald trump'), Query::orderAsc('ft'), ]); $this->assertEquals(2, count($documents)); - $documents = static::getDatabase()->find($collection, [ + $documents = $this->getDatabase()->find($collection, [ Query::search('ft', '"donald trump"'), // Exact match ]); @@ -2582,7 +2591,7 @@ public function testFindMultipleConditions(): void /** * Multiple conditions */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::equal('director', ['TBD']), Query::equal('year', [2026]), ]); @@ -2592,7 +2601,7 @@ public function testFindMultipleConditions(): void /** * Multiple conditions and OR values */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::equal('name', ['Frozen II', 'Captain Marvel']), ]); @@ -2606,7 +2615,7 @@ public function testFindByID(): void /** * $id condition */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::equal('$id', ['frozen']), ]); @@ -2626,7 +2635,7 @@ public function testFindByInternalID(array $data): void /** * Test that internal ID queries are handled correctly */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::equal('$internalId', [$data['$internalId']]), ]); @@ -2639,7 +2648,7 @@ public function testFindByInternalID(array $data): void */ public function testSelectInternalID(): void { - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::select(['$internalId', '$id']), Query::orderAsc(''), Query::limit(1), @@ -2650,7 +2659,7 @@ public function testSelectInternalID(): void $this->assertArrayHasKey('$internalId', $document); $this->assertCount(2, $document); - $document = static::getDatabase()->getDocument('movies', $document->getId(), [ + $document = $this->getDatabase()->getDocument('movies', $document->getId(), [ Query::select(['$internalId']), ]); @@ -2663,7 +2672,7 @@ public function testFindOrderBy(): void /** * ORDER BY */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(25), Query::offset(0), Query::orderDesc('price'), @@ -2684,11 +2693,11 @@ public function testFindOrderByNatural(): void /** * ORDER BY natural */ - $base = array_reverse(static::getDatabase()->find('movies', [ + $base = array_reverse($this->getDatabase()->find('movies', [ Query::limit(25), Query::offset(0), ])); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(25), Query::offset(0), Query::orderDesc(''), @@ -2708,7 +2717,7 @@ public function testFindOrderByMultipleAttributes(): void /** * ORDER BY - Multiple attributes */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(25), Query::offset(0), Query::orderDesc('price'), @@ -2729,12 +2738,12 @@ public function testFindOrderByCursorAfter(): void /** * ORDER BY - After */ - $movies = static::getDatabase()->find('movies', [ + $movies = $this->getDatabase()->find('movies', [ Query::limit(25), Query::offset(0), ]); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::cursorAfter($movies[1]) @@ -2743,7 +2752,7 @@ public function testFindOrderByCursorAfter(): void $this->assertEquals($movies[2]['name'], $documents[0]['name']); $this->assertEquals($movies[3]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::cursorAfter($movies[3]) @@ -2752,7 +2761,7 @@ public function testFindOrderByCursorAfter(): void $this->assertEquals($movies[4]['name'], $documents[0]['name']); $this->assertEquals($movies[5]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::cursorAfter($movies[4]) @@ -2760,7 +2769,7 @@ public function testFindOrderByCursorAfter(): void $this->assertEquals(1, count($documents)); $this->assertEquals($movies[5]['name'], $documents[0]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::cursorAfter($movies[5]) @@ -2774,12 +2783,12 @@ public function testFindOrderByCursorBefore(): void /** * ORDER BY - Before */ - $movies = static::getDatabase()->find('movies', [ + $movies = $this->getDatabase()->find('movies', [ Query::limit(25), Query::offset(0), ]); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::cursorBefore($movies[5]) @@ -2788,7 +2797,7 @@ public function testFindOrderByCursorBefore(): void $this->assertEquals($movies[3]['name'], $documents[0]['name']); $this->assertEquals($movies[4]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::cursorBefore($movies[3]) @@ -2797,7 +2806,7 @@ public function testFindOrderByCursorBefore(): void $this->assertEquals($movies[1]['name'], $documents[0]['name']); $this->assertEquals($movies[2]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::cursorBefore($movies[2]) @@ -2806,7 +2815,7 @@ public function testFindOrderByCursorBefore(): void $this->assertEquals($movies[0]['name'], $documents[0]['name']); $this->assertEquals($movies[1]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::cursorBefore($movies[1]) @@ -2814,7 +2823,7 @@ public function testFindOrderByCursorBefore(): void $this->assertEquals(1, count($documents)); $this->assertEquals($movies[0]['name'], $documents[0]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::cursorBefore($movies[0]) @@ -2827,12 +2836,12 @@ public function testFindOrderByAfterNaturalOrder(): void /** * ORDER BY - After by natural order */ - $movies = array_reverse(static::getDatabase()->find('movies', [ + $movies = array_reverse($this->getDatabase()->find('movies', [ Query::limit(25), Query::offset(0), ])); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc(''), @@ -2842,7 +2851,7 @@ public function testFindOrderByAfterNaturalOrder(): void $this->assertEquals($movies[2]['name'], $documents[0]['name']); $this->assertEquals($movies[3]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc(''), @@ -2852,7 +2861,7 @@ public function testFindOrderByAfterNaturalOrder(): void $this->assertEquals($movies[4]['name'], $documents[0]['name']); $this->assertEquals($movies[5]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc(''), @@ -2861,7 +2870,7 @@ public function testFindOrderByAfterNaturalOrder(): void $this->assertEquals(1, count($documents)); $this->assertEquals($movies[5]['name'], $documents[0]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc(''), @@ -2875,13 +2884,13 @@ public function testFindOrderByBeforeNaturalOrder(): void /** * ORDER BY - Before by natural order */ - $movies = static::getDatabase()->find('movies', [ + $movies = $this->getDatabase()->find('movies', [ Query::limit(25), Query::offset(0), Query::orderDesc(''), ]); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc(''), @@ -2891,7 +2900,7 @@ public function testFindOrderByBeforeNaturalOrder(): void $this->assertEquals($movies[3]['name'], $documents[0]['name']); $this->assertEquals($movies[4]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc(''), @@ -2901,7 +2910,7 @@ public function testFindOrderByBeforeNaturalOrder(): void $this->assertEquals($movies[1]['name'], $documents[0]['name']); $this->assertEquals($movies[2]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc(''), @@ -2911,7 +2920,7 @@ public function testFindOrderByBeforeNaturalOrder(): void $this->assertEquals($movies[0]['name'], $documents[0]['name']); $this->assertEquals($movies[1]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc(''), @@ -2920,7 +2929,7 @@ public function testFindOrderByBeforeNaturalOrder(): void $this->assertEquals(1, count($documents)); $this->assertEquals($movies[0]['name'], $documents[0]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc(''), @@ -2934,13 +2943,13 @@ public function testFindOrderBySingleAttributeAfter(): void /** * ORDER BY - Single Attribute After */ - $movies = static::getDatabase()->find('movies', [ + $movies = $this->getDatabase()->find('movies', [ Query::limit(25), Query::offset(0), Query::orderDesc('year') ]); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('year'), @@ -2951,7 +2960,7 @@ public function testFindOrderBySingleAttributeAfter(): void $this->assertEquals($movies[2]['name'], $documents[0]['name']); $this->assertEquals($movies[3]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('year'), @@ -2961,7 +2970,7 @@ public function testFindOrderBySingleAttributeAfter(): void $this->assertEquals($movies[4]['name'], $documents[0]['name']); $this->assertEquals($movies[5]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('year'), @@ -2970,7 +2979,7 @@ public function testFindOrderBySingleAttributeAfter(): void $this->assertEquals(1, count($documents)); $this->assertEquals($movies[5]['name'], $documents[0]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('year'), @@ -2984,13 +2993,13 @@ public function testFindOrderBySingleAttributeBefore(): void /** * ORDER BY - Single Attribute Before */ - $movies = static::getDatabase()->find('movies', [ + $movies = $this->getDatabase()->find('movies', [ Query::limit(25), Query::offset(0), Query::orderDesc('year') ]); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('year'), @@ -3000,7 +3009,7 @@ public function testFindOrderBySingleAttributeBefore(): void $this->assertEquals($movies[3]['name'], $documents[0]['name']); $this->assertEquals($movies[4]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('year'), @@ -3010,7 +3019,7 @@ public function testFindOrderBySingleAttributeBefore(): void $this->assertEquals($movies[1]['name'], $documents[0]['name']); $this->assertEquals($movies[2]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('year'), @@ -3020,7 +3029,7 @@ public function testFindOrderBySingleAttributeBefore(): void $this->assertEquals($movies[0]['name'], $documents[0]['name']); $this->assertEquals($movies[1]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('year'), @@ -3029,7 +3038,7 @@ public function testFindOrderBySingleAttributeBefore(): void $this->assertEquals(1, count($documents)); $this->assertEquals($movies[0]['name'], $documents[0]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('year'), @@ -3043,14 +3052,14 @@ public function testFindOrderByMultipleAttributeAfter(): void /** * ORDER BY - Multiple Attribute After */ - $movies = static::getDatabase()->find('movies', [ + $movies = $this->getDatabase()->find('movies', [ Query::limit(25), Query::offset(0), Query::orderDesc('price'), Query::orderAsc('year') ]); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('price'), @@ -3061,7 +3070,7 @@ public function testFindOrderByMultipleAttributeAfter(): void $this->assertEquals($movies[2]['name'], $documents[0]['name']); $this->assertEquals($movies[3]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('price'), @@ -3072,7 +3081,7 @@ public function testFindOrderByMultipleAttributeAfter(): void $this->assertEquals($movies[4]['name'], $documents[0]['name']); $this->assertEquals($movies[5]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('price'), @@ -3082,7 +3091,7 @@ public function testFindOrderByMultipleAttributeAfter(): void $this->assertEquals(1, count($documents)); $this->assertEquals($movies[5]['name'], $documents[0]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('price'), @@ -3097,14 +3106,14 @@ public function testFindOrderByMultipleAttributeBefore(): void /** * ORDER BY - Multiple Attribute Before */ - $movies = static::getDatabase()->find('movies', [ + $movies = $this->getDatabase()->find('movies', [ Query::limit(25), Query::offset(0), Query::orderDesc('price'), Query::orderAsc('year') ]); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('price'), @@ -3116,7 +3125,7 @@ public function testFindOrderByMultipleAttributeBefore(): void $this->assertEquals($movies[3]['name'], $documents[0]['name']); $this->assertEquals($movies[4]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('price'), @@ -3127,7 +3136,7 @@ public function testFindOrderByMultipleAttributeBefore(): void $this->assertEquals($movies[2]['name'], $documents[0]['name']); $this->assertEquals($movies[3]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('price'), @@ -3138,7 +3147,7 @@ public function testFindOrderByMultipleAttributeBefore(): void $this->assertEquals($movies[0]['name'], $documents[0]['name']); $this->assertEquals($movies[1]['name'], $documents[1]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('price'), @@ -3148,7 +3157,7 @@ public function testFindOrderByMultipleAttributeBefore(): void $this->assertEquals(1, count($documents)); $this->assertEquals($movies[0]['name'], $documents[0]['name']); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('price'), @@ -3163,12 +3172,12 @@ public function testFindOrderByAndCursor(): void /** * ORDER BY + CURSOR */ - $documentsTest = static::getDatabase()->find('movies', [ + $documentsTest = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('price'), ]); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(1), Query::offset(0), Query::orderDesc('price'), @@ -3183,12 +3192,12 @@ public function testFindOrderByIdAndCursor(): void /** * ORDER BY ID + CURSOR */ - $documentsTest = static::getDatabase()->find('movies', [ + $documentsTest = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('$id'), ]); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(1), Query::offset(0), Query::orderDesc('$id'), @@ -3203,13 +3212,13 @@ public function testFindOrderByCreateDateAndCursor(): void /** * ORDER BY CREATE DATE + CURSOR */ - $documentsTest = static::getDatabase()->find('movies', [ + $documentsTest = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('$createdAt'), ]); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(1), Query::offset(0), Query::orderDesc('$createdAt'), @@ -3224,12 +3233,12 @@ public function testFindOrderByUpdateDateAndCursor(): void /** * ORDER BY UPDATE DATE + CURSOR */ - $documentsTest = static::getDatabase()->find('movies', [ + $documentsTest = $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::orderDesc('$updatedAt'), ]); - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(1), Query::offset(0), Query::orderDesc('$updatedAt'), @@ -3244,7 +3253,7 @@ public function testFindLimit(): void /** * Limit */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(4), Query::offset(0), Query::orderAsc('name') @@ -3262,7 +3271,7 @@ public function testFindLimitAndOffset(): void /** * Limit + Offset */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::limit(4), Query::offset(2), Query::orderAsc('name') @@ -3280,7 +3289,7 @@ public function testFindOrQueries(): void /** * Test that OR queries are handled correctly */ - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::equal('director', ['TBD', 'Joe Johnston']), Query::equal('year', [2025]), ]); @@ -3298,7 +3307,7 @@ public function testFindOrderByAfterException(): void ]); $this->expectException(Exception::class); - static::getDatabase()->find('movies', [ + $this->getDatabase()->find('movies', [ Query::limit(2), Query::offset(0), Query::cursorAfter($document) @@ -3312,9 +3321,9 @@ public function testFindEdgeCases(Document $document): void { $collection = 'edgeCases'; - static::getDatabase()->createCollection($collection); + $this->getDatabase()->createCollection($collection); - $this->assertEquals(true, static::getDatabase()->createAttribute($collection, 'value', Database::VAR_STRING, 256, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute($collection, 'value', Database::VAR_STRING, 256, true)); $values = [ 'NormalString', @@ -3333,7 +3342,7 @@ public function testFindEdgeCases(Document $document): void ]; foreach ($values as $value) { - static::getDatabase()->createDocument($collection, new Document([ + $this->getDatabase()->createDocument($collection, new Document([ '$id' => ID::unique(), '$permissions' => [ Permission::read(Role::any()), @@ -3347,7 +3356,7 @@ public function testFindEdgeCases(Document $document): void /** * Check Basic */ - $documents = static::getDatabase()->find($collection); + $documents = $this->getDatabase()->find($collection); $this->assertEquals(count($values), count($documents)); $this->assertNotEmpty($documents[0]->getId()); @@ -3361,7 +3370,7 @@ public function testFindEdgeCases(Document $document): void * Check `equals` query */ foreach ($values as $value) { - $documents = static::getDatabase()->find($collection, [ + $documents = $this->getDatabase()->find($collection, [ Query::limit(25), Query::equal('value', [$value]) ]); @@ -3374,7 +3383,7 @@ public function testFindEdgeCases(Document $document): void public function testOrSingleQuery(): void { try { - static::getDatabase()->find('movies', [ + $this->getDatabase()->find('movies', [ Query::or([ Query::equal('active', [true]) ]) @@ -3393,8 +3402,8 @@ public function testOrMultipleQueries(): void Query::equal('name', ['Frozen II']) ]) ]; - $this->assertCount(4, static::getDatabase()->find('movies', $queries)); - $this->assertEquals(4, static::getDatabase()->count('movies', $queries)); + $this->assertCount(4, $this->getDatabase()->find('movies', $queries)); + $this->assertEquals(4, $this->getDatabase()->count('movies', $queries)); $queries = [ Query::equal('active', [true]), @@ -3405,8 +3414,8 @@ public function testOrMultipleQueries(): void ]) ]; - $this->assertCount(3, static::getDatabase()->find('movies', $queries)); - $this->assertEquals(3, static::getDatabase()->count('movies', $queries)); + $this->assertCount(3, $this->getDatabase()->find('movies', $queries)); + $this->assertEquals(3, $this->getDatabase()->count('movies', $queries)); } public function testOrNested(): void @@ -3423,18 +3432,18 @@ public function testOrNested(): void ]) ]; - $documents = static::getDatabase()->find('movies', $queries); + $documents = $this->getDatabase()->find('movies', $queries); $this->assertCount(1, $documents); $this->assertArrayNotHasKey('name', $documents[0]); - $count = static::getDatabase()->count('movies', $queries); + $count = $this->getDatabase()->count('movies', $queries); $this->assertEquals(1, $count); } public function testAndSingleQuery(): void { try { - static::getDatabase()->find('movies', [ + $this->getDatabase()->find('movies', [ Query::and([ Query::equal('active', [true]) ]) @@ -3453,8 +3462,8 @@ public function testAndMultipleQueries(): void Query::equal('name', ['Frozen II']) ]) ]; - $this->assertCount(1, static::getDatabase()->find('movies', $queries)); - $this->assertEquals(1, static::getDatabase()->count('movies', $queries)); + $this->assertCount(1, $this->getDatabase()->find('movies', $queries)); + $this->assertEquals(1, $this->getDatabase()->count('movies', $queries)); } public function testAndNested(): void @@ -3469,10 +3478,10 @@ public function testAndNested(): void ]) ]; - $documents = static::getDatabase()->find('movies', $queries); + $documents = $this->getDatabase()->find('movies', $queries); $this->assertCount(3, $documents); - $count = static::getDatabase()->count('movies', $queries); + $count = $this->getDatabase()->count('movies', $queries); $this->assertEquals(3, $count); } @@ -3481,7 +3490,7 @@ public function testAndNested(): void */ public function testFindOne(): void { - $document = static::getDatabase()->findOne('movies', [ + $document = $this->getDatabase()->findOne('movies', [ Query::offset(2), Query::orderAsc('name') ]); @@ -3489,7 +3498,7 @@ public function testFindOne(): void $this->assertTrue($document instanceof Document); $this->assertEquals('Frozen', $document->getAttribute('name')); - $document = static::getDatabase()->findOne('movies', [ + $document = $this->getDatabase()->findOne('movies', [ Query::offset(10) ]); $this->assertEquals(false, $document); @@ -3497,7 +3506,7 @@ public function testFindOne(): void public function testFindNull(): void { - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::isNull('nullable'), ]); @@ -3506,7 +3515,7 @@ public function testFindNull(): void public function testFindNotNull(): void { - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::isNotNull('nullable'), ]); @@ -3515,18 +3524,18 @@ public function testFindNotNull(): void public function testFindStartsWith(): void { - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::startsWith('name', 'Work'), ]); $this->assertEquals(2, count($documents)); if ($this->getDatabase()->getAdapter() instanceof SQL) { - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::startsWith('name', '%ork'), ]); } else { - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::startsWith('name', '.*ork'), ]); } @@ -3536,7 +3545,7 @@ public function testFindStartsWith(): void public function testFindStartsWithWords(): void { - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::startsWith('name', 'Work in Progress'), ]); @@ -3545,7 +3554,7 @@ public function testFindStartsWithWords(): void public function testFindEndsWith(): void { - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::endsWith('name', 'Marvel'), ]); @@ -3554,7 +3563,7 @@ public function testFindEndsWith(): void public function testFindSelect(): void { - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::select(['name', 'year']) ]); @@ -3572,7 +3581,7 @@ public function testFindSelect(): void $this->assertArrayNotHasKey('$permissions', $document); } - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::select(['name', 'year', '$id']) ]); @@ -3590,7 +3599,7 @@ public function testFindSelect(): void $this->assertArrayNotHasKey('$permissions', $document); } - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::select(['name', 'year', '$internalId']) ]); @@ -3608,7 +3617,7 @@ public function testFindSelect(): void $this->assertArrayNotHasKey('$permissions', $document); } - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::select(['name', 'year', '$collection']) ]); @@ -3626,7 +3635,7 @@ public function testFindSelect(): void $this->assertArrayNotHasKey('$permissions', $document); } - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::select(['name', 'year', '$createdAt']) ]); @@ -3644,7 +3653,7 @@ public function testFindSelect(): void $this->assertArrayNotHasKey('$permissions', $document); } - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::select(['name', 'year', '$updatedAt']) ]); @@ -3662,7 +3671,7 @@ public function testFindSelect(): void $this->assertArrayNotHasKey('$permissions', $document); } - $documents = static::getDatabase()->find('movies', [ + $documents = $this->getDatabase()->find('movies', [ Query::select(['name', 'year', '$permissions']) ]); @@ -3686,39 +3695,39 @@ public function testFindSelect(): void */ public function testCount(): void { - $count = static::getDatabase()->count('movies'); + $count = $this->getDatabase()->count('movies'); $this->assertEquals(6, $count); - $count = static::getDatabase()->count('movies', [Query::equal('year', [2019])]); + $count = $this->getDatabase()->count('movies', [Query::equal('year', [2019])]); $this->assertEquals(2, $count); - $count = static::getDatabase()->count('movies', [Query::equal('with-dash', ['Works'])]); + $count = $this->getDatabase()->count('movies', [Query::equal('with-dash', ['Works'])]); $this->assertEquals(2, $count); - $count = static::getDatabase()->count('movies', [Query::equal('with-dash', ['Works2', 'Works3'])]); + $count = $this->getDatabase()->count('movies', [Query::equal('with-dash', ['Works2', 'Works3'])]); $this->assertEquals(4, $count); - Authorization::unsetRole('user:x'); - $count = static::getDatabase()->count('movies'); + self::$authorization->removeRole('user:x'); + $count = $this->getDatabase()->count('movies'); $this->assertEquals(5, $count); - Authorization::disable(); - $count = static::getDatabase()->count('movies'); + self::$authorization->disable(); + $count = $this->getDatabase()->count('movies'); $this->assertEquals(6, $count); - Authorization::reset(); + self::$authorization->reset(); - Authorization::disable(); - $count = static::getDatabase()->count('movies', [], 3); + self::$authorization->disable(); + $count = $this->getDatabase()->count('movies', [], 3); $this->assertEquals(3, $count); - Authorization::reset(); + self::$authorization->reset(); /** * Test that OR queries are handled correctly */ - Authorization::disable(); - $count = static::getDatabase()->count('movies', [ + self::$authorization->disable(); + $count = $this->getDatabase()->count('movies', [ Query::equal('director', ['TBD', 'Joe Johnston']), Query::equal('year', [2025]), ]); $this->assertEquals(1, $count); - Authorization::reset(); + self::$authorization->reset(); } /** @@ -3726,28 +3735,28 @@ public function testCount(): void */ public function testSum(): void { - Authorization::setRole('user:x'); - $sum = static::getDatabase()->sum('movies', 'year', [Query::equal('year', [2019]),]); + self::$authorization->addRole('user:x'); + $sum = $this->getDatabase()->sum('movies', 'year', [Query::equal('year', [2019]),]); $this->assertEquals(2019 + 2019, $sum); - $sum = static::getDatabase()->sum('movies', 'year'); + $sum = $this->getDatabase()->sum('movies', 'year'); $this->assertEquals(2013 + 2019 + 2011 + 2019 + 2025 + 2026, $sum); - $sum = static::getDatabase()->sum('movies', 'price', [Query::equal('year', [2019]),]); + $sum = $this->getDatabase()->sum('movies', 'price', [Query::equal('year', [2019]),]); $this->assertEquals(round(39.50 + 25.99, 2), round($sum, 2)); - $sum = static::getDatabase()->sum('movies', 'price', [Query::equal('year', [2019]),]); + $sum = $this->getDatabase()->sum('movies', 'price', [Query::equal('year', [2019]),]); $this->assertEquals(round(39.50 + 25.99, 2), round($sum, 2)); - $sum = static::getDatabase()->sum('movies', 'year', [Query::equal('year', [2019])], 1); + $sum = $this->getDatabase()->sum('movies', 'year', [Query::equal('year', [2019])], 1); $this->assertEquals(2019, $sum); - Authorization::unsetRole('user:x'); - Authorization::unsetRole('userx'); - $sum = static::getDatabase()->sum('movies', 'year', [Query::equal('year', [2019]),]); + self::$authorization->removeRole('user:x'); + self::$authorization->removeRole('userx'); + $sum = $this->getDatabase()->sum('movies', 'year', [Query::equal('year', [2019]),]); $this->assertEquals(2019 + 2019, $sum); - $sum = static::getDatabase()->sum('movies', 'year'); + $sum = $this->getDatabase()->sum('movies', 'year'); $this->assertEquals(2013 + 2019 + 2011 + 2019 + 2025, $sum); - $sum = static::getDatabase()->sum('movies', 'price', [Query::equal('year', [2019]),]); + $sum = $this->getDatabase()->sum('movies', 'price', [Query::equal('year', [2019]),]); $this->assertEquals(round(39.50 + 25.99, 2), round($sum, 2)); - $sum = static::getDatabase()->sum('movies', 'price', [Query::equal('year', [2019]),]); + $sum = $this->getDatabase()->sum('movies', 'price', [Query::equal('year', [2019]),]); $this->assertEquals(round(39.50 + 25.99, 2), round($sum, 2)); } @@ -3942,7 +3951,7 @@ public function testEncodeDecode(): void ], ]); - $result = static::getDatabase()->encode($collection, $document); + $result = $this->getDatabase()->encode($collection, $document); $this->assertEquals('608fdbe51361a', $result->getAttribute('$id')); $this->assertContains('read("any")', $result->getAttribute('$permissions')); @@ -3966,7 +3975,7 @@ public function testEncodeDecode(): void $this->assertEquals(['admin', 'developer', 'tester',], $result->getAttribute('roles')); $this->assertEquals(['{"$id":"1","label":"x"}', '{"$id":"2","label":"y"}', '{"$id":"3","label":"z"}',], $result->getAttribute('tags')); - $result = static::getDatabase()->decode($collection, $document); + $result = $this->getDatabase()->decode($collection, $document); $this->assertEquals('608fdbe51361a', $result->getAttribute('$id')); $this->assertContains('read("any")', $result->getAttribute('$permissions')); @@ -4000,10 +4009,10 @@ public function testEncodeDecode(): void */ public function testReadPermissionsSuccess(Document $document): Document { - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::any()->toString()); - $document = static::getDatabase()->createDocument('documents', new Document([ + $document = $this->getDatabase()->createDocument('documents', new Document([ '$permissions' => [ Permission::read(Role::any()), Permission::create(Role::any()), @@ -4020,22 +4029,22 @@ public function testReadPermissionsSuccess(Document $document): Document $this->assertEquals(false, $document->isEmpty()); - Authorization::cleanRoles(); + self::$authorization->cleanRoles(); - $document = static::getDatabase()->getDocument($document->getCollection(), $document->getId()); + $document = $this->getDatabase()->getDocument($document->getCollection(), $document->getId()); $this->assertEquals(true, $document->isEmpty()); - Authorization::setRole(Role::any()->toString()); + self::$authorization->addRole(Role::any()->toString()); return $document; } public function testReadPermissionsFailure(): Document { - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::any()->toString()); - $document = static::getDatabase()->createDocument('documents', new Document([ + $document = $this->getDatabase()->createDocument('documents', new Document([ '$permissions' => [ Permission::read(Role::user('1')), Permission::create(Role::user('1')), @@ -4050,13 +4059,13 @@ public function testReadPermissionsFailure(): Document 'colors' => ['pink', 'green', 'blue'], ])); - Authorization::cleanRoles(); + self::$authorization->cleanRoles(); - $document = static::getDatabase()->getDocument($document->getCollection(), $document->getId()); + $document = $this->getDatabase()->getDocument($document->getCollection(), $document->getId()); $this->assertEquals(true, $document->isEmpty()); - Authorization::setRole(Role::any()->toString()); + self::$authorization->addRole(Role::any()->toString()); return $document; } @@ -4066,10 +4075,10 @@ public function testReadPermissionsFailure(): Document */ public function testWritePermissionsSuccess(Document $document): void { - Authorization::cleanRoles(); + self::$authorization->cleanRoles(); $this->expectException(AuthorizationException::class); - static::getDatabase()->createDocument('documents', new Document([ + $this->getDatabase()->createDocument('documents', new Document([ '$permissions' => [ Permission::read(Role::any()), Permission::create(Role::any()), @@ -4092,10 +4101,10 @@ public function testWritePermissionsUpdateFailure(Document $document): Document { $this->expectException(AuthorizationException::class); - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::any()->toString()); - $document = static::getDatabase()->createDocument('documents', new Document([ + $document = $this->getDatabase()->createDocument('documents', new Document([ '$permissions' => [ Permission::read(Role::any()), Permission::create(Role::any()), @@ -4110,9 +4119,9 @@ public function testWritePermissionsUpdateFailure(Document $document): Document 'colors' => ['pink', 'green', 'blue'], ])); - Authorization::cleanRoles(); + self::$authorization->cleanRoles(); - $document = static::getDatabase()->updateDocument('documents', $document->getId(), new Document([ + $document = $this->getDatabase()->updateDocument('documents', $document->getId(), new Document([ '$id' => ID::custom($document->getId()), '$permissions' => [ Permission::read(Role::any()), @@ -4136,7 +4145,7 @@ public function testWritePermissionsUpdateFailure(Document $document): Document */ public function testNoChangeUpdateDocumentWithoutPermission(Document $document): Document { - $document = static::getDatabase()->createDocument('documents', new Document([ + $document = $this->getDatabase()->createDocument('documents', new Document([ '$id' => ID::unique(), '$permissions' => [], 'string' => 'text📝', @@ -4147,7 +4156,7 @@ public function testNoChangeUpdateDocumentWithoutPermission(Document $document): 'colors' => ['pink', 'green', 'blue'], ])); - $updatedDocument = static::getDatabase()->updateDocument( + $updatedDocument = $this->getDatabase()->updateDocument( 'documents', $document->getId(), $document @@ -4162,22 +4171,22 @@ public function testNoChangeUpdateDocumentWithoutPermission(Document $document): public function testStructureValidationAfterRelationsAttribute(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection("structure_1", [], [], [Permission::create(Role::any())]); - static::getDatabase()->createCollection("structure_2", [], [], [Permission::create(Role::any())]); + $this->getDatabase()->createCollection("structure_1", [], [], [Permission::create(Role::any())]); + $this->getDatabase()->createCollection("structure_2", [], [], [Permission::create(Role::any())]); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: "structure_1", relatedCollection: "structure_2", type: Database::RELATION_ONE_TO_ONE, ); try { - static::getDatabase()->createDocument('structure_1', new Document([ + $this->getDatabase()->createDocument('structure_1', new Document([ '$permissions' => [ Permission::read(Role::any()), ], @@ -4192,7 +4201,7 @@ public function testStructureValidationAfterRelationsAttribute(): void public function testNoChangeUpdateDocumentWithRelationWithoutPermission(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } @@ -4213,13 +4222,13 @@ public function testNoChangeUpdateDocumentWithRelationWithoutPermission(): void Permission::delete(Role::any()), ]; for ($i = 1; $i < 6; $i++) { - static::getDatabase()->createCollection("level{$i}", [$attribute], [], $permissions); + $this->getDatabase()->createCollection("level{$i}", [$attribute], [], $permissions); } for ($i = 1; $i < 5; $i++) { $collectionId = $i; $relatedCollectionId = $i + 1; - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: "level{$collectionId}", relatedCollection: "level{$relatedCollectionId}", type: Database::RELATION_ONE_TO_ONE, @@ -4228,7 +4237,7 @@ public function testNoChangeUpdateDocumentWithRelationWithoutPermission(): void } // Create document with relationship with nested data - $level1 = static::getDatabase()->createDocument('level1', new Document([ + $level1 = $this->getDatabase()->createDocument('level1', new Document([ '$id' => 'level1', '$permissions' => [], 'name' => 'Level 1', @@ -4253,18 +4262,18 @@ public function testNoChangeUpdateDocumentWithRelationWithoutPermission(): void ], ], ])); - static::getDatabase()->updateDocument('level1', $level1->getId(), new Document($level1->getArrayCopy())); - $updatedLevel1 = static::getDatabase()->getDocument('level1', $level1->getId()); + $this->getDatabase()->updateDocument('level1', $level1->getId(), new Document($level1->getArrayCopy())); + $updatedLevel1 = $this->getDatabase()->getDocument('level1', $level1->getId()); $this->assertEquals($level1, $updatedLevel1); try { - static::getDatabase()->updateDocument('level1', $level1->getId(), $level1->setAttribute('name', 'haha')); + $this->getDatabase()->updateDocument('level1', $level1->getId(), $level1->setAttribute('name', 'haha')); $this->fail('Failed to throw exception'); } catch(Exception $e) { $this->assertInstanceOf(AuthorizationException::class, $e); } $level1->setAttribute('name', 'Level 1'); - static::getDatabase()->updateCollection('level3', [ + $this->getDatabase()->updateCollection('level3', [ Permission::read(Role::any()), Permission::create(Role::any()), Permission::update(Role::any()), @@ -4277,11 +4286,11 @@ public function testNoChangeUpdateDocumentWithRelationWithoutPermission(): void $level2->setAttribute('level3', $level3); $level1->setAttribute('level2', $level2); - $level1 = static::getDatabase()->updateDocument('level1', $level1->getId(), $level1); + $level1 = $this->getDatabase()->updateDocument('level1', $level1->getId(), $level1); $this->assertEquals('updated value', $level1['level2']['level3']['name']); for ($i = 1; $i < 6; $i++) { - static::getDatabase()->deleteCollection("level{$i}"); + $this->getDatabase()->deleteCollection("level{$i}"); } } @@ -4303,10 +4312,10 @@ public function testExceptionAttributeLimit(): void ]); } - static::getDatabase()->createCollection('attributeLimit', $attributes); + $this->getDatabase()->createCollection('attributeLimit', $attributes); $this->expectException(LimitException::class); - $this->assertEquals(false, static::getDatabase()->createAttribute('attributeLimit', "breaking", Database::VAR_INTEGER, 0, true)); + $this->assertEquals(false, $this->getDatabase()->createAttribute('attributeLimit', "breaking", Database::VAR_INTEGER, 0, true)); } // Default assertion for other adapters @@ -4319,7 +4328,7 @@ public function testExceptionAttributeLimit(): void public function testCheckAttributeCountLimit(): void { if ($this->getDatabase()->getLimitForAttributes() > 0) { - $collection = static::getDatabase()->getCollection('attributeLimit'); + $collection = $this->getDatabase()->getCollection('attributeLimit'); // create same attribute in testExceptionAttributeLimit $attribute = new Document([ @@ -4334,7 +4343,7 @@ public function testCheckAttributeCountLimit(): void ]); $this->expectException(LimitException::class); - $this->assertEquals(false, static::getDatabase()->checkAttribute($collection, $attribute)); + $this->assertEquals(false, $this->getDatabase()->checkAttribute($collection, $attribute)); } // Default assertion for other adapters @@ -4370,7 +4379,7 @@ public function rowWidthExceedsMaximum(): array */ public function testExceptionWidthLimit(int $key, int $stringSize, int $stringCount, int $intCount, int $floatCount, int $boolCount): void { - if (static::getDatabase()->getAdapter()::getDocumentSizeLimit() > 0) { + if ($this->getDatabase()->getAdapter()::getDocumentSizeLimit() > 0) { $attributes = []; // Load the collection up to the limit @@ -4430,10 +4439,10 @@ public function testExceptionWidthLimit(int $key, int $stringSize, int $stringCo ]); } - $collection = static::getDatabase()->createCollection("widthLimit{$key}", $attributes); + $collection = $this->getDatabase()->createCollection("widthLimit{$key}", $attributes); $this->expectException(LimitException::class); - $this->assertEquals(false, static::getDatabase()->createAttribute("widthLimit{$key}", "breaking", Database::VAR_STRING, 100, true)); + $this->assertEquals(false, $this->getDatabase()->createAttribute("widthLimit{$key}", "breaking", Database::VAR_STRING, 100, true)); } // Default assertion for other adapters @@ -4446,8 +4455,8 @@ public function testExceptionWidthLimit(int $key, int $stringSize, int $stringCo */ public function testCheckAttributeWidthLimit(int $key, int $stringSize, int $stringCount, int $intCount, int $floatCount, int $boolCount): void { - if (static::getDatabase()->getAdapter()::getDocumentSizeLimit() > 0) { - $collection = static::getDatabase()->getCollection("widthLimit{$key}"); + if ($this->getDatabase()->getAdapter()::getDocumentSizeLimit() > 0) { + $collection = $this->getDatabase()->getCollection("widthLimit{$key}"); // create same attribute in testExceptionWidthLimit $attribute = new Document([ @@ -4462,7 +4471,7 @@ public function testCheckAttributeWidthLimit(int $key, int $stringSize, int $str ]); $this->expectException(LimitException::class); - $this->assertEquals(false, static::getDatabase()->checkAttribute($collection, $attribute)); + $this->assertEquals(false, $this->getDatabase()->checkAttribute($collection, $attribute)); } // Default assertion for other adapters @@ -4471,22 +4480,22 @@ public function testCheckAttributeWidthLimit(int $key, int $stringSize, int $str public function testExceptionIndexLimit(): void { - static::getDatabase()->createCollection('indexLimit'); + $this->getDatabase()->createCollection('indexLimit'); // add unique attributes for indexing for ($i = 0; $i < 64; $i++) { - $this->assertEquals(true, static::getDatabase()->createAttribute('indexLimit', "test{$i}", Database::VAR_STRING, 16, true)); + $this->assertEquals(true, $this->getDatabase()->createAttribute('indexLimit', "test{$i}", Database::VAR_STRING, 16, true)); } // Testing for indexLimit // Add up to the limit, then check if the next index throws IndexLimitException for ($i = 0; $i < ($this->getDatabase()->getLimitForIndexes()); $i++) { - $this->assertEquals(true, static::getDatabase()->createIndex('indexLimit', "index{$i}", Database::INDEX_KEY, ["test{$i}"], [16])); + $this->assertEquals(true, $this->getDatabase()->createIndex('indexLimit', "index{$i}", Database::INDEX_KEY, ["test{$i}"], [16])); } $this->expectException(LimitException::class); - $this->assertEquals(false, static::getDatabase()->createIndex('indexLimit', "index64", Database::INDEX_KEY, ["test64"], [16])); + $this->assertEquals(false, $this->getDatabase()->createIndex('indexLimit', "index64", Database::INDEX_KEY, ["test64"], [16])); - static::getDatabase()->deleteCollection('indexLimit'); + $this->getDatabase()->deleteCollection('indexLimit'); } /** @@ -4495,10 +4504,10 @@ public function testExceptionIndexLimit(): void public function testExceptionDuplicate(Document $document): void { $document->setAttribute('$id', 'duplicated'); - static::getDatabase()->createDocument($document->getCollection(), $document); + $this->getDatabase()->createDocument($document->getCollection(), $document); $this->expectException(DuplicateException::class); - static::getDatabase()->createDocument($document->getCollection(), $document); + $this->getDatabase()->createDocument($document->getCollection(), $document); } /** @@ -4508,12 +4517,12 @@ public function testExceptionCaseInsensitiveDuplicate(Document $document): Docum { $document->setAttribute('$id', 'caseSensitive'); $document->setAttribute('$internalId', '200'); - static::getDatabase()->createDocument($document->getCollection(), $document); + $this->getDatabase()->createDocument($document->getCollection(), $document); $document->setAttribute('$id', 'CaseSensitive'); $this->expectException(DuplicateException::class); - static::getDatabase()->createDocument($document->getCollection(), $document); + $this->getDatabase()->createDocument($document->getCollection(), $document); return $document; } @@ -4525,9 +4534,9 @@ public function testUniqueIndexDuplicate(): void { $this->expectException(DuplicateException::class); - $this->assertEquals(true, static::getDatabase()->createIndex('movies', 'uniqueIndex', Database::INDEX_UNIQUE, ['name'], [128], [Database::ORDER_ASC])); + $this->assertEquals(true, $this->getDatabase()->createIndex('movies', 'uniqueIndex', Database::INDEX_UNIQUE, ['name'], [128], [Database::ORDER_ASC])); - static::getDatabase()->createDocument('movies', new Document([ + $this->getDatabase()->createDocument('movies', new Document([ '$permissions' => [ Permission::read(Role::any()), Permission::read(Role::user('1')), @@ -4557,9 +4566,9 @@ public function testUniqueIndexDuplicate(): void */ public function testUniqueIndexDuplicateUpdate(): void { - Authorization::setRole(Role::users()->toString()); + self::$authorization->addRole(Role::users()->toString()); // create document then update to conflict with index - $document = static::getDatabase()->createDocument('movies', new Document([ + $document = $this->getDatabase()->createDocument('movies', new Document([ '$permissions' => [ Permission::read(Role::any()), Permission::read(Role::user('1')), @@ -4585,7 +4594,7 @@ public function testUniqueIndexDuplicateUpdate(): void $this->expectException(DuplicateException::class); - static::getDatabase()->updateDocument('movies', $document->getId(), $document->setAttribute('name', 'Frozen')); + $this->getDatabase()->updateDocument('movies', $document->getId(), $document->setAttribute('name', 'Frozen')); } public function testGetAttributeLimit(): void @@ -4611,7 +4620,7 @@ public function testGetId(): void public function testRenameIndex(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $numbers = $database->createCollection('numbers'); $database->createAttribute('numbers', 'verbose', Database::VAR_STRING, 128, true); @@ -4637,7 +4646,7 @@ public function testRenameIndex(): void */ public function testRenameIndexMissing(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->expectExceptionMessage('Index not found'); $index = $database->renameIndex('numbers', 'index1', 'index4'); } @@ -4648,14 +4657,14 @@ public function testRenameIndexMissing(): void */ public function testRenameIndexExisting(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->expectExceptionMessage('Index name already used'); $index = $database->renameIndex('numbers', 'index3', 'index2'); } public function testRenameAttribute(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $colors = $database->createCollection('colors'); $database->createAttribute('colors', 'name', Database::VAR_STRING, 128, true); @@ -4701,7 +4710,7 @@ public function testRenameAttribute(): void */ public function textRenameAttributeMissing(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->expectExceptionMessage('Attribute not found'); $database->renameAttribute('colors', 'name2', 'name3'); } @@ -4712,14 +4721,14 @@ public function textRenameAttributeMissing(): void */ public function testRenameAttributeExisting(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->expectExceptionMessage('Attribute name already used'); $database->renameAttribute('colors', 'verbose', 'hex'); } public function testUpdateAttributeDefault(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $flowers = $database->createCollection('flowers'); $database->createAttribute('flowers', 'name', Database::VAR_STRING, 128, true); @@ -4774,7 +4783,7 @@ public function testUpdateAttributeDefault(): void */ public function testUpdateAttributeRequired(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->updateAttributeRequired('flowers', 'inStock', true); @@ -4796,7 +4805,7 @@ public function testUpdateAttributeRequired(): void */ public function testUpdateAttributeFilter(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createAttribute('flowers', 'cartModel', Database::VAR_STRING, 2000, false); @@ -4829,7 +4838,7 @@ public function testUpdateAttributeFilter(): void */ public function testUpdateAttributeFormat(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createAttribute('flowers', 'price', Database::VAR_INTEGER, 0, false); @@ -4890,7 +4899,7 @@ public function testUpdateAttributeStructure(): void return new Range($min, $max); }, Database::VAR_INTEGER); - $database = static::getDatabase(); + $database = $this->getDatabase(); // price attribute $collection = $database->getCollection('flowers'); @@ -5030,27 +5039,27 @@ public function testUpdateAttributeStructure(): void */ public function testCreatedAtUpdatedAtAssert(): void { - $document = static::getDatabase()->getDocument('created_at', 'uid123'); + $document = $this->getDatabase()->getDocument('created_at', 'uid123'); $this->assertEquals(true, !$document->isEmpty()); sleep(1); $document->setAttribute('title', 'new title'); - static::getDatabase()->updateDocument('created_at', 'uid123', $document); - $document = static::getDatabase()->getDocument('created_at', 'uid123'); + $this->getDatabase()->updateDocument('created_at', 'uid123', $document); + $document = $this->getDatabase()->getDocument('created_at', 'uid123'); $this->assertGreaterThan($document->getCreatedAt(), $document->getUpdatedAt()); $this->expectException(DuplicateException::class); - static::getDatabase()->createCollection('created_at'); + $this->getDatabase()->createCollection('created_at'); } public function testCreateDatetime(): void { - static::getDatabase()->createCollection('datetime'); + $this->getDatabase()->createCollection('datetime'); - $this->assertEquals(true, static::getDatabase()->createAttribute('datetime', 'date', Database::VAR_DATETIME, 0, true, null, true, false, null, [], ['datetime'])); - $this->assertEquals(true, static::getDatabase()->createAttribute('datetime', 'date2', Database::VAR_DATETIME, 0, false, null, true, false, null, [], ['datetime'])); + $this->assertEquals(true, $this->getDatabase()->createAttribute('datetime', 'date', Database::VAR_DATETIME, 0, true, null, true, false, null, [], ['datetime'])); + $this->assertEquals(true, $this->getDatabase()->createAttribute('datetime', 'date2', Database::VAR_DATETIME, 0, false, null, true, false, null, [], ['datetime'])); - $doc = static::getDatabase()->createDocument('datetime', new Document([ + $doc = $this->getDatabase()->createDocument('datetime', new Document([ '$id' => ID::custom('id1234'), '$permissions' => [ Permission::read(Role::any()), @@ -5068,25 +5077,25 @@ public function testCreateDatetime(): void $this->assertGreaterThan('2020-08-16T19:30:08.363+00:00', $doc->getCreatedAt()); $this->assertGreaterThan('2020-08-16T19:30:08.363+00:00', $doc->getUpdatedAt()); - $document = static::getDatabase()->getDocument('datetime', 'id1234'); + $document = $this->getDatabase()->getDocument('datetime', 'id1234'); $dateValidator = new DatetimeValidator(); $this->assertEquals(null, $document->getAttribute('date2')); $this->assertEquals(true, $dateValidator->isValid($document->getAttribute('date'))); $this->assertEquals(false, $dateValidator->isValid($document->getAttribute('date2'))); - $documents = static::getDatabase()->find('datetime', [ + $documents = $this->getDatabase()->find('datetime', [ Query::greaterThan('date', '1975-12-06 10:00:00+01:00'), Query::lessThan('date', '2030-12-06 10:00:00-01:00'), ]); $this->assertEquals(1, count($documents)); - $documents = static::getDatabase()->find('datetime', [ + $documents = $this->getDatabase()->find('datetime', [ Query::greaterThan('$createdAt', '1975-12-06 11:00:00.000'), ]); $this->assertCount(1, $documents); try { - static::getDatabase()->createDocument('datetime', new Document([ + $this->getDatabase()->createDocument('datetime', new Document([ 'date' => "1975-12-06 00:00:61" // 61 seconds is invalid ])); $this->fail('Failed to throw exception'); @@ -5101,7 +5110,7 @@ public function testCreateDatetime(): void foreach ($invalidDates as $date) { try { - static::getDatabase()->find('datetime', [ + $this->getDatabase()->find('datetime', [ Query::equal('date', [$date]) ]); $this->fail('Failed to throw exception'); @@ -5113,16 +5122,16 @@ public function testCreateDatetime(): void public function testCreateDateTimeAttributeFailure(): void { - static::getDatabase()->createCollection('datetime_fail'); + $this->getDatabase()->createCollection('datetime_fail'); /** Test for FAILURE */ $this->expectException(Exception::class); - static::getDatabase()->createAttribute('datetime_fail', 'date_fail', Database::VAR_DATETIME, 0, false); + $this->getDatabase()->createAttribute('datetime_fail', 'date_fail', Database::VAR_DATETIME, 0, false); } public function testKeywords(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $keywords = $database->getKeywords(); // Collection name tests @@ -5184,7 +5193,7 @@ public function testKeywords(): void $collection = $database->createCollection($collectionName); $this->assertEquals($collectionName, $collection->getId()); - $attribute = static::getDatabase()->createAttribute($collectionName, $keyword, Database::VAR_STRING, 128, true); + $attribute = $this->getDatabase()->createAttribute($collectionName, $keyword, Database::VAR_STRING, 128, true); $this->assertEquals(true, $attribute); $document = new Document([ @@ -5233,8 +5242,8 @@ public function testKeywords(): void public function testWritePermissions(): void { - Authorization::setRole(Role::any()->toString()); - $database = static::getDatabase(); + self::$authorization->addRole(Role::any()->toString()); + $database = $this->getDatabase(); $database->createCollection('animals', permissions: [ Permission::create(Role::any()), @@ -5299,7 +5308,7 @@ public function testWritePermissions(): void $newCat = $cat->setAttribute('type', 'newCat'); $database->updateDocument('animals', 'cat', $newCat); - $docs = Authorization::skip(fn () => $database->find('animals')); + $docs = self::$authorization->skip(fn () => $database->find('animals')); $this->assertCount(1, $docs); $this->assertEquals('cat', $docs[0]['$id']); $this->assertEquals('newCat', $docs[0]['type']); @@ -5307,19 +5316,19 @@ public function testWritePermissions(): void public function testNoInvalidKeysWithRelationships(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('species'); - static::getDatabase()->createCollection('creatures'); - static::getDatabase()->createCollection('characterstics'); + $this->getDatabase()->createCollection('species'); + $this->getDatabase()->createCollection('creatures'); + $this->getDatabase()->createCollection('characterstics'); - static::getDatabase()->createAttribute('species', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('creatures', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('characterstics', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('species', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('creatures', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('characterstics', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'species', relatedCollection: 'creatures', type: Database::RELATION_ONE_TO_ONE, @@ -5327,7 +5336,7 @@ public function testNoInvalidKeysWithRelationships(): void id: 'creature', twoWayKey:'species' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'creatures', relatedCollection: 'characterstics', type: Database::RELATION_ONE_TO_ONE, @@ -5336,7 +5345,7 @@ public function testNoInvalidKeysWithRelationships(): void twoWayKey:'creature' ); - $species = static::getDatabase()->createDocument('species', new Document([ + $species = $this->getDatabase()->createDocument('species', new Document([ '$id' => ID::custom('1'), '$permissions' => [ Permission::read(Role::any()), @@ -5358,7 +5367,7 @@ public function testNoInvalidKeysWithRelationships(): void ] ] ])); - static::getDatabase()->updateDocument('species', $species->getId(), new Document([ + $this->getDatabase()->updateDocument('species', $species->getId(), new Document([ '$id' => ID::custom('1'), '$collection' => 'species', 'creature' => [ @@ -5371,33 +5380,33 @@ public function testNoInvalidKeysWithRelationships(): void ] ] ])); - $updatedSpecies = static::getDatabase()->getDocument('species', $species->getId()); + $updatedSpecies = $this->getDatabase()->getDocument('species', $species->getId()); $this->assertEquals($species, $updatedSpecies); } // Relationships public function testOneToOneOneWayRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('person'); - static::getDatabase()->createCollection('library'); + $this->getDatabase()->createCollection('person'); + $this->getDatabase()->createCollection('library'); - static::getDatabase()->createAttribute('person', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('library', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('library', 'area', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('person', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('library', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('library', 'area', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'person', relatedCollection: 'library', type: Database::RELATION_ONE_TO_ONE ); // Check metadata for collection - $collection = static::getDatabase()->getCollection('person'); + $collection = $this->getDatabase()->getCollection('person'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { @@ -5413,14 +5422,14 @@ public function testOneToOneOneWayRelationship(): void } try { - static::getDatabase()->deleteAttribute('person', 'library'); + $this->getDatabase()->deleteAttribute('person', 'library'); $this->fail('Failed to throw Exception'); } catch (Exception $e) { $this->assertEquals('Cannot delete relationship as an attribute', $e->getMessage()); } // Create document with relationship with nested data - $person1 = static::getDatabase()->createDocument('person', new Document([ + $person1 = $this->getDatabase()->createDocument('person', new Document([ '$id' => 'person1', '$permissions' => [ Permission::read(Role::any()), @@ -5441,24 +5450,24 @@ public function testOneToOneOneWayRelationship(): void ])); // Update a document with non existing related document. It should not get added to the list. - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'person', 'person1', $person1->setAttribute('library', 'no-library') ); - $person1Document = static::getDatabase()->getDocument('person', 'person1'); + $person1Document = $this->getDatabase()->getDocument('person', 'person1'); // Assert document does not contain non existing relation document. $this->assertEquals(null, $person1Document->getAttribute('library')); - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'person', 'person1', $person1->setAttribute('library', 'library1') ); // Update through create - $library10 = static::getDatabase()->createDocument('library', new Document([ + $library10 = $this->getDatabase()->createDocument('library', new Document([ '$id' => 'library10', '$permissions' => [ Permission::read(Role::any()), @@ -5467,7 +5476,7 @@ public function testOneToOneOneWayRelationship(): void 'name' => 'Library 10', 'area' => 'Area 10', ])); - $person10 = static::getDatabase()->createDocument('person', new Document([ + $person10 = $this->getDatabase()->createDocument('person', new Document([ '$id' => 'person10', '$permissions' => [ Permission::read(Role::any()), @@ -5482,11 +5491,11 @@ public function testOneToOneOneWayRelationship(): void ], ])); $this->assertEquals('Library 10 Updated', $person10->getAttribute('library')->getAttribute('name')); - $library10 = static::getDatabase()->getDocument('library', $library10->getId()); + $library10 = $this->getDatabase()->getDocument('library', $library10->getId()); $this->assertEquals('Library 10 Updated', $library10->getAttribute('name')); // Create document with relationship with related ID - static::getDatabase()->createDocument('library', new Document([ + $this->getDatabase()->createDocument('library', new Document([ '$id' => 'library2', '$permissions' => [ Permission::read(Role::any()), @@ -5495,7 +5504,7 @@ public function testOneToOneOneWayRelationship(): void 'name' => 'Library 2', 'area' => 'Area 2', ])); - static::getDatabase()->createDocument('person', new Document([ + $this->getDatabase()->createDocument('person', new Document([ '$id' => 'person2', '$permissions' => [ Permission::read(Role::any()), @@ -5507,34 +5516,34 @@ public function testOneToOneOneWayRelationship(): void ])); // Get documents with relationship - $person1 = static::getDatabase()->getDocument('person', 'person1'); + $person1 = $this->getDatabase()->getDocument('person', 'person1'); $library = $person1->getAttribute('library'); $this->assertEquals('library1', $library['$id']); $this->assertArrayNotHasKey('person', $library); - $person = static::getDatabase()->getDocument('person', 'person2'); + $person = $this->getDatabase()->getDocument('person', 'person2'); $library = $person->getAttribute('library'); $this->assertEquals('library2', $library['$id']); $this->assertArrayNotHasKey('person', $library); // Get related documents - $library = static::getDatabase()->getDocument('library', 'library1'); + $library = $this->getDatabase()->getDocument('library', 'library1'); $this->assertArrayNotHasKey('person', $library); - $library = static::getDatabase()->getDocument('library', 'library2'); + $library = $this->getDatabase()->getDocument('library', 'library2'); $this->assertArrayNotHasKey('person', $library); - $people = static::getDatabase()->find('person', [ + $people = $this->getDatabase()->find('person', [ Query::select(['name']) ]); $this->assertArrayNotHasKey('library', $people[0]); - $people = static::getDatabase()->find('person'); + $people = $this->getDatabase()->find('person'); $this->assertEquals(3, \count($people)); // Select related document attributes - $person = static::getDatabase()->findOne('person', [ + $person = $this->getDatabase()->findOne('person', [ Query::select(['*', 'library.name']) ]); @@ -5545,7 +5554,7 @@ public function testOneToOneOneWayRelationship(): void $this->assertEquals('Library 1', $person->getAttribute('library')->getAttribute('name')); $this->assertArrayNotHasKey('area', $person->getAttribute('library')); - $person = static::getDatabase()->getDocument('person', 'person1', [ + $person = $this->getDatabase()->getDocument('person', 'person1', [ Query::select(['*', 'library.name', '$id']) ]); @@ -5554,36 +5563,36 @@ public function testOneToOneOneWayRelationship(): void - $document = static::getDatabase()->getDocument('person', $person->getId(), [ + $document = $this->getDatabase()->getDocument('person', $person->getId(), [ Query::select(['name']), ]); $this->assertArrayNotHasKey('library', $document); $this->assertEquals('Person 1', $document['name']); - $document = static::getDatabase()->getDocument('person', $person->getId(), [ + $document = $this->getDatabase()->getDocument('person', $person->getId(), [ Query::select(['*']), ]); $this->assertEquals('library1', $document['library']); - $document = static::getDatabase()->getDocument('person', $person->getId(), [ + $document = $this->getDatabase()->getDocument('person', $person->getId(), [ Query::select(['library.*']), ]); $this->assertEquals('Library 1', $document['library']['name']); $this->assertArrayNotHasKey('name', $document); // Update root document attribute without altering relationship - $person1 = static::getDatabase()->updateDocument( + $person1 = $this->getDatabase()->updateDocument( 'person', $person1->getId(), $person1->setAttribute('name', 'Person 1 Updated') ); $this->assertEquals('Person 1 Updated', $person1->getAttribute('name')); - $person1 = static::getDatabase()->getDocument('person', 'person1'); + $person1 = $this->getDatabase()->getDocument('person', 'person1'); $this->assertEquals('Person 1 Updated', $person1->getAttribute('name')); // Update nested document attribute - $person1 = static::getDatabase()->updateDocument( + $person1 = $this->getDatabase()->updateDocument( 'person', $person1->getId(), $person1->setAttribute( @@ -5595,11 +5604,11 @@ public function testOneToOneOneWayRelationship(): void ); $this->assertEquals('Library 1 Updated', $person1->getAttribute('library')->getAttribute('name')); - $person1 = static::getDatabase()->getDocument('person', 'person1'); + $person1 = $this->getDatabase()->getDocument('person', 'person1'); $this->assertEquals('Library 1 Updated', $person1->getAttribute('library')->getAttribute('name')); // Create new document with no relationship - $person3 = static::getDatabase()->createDocument('person', new Document([ + $person3 = $this->getDatabase()->createDocument('person', new Document([ '$id' => 'person3', '$permissions' => [ Permission::read(Role::any()), @@ -5610,7 +5619,7 @@ public function testOneToOneOneWayRelationship(): void ])); // Update to relate to created document - $person3 = static::getDatabase()->updateDocument( + $person3 = $this->getDatabase()->updateDocument( 'person', $person3->getId(), $person3->setAttribute('library', new Document([ @@ -5625,23 +5634,23 @@ public function testOneToOneOneWayRelationship(): void ); $this->assertEquals('library3', $person3->getAttribute('library')['$id']); - $person3 = static::getDatabase()->getDocument('person', 'person3'); + $person3 = $this->getDatabase()->getDocument('person', 'person3'); $this->assertEquals('Library 3', $person3['library']['name']); - $libraryDocument = static::getDatabase()->getDocument('library', 'library3'); + $libraryDocument = $this->getDatabase()->getDocument('library', 'library3'); $libraryDocument->setAttribute('name', 'Library 3 updated'); - static::getDatabase()->updateDocument('library', 'library3', $libraryDocument); - $libraryDocument = static::getDatabase()->getDocument('library', 'library3'); + $this->getDatabase()->updateDocument('library', 'library3', $libraryDocument); + $libraryDocument = $this->getDatabase()->getDocument('library', 'library3'); $this->assertEquals('Library 3 updated', $libraryDocument['name']); - $person3 = static::getDatabase()->getDocument('person', 'person3'); + $person3 = $this->getDatabase()->getDocument('person', 'person3'); // Todo: This is failing $this->assertEquals($libraryDocument['name'], $person3['library']['name']); $this->assertEquals('library3', $person3->getAttribute('library')['$id']); // One to one can't relate to multiple documents, unique index throws duplicate try { - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'person', $person1->getId(), $person1->setAttribute('library', 'library2') @@ -5652,7 +5661,7 @@ public function testOneToOneOneWayRelationship(): void } // Create new document - $library4 = static::getDatabase()->createDocument('library', new Document([ + $library4 = $this->getDatabase()->createDocument('library', new Document([ '$id' => 'library4', '$permissions' => [ Permission::read(Role::any()), @@ -5663,33 +5672,33 @@ public function testOneToOneOneWayRelationship(): void ])); // Relate existing document to new document - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'person', $person1->getId(), $person1->setAttribute('library', 'library4') ); // Relate existing document to new document as nested data - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'person', $person1->getId(), $person1->setAttribute('library', $library4) ); // Rename relationship key - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'person', id: 'library', newKey: 'newLibrary' ); // Get document with again - $person = static::getDatabase()->getDocument('person', 'person1'); + $person = $this->getDatabase()->getDocument('person', 'person1'); $library = $person->getAttribute('newLibrary'); $this->assertEquals('library4', $library['$id']); // Create person with no relationship - static::getDatabase()->createDocument('person', new Document([ + $this->getDatabase()->createDocument('person', new Document([ '$id' => 'person4', '$permissions' => [ Permission::read(Role::any()), @@ -5700,22 +5709,22 @@ public function testOneToOneOneWayRelationship(): void ])); // Can delete parent document with no relation with on delete set to restrict - $deleted = static::getDatabase()->deleteDocument('person', 'person4'); + $deleted = $this->getDatabase()->deleteDocument('person', 'person4'); $this->assertEquals(true, $deleted); - $person4 = static::getDatabase()->getDocument('person', 'person4'); + $person4 = $this->getDatabase()->getDocument('person', 'person4'); $this->assertEquals(true, $person4->isEmpty()); // Cannot delete document while still related to another with on delete set to restrict try { - static::getDatabase()->deleteDocument('person', 'person1'); + $this->getDatabase()->deleteDocument('person', 'person1'); $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertEquals('Cannot delete document because it has at least one related document.', $e->getMessage()); } // Can delete child document while still related to another with on delete set to restrict - $person5 = static::getDatabase()->createDocument('person', new Document([ + $person5 = $this->getDatabase()->createDocument('person', new Document([ '$id' => 'person5', '$permissions' => [ Permission::read(Role::any()), @@ -5733,60 +5742,60 @@ public function testOneToOneOneWayRelationship(): void 'area' => 'Area 5', ], ])); - $deleted = static::getDatabase()->deleteDocument('library', 'library5'); + $deleted = $this->getDatabase()->deleteDocument('library', 'library5'); $this->assertEquals(true, $deleted); - $person5 = static::getDatabase()->getDocument('person', 'person5'); + $person5 = $this->getDatabase()->getDocument('person', 'person5'); $this->assertEquals(null, $person5->getAttribute('newLibrary')); // Change on delete to set null - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'person', id: 'newLibrary', onDelete: Database::RELATION_MUTATE_SET_NULL ); // Delete parent, no effect on children for one-way - static::getDatabase()->deleteDocument('person', 'person1'); + $this->getDatabase()->deleteDocument('person', 'person1'); // Delete child, set parent relating attribute to null for one-way - static::getDatabase()->deleteDocument('library', 'library2'); + $this->getDatabase()->deleteDocument('library', 'library2'); // Check relation was set to null - $person2 = static::getDatabase()->getDocument('person', 'person2'); + $person2 = $this->getDatabase()->getDocument('person', 'person2'); $this->assertEquals(null, $person2->getAttribute('newLibrary', '')); // Relate to another document - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'person', $person2->getId(), $person2->setAttribute('newLibrary', 'library4') ); // Change on delete to cascade - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'person', id: 'newLibrary', onDelete: Database::RELATION_MUTATE_CASCADE ); // Delete parent, will delete child - static::getDatabase()->deleteDocument('person', 'person2'); + $this->getDatabase()->deleteDocument('person', 'person2'); // Check parent and child were deleted - $person = static::getDatabase()->getDocument('person', 'person2'); + $person = $this->getDatabase()->getDocument('person', 'person2'); $this->assertEquals(true, $person->isEmpty()); - $library = static::getDatabase()->getDocument('library', 'library4'); + $library = $this->getDatabase()->getDocument('library', 'library4'); $this->assertEquals(true, $library->isEmpty()); // Delete relationship - static::getDatabase()->deleteRelationship( + $this->getDatabase()->deleteRelationship( 'person', 'newLibrary' ); // Check parent doesn't have relationship anymore - $person = static::getDatabase()->getDocument('person', 'person1'); + $person = $this->getDatabase()->getDocument('person', 'person1'); $library = $person->getAttribute('newLibrary', ''); $this->assertEquals(null, $library); } @@ -5800,26 +5809,26 @@ public function testOneToOneOneWayRelationship(): void */ public function testOneToOneTwoWayRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('country'); - static::getDatabase()->createCollection('city'); + $this->getDatabase()->createCollection('country'); + $this->getDatabase()->createCollection('city'); - static::getDatabase()->createAttribute('country', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('city', 'code', Database::VAR_STRING, 3, true); - static::getDatabase()->createAttribute('city', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('country', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('city', 'code', Database::VAR_STRING, 3, true); + $this->getDatabase()->createAttribute('city', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'country', relatedCollection: 'city', type: Database::RELATION_ONE_TO_ONE, twoWay: true ); - $collection = static::getDatabase()->getCollection('country'); + $collection = $this->getDatabase()->getCollection('country'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { if ($attribute['key'] === 'city') { @@ -5833,7 +5842,7 @@ public function testOneToOneTwoWayRelationship(): void } } - $collection = static::getDatabase()->getCollection('city'); + $collection = $this->getDatabase()->getCollection('city'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { if ($attribute['key'] === 'country') { @@ -5868,40 +5877,40 @@ public function testOneToOneTwoWayRelationship(): void ], ]); - static::getDatabase()->createDocument('country', $doc); - $country1 = static::getDatabase()->getDocument('country', 'country1'); + $this->getDatabase()->createDocument('country', $doc); + $country1 = $this->getDatabase()->getDocument('country', 'country1'); $this->assertEquals('London', $country1->getAttribute('city')->getAttribute('name')); // Update a document with non existing related document. It should not get added to the list. - static::getDatabase()->updateDocument('country', 'country1', (new Document($doc->getArrayCopy()))->setAttribute('city', 'no-city')); + $this->getDatabase()->updateDocument('country', 'country1', (new Document($doc->getArrayCopy()))->setAttribute('city', 'no-city')); - $country1Document = static::getDatabase()->getDocument('country', 'country1'); + $country1Document = $this->getDatabase()->getDocument('country', 'country1'); // Assert document does not contain non existing relation document. $this->assertEquals(null, $country1Document->getAttribute('city')); - static::getDatabase()->updateDocument('country', 'country1', (new Document($doc->getArrayCopy()))->setAttribute('city', 'city1')); + $this->getDatabase()->updateDocument('country', 'country1', (new Document($doc->getArrayCopy()))->setAttribute('city', 'city1')); try { - static::getDatabase()->deleteDocument('country', 'country1'); + $this->getDatabase()->deleteDocument('country', 'country1'); $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertInstanceOf(RestrictedException::class, $e); } - $this->assertTrue(static::getDatabase()->deleteDocument('city', 'city1')); + $this->assertTrue($this->getDatabase()->deleteDocument('city', 'city1')); - $city1 = static::getDatabase()->getDocument('city', 'city1'); + $city1 = $this->getDatabase()->getDocument('city', 'city1'); $this->assertTrue($city1->isEmpty()); - $country1 = static::getDatabase()->getDocument('country', 'country1'); + $country1 = $this->getDatabase()->getDocument('country', 'country1'); $this->assertTrue($country1->getAttribute('city')->isEmpty()); - $this->assertTrue(static::getDatabase()->deleteDocument('country', 'country1')); + $this->assertTrue($this->getDatabase()->deleteDocument('country', 'country1')); - static::getDatabase()->createDocument('country', $doc); - $country1 = static::getDatabase()->getDocument('country', 'country1'); + $this->getDatabase()->createDocument('country', $doc); + $country1 = $this->getDatabase()->getDocument('country', 'country1'); $this->assertEquals('London', $country1->getAttribute('city')->getAttribute('name')); // Create document with relationship with related ID - static::getDatabase()->createDocument('city', new Document([ + $this->getDatabase()->createDocument('city', new Document([ '$id' => 'city2', '$permissions' => [ Permission::read(Role::any()), @@ -5911,7 +5920,7 @@ public function testOneToOneTwoWayRelationship(): void 'name' => 'Paris', 'code' => 'PAR', ])); - static::getDatabase()->createDocument('country', new Document([ + $this->getDatabase()->createDocument('country', new Document([ '$id' => 'country2', '$permissions' => [ Permission::read(Role::any()), @@ -5923,7 +5932,7 @@ public function testOneToOneTwoWayRelationship(): void ])); // Create from child side - static::getDatabase()->createDocument('city', new Document([ + $this->getDatabase()->createDocument('city', new Document([ '$id' => 'city3', '$permissions' => [ Permission::read(Role::any()), @@ -5942,7 +5951,7 @@ public function testOneToOneTwoWayRelationship(): void 'name' => 'New Zealand', ], ])); - static::getDatabase()->createDocument('country', new Document([ + $this->getDatabase()->createDocument('country', new Document([ '$id' => 'country4', '$permissions' => [ Permission::read(Role::any()), @@ -5951,7 +5960,7 @@ public function testOneToOneTwoWayRelationship(): void ], 'name' => 'Australia', ])); - static::getDatabase()->createDocument('city', new Document([ + $this->getDatabase()->createDocument('city', new Document([ '$id' => 'city4', '$permissions' => [ Permission::read(Role::any()), @@ -5964,53 +5973,53 @@ public function testOneToOneTwoWayRelationship(): void ])); // Get document with relationship - $city = static::getDatabase()->getDocument('city', 'city1'); + $city = $this->getDatabase()->getDocument('city', 'city1'); $country = $city->getAttribute('country'); $this->assertEquals('country1', $country['$id']); $this->assertArrayNotHasKey('city', $country); - $city = static::getDatabase()->getDocument('city', 'city2'); + $city = $this->getDatabase()->getDocument('city', 'city2'); $country = $city->getAttribute('country'); $this->assertEquals('country2', $country['$id']); $this->assertArrayNotHasKey('city', $country); - $city = static::getDatabase()->getDocument('city', 'city3'); + $city = $this->getDatabase()->getDocument('city', 'city3'); $country = $city->getAttribute('country'); $this->assertEquals('country3', $country['$id']); $this->assertArrayNotHasKey('city', $country); - $city = static::getDatabase()->getDocument('city', 'city4'); + $city = $this->getDatabase()->getDocument('city', 'city4'); $country = $city->getAttribute('country'); $this->assertEquals('country4', $country['$id']); $this->assertArrayNotHasKey('city', $country); // Get inverse document with relationship - $country = static::getDatabase()->getDocument('country', 'country1'); + $country = $this->getDatabase()->getDocument('country', 'country1'); $city = $country->getAttribute('city'); $this->assertEquals('city1', $city['$id']); $this->assertArrayNotHasKey('country', $city); - $country = static::getDatabase()->getDocument('country', 'country2'); + $country = $this->getDatabase()->getDocument('country', 'country2'); $city = $country->getAttribute('city'); $this->assertEquals('city2', $city['$id']); $this->assertArrayNotHasKey('country', $city); - $country = static::getDatabase()->getDocument('country', 'country3'); + $country = $this->getDatabase()->getDocument('country', 'country3'); $city = $country->getAttribute('city'); $this->assertEquals('city3', $city['$id']); $this->assertArrayNotHasKey('country', $city); - $country = static::getDatabase()->getDocument('country', 'country4'); + $country = $this->getDatabase()->getDocument('country', 'country4'); $city = $country->getAttribute('city'); $this->assertEquals('city4', $city['$id']); $this->assertArrayNotHasKey('country', $city); - $countries = static::getDatabase()->find('country'); + $countries = $this->getDatabase()->find('country'); $this->assertEquals(4, \count($countries)); // Select related document attributes - $country = static::getDatabase()->findOne('country', [ + $country = $this->getDatabase()->findOne('country', [ Query::select(['*', 'city.name']) ]); @@ -6021,41 +6030,41 @@ public function testOneToOneTwoWayRelationship(): void $this->assertEquals('London', $country->getAttribute('city')->getAttribute('name')); $this->assertArrayNotHasKey('code', $country->getAttribute('city')); - $country = static::getDatabase()->getDocument('country', 'country1', [ + $country = $this->getDatabase()->getDocument('country', 'country1', [ Query::select(['*', 'city.name']) ]); $this->assertEquals('London', $country->getAttribute('city')->getAttribute('name')); $this->assertArrayNotHasKey('code', $country->getAttribute('city')); - $country1 = static::getDatabase()->getDocument('country', 'country1'); + $country1 = $this->getDatabase()->getDocument('country', 'country1'); // Update root document attribute without altering relationship - $country1 = static::getDatabase()->updateDocument( + $country1 = $this->getDatabase()->updateDocument( 'country', $country1->getId(), $country1->setAttribute('name', 'Country 1 Updated') ); $this->assertEquals('Country 1 Updated', $country1->getAttribute('name')); - $country1 = static::getDatabase()->getDocument('country', 'country1'); + $country1 = $this->getDatabase()->getDocument('country', 'country1'); $this->assertEquals('Country 1 Updated', $country1->getAttribute('name')); - $city2 = static::getDatabase()->getDocument('city', 'city2'); + $city2 = $this->getDatabase()->getDocument('city', 'city2'); // Update inverse root document attribute without altering relationship - $city2 = static::getDatabase()->updateDocument( + $city2 = $this->getDatabase()->updateDocument( 'city', $city2->getId(), $city2->setAttribute('name', 'City 2 Updated') ); $this->assertEquals('City 2 Updated', $city2->getAttribute('name')); - $city2 = static::getDatabase()->getDocument('city', 'city2'); + $city2 = $this->getDatabase()->getDocument('city', 'city2'); $this->assertEquals('City 2 Updated', $city2->getAttribute('name')); // Update nested document attribute - $country1 = static::getDatabase()->updateDocument( + $country1 = $this->getDatabase()->updateDocument( 'country', $country1->getId(), $country1->setAttribute( @@ -6067,11 +6076,11 @@ public function testOneToOneTwoWayRelationship(): void ); $this->assertEquals('City 1 Updated', $country1->getAttribute('city')->getAttribute('name')); - $country1 = static::getDatabase()->getDocument('country', 'country1'); + $country1 = $this->getDatabase()->getDocument('country', 'country1'); $this->assertEquals('City 1 Updated', $country1->getAttribute('city')->getAttribute('name')); // Update inverse nested document attribute - $city2 = static::getDatabase()->updateDocument( + $city2 = $this->getDatabase()->updateDocument( 'city', $city2->getId(), $city2->setAttribute( @@ -6083,11 +6092,11 @@ public function testOneToOneTwoWayRelationship(): void ); $this->assertEquals('Country 2 Updated', $city2->getAttribute('country')->getAttribute('name')); - $city2 = static::getDatabase()->getDocument('city', 'city2'); + $city2 = $this->getDatabase()->getDocument('city', 'city2'); $this->assertEquals('Country 2 Updated', $city2->getAttribute('country')->getAttribute('name')); // Create new document with no relationship - $country5 = static::getDatabase()->createDocument('country', new Document([ + $country5 = $this->getDatabase()->createDocument('country', new Document([ '$id' => 'country5', '$permissions' => [ Permission::read(Role::any()), @@ -6098,7 +6107,7 @@ public function testOneToOneTwoWayRelationship(): void ])); // Update to relate to created document - $country5 = static::getDatabase()->updateDocument( + $country5 = $this->getDatabase()->updateDocument( 'country', $country5->getId(), $country5->setAttribute('city', new Document([ @@ -6113,11 +6122,11 @@ public function testOneToOneTwoWayRelationship(): void ); $this->assertEquals('city5', $country5->getAttribute('city')['$id']); - $country5 = static::getDatabase()->getDocument('country', 'country5'); + $country5 = $this->getDatabase()->getDocument('country', 'country5'); $this->assertEquals('city5', $country5->getAttribute('city')['$id']); // Create new document with no relationship - $city6 = static::getDatabase()->createDocument('city', new Document([ + $city6 = $this->getDatabase()->createDocument('city', new Document([ '$id' => 'city6', '$permissions' => [ Permission::read(Role::any()), @@ -6129,7 +6138,7 @@ public function testOneToOneTwoWayRelationship(): void ])); // Update to relate to created document - $city6 = static::getDatabase()->updateDocument( + $city6 = $this->getDatabase()->updateDocument( 'city', $city6->getId(), $city6->setAttribute('country', new Document([ @@ -6143,12 +6152,12 @@ public function testOneToOneTwoWayRelationship(): void ); $this->assertEquals('country6', $city6->getAttribute('country')['$id']); - $city6 = static::getDatabase()->getDocument('city', 'city6'); + $city6 = $this->getDatabase()->getDocument('city', 'city6'); $this->assertEquals('country6', $city6->getAttribute('country')['$id']); // One to one can't relate to multiple documents, unique index throws duplicate try { - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'country', $country1->getId(), $country1->setAttribute('city', 'city2') @@ -6158,21 +6167,21 @@ public function testOneToOneTwoWayRelationship(): void $this->assertInstanceOf(DuplicateException::class, $e); } - $city1 = static::getDatabase()->getDocument('city', 'city1'); + $city1 = $this->getDatabase()->getDocument('city', 'city1'); // Set relationship to null - $city1 = static::getDatabase()->updateDocument( + $city1 = $this->getDatabase()->updateDocument( 'city', $city1->getId(), $city1->setAttribute('country', null) ); $this->assertEquals(null, $city1->getAttribute('country')); - $city1 = static::getDatabase()->getDocument('city', 'city1'); + $city1 = $this->getDatabase()->getDocument('city', 'city1'); $this->assertEquals(null, $city1->getAttribute('country')); // Create a new city with no relation - $city7 = static::getDatabase()->createDocument('city', new Document([ + $city7 = $this->getDatabase()->createDocument('city', new Document([ '$id' => 'city7', '$permissions' => [ Permission::read(Role::any()), @@ -6184,21 +6193,21 @@ public function testOneToOneTwoWayRelationship(): void ])); // Update document with relation to new document - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'country', $country1->getId(), $country1->setAttribute('city', 'city7') ); // Relate existing document to new document as nested data - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'country', $country1->getId(), $country1->setAttribute('city', $city7) ); // Create a new country with no relation - static::getDatabase()->createDocument('country', new Document([ + $this->getDatabase()->createDocument('country', new Document([ '$id' => 'country7', '$permissions' => [ Permission::read(Role::any()), @@ -6209,14 +6218,14 @@ public function testOneToOneTwoWayRelationship(): void ])); // Update inverse document with new related document - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'city', $city1->getId(), $city1->setAttribute('country', 'country7') ); // Rename relationship keys on both sides - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( 'country', 'city', 'newCity', @@ -6224,17 +6233,17 @@ public function testOneToOneTwoWayRelationship(): void ); // Get document with new relationship key - $city = static::getDatabase()->getDocument('city', 'city1'); + $city = $this->getDatabase()->getDocument('city', 'city1'); $country = $city->getAttribute('newCountry'); $this->assertEquals('country7', $country['$id']); // Get inverse document with new relationship key - $country = static::getDatabase()->getDocument('country', 'country7'); + $country = $this->getDatabase()->getDocument('country', 'country7'); $city = $country->getAttribute('newCity'); $this->assertEquals('city1', $city['$id']); // Create a new country with no relation - static::getDatabase()->createDocument('country', new Document([ + $this->getDatabase()->createDocument('country', new Document([ '$id' => 'country8', '$permissions' => [ Permission::read(Role::any()), @@ -6245,87 +6254,87 @@ public function testOneToOneTwoWayRelationship(): void ])); // Can delete parent document with no relation with on delete set to restrict - $deleted = static::getDatabase()->deleteDocument('country', 'country8'); + $deleted = $this->getDatabase()->deleteDocument('country', 'country8'); $this->assertEquals(1, $deleted); - $country8 = static::getDatabase()->getDocument('country', 'country8'); + $country8 = $this->getDatabase()->getDocument('country', 'country8'); $this->assertEquals(true, $country8->isEmpty()); // Cannot delete document while still related to another with on delete set to restrict try { - static::getDatabase()->deleteDocument('country', 'country1'); + $this->getDatabase()->deleteDocument('country', 'country1'); $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertEquals('Cannot delete document because it has at least one related document.', $e->getMessage()); } // Change on delete to set null - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'country', id: 'newCity', onDelete: Database::RELATION_MUTATE_SET_NULL ); - static::getDatabase()->updateDocument('city', 'city1', new Document(['newCountry' => null, '$id' => 'city1'])); - $city1 = static::getDatabase()->getDocument('city', 'city1'); + $this->getDatabase()->updateDocument('city', 'city1', new Document(['newCountry' => null, '$id' => 'city1'])); + $city1 = $this->getDatabase()->getDocument('city', 'city1'); $this->assertNull($city1->getAttribute('newCountry')); // Check Delete TwoWay TRUE && RELATION_MUTATE_SET_NULL && related value NULL - $this->assertTrue(static::getDatabase()->deleteDocument('city', 'city1')); - $city1 = static::getDatabase()->getDocument('city', 'city1'); + $this->assertTrue($this->getDatabase()->deleteDocument('city', 'city1')); + $city1 = $this->getDatabase()->getDocument('city', 'city1'); $this->assertTrue($city1->isEmpty()); // Delete parent, will set child relationship to null for two-way - static::getDatabase()->deleteDocument('country', 'country1'); + $this->getDatabase()->deleteDocument('country', 'country1'); // Check relation was set to null - $city7 = static::getDatabase()->getDocument('city', 'city7'); + $city7 = $this->getDatabase()->getDocument('city', 'city7'); $this->assertEquals(null, $city7->getAttribute('country', '')); // Delete child, set parent relationship to null for two-way - static::getDatabase()->deleteDocument('city', 'city2'); + $this->getDatabase()->deleteDocument('city', 'city2'); // Check relation was set to null - $country2 = static::getDatabase()->getDocument('country', 'country2'); + $country2 = $this->getDatabase()->getDocument('country', 'country2'); $this->assertEquals(null, $country2->getAttribute('city', '')); // Relate again - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'city', $city7->getId(), $city7->setAttribute('newCountry', 'country2') ); // Change on delete to cascade - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'country', id: 'newCity', onDelete: Database::RELATION_MUTATE_CASCADE ); // Delete parent, will delete child - static::getDatabase()->deleteDocument('country', 'country7'); + $this->getDatabase()->deleteDocument('country', 'country7'); // Check parent and child were deleted - $library = static::getDatabase()->getDocument('country', 'country7'); + $library = $this->getDatabase()->getDocument('country', 'country7'); $this->assertEquals(true, $library->isEmpty()); - $library = static::getDatabase()->getDocument('city', 'city1'); + $library = $this->getDatabase()->getDocument('city', 'city1'); $this->assertEquals(true, $library->isEmpty()); // Delete child, will delete parent for two-way - static::getDatabase()->deleteDocument('city', 'city7'); + $this->getDatabase()->deleteDocument('city', 'city7'); // Check parent and child were deleted - $library = static::getDatabase()->getDocument('city', 'city7'); + $library = $this->getDatabase()->getDocument('city', 'city7'); $this->assertEquals(true, $library->isEmpty()); - $library = static::getDatabase()->getDocument('country', 'country2'); + $library = $this->getDatabase()->getDocument('country', 'country2'); $this->assertEquals(true, $library->isEmpty()); // Create new document to check after deleting relationship - static::getDatabase()->createDocument('city', new Document([ + $this->getDatabase()->createDocument('city', new Document([ '$id' => 'city7', '$permissions' => [ Permission::read(Role::any()), @@ -6341,33 +6350,33 @@ public function testOneToOneTwoWayRelationship(): void ])); // Delete relationship - static::getDatabase()->deleteRelationship( + $this->getDatabase()->deleteRelationship( 'country', 'newCity' ); // Try to get document again - $country = static::getDatabase()->getDocument('country', 'country4'); + $country = $this->getDatabase()->getDocument('country', 'country4'); $city = $country->getAttribute('newCity'); $this->assertEquals(null, $city); // Try to get inverse document again - $city = static::getDatabase()->getDocument('city', 'city7'); + $city = $this->getDatabase()->getDocument('city', 'city7'); $country = $city->getAttribute('newCountry'); $this->assertEquals(null, $country); } public function testIdenticalTwoWayKeyRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('parent'); - static::getDatabase()->createCollection('child'); + $this->getDatabase()->createCollection('parent'); + $this->getDatabase()->createCollection('child'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'parent', relatedCollection: 'child', type: Database::RELATION_ONE_TO_ONE, @@ -6375,7 +6384,7 @@ public function testIdenticalTwoWayKeyRelationship(): void ); try { - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'parent', relatedCollection: 'child', type: Database::RELATION_ONE_TO_MANY, @@ -6386,7 +6395,7 @@ public function testIdenticalTwoWayKeyRelationship(): void $this->assertEquals('Related attribute already exists', $e->getMessage()); } - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'parent', relatedCollection: 'child', type: Database::RELATION_ONE_TO_MANY, @@ -6394,7 +6403,7 @@ public function testIdenticalTwoWayKeyRelationship(): void twoWayKey: 'parent_id' ); - $collection = static::getDatabase()->getCollection('parent'); + $collection = $this->getDatabase()->getCollection('parent'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { if ($attribute['key'] === 'child1') { @@ -6406,7 +6415,7 @@ public function testIdenticalTwoWayKeyRelationship(): void } } - static::getDatabase()->createDocument('parent', new Document([ + $this->getDatabase()->createDocument('parent', new Document([ '$permissions' => [ Permission::read(Role::any()), Permission::update(Role::any()), @@ -6424,7 +6433,7 @@ public function testIdenticalTwoWayKeyRelationship(): void ], ])); - $documents = static::getDatabase()->find('parent', []); + $documents = $this->getDatabase()->find('parent', []); $document = array_pop($documents); $this->assertArrayHasKey('child1', $document); $this->assertEquals('foo', $document->getAttribute('child1')->getId()); @@ -6432,7 +6441,7 @@ public function testIdenticalTwoWayKeyRelationship(): void $this->assertEquals('bar', $document->getAttribute('children')[0]->getId()); try { - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'parent', id: 'children', newKey: 'child1' @@ -6443,7 +6452,7 @@ public function testIdenticalTwoWayKeyRelationship(): void } try { - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'parent', id: 'children', newTwoWayKey: 'parent' @@ -6456,19 +6465,19 @@ public function testIdenticalTwoWayKeyRelationship(): void public function testOneToManyOneWayRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('artist'); - static::getDatabase()->createCollection('album'); + $this->getDatabase()->createCollection('artist'); + $this->getDatabase()->createCollection('album'); - static::getDatabase()->createAttribute('artist', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('album', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('album', 'price', Database::VAR_FLOAT, 0, true); + $this->getDatabase()->createAttribute('artist', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('album', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('album', 'price', Database::VAR_FLOAT, 0, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'artist', relatedCollection: 'album', type: Database::RELATION_ONE_TO_MANY, @@ -6476,7 +6485,7 @@ public function testOneToManyOneWayRelationship(): void ); // Check metadata for collection - $collection = static::getDatabase()->getCollection('artist'); + $collection = $this->getDatabase()->getCollection('artist'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { @@ -6492,7 +6501,7 @@ public function testOneToManyOneWayRelationship(): void } // Create document with relationship with nested data - $artist1 = static::getDatabase()->createDocument('artist', new Document([ + $artist1 = $this->getDatabase()->createDocument('artist', new Document([ '$id' => 'artist1', '$permissions' => [ Permission::read(Role::any()), @@ -6514,14 +6523,14 @@ public function testOneToManyOneWayRelationship(): void ])); // Update a document with non existing related document. It should not get added to the list. - static::getDatabase()->updateDocument('artist', 'artist1', $artist1->setAttribute('albums', ['album1', 'no-album'])); + $this->getDatabase()->updateDocument('artist', 'artist1', $artist1->setAttribute('albums', ['album1', 'no-album'])); - $artist1Document = static::getDatabase()->getDocument('artist', 'artist1'); + $artist1Document = $this->getDatabase()->getDocument('artist', 'artist1'); // Assert document does not contain non existing relation document. $this->assertEquals(1, \count($artist1Document->getAttribute('albums'))); // Create document with relationship with related ID - static::getDatabase()->createDocument('album', new Document([ + $this->getDatabase()->createDocument('album', new Document([ '$id' => 'album2', '$permissions' => [ Permission::read(Role::any()), @@ -6531,7 +6540,7 @@ public function testOneToManyOneWayRelationship(): void 'name' => 'Album 2', 'price' => 19.99, ])); - static::getDatabase()->createDocument('artist', new Document([ + $this->getDatabase()->createDocument('artist', new Document([ '$id' => 'artist2', '$permissions' => [ Permission::read(Role::any()), @@ -6553,19 +6562,19 @@ public function testOneToManyOneWayRelationship(): void ] ])); - $documents = static::getDatabase()->find('artist', [ + $documents = $this->getDatabase()->find('artist', [ Query::select(['name']), Query::limit(1) ]); $this->assertArrayNotHasKey('albums', $documents[0]); // Get document with relationship - $artist = static::getDatabase()->getDocument('artist', 'artist1'); + $artist = $this->getDatabase()->getDocument('artist', 'artist1'); $albums = $artist->getAttribute('albums', []); $this->assertEquals('album1', $albums[0]['$id']); $this->assertArrayNotHasKey('artist', $albums[0]); - $artist = static::getDatabase()->getDocument('artist', 'artist2'); + $artist = $this->getDatabase()->getDocument('artist', 'artist2'); $albums = $artist->getAttribute('albums', []); $this->assertEquals('album2', $albums[0]['$id']); $this->assertArrayNotHasKey('artist', $albums[0]); @@ -6573,18 +6582,18 @@ public function testOneToManyOneWayRelationship(): void $this->assertCount(2, $albums); // Get related document - $album = static::getDatabase()->getDocument('album', 'album1'); + $album = $this->getDatabase()->getDocument('album', 'album1'); $this->assertArrayNotHasKey('artist', $album); - $album = static::getDatabase()->getDocument('album', 'album2'); + $album = $this->getDatabase()->getDocument('album', 'album2'); $this->assertArrayNotHasKey('artist', $album); - $artists = static::getDatabase()->find('artist'); + $artists = $this->getDatabase()->find('artist'); $this->assertEquals(2, \count($artists)); // Select related document attributes - $artist = static::getDatabase()->findOne('artist', [ + $artist = $this->getDatabase()->findOne('artist', [ Query::select(['*', 'albums.name']) ]); @@ -6595,7 +6604,7 @@ public function testOneToManyOneWayRelationship(): void $this->assertEquals('Album 1', $artist->getAttribute('albums')[0]->getAttribute('name')); $this->assertArrayNotHasKey('price', $artist->getAttribute('albums')[0]); - $artist = static::getDatabase()->getDocument('artist', 'artist1', [ + $artist = $this->getDatabase()->getDocument('artist', 'artist1', [ Query::select(['*', 'albums.name']) ]); @@ -6603,36 +6612,36 @@ public function testOneToManyOneWayRelationship(): void $this->assertArrayNotHasKey('price', $artist->getAttribute('albums')[0]); // Update root document attribute without altering relationship - $artist1 = static::getDatabase()->updateDocument( + $artist1 = $this->getDatabase()->updateDocument( 'artist', $artist1->getId(), $artist1->setAttribute('name', 'Artist 1 Updated') ); $this->assertEquals('Artist 1 Updated', $artist1->getAttribute('name')); - $artist1 = static::getDatabase()->getDocument('artist', 'artist1'); + $artist1 = $this->getDatabase()->getDocument('artist', 'artist1'); $this->assertEquals('Artist 1 Updated', $artist1->getAttribute('name')); // Update nested document attribute $albums = $artist1->getAttribute('albums', []); $albums[0]->setAttribute('name', 'Album 1 Updated'); - $artist1 = static::getDatabase()->updateDocument( + $artist1 = $this->getDatabase()->updateDocument( 'artist', $artist1->getId(), $artist1->setAttribute('albums', $albums) ); $this->assertEquals('Album 1 Updated', $artist1->getAttribute('albums')[0]->getAttribute('name')); - $artist1 = static::getDatabase()->getDocument('artist', 'artist1'); + $artist1 = $this->getDatabase()->getDocument('artist', 'artist1'); $this->assertEquals('Album 1 Updated', $artist1->getAttribute('albums')[0]->getAttribute('name')); $albumId = $artist1->getAttribute('albums')[0]->getAttribute('$id'); - $albumDocument = static::getDatabase()->getDocument('album', $albumId); + $albumDocument = $this->getDatabase()->getDocument('album', $albumId); $albumDocument->setAttribute('name', 'Album 1 Updated!!!'); - static::getDatabase()->updateDocument('album', $albumDocument->getId(), $albumDocument); - $albumDocument = static::getDatabase()->getDocument('album', $albumDocument->getId()); - $artist1 = static::getDatabase()->getDocument('artist', $artist1->getId()); + $this->getDatabase()->updateDocument('album', $albumDocument->getId(), $albumDocument); + $albumDocument = $this->getDatabase()->getDocument('album', $albumDocument->getId()); + $artist1 = $this->getDatabase()->getDocument('artist', $artist1->getId()); $this->assertEquals('Album 1 Updated!!!', $albumDocument['name']); $this->assertEquals($albumDocument->getId(), $artist1->getAttribute('albums')[0]->getId()); @@ -6640,7 +6649,7 @@ public function testOneToManyOneWayRelationship(): void $this->assertEquals($albumDocument->getAttribute('name'), $artist1->getAttribute('albums')[0]->getAttribute('name')); // Create new document with no relationship - $artist3 = static::getDatabase()->createDocument('artist', new Document([ + $artist3 = $this->getDatabase()->createDocument('artist', new Document([ '$id' => 'artist3', '$permissions' => [ Permission::read(Role::any()), @@ -6651,7 +6660,7 @@ public function testOneToManyOneWayRelationship(): void ])); // Update to relate to created document - $artist3 = static::getDatabase()->updateDocument( + $artist3 = $this->getDatabase()->updateDocument( 'artist', $artist3->getId(), $artist3->setAttribute('albums', [new Document([ @@ -6667,37 +6676,37 @@ public function testOneToManyOneWayRelationship(): void ); $this->assertEquals('Album 3', $artist3->getAttribute('albums')[0]->getAttribute('name')); - $artist3 = static::getDatabase()->getDocument('artist', 'artist3'); + $artist3 = $this->getDatabase()->getDocument('artist', 'artist3'); $this->assertEquals('Album 3', $artist3->getAttribute('albums')[0]->getAttribute('name')); // Update document with new related documents, will remove existing relations - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'artist', $artist1->getId(), $artist1->setAttribute('albums', ['album2']) ); // Update document with new related documents, will remove existing relations - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'artist', $artist1->getId(), $artist1->setAttribute('albums', ['album1', 'album2']) ); // Rename relationship key - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( 'artist', 'albums', 'newAlbums' ); // Get document with new relationship key - $artist = static::getDatabase()->getDocument('artist', 'artist1'); + $artist = $this->getDatabase()->getDocument('artist', 'artist1'); $albums = $artist->getAttribute('newAlbums'); $this->assertEquals('album1', $albums[0]['$id']); // Create new document with no relationship - static::getDatabase()->createDocument('artist', new Document([ + $this->getDatabase()->createDocument('artist', new Document([ '$id' => 'artist4', '$permissions' => [ Permission::read(Role::any()), @@ -6708,56 +6717,56 @@ public function testOneToManyOneWayRelationship(): void ])); // Can delete document with no relationship when on delete is set to restrict - $deleted = static::getDatabase()->deleteDocument('artist', 'artist4'); + $deleted = $this->getDatabase()->deleteDocument('artist', 'artist4'); $this->assertEquals(true, $deleted); - $artist4 = static::getDatabase()->getDocument('artist', 'artist4'); + $artist4 = $this->getDatabase()->getDocument('artist', 'artist4'); $this->assertEquals(true, $artist4->isEmpty()); // Try to delete document while still related to another with on delete: restrict try { - static::getDatabase()->deleteDocument('artist', 'artist1'); + $this->getDatabase()->deleteDocument('artist', 'artist1'); $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertEquals('Cannot delete document because it has at least one related document.', $e->getMessage()); } // Change on delete to set null - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'artist', id: 'newAlbums', onDelete: Database::RELATION_MUTATE_SET_NULL ); // Delete parent, set child relationship to null - static::getDatabase()->deleteDocument('artist', 'artist1'); + $this->getDatabase()->deleteDocument('artist', 'artist1'); // Check relation was set to null - $album2 = static::getDatabase()->getDocument('album', 'album2'); + $album2 = $this->getDatabase()->getDocument('album', 'album2'); $this->assertEquals(null, $album2->getAttribute('artist', '')); // Relate again - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'album', $album2->getId(), $album2->setAttribute('artist', 'artist2') ); // Change on delete to cascade - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'artist', id: 'newAlbums', onDelete: Database::RELATION_MUTATE_CASCADE ); // Delete parent, will delete child - static::getDatabase()->deleteDocument('artist', 'artist2'); + $this->getDatabase()->deleteDocument('artist', 'artist2'); // Check parent and child were deleted - $library = static::getDatabase()->getDocument('artist', 'artist2'); + $library = $this->getDatabase()->getDocument('artist', 'artist2'); $this->assertEquals(true, $library->isEmpty()); - $library = static::getDatabase()->getDocument('album', 'album2'); + $library = $this->getDatabase()->getDocument('album', 'album2'); $this->assertEquals(true, $library->isEmpty()); $albums = []; @@ -6774,7 +6783,7 @@ public function testOneToManyOneWayRelationship(): void ]; } - $artist = static::getDatabase()->createDocument('artist', new Document([ + $artist = $this->getDatabase()->createDocument('artist', new Document([ '$permissions' => [ Permission::read(Role::any()), Permission::delete(Role::any()), @@ -6783,29 +6792,29 @@ public function testOneToManyOneWayRelationship(): void 'newAlbums' => $albums ])); - $artist = static::getDatabase()->getDocument('artist', $artist->getId()); + $artist = $this->getDatabase()->getDocument('artist', $artist->getId()); $this->assertCount(50, $artist->getAttribute('newAlbums')); - $albums = static::getDatabase()->find('album', [ + $albums = $this->getDatabase()->find('album', [ Query::equal('artist', [$artist->getId()]), Query::limit(999) ]); $this->assertCount(50, $albums); - $count = static::getDatabase()->count('album', [ + $count = $this->getDatabase()->count('album', [ Query::equal('artist', [$artist->getId()]), ]); $this->assertEquals(50, $count); - static::getDatabase()->deleteDocument('album', 'album_1'); - $artist = static::getDatabase()->getDocument('artist', $artist->getId()); + $this->getDatabase()->deleteDocument('album', 'album_1'); + $artist = $this->getDatabase()->getDocument('artist', $artist->getId()); $this->assertCount(49, $artist->getAttribute('newAlbums')); - static::getDatabase()->deleteDocument('artist', $artist->getId()); + $this->getDatabase()->deleteDocument('artist', $artist->getId()); - $albums = static::getDatabase()->find('album', [ + $albums = $this->getDatabase()->find('album', [ Query::equal('artist', [$artist->getId()]), Query::limit(999) ]); @@ -6813,32 +6822,32 @@ public function testOneToManyOneWayRelationship(): void $this->assertCount(0, $albums); // Delete relationship - static::getDatabase()->deleteRelationship( + $this->getDatabase()->deleteRelationship( 'artist', 'newAlbums' ); // Try to get document again - $artist = static::getDatabase()->getDocument('artist', 'artist1'); + $artist = $this->getDatabase()->getDocument('artist', 'artist1'); $albums = $artist->getAttribute('newAlbums', ''); $this->assertEquals(null, $albums); } public function testOneToManyTwoWayRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('customer'); - static::getDatabase()->createCollection('account'); + $this->getDatabase()->createCollection('customer'); + $this->getDatabase()->createCollection('account'); - static::getDatabase()->createAttribute('customer', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('account', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('account', 'number', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('customer', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('account', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('account', 'number', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'customer', relatedCollection: 'account', type: Database::RELATION_ONE_TO_MANY, @@ -6847,7 +6856,7 @@ public function testOneToManyTwoWayRelationship(): void ); // Check metadata for collection - $collection = static::getDatabase()->getCollection('customer'); + $collection = $this->getDatabase()->getCollection('customer'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { if ($attribute['key'] === 'accounts') { @@ -6862,7 +6871,7 @@ public function testOneToManyTwoWayRelationship(): void } // Check metadata for related collection - $collection = static::getDatabase()->getCollection('account'); + $collection = $this->getDatabase()->getCollection('account'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { if ($attribute['key'] === 'customer') { @@ -6877,7 +6886,7 @@ public function testOneToManyTwoWayRelationship(): void } // Create document with relationship with nested data - $customer1 = static::getDatabase()->createDocument('customer', new Document([ + $customer1 = $this->getDatabase()->createDocument('customer', new Document([ '$id' => 'customer1', '$permissions' => [ Permission::read(Role::any()), @@ -6900,14 +6909,14 @@ public function testOneToManyTwoWayRelationship(): void ])); // Update a document with non existing related document. It should not get added to the list. - static::getDatabase()->updateDocument('customer', 'customer1', $customer1->setAttribute('accounts', ['account1','no-account'])); + $this->getDatabase()->updateDocument('customer', 'customer1', $customer1->setAttribute('accounts', ['account1','no-account'])); - $customer1Document = static::getDatabase()->getDocument('customer', 'customer1'); + $customer1Document = $this->getDatabase()->getDocument('customer', 'customer1'); // Assert document does not contain non existing relation document. $this->assertEquals(1, \count($customer1Document->getAttribute('accounts'))); // Create document with relationship with related ID - $account2 = static::getDatabase()->createDocument('account', new Document([ + $account2 = $this->getDatabase()->createDocument('account', new Document([ '$id' => 'account2', '$permissions' => [ Permission::read(Role::any()), @@ -6917,7 +6926,7 @@ public function testOneToManyTwoWayRelationship(): void 'name' => 'Account 2', 'number' => '987654321', ])); - static::getDatabase()->createDocument('customer', new Document([ + $this->getDatabase()->createDocument('customer', new Document([ '$id' => 'customer2', '$permissions' => [ Permission::read(Role::any()), @@ -6931,7 +6940,7 @@ public function testOneToManyTwoWayRelationship(): void ])); // Create from child side - static::getDatabase()->createDocument('account', new Document([ + $this->getDatabase()->createDocument('account', new Document([ '$id' => 'account3', '$permissions' => [ Permission::read(Role::any()), @@ -6950,7 +6959,7 @@ public function testOneToManyTwoWayRelationship(): void 'name' => 'Customer 3' ] ])); - static::getDatabase()->createDocument('customer', new Document([ + $this->getDatabase()->createDocument('customer', new Document([ '$id' => 'customer4', '$permissions' => [ Permission::read(Role::any()), @@ -6959,7 +6968,7 @@ public function testOneToManyTwoWayRelationship(): void ], 'name' => 'Customer 4', ])); - static::getDatabase()->createDocument('account', new Document([ + $this->getDatabase()->createDocument('account', new Document([ '$id' => 'account4', '$permissions' => [ Permission::read(Role::any()), @@ -6972,53 +6981,53 @@ public function testOneToManyTwoWayRelationship(): void ])); // Get documents with relationship - $customer = static::getDatabase()->getDocument('customer', 'customer1'); + $customer = $this->getDatabase()->getDocument('customer', 'customer1'); $accounts = $customer->getAttribute('accounts', []); $this->assertEquals('account1', $accounts[0]['$id']); $this->assertArrayNotHasKey('customer', $accounts[0]); - $customer = static::getDatabase()->getDocument('customer', 'customer2'); + $customer = $this->getDatabase()->getDocument('customer', 'customer2'); $accounts = $customer->getAttribute('accounts', []); $this->assertEquals('account2', $accounts[0]['$id']); $this->assertArrayNotHasKey('customer', $accounts[0]); - $customer = static::getDatabase()->getDocument('customer', 'customer3'); + $customer = $this->getDatabase()->getDocument('customer', 'customer3'); $accounts = $customer->getAttribute('accounts', []); $this->assertEquals('account3', $accounts[0]['$id']); $this->assertArrayNotHasKey('customer', $accounts[0]); - $customer = static::getDatabase()->getDocument('customer', 'customer4'); + $customer = $this->getDatabase()->getDocument('customer', 'customer4'); $accounts = $customer->getAttribute('accounts', []); $this->assertEquals('account4', $accounts[0]['$id']); $this->assertArrayNotHasKey('customer', $accounts[0]); // Get related documents - $account = static::getDatabase()->getDocument('account', 'account1'); + $account = $this->getDatabase()->getDocument('account', 'account1'); $customer = $account->getAttribute('customer'); $this->assertEquals('customer1', $customer['$id']); $this->assertArrayNotHasKey('accounts', $customer); - $account = static::getDatabase()->getDocument('account', 'account2'); + $account = $this->getDatabase()->getDocument('account', 'account2'); $customer = $account->getAttribute('customer'); $this->assertEquals('customer2', $customer['$id']); $this->assertArrayNotHasKey('accounts', $customer); - $account = static::getDatabase()->getDocument('account', 'account3'); + $account = $this->getDatabase()->getDocument('account', 'account3'); $customer = $account->getAttribute('customer'); $this->assertEquals('customer3', $customer['$id']); $this->assertArrayNotHasKey('accounts', $customer); - $account = static::getDatabase()->getDocument('account', 'account4'); + $account = $this->getDatabase()->getDocument('account', 'account4'); $customer = $account->getAttribute('customer'); $this->assertEquals('customer4', $customer['$id']); $this->assertArrayNotHasKey('accounts', $customer); - $customers = static::getDatabase()->find('customer'); + $customers = $this->getDatabase()->find('customer'); $this->assertEquals(4, \count($customers)); // Select related document attributes - $customer = static::getDatabase()->findOne('customer', [ + $customer = $this->getDatabase()->findOne('customer', [ Query::select(['*', 'accounts.name']) ]); @@ -7029,7 +7038,7 @@ public function testOneToManyTwoWayRelationship(): void $this->assertEquals('Account 1', $customer->getAttribute('accounts')[0]->getAttribute('name')); $this->assertArrayNotHasKey('number', $customer->getAttribute('accounts')[0]); - $customer = static::getDatabase()->getDocument('customer', 'customer1', [ + $customer = $this->getDatabase()->getDocument('customer', 'customer1', [ Query::select(['*', 'accounts.name']) ]); @@ -7037,45 +7046,45 @@ public function testOneToManyTwoWayRelationship(): void $this->assertArrayNotHasKey('number', $customer->getAttribute('accounts')[0]); // Update root document attribute without altering relationship - $customer1 = static::getDatabase()->updateDocument( + $customer1 = $this->getDatabase()->updateDocument( 'customer', $customer1->getId(), $customer1->setAttribute('name', 'Customer 1 Updated') ); $this->assertEquals('Customer 1 Updated', $customer1->getAttribute('name')); - $customer1 = static::getDatabase()->getDocument('customer', 'customer1'); + $customer1 = $this->getDatabase()->getDocument('customer', 'customer1'); $this->assertEquals('Customer 1 Updated', $customer1->getAttribute('name')); - $account2 = static::getDatabase()->getDocument('account', 'account2'); + $account2 = $this->getDatabase()->getDocument('account', 'account2'); // Update inverse root document attribute without altering relationship - $account2 = static::getDatabase()->updateDocument( + $account2 = $this->getDatabase()->updateDocument( 'account', $account2->getId(), $account2->setAttribute('name', 'Account 2 Updated') ); $this->assertEquals('Account 2 Updated', $account2->getAttribute('name')); - $account2 = static::getDatabase()->getDocument('account', 'account2'); + $account2 = $this->getDatabase()->getDocument('account', 'account2'); $this->assertEquals('Account 2 Updated', $account2->getAttribute('name')); // Update nested document attribute $accounts = $customer1->getAttribute('accounts', []); $accounts[0]->setAttribute('name', 'Account 1 Updated'); - $customer1 = static::getDatabase()->updateDocument( + $customer1 = $this->getDatabase()->updateDocument( 'customer', $customer1->getId(), $customer1->setAttribute('accounts', $accounts) ); $this->assertEquals('Account 1 Updated', $customer1->getAttribute('accounts')[0]->getAttribute('name')); - $customer1 = static::getDatabase()->getDocument('customer', 'customer1'); + $customer1 = $this->getDatabase()->getDocument('customer', 'customer1'); $this->assertEquals('Account 1 Updated', $customer1->getAttribute('accounts')[0]->getAttribute('name')); // Update inverse nested document attribute - $account2 = static::getDatabase()->updateDocument( + $account2 = $this->getDatabase()->updateDocument( 'account', $account2->getId(), $account2->setAttribute( @@ -7087,11 +7096,11 @@ public function testOneToManyTwoWayRelationship(): void ); $this->assertEquals('Customer 2 Updated', $account2->getAttribute('customer')->getAttribute('name')); - $account2 = static::getDatabase()->getDocument('account', 'account2'); + $account2 = $this->getDatabase()->getDocument('account', 'account2'); $this->assertEquals('Customer 2 Updated', $account2->getAttribute('customer')->getAttribute('name')); // Create new document with no relationship - $customer5 = static::getDatabase()->createDocument('customer', new Document([ + $customer5 = $this->getDatabase()->createDocument('customer', new Document([ '$id' => 'customer5', '$permissions' => [ Permission::read(Role::any()), @@ -7102,7 +7111,7 @@ public function testOneToManyTwoWayRelationship(): void ])); // Update to relate to created document - $customer5 = static::getDatabase()->updateDocument( + $customer5 = $this->getDatabase()->updateDocument( 'customer', $customer5->getId(), $customer5->setAttribute('accounts', [new Document([ @@ -7118,11 +7127,11 @@ public function testOneToManyTwoWayRelationship(): void ); $this->assertEquals('Account 5', $customer5->getAttribute('accounts')[0]->getAttribute('name')); - $customer5 = static::getDatabase()->getDocument('customer', 'customer5'); + $customer5 = $this->getDatabase()->getDocument('customer', 'customer5'); $this->assertEquals('Account 5', $customer5->getAttribute('accounts')[0]->getAttribute('name')); // Create new child document with no relationship - $account6 = static::getDatabase()->createDocument('account', new Document([ + $account6 = $this->getDatabase()->createDocument('account', new Document([ '$id' => 'account6', '$permissions' => [ Permission::read(Role::any()), @@ -7134,7 +7143,7 @@ public function testOneToManyTwoWayRelationship(): void ])); // Update inverse to relate to created document - $account6 = static::getDatabase()->updateDocument( + $account6 = $this->getDatabase()->updateDocument( 'account', $account6->getId(), $account6->setAttribute('customer', new Document([ @@ -7149,32 +7158,32 @@ public function testOneToManyTwoWayRelationship(): void ); $this->assertEquals('Customer 6', $account6->getAttribute('customer')->getAttribute('name')); - $account6 = static::getDatabase()->getDocument('account', 'account6'); + $account6 = $this->getDatabase()->getDocument('account', 'account6'); $this->assertEquals('Customer 6', $account6->getAttribute('customer')->getAttribute('name')); // Update document with new related document, will remove existing relations - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'customer', $customer1->getId(), $customer1->setAttribute('accounts', ['account2']) ); // Update document with new related document - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'customer', $customer1->getId(), $customer1->setAttribute('accounts', ['account1', 'account2']) ); // Update inverse document - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'account', $account2->getId(), $account2->setAttribute('customer', 'customer2') ); // Rename relationship keys on both sides - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( 'customer', 'accounts', 'newAccounts', @@ -7182,17 +7191,17 @@ public function testOneToManyTwoWayRelationship(): void ); // Get document with new relationship key - $customer = static::getDatabase()->getDocument('customer', 'customer1'); + $customer = $this->getDatabase()->getDocument('customer', 'customer1'); $accounts = $customer->getAttribute('newAccounts'); $this->assertEquals('account1', $accounts[0]['$id']); // Get inverse document with new relationship key - $account = static::getDatabase()->getDocument('account', 'account1'); + $account = $this->getDatabase()->getDocument('account', 'account1'); $customer = $account->getAttribute('newCustomer'); $this->assertEquals('customer1', $customer['$id']); // Create new document with no relationship - static::getDatabase()->createDocument('customer', new Document([ + $this->getDatabase()->createDocument('customer', new Document([ '$id' => 'customer7', '$permissions' => [ Permission::read(Role::any()), @@ -7203,91 +7212,91 @@ public function testOneToManyTwoWayRelationship(): void ])); // Can delete document with no relationship when on delete is set to restrict - $deleted = static::getDatabase()->deleteDocument('customer', 'customer7'); + $deleted = $this->getDatabase()->deleteDocument('customer', 'customer7'); $this->assertEquals(true, $deleted); - $customer7 = static::getDatabase()->getDocument('customer', 'customer7'); + $customer7 = $this->getDatabase()->getDocument('customer', 'customer7'); $this->assertEquals(true, $customer7->isEmpty()); // Try to delete document while still related to another with on delete: restrict try { - static::getDatabase()->deleteDocument('customer', 'customer1'); + $this->getDatabase()->deleteDocument('customer', 'customer1'); $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertEquals('Cannot delete document because it has at least one related document.', $e->getMessage()); } // Change on delete to set null - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'customer', id: 'newAccounts', onDelete: Database::RELATION_MUTATE_SET_NULL ); // Delete parent, set child relationship to null - static::getDatabase()->deleteDocument('customer', 'customer1'); + $this->getDatabase()->deleteDocument('customer', 'customer1'); // Check relation was set to null - $account1 = static::getDatabase()->getDocument('account', 'account1'); + $account1 = $this->getDatabase()->getDocument('account', 'account1'); $this->assertEquals(null, $account2->getAttribute('newCustomer', '')); // Relate again - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'account', $account1->getId(), $account1->setAttribute('newCustomer', 'customer2') ); // Change on delete to cascade - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'customer', id: 'newAccounts', onDelete: Database::RELATION_MUTATE_CASCADE ); // Delete parent, will delete child - static::getDatabase()->deleteDocument('customer', 'customer2'); + $this->getDatabase()->deleteDocument('customer', 'customer2'); // Check parent and child were deleted - $library = static::getDatabase()->getDocument('customer', 'customer2'); + $library = $this->getDatabase()->getDocument('customer', 'customer2'); $this->assertEquals(true, $library->isEmpty()); - $library = static::getDatabase()->getDocument('account', 'account2'); + $library = $this->getDatabase()->getDocument('account', 'account2'); $this->assertEquals(true, $library->isEmpty()); // Delete relationship - static::getDatabase()->deleteRelationship( + $this->getDatabase()->deleteRelationship( 'customer', 'newAccounts' ); // Try to get document again - $customer = static::getDatabase()->getDocument('customer', 'customer1'); + $customer = $this->getDatabase()->getDocument('customer', 'customer1'); $accounts = $customer->getAttribute('newAccounts'); $this->assertEquals(null, $accounts); // Try to get inverse document again - $accounts = static::getDatabase()->getDocument('account', 'account1'); + $accounts = $this->getDatabase()->getDocument('account', 'account1'); $customer = $accounts->getAttribute('newCustomer'); $this->assertEquals(null, $customer); } public function testManyToOneOneWayRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('review'); - static::getDatabase()->createCollection('movie'); + $this->getDatabase()->createCollection('review'); + $this->getDatabase()->createCollection('movie'); - static::getDatabase()->createAttribute('review', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('movie', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('movie', 'length', Database::VAR_INTEGER, 0, true, formatOptions: ['min' => 0, 'max' => 999]); - static::getDatabase()->createAttribute('movie', 'date', Database::VAR_DATETIME, 0, false, filters: ['datetime']); - static::getDatabase()->createAttribute('review', 'date', Database::VAR_DATETIME, 0, false, filters: ['datetime']); - static::getDatabase()->createRelationship( + $this->getDatabase()->createAttribute('review', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('movie', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('movie', 'length', Database::VAR_INTEGER, 0, true, formatOptions: ['min' => 0, 'max' => 999]); + $this->getDatabase()->createAttribute('movie', 'date', Database::VAR_DATETIME, 0, false, filters: ['datetime']); + $this->getDatabase()->createAttribute('review', 'date', Database::VAR_DATETIME, 0, false, filters: ['datetime']); + $this->getDatabase()->createRelationship( collection: 'review', relatedCollection: 'movie', type: Database::RELATION_MANY_TO_ONE, @@ -7295,7 +7304,7 @@ public function testManyToOneOneWayRelationship(): void ); // Check metadata for collection - $collection = static::getDatabase()->getCollection('review'); + $collection = $this->getDatabase()->getCollection('review'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { if ($attribute['key'] === 'movie') { @@ -7310,7 +7319,7 @@ public function testManyToOneOneWayRelationship(): void } // Check metadata for related collection - $collection = static::getDatabase()->getCollection('movie'); + $collection = $this->getDatabase()->getCollection('movie'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { if ($attribute['key'] === 'reviews') { @@ -7325,7 +7334,7 @@ public function testManyToOneOneWayRelationship(): void } // Create document with relationship with nested data - $review1 = static::getDatabase()->createDocument('review', new Document([ + $review1 = $this->getDatabase()->createDocument('review', new Document([ '$id' => 'review1', '$permissions' => [ Permission::read(Role::any()), @@ -7348,16 +7357,16 @@ public function testManyToOneOneWayRelationship(): void ])); // Update a document with non existing related document. It should not get added to the list. - static::getDatabase()->updateDocument('review', 'review1', $review1->setAttribute('movie', 'no-movie')); + $this->getDatabase()->updateDocument('review', 'review1', $review1->setAttribute('movie', 'no-movie')); - $review1Document = static::getDatabase()->getDocument('review', 'review1'); + $review1Document = $this->getDatabase()->getDocument('review', 'review1'); // Assert document does not contain non existing relation document. $this->assertEquals(null, $review1Document->getAttribute('movie')); - static::getDatabase()->updateDocument('review', 'review1', $review1->setAttribute('movie', 'movie1')); + $this->getDatabase()->updateDocument('review', 'review1', $review1->setAttribute('movie', 'movie1')); // Create document with relationship to existing document by ID - $review10 = static::getDatabase()->createDocument('review', new Document([ + $review10 = $this->getDatabase()->createDocument('review', new Document([ '$id' => 'review10', '$permissions' => [ Permission::read(Role::any()), @@ -7370,7 +7379,7 @@ public function testManyToOneOneWayRelationship(): void ])); // Create document with relationship with related ID - static::getDatabase()->createDocument('movie', new Document([ + $this->getDatabase()->createDocument('movie', new Document([ '$id' => 'movie2', '$permissions' => [ Permission::read(Role::any()), @@ -7381,7 +7390,7 @@ public function testManyToOneOneWayRelationship(): void 'length' => 90, 'date' => '2023-04-03 10:35:27.390', ])); - static::getDatabase()->createDocument('review', new Document([ + $this->getDatabase()->createDocument('review', new Document([ '$id' => 'review2', '$permissions' => [ Permission::read(Role::any()), @@ -7394,12 +7403,12 @@ public function testManyToOneOneWayRelationship(): void ])); // Get document with relationship - $review = static::getDatabase()->getDocument('review', 'review1'); + $review = $this->getDatabase()->getDocument('review', 'review1'); $movie = $review->getAttribute('movie', []); $this->assertEquals('movie1', $movie['$id']); $this->assertArrayNotHasKey('reviews', $movie); - $documents = static::getDatabase()->find('review', [ + $documents = $this->getDatabase()->find('review', [ Query::select(['date', 'movie.date']) ]); @@ -7413,24 +7422,24 @@ public function testManyToOneOneWayRelationship(): void $this->assertEquals(29, strlen($document['date'])); // checks filter $this->assertEquals(29, strlen($document['movie']['date'])); - $review = static::getDatabase()->getDocument('review', 'review2'); + $review = $this->getDatabase()->getDocument('review', 'review2'); $movie = $review->getAttribute('movie', []); $this->assertEquals('movie2', $movie['$id']); $this->assertArrayNotHasKey('reviews', $movie); // Get related document - $movie = static::getDatabase()->getDocument('movie', 'movie1'); + $movie = $this->getDatabase()->getDocument('movie', 'movie1'); $this->assertArrayNotHasKey('reviews', $movie); - $movie = static::getDatabase()->getDocument('movie', 'movie2'); + $movie = $this->getDatabase()->getDocument('movie', 'movie2'); $this->assertArrayNotHasKey('reviews', $movie); - $reviews = static::getDatabase()->find('review'); + $reviews = $this->getDatabase()->find('review'); $this->assertEquals(3, \count($reviews)); // Select related document attributes - $review = static::getDatabase()->findOne('review', [ + $review = $this->getDatabase()->findOne('review', [ Query::select(['*', 'movie.name']) ]); @@ -7441,7 +7450,7 @@ public function testManyToOneOneWayRelationship(): void $this->assertEquals('Movie 1', $review->getAttribute('movie')->getAttribute('name')); $this->assertArrayNotHasKey('length', $review->getAttribute('movie')); - $review = static::getDatabase()->getDocument('review', 'review1', [ + $review = $this->getDatabase()->getDocument('review', 'review1', [ Query::select(['*', 'movie.name']) ]); @@ -7449,32 +7458,32 @@ public function testManyToOneOneWayRelationship(): void $this->assertArrayNotHasKey('length', $review->getAttribute('movie')); // Update root document attribute without altering relationship - $review1 = static::getDatabase()->updateDocument( + $review1 = $this->getDatabase()->updateDocument( 'review', $review1->getId(), $review1->setAttribute('name', 'Review 1 Updated') ); $this->assertEquals('Review 1 Updated', $review1->getAttribute('name')); - $review1 = static::getDatabase()->getDocument('review', 'review1'); + $review1 = $this->getDatabase()->getDocument('review', 'review1'); $this->assertEquals('Review 1 Updated', $review1->getAttribute('name')); // Update nested document attribute $movie = $review1->getAttribute('movie'); $movie->setAttribute('name', 'Movie 1 Updated'); - $review1 = static::getDatabase()->updateDocument( + $review1 = $this->getDatabase()->updateDocument( 'review', $review1->getId(), $review1->setAttribute('movie', $movie) ); $this->assertEquals('Movie 1 Updated', $review1->getAttribute('movie')->getAttribute('name')); - $review1 = static::getDatabase()->getDocument('review', 'review1'); + $review1 = $this->getDatabase()->getDocument('review', 'review1'); $this->assertEquals('Movie 1 Updated', $review1->getAttribute('movie')->getAttribute('name')); // Create new document with no relationship - $review5 = static::getDatabase()->createDocument('review', new Document([ + $review5 = $this->getDatabase()->createDocument('review', new Document([ '$id' => 'review5', '$permissions' => [ Permission::read(Role::any()), @@ -7485,7 +7494,7 @@ public function testManyToOneOneWayRelationship(): void ])); // Update to relate to created document - $review5 = static::getDatabase()->updateDocument( + $review5 = $this->getDatabase()->updateDocument( 'review', $review5->getId(), $review5->setAttribute('movie', new Document([ @@ -7501,39 +7510,39 @@ public function testManyToOneOneWayRelationship(): void ); $this->assertEquals('Movie 5', $review5->getAttribute('movie')->getAttribute('name')); - $review5 = static::getDatabase()->getDocument('review', 'review5'); + $review5 = $this->getDatabase()->getDocument('review', 'review5'); $this->assertEquals('Movie 5', $review5->getAttribute('movie')->getAttribute('name')); // Update document with new related document - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'review', $review1->getId(), $review1->setAttribute('movie', 'movie2') ); // Rename relationship keys on both sides - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( 'review', 'movie', 'newMovie', ); // Get document with new relationship key - $review = static::getDatabase()->getDocument('review', 'review1'); + $review = $this->getDatabase()->getDocument('review', 'review1'); $movie = $review->getAttribute('newMovie'); $this->assertEquals('movie2', $movie['$id']); // Reset values - $review1 = static::getDatabase()->getDocument('review', 'review1'); + $review1 = $this->getDatabase()->getDocument('review', 'review1'); - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'review', $review1->getId(), $review1->setAttribute('newMovie', 'movie1') ); // Create new document with no relationship - static::getDatabase()->createDocument('movie', new Document([ + $this->getDatabase()->createDocument('movie', new Document([ '$id' => 'movie3', '$permissions' => [ Permission::read(Role::any()), @@ -7545,78 +7554,78 @@ public function testManyToOneOneWayRelationship(): void ])); // Can delete document with no relationship when on delete is set to restrict - $deleted = static::getDatabase()->deleteDocument('movie', 'movie3'); + $deleted = $this->getDatabase()->deleteDocument('movie', 'movie3'); $this->assertEquals(true, $deleted); - $movie3 = static::getDatabase()->getDocument('movie', 'movie3'); + $movie3 = $this->getDatabase()->getDocument('movie', 'movie3'); $this->assertEquals(true, $movie3->isEmpty()); // Try to delete document while still related to another with on delete: restrict try { - static::getDatabase()->deleteDocument('movie', 'movie1'); + $this->getDatabase()->deleteDocument('movie', 'movie1'); $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertEquals('Cannot delete document because it has at least one related document.', $e->getMessage()); } // Change on delete to set null - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'review', id: 'newMovie', onDelete: Database::RELATION_MUTATE_SET_NULL ); // Delete child, set parent relationship to null - static::getDatabase()->deleteDocument('movie', 'movie1'); + $this->getDatabase()->deleteDocument('movie', 'movie1'); // Check relation was set to null - $review1 = static::getDatabase()->getDocument('review', 'review1'); + $review1 = $this->getDatabase()->getDocument('review', 'review1'); $this->assertEquals(null, $review1->getAttribute('newMovie')); // Change on delete to cascade - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'review', id: 'newMovie', onDelete: Database::RELATION_MUTATE_CASCADE ); // Delete child, will delete parent - static::getDatabase()->deleteDocument('movie', 'movie2'); + $this->getDatabase()->deleteDocument('movie', 'movie2'); // Check parent and child were deleted - $library = static::getDatabase()->getDocument('movie', 'movie2'); + $library = $this->getDatabase()->getDocument('movie', 'movie2'); $this->assertEquals(true, $library->isEmpty()); - $library = static::getDatabase()->getDocument('review', 'review2'); + $library = $this->getDatabase()->getDocument('review', 'review2'); $this->assertEquals(true, $library->isEmpty()); // Delete relationship - static::getDatabase()->deleteRelationship( + $this->getDatabase()->deleteRelationship( 'review', 'newMovie' ); // Try to get document again - $review = static::getDatabase()->getDocument('review', 'review1'); + $review = $this->getDatabase()->getDocument('review', 'review1'); $movie = $review->getAttribute('newMovie'); $this->assertEquals(null, $movie); } public function testManyToOneTwoWayRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('product'); - static::getDatabase()->createCollection('store'); + $this->getDatabase()->createCollection('product'); + $this->getDatabase()->createCollection('store'); - static::getDatabase()->createAttribute('store', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('store', 'opensAt', Database::VAR_STRING, 5, true); + $this->getDatabase()->createAttribute('store', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('store', 'opensAt', Database::VAR_STRING, 5, true); - static::getDatabase()->createAttribute( + $this->getDatabase()->createAttribute( collection: 'product', id: 'name', type: Database::VAR_STRING, @@ -7624,7 +7633,7 @@ public function testManyToOneTwoWayRelationship(): void required: true ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'product', relatedCollection: 'store', type: Database::RELATION_MANY_TO_ONE, @@ -7633,7 +7642,7 @@ public function testManyToOneTwoWayRelationship(): void ); // Check metadata for collection - $collection = static::getDatabase()->getCollection('product'); + $collection = $this->getDatabase()->getCollection('product'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { if ($attribute['key'] === 'store') { @@ -7648,7 +7657,7 @@ public function testManyToOneTwoWayRelationship(): void } // Check metadata for related collection - $collection = static::getDatabase()->getCollection('store'); + $collection = $this->getDatabase()->getCollection('store'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { if ($attribute['key'] === 'products') { @@ -7663,7 +7672,7 @@ public function testManyToOneTwoWayRelationship(): void } // Create document with relationship with nested data - $product1 = static::getDatabase()->createDocument('product', new Document([ + $product1 = $this->getDatabase()->createDocument('product', new Document([ '$id' => 'product1', '$permissions' => [ Permission::read(Role::any()), @@ -7684,16 +7693,16 @@ public function testManyToOneTwoWayRelationship(): void ])); // Update a document with non existing related document. It should not get added to the list. - static::getDatabase()->updateDocument('product', 'product1', $product1->setAttribute('store', 'no-store')); + $this->getDatabase()->updateDocument('product', 'product1', $product1->setAttribute('store', 'no-store')); - $product1Document = static::getDatabase()->getDocument('product', 'product1'); + $product1Document = $this->getDatabase()->getDocument('product', 'product1'); // Assert document does not contain non existing relation document. $this->assertEquals(null, $product1Document->getAttribute('store')); - static::getDatabase()->updateDocument('product', 'product1', $product1->setAttribute('store', 'store1')); + $this->getDatabase()->updateDocument('product', 'product1', $product1->setAttribute('store', 'store1')); // Create document with relationship with related ID - static::getDatabase()->createDocument('store', new Document([ + $this->getDatabase()->createDocument('store', new Document([ '$id' => 'store2', '$permissions' => [ Permission::read(Role::any()), @@ -7703,7 +7712,7 @@ public function testManyToOneTwoWayRelationship(): void 'name' => 'Store 2', 'opensAt' => '09:30', ])); - static::getDatabase()->createDocument('product', new Document([ + $this->getDatabase()->createDocument('product', new Document([ '$id' => 'product2', '$permissions' => [ Permission::read(Role::any()), @@ -7715,7 +7724,7 @@ public function testManyToOneTwoWayRelationship(): void ])); // Create from child side - static::getDatabase()->createDocument('store', new Document([ + $this->getDatabase()->createDocument('store', new Document([ '$id' => 'store3', '$permissions' => [ Permission::read(Role::any()), @@ -7737,7 +7746,7 @@ public function testManyToOneTwoWayRelationship(): void ], ])); - static::getDatabase()->createDocument('product', new Document([ + $this->getDatabase()->createDocument('product', new Document([ '$id' => 'product4', '$permissions' => [ Permission::read(Role::any()), @@ -7746,7 +7755,7 @@ public function testManyToOneTwoWayRelationship(): void ], 'name' => 'Product 4', ])); - static::getDatabase()->createDocument('store', new Document([ + $this->getDatabase()->createDocument('store', new Document([ '$id' => 'store4', '$permissions' => [ Permission::read(Role::any()), @@ -7761,53 +7770,53 @@ public function testManyToOneTwoWayRelationship(): void ])); // Get document with relationship - $product = static::getDatabase()->getDocument('product', 'product1'); + $product = $this->getDatabase()->getDocument('product', 'product1'); $store = $product->getAttribute('store', []); $this->assertEquals('store1', $store['$id']); $this->assertArrayNotHasKey('products', $store); - $product = static::getDatabase()->getDocument('product', 'product2'); + $product = $this->getDatabase()->getDocument('product', 'product2'); $store = $product->getAttribute('store', []); $this->assertEquals('store2', $store['$id']); $this->assertArrayNotHasKey('products', $store); - $product = static::getDatabase()->getDocument('product', 'product3'); + $product = $this->getDatabase()->getDocument('product', 'product3'); $store = $product->getAttribute('store', []); $this->assertEquals('store3', $store['$id']); $this->assertArrayNotHasKey('products', $store); - $product = static::getDatabase()->getDocument('product', 'product4'); + $product = $this->getDatabase()->getDocument('product', 'product4'); $store = $product->getAttribute('store', []); $this->assertEquals('store4', $store['$id']); $this->assertArrayNotHasKey('products', $store); // Get related document - $store = static::getDatabase()->getDocument('store', 'store1'); + $store = $this->getDatabase()->getDocument('store', 'store1'); $products = $store->getAttribute('products'); $this->assertEquals('product1', $products[0]['$id']); $this->assertArrayNotHasKey('store', $products[0]); - $store = static::getDatabase()->getDocument('store', 'store2'); + $store = $this->getDatabase()->getDocument('store', 'store2'); $products = $store->getAttribute('products'); $this->assertEquals('product2', $products[0]['$id']); $this->assertArrayNotHasKey('store', $products[0]); - $store = static::getDatabase()->getDocument('store', 'store3'); + $store = $this->getDatabase()->getDocument('store', 'store3'); $products = $store->getAttribute('products'); $this->assertEquals('product3', $products[0]['$id']); $this->assertArrayNotHasKey('store', $products[0]); - $store = static::getDatabase()->getDocument('store', 'store4'); + $store = $this->getDatabase()->getDocument('store', 'store4'); $products = $store->getAttribute('products'); $this->assertEquals('product4', $products[0]['$id']); $this->assertArrayNotHasKey('store', $products[0]); - $products = static::getDatabase()->find('product'); + $products = $this->getDatabase()->find('product'); $this->assertEquals(4, \count($products)); // Select related document attributes - $product = static::getDatabase()->findOne('product', [ + $product = $this->getDatabase()->findOne('product', [ Query::select(['*', 'store.name']) ]); @@ -7818,7 +7827,7 @@ public function testManyToOneTwoWayRelationship(): void $this->assertEquals('Store 1', $product->getAttribute('store')->getAttribute('name')); $this->assertArrayNotHasKey('opensAt', $product->getAttribute('store')); - $product = static::getDatabase()->getDocument('product', 'product1', [ + $product = $this->getDatabase()->getDocument('product', 'product1', [ Query::select(['*', 'store.name']) ]); @@ -7826,58 +7835,58 @@ public function testManyToOneTwoWayRelationship(): void $this->assertArrayNotHasKey('opensAt', $product->getAttribute('store')); // Update root document attribute without altering relationship - $product1 = static::getDatabase()->updateDocument( + $product1 = $this->getDatabase()->updateDocument( 'product', $product1->getId(), $product1->setAttribute('name', 'Product 1 Updated') ); $this->assertEquals('Product 1 Updated', $product1->getAttribute('name')); - $product1 = static::getDatabase()->getDocument('product', 'product1'); + $product1 = $this->getDatabase()->getDocument('product', 'product1'); $this->assertEquals('Product 1 Updated', $product1->getAttribute('name')); // Update inverse document attribute without altering relationship - $store1 = static::getDatabase()->getDocument('store', 'store1'); - $store1 = static::getDatabase()->updateDocument( + $store1 = $this->getDatabase()->getDocument('store', 'store1'); + $store1 = $this->getDatabase()->updateDocument( 'store', $store1->getId(), $store1->setAttribute('name', 'Store 1 Updated') ); $this->assertEquals('Store 1 Updated', $store1->getAttribute('name')); - $store1 = static::getDatabase()->getDocument('store', 'store1'); + $store1 = $this->getDatabase()->getDocument('store', 'store1'); $this->assertEquals('Store 1 Updated', $store1->getAttribute('name')); // Update nested document attribute $store = $product1->getAttribute('store'); $store->setAttribute('name', 'Store 1 Updated'); - $product1 = static::getDatabase()->updateDocument( + $product1 = $this->getDatabase()->updateDocument( 'product', $product1->getId(), $product1->setAttribute('store', $store) ); $this->assertEquals('Store 1 Updated', $product1->getAttribute('store')->getAttribute('name')); - $product1 = static::getDatabase()->getDocument('product', 'product1'); + $product1 = $this->getDatabase()->getDocument('product', 'product1'); $this->assertEquals('Store 1 Updated', $product1->getAttribute('store')->getAttribute('name')); // Update inverse nested document attribute $product = $store1->getAttribute('products')[0]; $product->setAttribute('name', 'Product 1 Updated'); - $store1 = static::getDatabase()->updateDocument( + $store1 = $this->getDatabase()->updateDocument( 'store', $store1->getId(), $store1->setAttribute('products', [$product]) ); $this->assertEquals('Product 1 Updated', $store1->getAttribute('products')[0]->getAttribute('name')); - $store1 = static::getDatabase()->getDocument('store', 'store1'); + $store1 = $this->getDatabase()->getDocument('store', 'store1'); $this->assertEquals('Product 1 Updated', $store1->getAttribute('products')[0]->getAttribute('name')); // Create new document with no relationship - $product5 = static::getDatabase()->createDocument('product', new Document([ + $product5 = $this->getDatabase()->createDocument('product', new Document([ '$id' => 'product5', '$permissions' => [ Permission::read(Role::any()), @@ -7888,7 +7897,7 @@ public function testManyToOneTwoWayRelationship(): void ])); // Update to relate to created document - $product5 = static::getDatabase()->updateDocument( + $product5 = $this->getDatabase()->updateDocument( 'product', $product5->getId(), $product5->setAttribute('store', new Document([ @@ -7904,11 +7913,11 @@ public function testManyToOneTwoWayRelationship(): void ); $this->assertEquals('Store 5', $product5->getAttribute('store')->getAttribute('name')); - $product5 = static::getDatabase()->getDocument('product', 'product5'); + $product5 = $this->getDatabase()->getDocument('product', 'product5'); $this->assertEquals('Store 5', $product5->getAttribute('store')->getAttribute('name')); // Create new child document with no relationship - $store6 = static::getDatabase()->createDocument('store', new Document([ + $store6 = $this->getDatabase()->createDocument('store', new Document([ '$id' => 'store6', '$permissions' => [ Permission::read(Role::any()), @@ -7920,7 +7929,7 @@ public function testManyToOneTwoWayRelationship(): void ])); // Update inverse to related to newly created document - $store6 = static::getDatabase()->updateDocument( + $store6 = $this->getDatabase()->updateDocument( 'store', $store6->getId(), $store6->setAttribute('products', [new Document([ @@ -7935,36 +7944,36 @@ public function testManyToOneTwoWayRelationship(): void ); $this->assertEquals('Product 6', $store6->getAttribute('products')[0]->getAttribute('name')); - $store6 = static::getDatabase()->getDocument('store', 'store6'); + $store6 = $this->getDatabase()->getDocument('store', 'store6'); $this->assertEquals('Product 6', $store6->getAttribute('products')[0]->getAttribute('name')); // Update document with new related document - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'product', $product1->getId(), $product1->setAttribute('store', 'store2') ); - $store1 = static::getDatabase()->getDocument('store', 'store1'); + $store1 = $this->getDatabase()->getDocument('store', 'store1'); // Update inverse document - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'store', $store1->getId(), $store1->setAttribute('products', ['product1']) ); - $store2 = static::getDatabase()->getDocument('store', 'store2'); + $store2 = $this->getDatabase()->getDocument('store', 'store2'); // Update inverse document - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'store', $store2->getId(), $store2->setAttribute('products', ['product1', 'product2']) ); // Rename relationship keys on both sides - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( 'product', 'store', 'newStore', @@ -7972,25 +7981,25 @@ public function testManyToOneTwoWayRelationship(): void ); // Get document with new relationship key - $store = static::getDatabase()->getDocument('store', 'store2'); + $store = $this->getDatabase()->getDocument('store', 'store2'); $products = $store->getAttribute('newProducts'); $this->assertEquals('product1', $products[0]['$id']); // Get inverse document with new relationship key - $product = static::getDatabase()->getDocument('product', 'product1'); + $product = $this->getDatabase()->getDocument('product', 'product1'); $store = $product->getAttribute('newStore'); $this->assertEquals('store2', $store['$id']); // Reset relationships - $store1 = static::getDatabase()->getDocument('store', 'store1'); - static::getDatabase()->updateDocument( + $store1 = $this->getDatabase()->getDocument('store', 'store1'); + $this->getDatabase()->updateDocument( 'store', $store1->getId(), $store1->setAttribute('newProducts', ['product1']) ); // Create new document with no relationship - static::getDatabase()->createDocument('store', new Document([ + $this->getDatabase()->createDocument('store', new Document([ '$id' => 'store7', '$permissions' => [ Permission::read(Role::any()), @@ -8002,87 +8011,87 @@ public function testManyToOneTwoWayRelationship(): void ])); // Can delete document with no relationship when on delete is set to restrict - $deleted = static::getDatabase()->deleteDocument('store', 'store7'); + $deleted = $this->getDatabase()->deleteDocument('store', 'store7'); $this->assertEquals(true, $deleted); - $store7 = static::getDatabase()->getDocument('store', 'store7'); + $store7 = $this->getDatabase()->getDocument('store', 'store7'); $this->assertEquals(true, $store7->isEmpty()); // Try to delete child while still related to another with on delete: restrict try { - static::getDatabase()->deleteDocument('store', 'store1'); + $this->getDatabase()->deleteDocument('store', 'store1'); $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertEquals('Cannot delete document because it has at least one related document.', $e->getMessage()); } // Delete parent while still related to another with on delete: restrict - $result = static::getDatabase()->deleteDocument('product', 'product5'); + $result = $this->getDatabase()->deleteDocument('product', 'product5'); $this->assertEquals(true, $result); // Change on delete to set null - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'product', id: 'newStore', onDelete: Database::RELATION_MUTATE_SET_NULL ); // Delete child, set parent relationship to null - static::getDatabase()->deleteDocument('store', 'store1'); + $this->getDatabase()->deleteDocument('store', 'store1'); // Check relation was set to null - static::getDatabase()->getDocument('product', 'product1'); + $this->getDatabase()->getDocument('product', 'product1'); $this->assertEquals(null, $product1->getAttribute('newStore')); // Change on delete to cascade - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'product', id: 'newStore', onDelete: Database::RELATION_MUTATE_CASCADE ); // Delete child, will delete parent - static::getDatabase()->deleteDocument('store', 'store2'); + $this->getDatabase()->deleteDocument('store', 'store2'); // Check parent and child were deleted - $library = static::getDatabase()->getDocument('store', 'store2'); + $library = $this->getDatabase()->getDocument('store', 'store2'); $this->assertEquals(true, $library->isEmpty()); - $library = static::getDatabase()->getDocument('product', 'product2'); + $library = $this->getDatabase()->getDocument('product', 'product2'); $this->assertEquals(true, $library->isEmpty()); // Delete relationship - static::getDatabase()->deleteRelationship( + $this->getDatabase()->deleteRelationship( 'product', 'newStore' ); // Try to get document again - $products = static::getDatabase()->getDocument('product', 'product1'); + $products = $this->getDatabase()->getDocument('product', 'product1'); $store = $products->getAttribute('newStore'); $this->assertEquals(null, $store); // Try to get inverse document again - $store = static::getDatabase()->getDocument('store', 'store1'); + $store = $this->getDatabase()->getDocument('store', 'store1'); $products = $store->getAttribute('newProducts'); $this->assertEquals(null, $products); } public function testManyToManyOneWayRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('playlist'); - static::getDatabase()->createCollection('song'); + $this->getDatabase()->createCollection('playlist'); + $this->getDatabase()->createCollection('song'); - static::getDatabase()->createAttribute('playlist', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('song', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('song', 'length', Database::VAR_INTEGER, 0, true); + $this->getDatabase()->createAttribute('playlist', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('song', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('song', 'length', Database::VAR_INTEGER, 0, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'playlist', relatedCollection: 'song', type: Database::RELATION_MANY_TO_MANY, @@ -8090,7 +8099,7 @@ public function testManyToManyOneWayRelationship(): void ); // Check metadata for collection - $collection = static::getDatabase()->getCollection('playlist'); + $collection = $this->getDatabase()->getCollection('playlist'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { @@ -8106,7 +8115,7 @@ public function testManyToManyOneWayRelationship(): void } // Create document with relationship with nested data - $playlist1 = static::getDatabase()->createDocument('playlist', new Document([ + $playlist1 = $this->getDatabase()->createDocument('playlist', new Document([ '$id' => 'playlist1', '$permissions' => [ Permission::read(Role::any()), @@ -8129,7 +8138,7 @@ public function testManyToManyOneWayRelationship(): void ])); // Create document with relationship with related ID - static::getDatabase()->createDocument('song', new Document([ + $this->getDatabase()->createDocument('song', new Document([ '$id' => 'song2', '$permissions' => [ Permission::read(Role::any()), @@ -8139,7 +8148,7 @@ public function testManyToManyOneWayRelationship(): void 'name' => 'Song 2', 'length' => 140, ])); - static::getDatabase()->createDocument('playlist', new Document([ + $this->getDatabase()->createDocument('playlist', new Document([ '$id' => 'playlist2', '$permissions' => [ Permission::read(Role::any()), @@ -8153,13 +8162,13 @@ public function testManyToManyOneWayRelationship(): void ])); // Update a document with non existing related document. It should not get added to the list. - static::getDatabase()->updateDocument('playlist', 'playlist1', $playlist1->setAttribute('songs', ['song1','no-song'])); + $this->getDatabase()->updateDocument('playlist', 'playlist1', $playlist1->setAttribute('songs', ['song1','no-song'])); - $playlist1Document = static::getDatabase()->getDocument('playlist', 'playlist1'); + $playlist1Document = $this->getDatabase()->getDocument('playlist', 'playlist1'); // Assert document does not contain non existing relation document. $this->assertEquals(1, \count($playlist1Document->getAttribute('songs'))); - $documents = static::getDatabase()->find('playlist', [ + $documents = $this->getDatabase()->find('playlist', [ Query::select(['name']), Query::limit(1) ]); @@ -8167,29 +8176,29 @@ public function testManyToManyOneWayRelationship(): void $this->assertArrayNotHasKey('songs', $documents[0]); // Get document with relationship - $playlist = static::getDatabase()->getDocument('playlist', 'playlist1'); + $playlist = $this->getDatabase()->getDocument('playlist', 'playlist1'); $songs = $playlist->getAttribute('songs', []); $this->assertEquals('song1', $songs[0]['$id']); $this->assertArrayNotHasKey('playlist', $songs[0]); - $playlist = static::getDatabase()->getDocument('playlist', 'playlist2'); + $playlist = $this->getDatabase()->getDocument('playlist', 'playlist2'); $songs = $playlist->getAttribute('songs', []); $this->assertEquals('song2', $songs[0]['$id']); $this->assertArrayNotHasKey('playlist', $songs[0]); // Get related document - $library = static::getDatabase()->getDocument('song', 'song1'); + $library = $this->getDatabase()->getDocument('song', 'song1'); $this->assertArrayNotHasKey('songs', $library); - $library = static::getDatabase()->getDocument('song', 'song2'); + $library = $this->getDatabase()->getDocument('song', 'song2'); $this->assertArrayNotHasKey('songs', $library); - $playlists = static::getDatabase()->find('playlist'); + $playlists = $this->getDatabase()->find('playlist'); $this->assertEquals(2, \count($playlists)); // Select related document attributes - $playlist = static::getDatabase()->findOne('playlist', [ + $playlist = $this->getDatabase()->findOne('playlist', [ Query::select(['*', 'songs.name']) ]); @@ -8200,7 +8209,7 @@ public function testManyToManyOneWayRelationship(): void $this->assertEquals('Song 1', $playlist->getAttribute('songs')[0]->getAttribute('name')); $this->assertArrayNotHasKey('length', $playlist->getAttribute('songs')[0]); - $playlist = static::getDatabase()->getDocument('playlist', 'playlist1', [ + $playlist = $this->getDatabase()->getDocument('playlist', 'playlist1', [ Query::select(['*', 'songs.name']) ]); @@ -8208,32 +8217,32 @@ public function testManyToManyOneWayRelationship(): void $this->assertArrayNotHasKey('length', $playlist->getAttribute('songs')[0]); // Update root document attribute without altering relationship - $playlist1 = static::getDatabase()->updateDocument( + $playlist1 = $this->getDatabase()->updateDocument( 'playlist', $playlist1->getId(), $playlist1->setAttribute('name', 'Playlist 1 Updated') ); $this->assertEquals('Playlist 1 Updated', $playlist1->getAttribute('name')); - $playlist1 = static::getDatabase()->getDocument('playlist', 'playlist1'); + $playlist1 = $this->getDatabase()->getDocument('playlist', 'playlist1'); $this->assertEquals('Playlist 1 Updated', $playlist1->getAttribute('name')); // Update nested document attribute $songs = $playlist1->getAttribute('songs', []); $songs[0]->setAttribute('name', 'Song 1 Updated'); - $playlist1 = static::getDatabase()->updateDocument( + $playlist1 = $this->getDatabase()->updateDocument( 'playlist', $playlist1->getId(), $playlist1->setAttribute('songs', $songs) ); $this->assertEquals('Song 1 Updated', $playlist1->getAttribute('songs')[0]->getAttribute('name')); - $playlist1 = static::getDatabase()->getDocument('playlist', 'playlist1'); + $playlist1 = $this->getDatabase()->getDocument('playlist', 'playlist1'); $this->assertEquals('Song 1 Updated', $playlist1->getAttribute('songs')[0]->getAttribute('name')); // Create new document with no relationship - $playlist5 = static::getDatabase()->createDocument('playlist', new Document([ + $playlist5 = $this->getDatabase()->createDocument('playlist', new Document([ '$id' => 'playlist5', '$permissions' => [ Permission::read(Role::any()), @@ -8244,7 +8253,7 @@ public function testManyToManyOneWayRelationship(): void ])); // Update to relate to created document - $playlist5 = static::getDatabase()->updateDocument( + $playlist5 = $this->getDatabase()->updateDocument( 'playlist', $playlist5->getId(), $playlist5->setAttribute('songs', [new Document([ @@ -8260,7 +8269,7 @@ public function testManyToManyOneWayRelationship(): void ); // Playlist relating to existing songs that belong to other playlists - static::getDatabase()->createDocument('playlist', new Document([ + $this->getDatabase()->createDocument('playlist', new Document([ '$id' => 'playlist6', '$permissions' => [ Permission::read(Role::any()), @@ -8276,30 +8285,30 @@ public function testManyToManyOneWayRelationship(): void ])); $this->assertEquals('Song 5', $playlist5->getAttribute('songs')[0]->getAttribute('name')); - $playlist5 = static::getDatabase()->getDocument('playlist', 'playlist5'); + $playlist5 = $this->getDatabase()->getDocument('playlist', 'playlist5'); $this->assertEquals('Song 5', $playlist5->getAttribute('songs')[0]->getAttribute('name')); // Update document with new related document - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'playlist', $playlist1->getId(), $playlist1->setAttribute('songs', ['song2']) ); // Rename relationship key - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( 'playlist', 'songs', 'newSongs' ); // Get document with new relationship key - $playlist = static::getDatabase()->getDocument('playlist', 'playlist1'); + $playlist = $this->getDatabase()->getDocument('playlist', 'playlist1'); $songs = $playlist->getAttribute('newSongs'); $this->assertEquals('song2', $songs[0]['$id']); // Create new document with no relationship - static::getDatabase()->createDocument('playlist', new Document([ + $this->getDatabase()->createDocument('playlist', new Document([ '$id' => 'playlist3', '$permissions' => [ Permission::read(Role::any()), @@ -8310,87 +8319,87 @@ public function testManyToManyOneWayRelationship(): void ])); // Can delete document with no relationship when on delete is set to restrict - $deleted = static::getDatabase()->deleteDocument('playlist', 'playlist3'); + $deleted = $this->getDatabase()->deleteDocument('playlist', 'playlist3'); $this->assertEquals(true, $deleted); - $playlist3 = static::getDatabase()->getDocument('playlist', 'playlist3'); + $playlist3 = $this->getDatabase()->getDocument('playlist', 'playlist3'); $this->assertEquals(true, $playlist3->isEmpty()); // Try to delete document while still related to another with on delete: restrict try { - static::getDatabase()->deleteDocument('playlist', 'playlist1'); + $this->getDatabase()->deleteDocument('playlist', 'playlist1'); $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertEquals('Cannot delete document because it has at least one related document.', $e->getMessage()); } // Change on delete to set null - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'playlist', id: 'newSongs', onDelete: Database::RELATION_MUTATE_SET_NULL ); - $playlist1 = static::getDatabase()->getDocument('playlist', 'playlist1'); + $playlist1 = $this->getDatabase()->getDocument('playlist', 'playlist1'); // Reset relationships - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'playlist', $playlist1->getId(), $playlist1->setAttribute('newSongs', ['song1']) ); // Delete child, will delete junction - static::getDatabase()->deleteDocument('song', 'song1'); + $this->getDatabase()->deleteDocument('song', 'song1'); // Check relation was set to null - $playlist1 = static::getDatabase()->getDocument('playlist', 'playlist1'); + $playlist1 = $this->getDatabase()->getDocument('playlist', 'playlist1'); $this->assertEquals(0, \count($playlist1->getAttribute('newSongs'))); // Change on delete to cascade - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'playlist', id: 'newSongs', onDelete: Database::RELATION_MUTATE_CASCADE ); // Delete parent, will delete child - static::getDatabase()->deleteDocument('playlist', 'playlist2'); + $this->getDatabase()->deleteDocument('playlist', 'playlist2'); // Check parent and child were deleted - $library = static::getDatabase()->getDocument('playlist', 'playlist2'); + $library = $this->getDatabase()->getDocument('playlist', 'playlist2'); $this->assertEquals(true, $library->isEmpty()); - $library = static::getDatabase()->getDocument('song', 'song2'); + $library = $this->getDatabase()->getDocument('song', 'song2'); $this->assertEquals(true, $library->isEmpty()); // Delete relationship - static::getDatabase()->deleteRelationship( + $this->getDatabase()->deleteRelationship( 'playlist', 'newSongs' ); // Try to get document again - $playlist = static::getDatabase()->getDocument('playlist', 'playlist1'); + $playlist = $this->getDatabase()->getDocument('playlist', 'playlist1'); $songs = $playlist->getAttribute('newSongs'); $this->assertEquals(null, $songs); } public function testManyToManyTwoWayRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('students'); - static::getDatabase()->createCollection('classes'); + $this->getDatabase()->createCollection('students'); + $this->getDatabase()->createCollection('classes'); - static::getDatabase()->createAttribute('students', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('classes', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('classes', 'number', Database::VAR_INTEGER, 0, true); + $this->getDatabase()->createAttribute('students', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('classes', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('classes', 'number', Database::VAR_INTEGER, 0, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'students', relatedCollection: 'classes', type: Database::RELATION_MANY_TO_MANY, @@ -8398,7 +8407,7 @@ public function testManyToManyTwoWayRelationship(): void ); // Check metadata for collection - $collection = static::getDatabase()->getCollection('students'); + $collection = $this->getDatabase()->getCollection('students'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { if ($attribute['key'] === 'students') { @@ -8413,7 +8422,7 @@ public function testManyToManyTwoWayRelationship(): void } // Check metadata for related collection - $collection = static::getDatabase()->getCollection('classes'); + $collection = $this->getDatabase()->getCollection('classes'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { if ($attribute['key'] === 'classes') { @@ -8428,7 +8437,7 @@ public function testManyToManyTwoWayRelationship(): void } // Create document with relationship with nested data - $student1 = static::getDatabase()->createDocument('students', new Document([ + $student1 = $this->getDatabase()->createDocument('students', new Document([ '$id' => 'student1', '$permissions' => [ Permission::read(Role::any()), @@ -8451,14 +8460,14 @@ public function testManyToManyTwoWayRelationship(): void ])); // Update a document with non existing related document. It should not get added to the list. - static::getDatabase()->updateDocument('students', 'student1', $student1->setAttribute('classes', ['class1', 'no-class'])); + $this->getDatabase()->updateDocument('students', 'student1', $student1->setAttribute('classes', ['class1', 'no-class'])); - $student1Document = static::getDatabase()->getDocument('students', 'student1'); + $student1Document = $this->getDatabase()->getDocument('students', 'student1'); // Assert document does not contain non existing relation document. $this->assertEquals(1, \count($student1Document->getAttribute('classes'))); // Create document with relationship with related ID - static::getDatabase()->createDocument('classes', new Document([ + $this->getDatabase()->createDocument('classes', new Document([ '$id' => 'class2', '$permissions' => [ Permission::read(Role::any()), @@ -8469,7 +8478,7 @@ public function testManyToManyTwoWayRelationship(): void 'name' => 'Class 2', 'number' => 2, ])); - static::getDatabase()->createDocument('students', new Document([ + $this->getDatabase()->createDocument('students', new Document([ '$id' => 'student2', '$permissions' => [ Permission::read(Role::any()), @@ -8483,7 +8492,7 @@ public function testManyToManyTwoWayRelationship(): void ])); // Create from child side - static::getDatabase()->createDocument('classes', new Document([ + $this->getDatabase()->createDocument('classes', new Document([ '$id' => 'class3', '$permissions' => [ Permission::read(Role::any()), @@ -8504,7 +8513,7 @@ public function testManyToManyTwoWayRelationship(): void ] ], ])); - static::getDatabase()->createDocument('students', new Document([ + $this->getDatabase()->createDocument('students', new Document([ '$id' => 'student4', '$permissions' => [ Permission::read(Role::any()), @@ -8513,7 +8522,7 @@ public function testManyToManyTwoWayRelationship(): void ], 'name' => 'Student 4' ])); - static::getDatabase()->createDocument('classes', new Document([ + $this->getDatabase()->createDocument('classes', new Document([ '$id' => 'class4', '$permissions' => [ Permission::read(Role::any()), @@ -8529,49 +8538,49 @@ public function testManyToManyTwoWayRelationship(): void ])); // Get document with relationship - $student = static::getDatabase()->getDocument('students', 'student1'); + $student = $this->getDatabase()->getDocument('students', 'student1'); $classes = $student->getAttribute('classes', []); $this->assertEquals('class1', $classes[0]['$id']); $this->assertArrayNotHasKey('students', $classes[0]); - $student = static::getDatabase()->getDocument('students', 'student2'); + $student = $this->getDatabase()->getDocument('students', 'student2'); $classes = $student->getAttribute('classes', []); $this->assertEquals('class2', $classes[0]['$id']); $this->assertArrayNotHasKey('students', $classes[0]); - $student = static::getDatabase()->getDocument('students', 'student3'); + $student = $this->getDatabase()->getDocument('students', 'student3'); $classes = $student->getAttribute('classes', []); $this->assertEquals('class3', $classes[0]['$id']); $this->assertArrayNotHasKey('students', $classes[0]); - $student = static::getDatabase()->getDocument('students', 'student4'); + $student = $this->getDatabase()->getDocument('students', 'student4'); $classes = $student->getAttribute('classes', []); $this->assertEquals('class4', $classes[0]['$id']); $this->assertArrayNotHasKey('students', $classes[0]); // Get related document - $class = static::getDatabase()->getDocument('classes', 'class1'); + $class = $this->getDatabase()->getDocument('classes', 'class1'); $student = $class->getAttribute('students'); $this->assertEquals('student1', $student[0]['$id']); $this->assertArrayNotHasKey('classes', $student[0]); - $class = static::getDatabase()->getDocument('classes', 'class2'); + $class = $this->getDatabase()->getDocument('classes', 'class2'); $student = $class->getAttribute('students'); $this->assertEquals('student2', $student[0]['$id']); $this->assertArrayNotHasKey('classes', $student[0]); - $class = static::getDatabase()->getDocument('classes', 'class3'); + $class = $this->getDatabase()->getDocument('classes', 'class3'); $student = $class->getAttribute('students'); $this->assertEquals('student3', $student[0]['$id']); $this->assertArrayNotHasKey('classes', $student[0]); - $class = static::getDatabase()->getDocument('classes', 'class4'); + $class = $this->getDatabase()->getDocument('classes', 'class4'); $student = $class->getAttribute('students'); $this->assertEquals('student4', $student[0]['$id']); $this->assertArrayNotHasKey('classes', $student[0]); // Select related document attributes - $student = static::getDatabase()->findOne('students', [ + $student = $this->getDatabase()->findOne('students', [ Query::select(['*', 'classes.name']) ]); @@ -8582,7 +8591,7 @@ public function testManyToManyTwoWayRelationship(): void $this->assertEquals('Class 1', $student->getAttribute('classes')[0]->getAttribute('name')); $this->assertArrayNotHasKey('number', $student->getAttribute('classes')[0]); - $student = static::getDatabase()->getDocument('students', 'student1', [ + $student = $this->getDatabase()->getDocument('students', 'student1', [ Query::select(['*', 'classes.name']) ]); @@ -8590,58 +8599,58 @@ public function testManyToManyTwoWayRelationship(): void $this->assertArrayNotHasKey('number', $student->getAttribute('classes')[0]); // Update root document attribute without altering relationship - $student1 = static::getDatabase()->updateDocument( + $student1 = $this->getDatabase()->updateDocument( 'students', $student1->getId(), $student1->setAttribute('name', 'Student 1 Updated') ); $this->assertEquals('Student 1 Updated', $student1->getAttribute('name')); - $student1 = static::getDatabase()->getDocument('students', 'student1'); + $student1 = $this->getDatabase()->getDocument('students', 'student1'); $this->assertEquals('Student 1 Updated', $student1->getAttribute('name')); // Update inverse root document attribute without altering relationship - $class2 = static::getDatabase()->getDocument('classes', 'class2'); - $class2 = static::getDatabase()->updateDocument( + $class2 = $this->getDatabase()->getDocument('classes', 'class2'); + $class2 = $this->getDatabase()->updateDocument( 'classes', $class2->getId(), $class2->setAttribute('name', 'Class 2 Updated') ); $this->assertEquals('Class 2 Updated', $class2->getAttribute('name')); - $class2 = static::getDatabase()->getDocument('classes', 'class2'); + $class2 = $this->getDatabase()->getDocument('classes', 'class2'); $this->assertEquals('Class 2 Updated', $class2->getAttribute('name')); // Update nested document attribute $classes = $student1->getAttribute('classes', []); $classes[0]->setAttribute('name', 'Class 1 Updated'); - $student1 = static::getDatabase()->updateDocument( + $student1 = $this->getDatabase()->updateDocument( 'students', $student1->getId(), $student1->setAttribute('classes', $classes) ); $this->assertEquals('Class 1 Updated', $student1->getAttribute('classes')[0]->getAttribute('name')); - $student1 = static::getDatabase()->getDocument('students', 'student1'); + $student1 = $this->getDatabase()->getDocument('students', 'student1'); $this->assertEquals('Class 1 Updated', $student1->getAttribute('classes')[0]->getAttribute('name')); // Update inverse nested document attribute $students = $class2->getAttribute('students', []); $students[0]->setAttribute('name', 'Student 2 Updated'); - $class2 = static::getDatabase()->updateDocument( + $class2 = $this->getDatabase()->updateDocument( 'classes', $class2->getId(), $class2->setAttribute('students', $students) ); $this->assertEquals('Student 2 Updated', $class2->getAttribute('students')[0]->getAttribute('name')); - $class2 = static::getDatabase()->getDocument('classes', 'class2'); + $class2 = $this->getDatabase()->getDocument('classes', 'class2'); $this->assertEquals('Student 2 Updated', $class2->getAttribute('students')[0]->getAttribute('name')); // Create new document with no relationship - $student5 = static::getDatabase()->createDocument('students', new Document([ + $student5 = $this->getDatabase()->createDocument('students', new Document([ '$id' => 'student5', '$permissions' => [ Permission::read(Role::any()), @@ -8652,7 +8661,7 @@ public function testManyToManyTwoWayRelationship(): void ])); // Update to relate to created document - $student5 = static::getDatabase()->updateDocument( + $student5 = $this->getDatabase()->updateDocument( 'students', $student5->getId(), $student5->setAttribute('classes', [new Document([ @@ -8668,11 +8677,11 @@ public function testManyToManyTwoWayRelationship(): void ); $this->assertEquals('Class 5', $student5->getAttribute('classes')[0]->getAttribute('name')); - $student5 = static::getDatabase()->getDocument('students', 'student5'); + $student5 = $this->getDatabase()->getDocument('students', 'student5'); $this->assertEquals('Class 5', $student5->getAttribute('classes')[0]->getAttribute('name')); // Create child document with no relationship - $class6 = static::getDatabase()->createDocument('classes', new Document([ + $class6 = $this->getDatabase()->createDocument('classes', new Document([ '$id' => 'class6', '$permissions' => [ Permission::read(Role::any()), @@ -8684,7 +8693,7 @@ public function testManyToManyTwoWayRelationship(): void ])); // Update to relate to created document - $class6 = static::getDatabase()->updateDocument( + $class6 = $this->getDatabase()->updateDocument( 'classes', $class6->getId(), $class6->setAttribute('students', [new Document([ @@ -8699,27 +8708,27 @@ public function testManyToManyTwoWayRelationship(): void ); $this->assertEquals('Student 6', $class6->getAttribute('students')[0]->getAttribute('name')); - $class6 = static::getDatabase()->getDocument('classes', 'class6'); + $class6 = $this->getDatabase()->getDocument('classes', 'class6'); $this->assertEquals('Student 6', $class6->getAttribute('students')[0]->getAttribute('name')); // Update document with new related document - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'students', $student1->getId(), $student1->setAttribute('classes', ['class2']) ); - $class1 = static::getDatabase()->getDocument('classes', 'class1'); + $class1 = $this->getDatabase()->getDocument('classes', 'class1'); // Update inverse document - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'classes', $class1->getId(), $class1->setAttribute('students', ['student1']) ); // Rename relationship keys on both sides - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( 'students', 'classes', 'newClasses', @@ -8727,17 +8736,17 @@ public function testManyToManyTwoWayRelationship(): void ); // Get document with new relationship key - $students = static::getDatabase()->getDocument('students', 'student1'); + $students = $this->getDatabase()->getDocument('students', 'student1'); $classes = $students->getAttribute('newClasses'); $this->assertEquals('class2', $classes[0]['$id']); // Get inverse document with new relationship key - $class = static::getDatabase()->getDocument('classes', 'class1'); + $class = $this->getDatabase()->getDocument('classes', 'class1'); $students = $class->getAttribute('newStudents'); $this->assertEquals('student1', $students[0]['$id']); // Create new document with no relationship - static::getDatabase()->createDocument('students', new Document([ + $this->getDatabase()->createDocument('students', new Document([ '$id' => 'student7', '$permissions' => [ Permission::read(Role::any()), @@ -8748,107 +8757,107 @@ public function testManyToManyTwoWayRelationship(): void ])); // Can delete document with no relationship when on delete is set to restrict - $deleted = static::getDatabase()->deleteDocument('students', 'student7'); + $deleted = $this->getDatabase()->deleteDocument('students', 'student7'); $this->assertEquals(true, $deleted); - $student6 = static::getDatabase()->getDocument('students', 'student7'); + $student6 = $this->getDatabase()->getDocument('students', 'student7'); $this->assertEquals(true, $student6->isEmpty()); // Try to delete document while still related to another with on delete: restrict try { - static::getDatabase()->deleteDocument('students', 'student1'); + $this->getDatabase()->deleteDocument('students', 'student1'); $this->fail('Failed to throw exception'); } catch (Exception $e) { $this->assertEquals('Cannot delete document because it has at least one related document.', $e->getMessage()); } // Change on delete to set null - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'students', id: 'newClasses', onDelete: Database::RELATION_MUTATE_SET_NULL ); - $student1 = static::getDatabase()->getDocument('students', 'student1'); + $student1 = $this->getDatabase()->getDocument('students', 'student1'); // Reset relationships - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'students', $student1->getId(), $student1->setAttribute('newClasses', ['class1']) ); // Delete child, will delete junction - static::getDatabase()->deleteDocument('classes', 'class1'); + $this->getDatabase()->deleteDocument('classes', 'class1'); // Check relation was set to null - $student1 = static::getDatabase()->getDocument('students', 'student1'); + $student1 = $this->getDatabase()->getDocument('students', 'student1'); $this->assertEquals(0, \count($student1->getAttribute('newClasses'))); // Change on delete to cascade - static::getDatabase()->updateRelationship( + $this->getDatabase()->updateRelationship( collection: 'students', id: 'newClasses', onDelete: Database::RELATION_MUTATE_CASCADE ); // Delete parent, will delete child - static::getDatabase()->deleteDocument('students', 'student2'); + $this->getDatabase()->deleteDocument('students', 'student2'); // Check parent and child were deleted - $library = static::getDatabase()->getDocument('students', 'student2'); + $library = $this->getDatabase()->getDocument('students', 'student2'); $this->assertEquals(true, $library->isEmpty()); // Delete child, should not delete parent - static::getDatabase()->deleteDocument('classes', 'class6'); + $this->getDatabase()->deleteDocument('classes', 'class6'); // Check only child was deleted - $student6 = static::getDatabase()->getDocument('students', 'student6'); + $student6 = $this->getDatabase()->getDocument('students', 'student6'); $this->assertEquals(false, $student6->isEmpty()); $this->assertEmpty($student6->getAttribute('newClasses')); - $library = static::getDatabase()->getDocument('classes', 'class2'); + $library = $this->getDatabase()->getDocument('classes', 'class2'); $this->assertEquals(true, $library->isEmpty()); // Delete relationship - static::getDatabase()->deleteRelationship( + $this->getDatabase()->deleteRelationship( 'students', 'newClasses' ); // Try to get documents again - $student = static::getDatabase()->getDocument('students', 'student1'); + $student = $this->getDatabase()->getDocument('students', 'student1'); $classes = $student->getAttribute('newClasses'); $this->assertEquals(null, $classes); // Try to get inverse documents again - $classes = static::getDatabase()->getDocument('classes', 'class1'); + $classes = $this->getDatabase()->getDocument('classes', 'class1'); $students = $classes->getAttribute('newStudents'); $this->assertEquals(null, $students); } public function testSelectRelationshipAttributes(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('make'); - static::getDatabase()->createCollection('model'); + $this->getDatabase()->createCollection('make'); + $this->getDatabase()->createCollection('model'); - static::getDatabase()->createAttribute('make', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('model', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('model', 'year', Database::VAR_INTEGER, 0, true); + $this->getDatabase()->createAttribute('make', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('model', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('model', 'year', Database::VAR_INTEGER, 0, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'make', relatedCollection: 'model', type: Database::RELATION_ONE_TO_MANY, id: 'models' ); - static::getDatabase()->createDocument('make', new Document([ + $this->getDatabase()->createDocument('make', new Document([ '$id' => 'ford', '$permissions' => [ Permission::read(Role::any()), @@ -8875,7 +8884,7 @@ public function testSelectRelationshipAttributes(): void ])); // Select some parent attributes, some child attributes - $make = static::getDatabase()->findOne('make', [ + $make = $this->getDatabase()->findOne('make', [ Query::select(['name', 'models.name']), ]); @@ -8897,7 +8906,7 @@ public function testSelectRelationshipAttributes(): void $this->assertArrayNotHasKey('$updatedAt', $make); // Select internal attributes - $make = static::getDatabase()->findOne('make', [ + $make = $this->getDatabase()->findOne('make', [ Query::select(['name', '$id']), ]); @@ -8912,7 +8921,7 @@ public function testSelectRelationshipAttributes(): void $this->assertArrayNotHasKey('$updatedAt', $make); $this->assertArrayNotHasKey('$permissions', $make); - $make = static::getDatabase()->findOne('make', [ + $make = $this->getDatabase()->findOne('make', [ Query::select(['name', '$internalId']), ]); @@ -8927,7 +8936,7 @@ public function testSelectRelationshipAttributes(): void $this->assertArrayNotHasKey('$updatedAt', $make); $this->assertArrayNotHasKey('$permissions', $make); - $make = static::getDatabase()->findOne('make', [ + $make = $this->getDatabase()->findOne('make', [ Query::select(['name', '$collection']), ]); @@ -8942,7 +8951,7 @@ public function testSelectRelationshipAttributes(): void $this->assertArrayNotHasKey('$updatedAt', $make); $this->assertArrayNotHasKey('$permissions', $make); - $make = static::getDatabase()->findOne('make', [ + $make = $this->getDatabase()->findOne('make', [ Query::select(['name', '$createdAt']), ]); @@ -8957,7 +8966,7 @@ public function testSelectRelationshipAttributes(): void $this->assertArrayNotHasKey('$updatedAt', $make); $this->assertArrayNotHasKey('$permissions', $make); - $make = static::getDatabase()->findOne('make', [ + $make = $this->getDatabase()->findOne('make', [ Query::select(['name', '$updatedAt']), ]); @@ -8972,7 +8981,7 @@ public function testSelectRelationshipAttributes(): void $this->assertArrayHasKey('$updatedAt', $make); $this->assertArrayNotHasKey('$permissions', $make); - $make = static::getDatabase()->findOne('make', [ + $make = $this->getDatabase()->findOne('make', [ Query::select(['name', '$permissions']), ]); @@ -8988,7 +8997,7 @@ public function testSelectRelationshipAttributes(): void $this->assertArrayHasKey('$permissions', $make); // Select all parent attributes, some child attributes - $make = static::getDatabase()->findOne('make', [ + $make = $this->getDatabase()->findOne('make', [ Query::select(['*', 'models.year']), ]); @@ -9004,7 +9013,7 @@ public function testSelectRelationshipAttributes(): void $this->assertEquals(2011, $make['models'][1]['year']); // Select all parent attributes, all child attributes - $make = static::getDatabase()->findOne('make', [ + $make = $this->getDatabase()->findOne('make', [ Query::select(['*', 'models.*']), ]); @@ -9021,7 +9030,7 @@ public function testSelectRelationshipAttributes(): void // Select all parent attributes, all child attributes // Must select parent if selecting children - $make = static::getDatabase()->findOne('make', [ + $make = $this->getDatabase()->findOne('make', [ Query::select(['models.*']), ]); @@ -9037,7 +9046,7 @@ public function testSelectRelationshipAttributes(): void $this->assertEquals(2011, $make['models'][1]['year']); // Select all parent attributes, no child attributes - $make = static::getDatabase()->findOne('make', [ + $make = $this->getDatabase()->findOne('make', [ Query::select(['name']), ]); @@ -9051,20 +9060,20 @@ public function testSelectRelationshipAttributes(): void public function testNestedOneToOne_OneToOneRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('pattern'); - static::getDatabase()->createCollection('shirt'); - static::getDatabase()->createCollection('team'); + $this->getDatabase()->createCollection('pattern'); + $this->getDatabase()->createCollection('shirt'); + $this->getDatabase()->createCollection('team'); - static::getDatabase()->createAttribute('pattern', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('shirt', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('team', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('pattern', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('shirt', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('team', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'pattern', relatedCollection: 'shirt', type: Database::RELATION_ONE_TO_ONE, @@ -9072,7 +9081,7 @@ public function testNestedOneToOne_OneToOneRelationship(): void id: 'shirt', twoWayKey: 'pattern' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'shirt', relatedCollection: 'team', type: Database::RELATION_ONE_TO_ONE, @@ -9081,7 +9090,7 @@ public function testNestedOneToOne_OneToOneRelationship(): void twoWayKey: 'shirt' ); - static::getDatabase()->createDocument('pattern', new Document([ + $this->getDatabase()->createDocument('pattern', new Document([ '$id' => 'stripes', '$permissions' => [ Permission::read(Role::any()), @@ -9103,13 +9112,13 @@ public function testNestedOneToOne_OneToOneRelationship(): void ], ])); - $pattern = static::getDatabase()->getDocument('pattern', 'stripes'); + $pattern = $this->getDatabase()->getDocument('pattern', 'stripes'); $this->assertEquals('red', $pattern['shirt']['$id']); $this->assertArrayNotHasKey('pattern', $pattern['shirt']); $this->assertEquals('reds', $pattern['shirt']['team']['$id']); $this->assertArrayNotHasKey('shirt', $pattern['shirt']['team']); - static::getDatabase()->createDocument('team', new Document([ + $this->getDatabase()->createDocument('team', new Document([ '$id' => 'blues', '$permissions' => [ Permission::read(Role::any()), @@ -9131,7 +9140,7 @@ public function testNestedOneToOne_OneToOneRelationship(): void ], ])); - $team = static::getDatabase()->getDocument('team', 'blues'); + $team = $this->getDatabase()->getDocument('team', 'blues'); $this->assertEquals('blue', $team['shirt']['$id']); $this->assertArrayNotHasKey('team', $team['shirt']); $this->assertEquals('plain', $team['shirt']['pattern']['$id']); @@ -9140,20 +9149,20 @@ public function testNestedOneToOne_OneToOneRelationship(): void public function testNestedOneToOne_OneToManyRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('teachers'); - static::getDatabase()->createCollection('classrooms'); - static::getDatabase()->createCollection('children'); + $this->getDatabase()->createCollection('teachers'); + $this->getDatabase()->createCollection('classrooms'); + $this->getDatabase()->createCollection('children'); - static::getDatabase()->createAttribute('children', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('teachers', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('classrooms', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('children', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('teachers', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('classrooms', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'teachers', relatedCollection: 'classrooms', type: Database::RELATION_ONE_TO_ONE, @@ -9161,7 +9170,7 @@ public function testNestedOneToOne_OneToManyRelationship(): void id: 'classroom', twoWayKey: 'teacher' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'classrooms', relatedCollection: 'children', type: Database::RELATION_ONE_TO_MANY, @@ -9169,7 +9178,7 @@ public function testNestedOneToOne_OneToManyRelationship(): void twoWayKey: 'classroom' ); - static::getDatabase()->createDocument('teachers', new Document([ + $this->getDatabase()->createDocument('teachers', new Document([ '$id' => 'teacher1', '$permissions' => [ Permission::read(Role::any()), @@ -9200,14 +9209,14 @@ public function testNestedOneToOne_OneToManyRelationship(): void ], ])); - $teacher1 = static::getDatabase()->getDocument('teachers', 'teacher1'); + $teacher1 = $this->getDatabase()->getDocument('teachers', 'teacher1'); $this->assertEquals('classroom1', $teacher1['classroom']['$id']); $this->assertArrayNotHasKey('teacher', $teacher1['classroom']); $this->assertEquals(2, \count($teacher1['classroom']['children'])); $this->assertEquals('Child 1', $teacher1['classroom']['children'][0]['name']); $this->assertEquals('Child 2', $teacher1['classroom']['children'][1]['name']); - static::getDatabase()->createDocument('children', new Document([ + $this->getDatabase()->createDocument('children', new Document([ '$id' => 'child3', '$permissions' => [ Permission::read(Role::any()), @@ -9229,7 +9238,7 @@ public function testNestedOneToOne_OneToManyRelationship(): void ], ])); - $child3 = static::getDatabase()->getDocument('children', 'child3'); + $child3 = $this->getDatabase()->getDocument('children', 'child3'); $this->assertEquals('classroom2', $child3['classroom']['$id']); $this->assertArrayNotHasKey('children', $child3['classroom']); $this->assertEquals('teacher2', $child3['classroom']['teacher']['$id']); @@ -9238,20 +9247,20 @@ public function testNestedOneToOne_OneToManyRelationship(): void public function testNestedOneToOne_ManyToOneRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('users'); - static::getDatabase()->createCollection('profiles'); - static::getDatabase()->createCollection('avatars'); + $this->getDatabase()->createCollection('users'); + $this->getDatabase()->createCollection('profiles'); + $this->getDatabase()->createCollection('avatars'); - static::getDatabase()->createAttribute('users', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('profiles', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('avatars', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('users', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('profiles', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('avatars', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'users', relatedCollection: 'profiles', type: Database::RELATION_ONE_TO_ONE, @@ -9259,7 +9268,7 @@ public function testNestedOneToOne_ManyToOneRelationship(): void id: 'profile', twoWayKey: 'user' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'profiles', relatedCollection: 'avatars', type: Database::RELATION_MANY_TO_ONE, @@ -9267,7 +9276,7 @@ public function testNestedOneToOne_ManyToOneRelationship(): void id: 'avatar', ); - static::getDatabase()->createDocument('users', new Document([ + $this->getDatabase()->createDocument('users', new Document([ '$id' => 'user1', '$permissions' => [ Permission::read(Role::any()), @@ -9289,13 +9298,13 @@ public function testNestedOneToOne_ManyToOneRelationship(): void ], ])); - $user1 = static::getDatabase()->getDocument('users', 'user1'); + $user1 = $this->getDatabase()->getDocument('users', 'user1'); $this->assertEquals('profile1', $user1['profile']['$id']); $this->assertArrayNotHasKey('user', $user1['profile']); $this->assertEquals('avatar1', $user1['profile']['avatar']['$id']); $this->assertArrayNotHasKey('profile', $user1['profile']['avatar']); - static::getDatabase()->createDocument('avatars', new Document([ + $this->getDatabase()->createDocument('avatars', new Document([ '$id' => 'avatar2', '$permissions' => [ Permission::read(Role::any()), @@ -9319,7 +9328,7 @@ public function testNestedOneToOne_ManyToOneRelationship(): void ], ])); - $avatar2 = static::getDatabase()->getDocument('avatars', 'avatar2'); + $avatar2 = $this->getDatabase()->getDocument('avatars', 'avatar2'); $this->assertEquals('profile2', $avatar2['profiles'][0]['$id']); $this->assertArrayNotHasKey('avatars', $avatar2['profiles'][0]); $this->assertEquals('user2', $avatar2['profiles'][0]['user']['$id']); @@ -9328,20 +9337,20 @@ public function testNestedOneToOne_ManyToOneRelationship(): void public function testNestedOneToOne_ManyToManyRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('addresses'); - static::getDatabase()->createCollection('houses'); - static::getDatabase()->createCollection('buildings'); + $this->getDatabase()->createCollection('addresses'); + $this->getDatabase()->createCollection('houses'); + $this->getDatabase()->createCollection('buildings'); - static::getDatabase()->createAttribute('addresses', 'street', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('houses', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('buildings', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('addresses', 'street', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('houses', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('buildings', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'addresses', relatedCollection: 'houses', type: Database::RELATION_ONE_TO_ONE, @@ -9349,14 +9358,14 @@ public function testNestedOneToOne_ManyToManyRelationship(): void id: 'house', twoWayKey: 'address' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'houses', relatedCollection: 'buildings', type: Database::RELATION_MANY_TO_MANY, twoWay: true, ); - static::getDatabase()->createDocument('addresses', new Document([ + $this->getDatabase()->createDocument('addresses', new Document([ '$id' => 'address1', '$permissions' => [ Permission::read(Role::any()), @@ -9387,7 +9396,7 @@ public function testNestedOneToOne_ManyToManyRelationship(): void ], ])); - $address1 = static::getDatabase()->getDocument('addresses', 'address1'); + $address1 = $this->getDatabase()->getDocument('addresses', 'address1'); $this->assertEquals('house1', $address1['house']['$id']); $this->assertArrayNotHasKey('address', $address1['house']); $this->assertEquals('building1', $address1['house']['buildings'][0]['$id']); @@ -9395,7 +9404,7 @@ public function testNestedOneToOne_ManyToManyRelationship(): void $this->assertArrayNotHasKey('houses', $address1['house']['buildings'][0]); $this->assertArrayNotHasKey('houses', $address1['house']['buildings'][1]); - static::getDatabase()->createDocument('buildings', new Document([ + $this->getDatabase()->createDocument('buildings', new Document([ '$id' => 'building3', '$permissions' => [ Permission::read(Role::any()), @@ -9422,27 +9431,27 @@ public function testNestedOneToOne_ManyToManyRelationship(): void public function testNestedOneToMany_OneToOneRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('countries'); - static::getDatabase()->createCollection('cities'); - static::getDatabase()->createCollection('mayors'); + $this->getDatabase()->createCollection('countries'); + $this->getDatabase()->createCollection('cities'); + $this->getDatabase()->createCollection('mayors'); - static::getDatabase()->createAttribute('cities', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('countries', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('mayors', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('cities', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('countries', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('mayors', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'countries', relatedCollection: 'cities', type: Database::RELATION_ONE_TO_MANY, twoWay: true, twoWayKey: 'country' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'cities', relatedCollection: 'mayors', type: Database::RELATION_ONE_TO_ONE, @@ -9451,7 +9460,7 @@ public function testNestedOneToMany_OneToOneRelationship(): void twoWayKey: 'city' ); - static::getDatabase()->createDocument('countries', new Document([ + $this->getDatabase()->createDocument('countries', new Document([ '$id' => 'country1', '$permissions' => [ Permission::read(Role::any()), @@ -9492,26 +9501,26 @@ public function testNestedOneToMany_OneToOneRelationship(): void ], ])); - $documents = static::getDatabase()->find('countries', [ + $documents = $this->getDatabase()->find('countries', [ Query::limit(1) ]); $this->assertEquals('Mayor 1', $documents[0]['cities'][0]['mayor']['name']); - $documents = static::getDatabase()->find('countries', [ + $documents = $this->getDatabase()->find('countries', [ Query::select(['name']), Query::limit(1) ]); $this->assertArrayHasKey('name', $documents[0]); $this->assertArrayNotHasKey('cities', $documents[0]); - $documents = static::getDatabase()->find('countries', [ + $documents = $this->getDatabase()->find('countries', [ Query::select(['*']), Query::limit(1) ]); $this->assertArrayHasKey('name', $documents[0]); $this->assertArrayNotHasKey('cities', $documents[0]); - $documents = static::getDatabase()->find('countries', [ + $documents = $this->getDatabase()->find('countries', [ Query::select(['*', 'cities.*', 'cities.mayor.*']), Query::limit(1) ]); @@ -9519,12 +9528,12 @@ public function testNestedOneToMany_OneToOneRelationship(): void $this->assertEquals('Mayor 1', $documents[0]['cities'][0]['mayor']['name']); // Insert docs to cache: - $country1 = static::getDatabase()->getDocument('countries', 'country1'); - $mayor1 = static::getDatabase()->getDocument('mayors', 'mayor1'); + $country1 = $this->getDatabase()->getDocument('countries', 'country1'); + $mayor1 = $this->getDatabase()->getDocument('mayors', 'mayor1'); $this->assertEquals('City 1', $mayor1['city']['name']); $this->assertEquals('City 1', $country1['cities'][0]['name']); - static::getDatabase()->updateDocument('cities', 'city1', new Document([ + $this->getDatabase()->updateDocument('cities', 'city1', new Document([ '$id' => 'city1', '$collection' => 'cities', 'name' => 'City 1 updated', @@ -9535,8 +9544,8 @@ public function testNestedOneToMany_OneToOneRelationship(): void ], ])); - $mayor1 = static::getDatabase()->getDocument('mayors', 'mayor1'); - $country1 = static::getDatabase()->getDocument('countries', 'country1'); + $mayor1 = $this->getDatabase()->getDocument('mayors', 'mayor1'); + $country1 = $this->getDatabase()->getDocument('countries', 'country1'); $this->assertEquals('City 1 updated', $mayor1['city']['name']); $this->assertEquals('City 1 updated', $country1['cities'][0]['name']); @@ -9547,7 +9556,7 @@ public function testNestedOneToMany_OneToOneRelationship(): void $this->assertArrayNotHasKey('city', $country1['cities'][0]['mayor']); $this->assertArrayNotHasKey('city', $country1['cities'][1]['mayor']); - static::getDatabase()->createDocument('mayors', new Document([ + $this->getDatabase()->createDocument('mayors', new Document([ '$id' => 'mayor3', '$permissions' => [ Permission::read(Role::any()), @@ -9569,7 +9578,7 @@ public function testNestedOneToMany_OneToOneRelationship(): void ], ])); - $country2 = static::getDatabase()->getDocument('countries', 'country2'); + $country2 = $this->getDatabase()->getDocument('countries', 'country2'); $this->assertEquals('city3', $country2['cities'][0]['$id']); $this->assertEquals('mayor3', $country2['cities'][0]['mayor']['$id']); $this->assertArrayNotHasKey('country', $country2['cities'][0]); @@ -9578,27 +9587,27 @@ public function testNestedOneToMany_OneToOneRelationship(): void public function testNestedOneToMany_OneToManyRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('dormitories'); - static::getDatabase()->createCollection('occupants'); - static::getDatabase()->createCollection('pets'); + $this->getDatabase()->createCollection('dormitories'); + $this->getDatabase()->createCollection('occupants'); + $this->getDatabase()->createCollection('pets'); - static::getDatabase()->createAttribute('dormitories', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('occupants', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('pets', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('dormitories', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('occupants', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('pets', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'dormitories', relatedCollection: 'occupants', type: Database::RELATION_ONE_TO_MANY, twoWay: true, twoWayKey: 'dormitory' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'occupants', relatedCollection: 'pets', type: Database::RELATION_ONE_TO_MANY, @@ -9606,7 +9615,7 @@ public function testNestedOneToMany_OneToManyRelationship(): void twoWayKey: 'occupant' ); - static::getDatabase()->createDocument('dormitories', new Document([ + $this->getDatabase()->createDocument('dormitories', new Document([ '$id' => 'dormitory1', '$permissions' => [ Permission::read(Role::any()), @@ -9662,7 +9671,7 @@ public function testNestedOneToMany_OneToManyRelationship(): void ], ])); - $dormitory1 = static::getDatabase()->getDocument('dormitories', 'dormitory1'); + $dormitory1 = $this->getDatabase()->getDocument('dormitories', 'dormitory1'); $this->assertEquals('occupant1', $dormitory1['occupants'][0]['$id']); $this->assertEquals('occupant2', $dormitory1['occupants'][1]['$id']); $this->assertEquals('pet1', $dormitory1['occupants'][0]['pets'][0]['$id']); @@ -9676,7 +9685,7 @@ public function testNestedOneToMany_OneToManyRelationship(): void $this->assertArrayNotHasKey('occupant', $dormitory1['occupants'][1]['pets'][0]); $this->assertArrayNotHasKey('occupant', $dormitory1['occupants'][1]['pets'][1]); - static::getDatabase()->createDocument('pets', new Document([ + $this->getDatabase()->createDocument('pets', new Document([ '$id' => 'pet5', '$permissions' => [ Permission::read(Role::any()), @@ -9698,7 +9707,7 @@ public function testNestedOneToMany_OneToManyRelationship(): void ], ])); - $pet5 = static::getDatabase()->getDocument('pets', 'pet5'); + $pet5 = $this->getDatabase()->getDocument('pets', 'pet5'); $this->assertEquals('occupant3', $pet5['occupant']['$id']); $this->assertEquals('dormitory2', $pet5['occupant']['dormitory']['$id']); $this->assertArrayNotHasKey('pets', $pet5['occupant']); @@ -9707,26 +9716,26 @@ public function testNestedOneToMany_OneToManyRelationship(): void public function testNestedOneToMany_ManyToOneRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('home'); - static::getDatabase()->createCollection('renters'); - static::getDatabase()->createCollection('floors'); + $this->getDatabase()->createCollection('home'); + $this->getDatabase()->createCollection('renters'); + $this->getDatabase()->createCollection('floors'); - static::getDatabase()->createAttribute('home', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('renters', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('floors', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('home', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('renters', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('floors', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'home', relatedCollection: 'renters', type: Database::RELATION_ONE_TO_MANY, twoWay: true ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'renters', relatedCollection: 'floors', type: Database::RELATION_MANY_TO_ONE, @@ -9734,7 +9743,7 @@ public function testNestedOneToMany_ManyToOneRelationship(): void id: 'floor' ); - static::getDatabase()->createDocument('home', new Document([ + $this->getDatabase()->createDocument('home', new Document([ '$id' => 'home1', '$permissions' => [ Permission::read(Role::any()), @@ -9758,13 +9767,13 @@ public function testNestedOneToMany_ManyToOneRelationship(): void ], ])); - $home1 = static::getDatabase()->getDocument('home', 'home1'); + $home1 = $this->getDatabase()->getDocument('home', 'home1'); $this->assertEquals('renter1', $home1['renters'][0]['$id']); $this->assertEquals('floor1', $home1['renters'][0]['floor']['$id']); $this->assertArrayNotHasKey('home', $home1['renters'][0]); $this->assertArrayNotHasKey('renters', $home1['renters'][0]['floor']); - static::getDatabase()->createDocument('floors', new Document([ + $this->getDatabase()->createDocument('floors', new Document([ '$id' => 'floor2', '$permissions' => [ Permission::read(Role::any()), @@ -9788,7 +9797,7 @@ public function testNestedOneToMany_ManyToOneRelationship(): void ], ])); - $floor2 = static::getDatabase()->getDocument('floors', 'floor2'); + $floor2 = $this->getDatabase()->getDocument('floors', 'floor2'); $this->assertEquals('renter2', $floor2['renters'][0]['$id']); $this->assertArrayNotHasKey('floor', $floor2['renters'][0]); $this->assertEquals('home2', $floor2['renters'][0]['home']['$id']); @@ -9797,34 +9806,34 @@ public function testNestedOneToMany_ManyToOneRelationship(): void public function testNestedOneToMany_ManyToManyRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('owners'); - static::getDatabase()->createCollection('cats'); - static::getDatabase()->createCollection('toys'); + $this->getDatabase()->createCollection('owners'); + $this->getDatabase()->createCollection('cats'); + $this->getDatabase()->createCollection('toys'); - static::getDatabase()->createAttribute('owners', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('cats', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('toys', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('owners', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('cats', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('toys', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'owners', relatedCollection: 'cats', type: Database::RELATION_ONE_TO_MANY, twoWay: true, twoWayKey: 'owner' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'cats', relatedCollection: 'toys', type: Database::RELATION_MANY_TO_MANY, twoWay: true ); - static::getDatabase()->createDocument('owners', new Document([ + $this->getDatabase()->createDocument('owners', new Document([ '$id' => 'owner1', '$permissions' => [ Permission::read(Role::any()), @@ -9850,13 +9859,13 @@ public function testNestedOneToMany_ManyToManyRelationship(): void ], ])); - $owner1 = static::getDatabase()->getDocument('owners', 'owner1'); + $owner1 = $this->getDatabase()->getDocument('owners', 'owner1'); $this->assertEquals('cat1', $owner1['cats'][0]['$id']); $this->assertArrayNotHasKey('owner', $owner1['cats'][0]); $this->assertEquals('toy1', $owner1['cats'][0]['toys'][0]['$id']); $this->assertArrayNotHasKey('cats', $owner1['cats'][0]['toys'][0]); - static::getDatabase()->createDocument('toys', new Document([ + $this->getDatabase()->createDocument('toys', new Document([ '$id' => 'toy2', '$permissions' => [ Permission::read(Role::any()), @@ -9880,7 +9889,7 @@ public function testNestedOneToMany_ManyToManyRelationship(): void ], ])); - $toy2 = static::getDatabase()->getDocument('toys', 'toy2'); + $toy2 = $this->getDatabase()->getDocument('toys', 'toy2'); $this->assertEquals('cat2', $toy2['cats'][0]['$id']); $this->assertArrayNotHasKey('toys', $toy2['cats'][0]); $this->assertEquals('owner2', $toy2['cats'][0]['owner']['$id']); @@ -9889,27 +9898,27 @@ public function testNestedOneToMany_ManyToManyRelationship(): void public function testNestedManyToOne_OneToOneRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('towns'); - static::getDatabase()->createCollection('homelands'); - static::getDatabase()->createCollection('capitals'); + $this->getDatabase()->createCollection('towns'); + $this->getDatabase()->createCollection('homelands'); + $this->getDatabase()->createCollection('capitals'); - static::getDatabase()->createAttribute('towns', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('homelands', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('capitals', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('towns', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('homelands', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('capitals', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'towns', relatedCollection: 'homelands', type: Database::RELATION_MANY_TO_ONE, twoWay: true, id: 'homeland' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'homelands', relatedCollection: 'capitals', type: Database::RELATION_ONE_TO_ONE, @@ -9918,7 +9927,7 @@ public function testNestedManyToOne_OneToOneRelationship(): void twoWayKey: 'homeland' ); - static::getDatabase()->createDocument('towns', new Document([ + $this->getDatabase()->createDocument('towns', new Document([ '$id' => 'town1', '$permissions' => [ Permission::read(Role::any()), @@ -9940,13 +9949,13 @@ public function testNestedManyToOne_OneToOneRelationship(): void ], ])); - $town1 = static::getDatabase()->getDocument('towns', 'town1'); + $town1 = $this->getDatabase()->getDocument('towns', 'town1'); $this->assertEquals('homeland1', $town1['homeland']['$id']); $this->assertArrayNotHasKey('towns', $town1['homeland']); $this->assertEquals('capital1', $town1['homeland']['capital']['$id']); $this->assertArrayNotHasKey('homeland', $town1['homeland']['capital']); - static::getDatabase()->createDocument('capitals', new Document([ + $this->getDatabase()->createDocument('capitals', new Document([ '$id' => 'capital2', '$permissions' => [ Permission::read(Role::any()), @@ -9977,7 +9986,7 @@ public function testNestedManyToOne_OneToOneRelationship(): void ], ])); - $capital2 = static::getDatabase()->getDocument('capitals', 'capital2'); + $capital2 = $this->getDatabase()->getDocument('capitals', 'capital2'); $this->assertEquals('homeland2', $capital2['homeland']['$id']); $this->assertArrayNotHasKey('capital', $capital2['homeland']); $this->assertEquals(2, \count($capital2['homeland']['towns'])); @@ -9987,27 +9996,27 @@ public function testNestedManyToOne_OneToOneRelationship(): void public function testNestedManyToOne_OneToManyRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('players'); - static::getDatabase()->createCollection('teams'); - static::getDatabase()->createCollection('supporters'); + $this->getDatabase()->createCollection('players'); + $this->getDatabase()->createCollection('teams'); + $this->getDatabase()->createCollection('supporters'); - static::getDatabase()->createAttribute('players', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('teams', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('supporters', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('players', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('teams', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('supporters', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'players', relatedCollection: 'teams', type: Database::RELATION_MANY_TO_ONE, twoWay: true, id: 'team' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'teams', relatedCollection: 'supporters', type: Database::RELATION_ONE_TO_MANY, @@ -10016,7 +10025,7 @@ public function testNestedManyToOne_OneToManyRelationship(): void twoWayKey: 'team' ); - static::getDatabase()->createDocument('players', new Document([ + $this->getDatabase()->createDocument('players', new Document([ '$id' => 'player1', '$permissions' => [ Permission::read(Role::any()), @@ -10047,14 +10056,14 @@ public function testNestedManyToOne_OneToManyRelationship(): void ], ])); - $player1 = static::getDatabase()->getDocument('players', 'player1'); + $player1 = $this->getDatabase()->getDocument('players', 'player1'); $this->assertEquals('team1', $player1['team']['$id']); $this->assertArrayNotHasKey('players', $player1['team']); $this->assertEquals(2, \count($player1['team']['supporters'])); $this->assertEquals('supporter1', $player1['team']['supporters'][0]['$id']); $this->assertEquals('supporter2', $player1['team']['supporters'][1]['$id']); - static::getDatabase()->createDocument('supporters', new Document([ + $this->getDatabase()->createDocument('supporters', new Document([ '$id' => 'supporter3', '$permissions' => [ Permission::read(Role::any()), @@ -10085,7 +10094,7 @@ public function testNestedManyToOne_OneToManyRelationship(): void ], ])); - $supporter3 = static::getDatabase()->getDocument('supporters', 'supporter3'); + $supporter3 = $this->getDatabase()->getDocument('supporters', 'supporter3'); $this->assertEquals('team2', $supporter3['team']['$id']); $this->assertArrayNotHasKey('supporters', $supporter3['team']); $this->assertEquals(2, \count($supporter3['team']['players'])); @@ -10095,27 +10104,27 @@ public function testNestedManyToOne_OneToManyRelationship(): void public function testNestedManyToOne_ManyToOne(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('cows'); - static::getDatabase()->createCollection('farms'); - static::getDatabase()->createCollection('farmer'); + $this->getDatabase()->createCollection('cows'); + $this->getDatabase()->createCollection('farms'); + $this->getDatabase()->createCollection('farmer'); - static::getDatabase()->createAttribute('cows', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('farms', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('farmer', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('cows', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('farms', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('farmer', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'cows', relatedCollection: 'farms', type: Database::RELATION_MANY_TO_ONE, twoWay: true, id: 'farm' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'farms', relatedCollection: 'farmer', type: Database::RELATION_MANY_TO_ONE, @@ -10123,7 +10132,7 @@ public function testNestedManyToOne_ManyToOne(): void id: 'farmer' ); - static::getDatabase()->createDocument('cows', new Document([ + $this->getDatabase()->createDocument('cows', new Document([ '$id' => 'cow1', '$permissions' => [ Permission::read(Role::any()), @@ -10145,13 +10154,13 @@ public function testNestedManyToOne_ManyToOne(): void ], ])); - $cow1 = static::getDatabase()->getDocument('cows', 'cow1'); + $cow1 = $this->getDatabase()->getDocument('cows', 'cow1'); $this->assertEquals('farm1', $cow1['farm']['$id']); $this->assertArrayNotHasKey('cows', $cow1['farm']); $this->assertEquals('farmer1', $cow1['farm']['farmer']['$id']); $this->assertArrayNotHasKey('farms', $cow1['farm']['farmer']); - static::getDatabase()->createDocument('farmer', new Document([ + $this->getDatabase()->createDocument('farmer', new Document([ '$id' => 'farmer2', '$permissions' => [ Permission::read(Role::any()), @@ -10184,7 +10193,7 @@ public function testNestedManyToOne_ManyToOne(): void ], ])); - $farmer2 = static::getDatabase()->getDocument('farmer', 'farmer2'); + $farmer2 = $this->getDatabase()->getDocument('farmer', 'farmer2'); $this->assertEquals('farm2', $farmer2['farms'][0]['$id']); $this->assertArrayNotHasKey('farmer', $farmer2['farms'][0]); $this->assertEquals(2, \count($farmer2['farms'][0]['cows'])); @@ -10194,34 +10203,34 @@ public function testNestedManyToOne_ManyToOne(): void public function testNestedManyToOne_ManyToManyRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('books'); - static::getDatabase()->createCollection('entrants'); - static::getDatabase()->createCollection('rooms'); + $this->getDatabase()->createCollection('books'); + $this->getDatabase()->createCollection('entrants'); + $this->getDatabase()->createCollection('rooms'); - static::getDatabase()->createAttribute('books', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('entrants', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('rooms', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('books', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('entrants', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('rooms', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'books', relatedCollection: 'entrants', type: Database::RELATION_MANY_TO_ONE, twoWay: true, id: 'entrant' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'entrants', relatedCollection: 'rooms', type: Database::RELATION_MANY_TO_MANY, twoWay: true, ); - static::getDatabase()->createDocument('books', new Document([ + $this->getDatabase()->createDocument('books', new Document([ '$id' => 'book1', '$permissions' => [ Permission::read(Role::any()), @@ -10252,7 +10261,7 @@ public function testNestedManyToOne_ManyToManyRelationship(): void ], ])); - $book1 = static::getDatabase()->getDocument('books', 'book1'); + $book1 = $this->getDatabase()->getDocument('books', 'book1'); $this->assertEquals('entrant1', $book1['entrant']['$id']); $this->assertArrayNotHasKey('books', $book1['entrant']); $this->assertEquals(2, \count($book1['entrant']['rooms'])); @@ -10262,26 +10271,26 @@ public function testNestedManyToOne_ManyToManyRelationship(): void public function testNestedManyToMany_OneToOneRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('stones'); - static::getDatabase()->createCollection('hearths'); - static::getDatabase()->createCollection('plots'); + $this->getDatabase()->createCollection('stones'); + $this->getDatabase()->createCollection('hearths'); + $this->getDatabase()->createCollection('plots'); - static::getDatabase()->createAttribute('stones', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('hearths', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('plots', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('stones', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('hearths', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('plots', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'stones', relatedCollection: 'hearths', type: Database::RELATION_MANY_TO_MANY, twoWay: true, ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'hearths', relatedCollection: 'plots', type: Database::RELATION_ONE_TO_ONE, @@ -10290,7 +10299,7 @@ public function testNestedManyToMany_OneToOneRelationship(): void twoWayKey: 'hearth' ); - static::getDatabase()->createDocument('stones', new Document([ + $this->getDatabase()->createDocument('stones', new Document([ '$id' => 'stone1', '$permissions' => [ Permission::read(Role::any()), @@ -10328,7 +10337,7 @@ public function testNestedManyToMany_OneToOneRelationship(): void ], ])); - $stone1 = static::getDatabase()->getDocument('stones', 'stone1'); + $stone1 = $this->getDatabase()->getDocument('stones', 'stone1'); $this->assertEquals(2, \count($stone1['hearths'])); $this->assertEquals('hearth1', $stone1['hearths'][0]['$id']); $this->assertEquals('hearth2', $stone1['hearths'][1]['$id']); @@ -10337,7 +10346,7 @@ public function testNestedManyToMany_OneToOneRelationship(): void $this->assertEquals('plot2', $stone1['hearths'][1]['plot']['$id']); $this->assertArrayNotHasKey('hearth', $stone1['hearths'][0]['plot']); - static::getDatabase()->createDocument('plots', new Document([ + $this->getDatabase()->createDocument('plots', new Document([ '$id' => 'plot3', '$permissions' => [ Permission::read(Role::any()), @@ -10361,7 +10370,7 @@ public function testNestedManyToMany_OneToOneRelationship(): void ], ])); - $plot3 = static::getDatabase()->getDocument('plots', 'plot3'); + $plot3 = $this->getDatabase()->getDocument('plots', 'plot3'); $this->assertEquals('hearth3', $plot3['hearth']['$id']); $this->assertArrayNotHasKey('plot', $plot3['hearth']); $this->assertEquals('stone2', $plot3['hearth']['stones'][0]['$id']); @@ -10370,26 +10379,26 @@ public function testNestedManyToMany_OneToOneRelationship(): void public function testNestedManyToMany_OneToManyRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('groups'); - static::getDatabase()->createCollection('tounaments'); - static::getDatabase()->createCollection('prizes'); + $this->getDatabase()->createCollection('groups'); + $this->getDatabase()->createCollection('tounaments'); + $this->getDatabase()->createCollection('prizes'); - static::getDatabase()->createAttribute('groups', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('tounaments', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('prizes', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('groups', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('tounaments', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('prizes', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'groups', relatedCollection: 'tounaments', type: Database::RELATION_MANY_TO_MANY, twoWay: true, ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'tounaments', relatedCollection: 'prizes', type: Database::RELATION_ONE_TO_MANY, @@ -10398,7 +10407,7 @@ public function testNestedManyToMany_OneToManyRelationship(): void twoWayKey: 'tounament' ); - static::getDatabase()->createDocument('groups', new Document([ + $this->getDatabase()->createDocument('groups', new Document([ '$id' => 'group1', '$permissions' => [ Permission::read(Role::any()), @@ -10454,7 +10463,7 @@ public function testNestedManyToMany_OneToManyRelationship(): void ], ])); - $group1 = static::getDatabase()->getDocument('groups', 'group1'); + $group1 = $this->getDatabase()->getDocument('groups', 'group1'); $this->assertEquals(2, \count($group1['tounaments'])); $this->assertEquals('tounament1', $group1['tounaments'][0]['$id']); $this->assertEquals('tounament2', $group1['tounaments'][1]['$id']); @@ -10467,26 +10476,26 @@ public function testNestedManyToMany_OneToManyRelationship(): void public function testNestedManyToMany_ManyToOneRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('platforms'); - static::getDatabase()->createCollection('games'); - static::getDatabase()->createCollection('publishers'); + $this->getDatabase()->createCollection('platforms'); + $this->getDatabase()->createCollection('games'); + $this->getDatabase()->createCollection('publishers'); - static::getDatabase()->createAttribute('platforms', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('games', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('publishers', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('platforms', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('games', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('publishers', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'platforms', relatedCollection: 'games', type: Database::RELATION_MANY_TO_MANY, twoWay: true, ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'games', relatedCollection: 'publishers', type: Database::RELATION_MANY_TO_ONE, @@ -10495,7 +10504,7 @@ public function testNestedManyToMany_ManyToOneRelationship(): void twoWayKey: 'games' ); - static::getDatabase()->createDocument('platforms', new Document([ + $this->getDatabase()->createDocument('platforms', new Document([ '$id' => 'platform1', '$permissions' => [ Permission::read(Role::any()), @@ -10533,7 +10542,7 @@ public function testNestedManyToMany_ManyToOneRelationship(): void ] ])); - $platform1 = static::getDatabase()->getDocument('platforms', 'platform1'); + $platform1 = $this->getDatabase()->getDocument('platforms', 'platform1'); $this->assertEquals(2, \count($platform1['games'])); $this->assertEquals('game1', $platform1['games'][0]['$id']); $this->assertEquals('game2', $platform1['games'][1]['$id']); @@ -10542,7 +10551,7 @@ public function testNestedManyToMany_ManyToOneRelationship(): void $this->assertEquals('publisher2', $platform1['games'][1]['publisher']['$id']); $this->assertArrayNotHasKey('games', $platform1['games'][0]['publisher']); - static::getDatabase()->createDocument('publishers', new Document([ + $this->getDatabase()->createDocument('publishers', new Document([ '$id' => 'publisher3', '$permissions' => [ Permission::read(Role::any()), @@ -10568,7 +10577,7 @@ public function testNestedManyToMany_ManyToOneRelationship(): void ], ])); - $publisher3 = static::getDatabase()->getDocument('publishers', 'publisher3'); + $publisher3 = $this->getDatabase()->getDocument('publishers', 'publisher3'); $this->assertEquals(1, \count($publisher3['games'])); $this->assertEquals('game3', $publisher3['games'][0]['$id']); $this->assertArrayNotHasKey('publisher', $publisher3['games'][0]); @@ -10578,26 +10587,26 @@ public function testNestedManyToMany_ManyToOneRelationship(): void public function testNestedManyToMany_ManyToManyRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('sauces'); - static::getDatabase()->createCollection('pizzas'); - static::getDatabase()->createCollection('toppings'); + $this->getDatabase()->createCollection('sauces'); + $this->getDatabase()->createCollection('pizzas'); + $this->getDatabase()->createCollection('toppings'); - static::getDatabase()->createAttribute('sauces', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('pizzas', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('toppings', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('sauces', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('pizzas', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('toppings', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'sauces', relatedCollection: 'pizzas', type: Database::RELATION_MANY_TO_MANY, twoWay: true, ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'pizzas', relatedCollection: 'toppings', type: Database::RELATION_MANY_TO_MANY, @@ -10606,7 +10615,7 @@ public function testNestedManyToMany_ManyToManyRelationship(): void twoWayKey: 'pizzas' ); - static::getDatabase()->createDocument('sauces', new Document([ + $this->getDatabase()->createDocument('sauces', new Document([ '$id' => 'sauce1', '$permissions' => [ Permission::read(Role::any()), @@ -10662,7 +10671,7 @@ public function testNestedManyToMany_ManyToManyRelationship(): void ] ])); - $sauce1 = static::getDatabase()->getDocument('sauces', 'sauce1'); + $sauce1 = $this->getDatabase()->getDocument('sauces', 'sauce1'); $this->assertEquals(2, \count($sauce1['pizzas'])); $this->assertEquals('pizza1', $sauce1['pizzas'][0]['$id']); $this->assertEquals('pizza2', $sauce1['pizzas'][1]['$id']); @@ -10679,20 +10688,20 @@ public function testNestedManyToMany_ManyToManyRelationship(): void public function testInheritRelationshipPermissions(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('lawns', permissions: [Permission::create(Role::any())], documentSecurity: true); - static::getDatabase()->createCollection('trees', permissions: [Permission::create(Role::any())], documentSecurity: true); - static::getDatabase()->createCollection('birds', permissions: [Permission::create(Role::any())], documentSecurity: true); + $this->getDatabase()->createCollection('lawns', permissions: [Permission::create(Role::any())], documentSecurity: true); + $this->getDatabase()->createCollection('trees', permissions: [Permission::create(Role::any())], documentSecurity: true); + $this->getDatabase()->createCollection('birds', permissions: [Permission::create(Role::any())], documentSecurity: true); - static::getDatabase()->createAttribute('lawns', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('trees', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('birds', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('lawns', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('trees', 'name', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('birds', 'name', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'lawns', relatedCollection: 'trees', type: Database::RELATION_ONE_TO_MANY, @@ -10700,7 +10709,7 @@ public function testInheritRelationshipPermissions(): void twoWayKey: 'lawn', onDelete: Database::RELATION_MUTATE_CASCADE, ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'trees', relatedCollection: 'birds', type: Database::RELATION_MANY_TO_MANY, @@ -10715,7 +10724,7 @@ public function testInheritRelationshipPermissions(): void Permission::delete(Role::user('user2')), ]; - static::getDatabase()->createDocument('lawns', new Document([ + $this->getDatabase()->createDocument('lawns', new Document([ '$id' => 'lawn1', '$permissions' => $permissions, 'name' => 'Lawn 1', @@ -10737,13 +10746,13 @@ public function testInheritRelationshipPermissions(): void ], ])); - $lawn1 = static::getDatabase()->getDocument('lawns', 'lawn1'); + $lawn1 = $this->getDatabase()->getDocument('lawns', 'lawn1'); $this->assertEquals($permissions, $lawn1->getPermissions()); $this->assertEquals($permissions, $lawn1['trees'][0]->getPermissions()); $this->assertEquals($permissions, $lawn1['trees'][0]['birds'][0]->getPermissions()); $this->assertEquals($permissions, $lawn1['trees'][0]['birds'][1]->getPermissions()); - $tree1 = static::getDatabase()->getDocument('trees', 'tree1'); + $tree1 = $this->getDatabase()->getDocument('trees', 'tree1'); $this->assertEquals($permissions, $tree1->getPermissions()); $this->assertEquals($permissions, $tree1['lawn']->getPermissions()); $this->assertEquals($permissions, $tree1['birds'][0]->getPermissions()); @@ -10755,18 +10764,18 @@ public function testInheritRelationshipPermissions(): void */ public function testEnforceRelationshipPermissions(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); - $lawn1 = static::getDatabase()->getDocument('lawns', 'lawn1'); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::any()->toString()); + $lawn1 = $this->getDatabase()->getDocument('lawns', 'lawn1'); $this->assertEquals('Lawn 1', $lawn1['name']); // Try update root document try { - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'lawns', $lawn1->getId(), $lawn1->setAttribute('name', 'Lawn 1 Updated') @@ -10778,7 +10787,7 @@ public function testEnforceRelationshipPermissions(): void // Try delete root document try { - static::getDatabase()->deleteDocument( + $this->getDatabase()->deleteDocument( 'lawns', $lawn1->getId(), ); @@ -10787,11 +10796,11 @@ public function testEnforceRelationshipPermissions(): void $this->assertEquals('Missing "delete" permission for role "user:user2". Only "["any"]" scopes are allowed and "["user:user2"]" was given.', $e->getMessage()); } - $tree1 = static::getDatabase()->getDocument('trees', 'tree1'); + $tree1 = $this->getDatabase()->getDocument('trees', 'tree1'); // Try update nested document try { - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'trees', $tree1->getId(), $tree1->setAttribute('name', 'Tree 1 Updated') @@ -10803,7 +10812,7 @@ public function testEnforceRelationshipPermissions(): void // Try delete nested document try { - static::getDatabase()->deleteDocument( + $this->getDatabase()->deleteDocument( 'trees', $tree1->getId(), ); @@ -10812,11 +10821,11 @@ public function testEnforceRelationshipPermissions(): void $this->assertEquals('Missing "delete" permission for role "user:user2". Only "["any"]" scopes are allowed and "["user:user2"]" was given.', $e->getMessage()); } - $bird1 = static::getDatabase()->getDocument('birds', 'bird1'); + $bird1 = $this->getDatabase()->getDocument('birds', 'bird1'); // Try update multi-level nested document try { - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( 'birds', $bird1->getId(), $bird1->setAttribute('name', 'Bird 1 Updated') @@ -10828,7 +10837,7 @@ public function testEnforceRelationshipPermissions(): void // Try delete multi-level nested document try { - static::getDatabase()->deleteDocument( + $this->getDatabase()->deleteDocument( 'birds', $bird1->getId(), ); @@ -10837,12 +10846,12 @@ public function testEnforceRelationshipPermissions(): void $this->assertEquals('Missing "delete" permission for role "user:user2". Only "["any"]" scopes are allowed and "["user:user2"]" was given.', $e->getMessage()); } - Authorization::setRole(Role::user('user1')->toString()); + self::$authorization->addRole(Role::user('user1')->toString()); - $bird1 = static::getDatabase()->getDocument('birds', 'bird1'); + $bird1 = $this->getDatabase()->getDocument('birds', 'bird1'); // Try update multi-level nested document - $bird1 = static::getDatabase()->updateDocument( + $bird1 = $this->getDatabase()->updateDocument( 'birds', $bird1->getId(), $bird1->setAttribute('name', 'Bird 1 Updated') @@ -10850,20 +10859,20 @@ public function testEnforceRelationshipPermissions(): void $this->assertEquals('Bird 1 Updated', $bird1['name']); - Authorization::setRole(Role::user('user2')->toString()); + self::$authorization->addRole(Role::user('user2')->toString()); // Try delete multi-level nested document - $deleted = static::getDatabase()->deleteDocument( + $deleted = $this->getDatabase()->deleteDocument( 'birds', $bird1->getId(), ); $this->assertEquals(true, $deleted); - $tree1 = static::getDatabase()->getDocument('trees', 'tree1'); + $tree1 = $this->getDatabase()->getDocument('trees', 'tree1'); $this->assertEquals(1, count($tree1['birds'])); // Try update nested document - $tree1 = static::getDatabase()->updateDocument( + $tree1 = $this->getDatabase()->updateDocument( 'trees', $tree1->getId(), $tree1->setAttribute('name', 'Tree 1 Updated') @@ -10872,17 +10881,17 @@ public function testEnforceRelationshipPermissions(): void $this->assertEquals('Tree 1 Updated', $tree1['name']); // Try delete nested document - $deleted = static::getDatabase()->deleteDocument( + $deleted = $this->getDatabase()->deleteDocument( 'trees', $tree1->getId(), ); $this->assertEquals(true, $deleted); - $lawn1 = static::getDatabase()->getDocument('lawns', 'lawn1'); + $lawn1 = $this->getDatabase()->getDocument('lawns', 'lawn1'); $this->assertEquals(0, count($lawn1['trees'])); // Create document with no permissions - static::getDatabase()->createDocument('lawns', new Document([ + $this->getDatabase()->createDocument('lawns', new Document([ '$id' => 'lawn2', 'name' => 'Lawn 2', 'trees' => [ @@ -10899,19 +10908,19 @@ public function testEnforceRelationshipPermissions(): void ], ])); - $lawn2 = static::getDatabase()->getDocument('lawns', 'lawn2'); + $lawn2 = $this->getDatabase()->getDocument('lawns', 'lawn2'); $this->assertEquals(true, $lawn2->isEmpty()); - $tree2 = static::getDatabase()->getDocument('trees', 'tree2'); + $tree2 = $this->getDatabase()->getDocument('trees', 'tree2'); $this->assertEquals(true, $tree2->isEmpty()); - $bird3 = static::getDatabase()->getDocument('birds', 'bird3'); + $bird3 = $this->getDatabase()->getDocument('birds', 'bird3'); $this->assertEquals(true, $bird3->isEmpty()); } public function testExceedMaxDepthOneToMany(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } @@ -10921,24 +10930,24 @@ public function testExceedMaxDepthOneToMany(): void $level3Collection = 'level3OneToMany'; $level4Collection = 'level4OneToMany'; - static::getDatabase()->createCollection($level1Collection); - static::getDatabase()->createCollection($level2Collection); - static::getDatabase()->createCollection($level3Collection); - static::getDatabase()->createCollection($level4Collection); + $this->getDatabase()->createCollection($level1Collection); + $this->getDatabase()->createCollection($level2Collection); + $this->getDatabase()->createCollection($level3Collection); + $this->getDatabase()->createCollection($level4Collection); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: $level1Collection, relatedCollection: $level2Collection, type: Database::RELATION_ONE_TO_MANY, twoWay: true, ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: $level2Collection, relatedCollection: $level3Collection, type: Database::RELATION_ONE_TO_MANY, twoWay: true, ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: $level3Collection, relatedCollection: $level4Collection, type: Database::RELATION_ONE_TO_MANY, @@ -10946,7 +10955,7 @@ public function testExceedMaxDepthOneToMany(): void ); // Exceed create depth - $level1 = static::getDatabase()->createDocument($level1Collection, new Document([ + $level1 = $this->getDatabase()->createDocument($level1Collection, new Document([ '$id' => 'level1', '$permissions' => [ Permission::read(Role::any()), @@ -10975,13 +10984,13 @@ public function testExceedMaxDepthOneToMany(): void $this->assertArrayNotHasKey('level4', $level1[$level2Collection][0][$level3Collection][0]); // Make sure level 4 document was not created - $level3 = static::getDatabase()->getDocument($level3Collection, 'level3'); + $level3 = $this->getDatabase()->getDocument($level3Collection, 'level3'); $this->assertEquals(0, count($level3[$level4Collection])); - $level4 = static::getDatabase()->getDocument($level4Collection, 'level4'); + $level4 = $this->getDatabase()->getDocument($level4Collection, 'level4'); $this->assertTrue($level4->isEmpty()); // Exceed fetch depth - $level1 = static::getDatabase()->getDocument($level1Collection, 'level1'); + $level1 = $this->getDatabase()->getDocument($level1Collection, 'level1'); $this->assertEquals(1, count($level1[$level2Collection])); $this->assertEquals('level2', $level1[$level2Collection][0]->getId()); $this->assertEquals(1, count($level1[$level2Collection][0][$level3Collection])); @@ -10990,7 +10999,7 @@ public function testExceedMaxDepthOneToMany(): void // Exceed update depth - $level1 = static::getDatabase()->updateDocument( + $level1 = $this->getDatabase()->updateDocument( $level1Collection, 'level1', $level1 @@ -11015,15 +11024,15 @@ public function testExceedMaxDepthOneToMany(): void $this->assertArrayNotHasKey($level4Collection, $level1[$level2Collection][0][$level3Collection][0]); // Make sure level 4 document was not created - $level3 = static::getDatabase()->getDocument($level3Collection, 'level3new'); + $level3 = $this->getDatabase()->getDocument($level3Collection, 'level3new'); $this->assertEquals(0, count($level3[$level4Collection])); - $level4 = static::getDatabase()->getDocument($level4Collection, 'level4new'); + $level4 = $this->getDatabase()->getDocument($level4Collection, 'level4new'); $this->assertTrue($level4->isEmpty()); } public function testExceedMaxDepthOneToOne(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } @@ -11033,24 +11042,24 @@ public function testExceedMaxDepthOneToOne(): void $level3Collection = 'level3OneToOne'; $level4Collection = 'level4OneToOne'; - static::getDatabase()->createCollection($level1Collection); - static::getDatabase()->createCollection($level2Collection); - static::getDatabase()->createCollection($level3Collection); - static::getDatabase()->createCollection($level4Collection); + $this->getDatabase()->createCollection($level1Collection); + $this->getDatabase()->createCollection($level2Collection); + $this->getDatabase()->createCollection($level3Collection); + $this->getDatabase()->createCollection($level4Collection); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: $level1Collection, relatedCollection: $level2Collection, type: Database::RELATION_ONE_TO_ONE, twoWay: true, ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: $level2Collection, relatedCollection: $level3Collection, type: Database::RELATION_ONE_TO_ONE, twoWay: true, ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: $level3Collection, relatedCollection: $level4Collection, type: Database::RELATION_ONE_TO_ONE, @@ -11058,7 +11067,7 @@ public function testExceedMaxDepthOneToOne(): void ); // Exceed create depth - $level1 = static::getDatabase()->createDocument($level1Collection, new Document([ + $level1 = $this->getDatabase()->createDocument($level1Collection, new Document([ '$id' => 'level1', '$permissions' => [ Permission::read(Role::any()), @@ -11081,18 +11090,18 @@ public function testExceedMaxDepthOneToOne(): void $this->assertArrayNotHasKey($level4Collection, $level1[$level2Collection][$level3Collection]); // Confirm the 4th level document does not exist - $level3 = static::getDatabase()->getDocument($level3Collection, 'level3'); + $level3 = $this->getDatabase()->getDocument($level3Collection, 'level3'); $this->assertNull($level3[$level4Collection]); // Create level 4 document $level3->setAttribute($level4Collection, new Document([ '$id' => 'level4', ])); - $level3 = static::getDatabase()->updateDocument($level3Collection, $level3->getId(), $level3); + $level3 = $this->getDatabase()->updateDocument($level3Collection, $level3->getId(), $level3); $this->assertEquals('level4', $level3[$level4Collection]->getId()); // Exceed fetch depth - $level1 = static::getDatabase()->getDocument($level1Collection, 'level1'); + $level1 = $this->getDatabase()->getDocument($level1Collection, 'level1'); $this->assertArrayHasKey($level2Collection, $level1); $this->assertEquals('level2', $level1[$level2Collection]->getId()); $this->assertArrayHasKey($level3Collection, $level1[$level2Collection]); @@ -11102,7 +11111,7 @@ public function testExceedMaxDepthOneToOne(): void public function testExceedMaxDepthOneToOneNull(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } @@ -11112,31 +11121,31 @@ public function testExceedMaxDepthOneToOneNull(): void $level3Collection = 'level3OneToOneNull'; $level4Collection = 'level4OneToOneNull'; - static::getDatabase()->createCollection($level1Collection); - static::getDatabase()->createCollection($level2Collection); - static::getDatabase()->createCollection($level3Collection); - static::getDatabase()->createCollection($level4Collection); + $this->getDatabase()->createCollection($level1Collection); + $this->getDatabase()->createCollection($level2Collection); + $this->getDatabase()->createCollection($level3Collection); + $this->getDatabase()->createCollection($level4Collection); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: $level1Collection, relatedCollection: $level2Collection, type: Database::RELATION_ONE_TO_ONE, twoWay: true, ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: $level2Collection, relatedCollection: $level3Collection, type: Database::RELATION_ONE_TO_ONE, twoWay: true, ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: $level3Collection, relatedCollection: $level4Collection, type: Database::RELATION_ONE_TO_ONE, twoWay: true, ); - $level1 = static::getDatabase()->createDocument($level1Collection, new Document([ + $level1 = $this->getDatabase()->createDocument($level1Collection, new Document([ '$id' => 'level1', '$permissions' => [ Permission::read(Role::any()), @@ -11159,20 +11168,20 @@ public function testExceedMaxDepthOneToOneNull(): void $this->assertArrayNotHasKey($level4Collection, $level1[$level2Collection][$level3Collection]); // Confirm the 4th level document does not exist - $level3 = static::getDatabase()->getDocument($level3Collection, 'level3'); + $level3 = $this->getDatabase()->getDocument($level3Collection, 'level3'); $this->assertNull($level3[$level4Collection]); // Create level 4 document $level3->setAttribute($level4Collection, new Document([ '$id' => 'level4', ])); - $level3 = static::getDatabase()->updateDocument($level3Collection, $level3->getId(), $level3); + $level3 = $this->getDatabase()->updateDocument($level3Collection, $level3->getId(), $level3); $this->assertEquals('level4', $level3[$level4Collection]->getId()); - $level3 = static::getDatabase()->getDocument($level3Collection, 'level3'); + $level3 = $this->getDatabase()->getDocument($level3Collection, 'level3'); $this->assertEquals('level4', $level3[$level4Collection]->getId()); // Exceed fetch depth - $level1 = static::getDatabase()->getDocument($level1Collection, 'level1'); + $level1 = $this->getDatabase()->getDocument($level1Collection, 'level1'); $this->assertArrayHasKey($level2Collection, $level1); $this->assertEquals('level2', $level1[$level2Collection]->getId()); $this->assertArrayHasKey($level3Collection, $level1[$level2Collection]); @@ -11182,7 +11191,7 @@ public function testExceedMaxDepthOneToOneNull(): void public function testExceedMaxDepthManyToOneParent(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } @@ -11192,31 +11201,31 @@ public function testExceedMaxDepthManyToOneParent(): void $level3Collection = 'level3ManyToOneParent'; $level4Collection = 'level4ManyToOneParent'; - static::getDatabase()->createCollection($level1Collection); - static::getDatabase()->createCollection($level2Collection); - static::getDatabase()->createCollection($level3Collection); - static::getDatabase()->createCollection($level4Collection); + $this->getDatabase()->createCollection($level1Collection); + $this->getDatabase()->createCollection($level2Collection); + $this->getDatabase()->createCollection($level3Collection); + $this->getDatabase()->createCollection($level4Collection); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: $level1Collection, relatedCollection: $level2Collection, type: Database::RELATION_MANY_TO_ONE, twoWay: true, ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: $level2Collection, relatedCollection: $level3Collection, type: Database::RELATION_MANY_TO_ONE, twoWay: true, ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: $level3Collection, relatedCollection: $level4Collection, type: Database::RELATION_MANY_TO_ONE, twoWay: true, ); - $level1 = static::getDatabase()->createDocument($level1Collection, new Document([ + $level1 = $this->getDatabase()->createDocument($level1Collection, new Document([ '$id' => 'level1', '$permissions' => [ Permission::read(Role::any()), @@ -11239,20 +11248,20 @@ public function testExceedMaxDepthManyToOneParent(): void $this->assertArrayNotHasKey($level4Collection, $level1[$level2Collection][$level3Collection]); // Confirm the 4th level document does not exist - $level3 = static::getDatabase()->getDocument($level3Collection, 'level3'); + $level3 = $this->getDatabase()->getDocument($level3Collection, 'level3'); $this->assertNull($level3[$level4Collection]); // Create level 4 document $level3->setAttribute($level4Collection, new Document([ '$id' => 'level4', ])); - $level3 = static::getDatabase()->updateDocument($level3Collection, $level3->getId(), $level3); + $level3 = $this->getDatabase()->updateDocument($level3Collection, $level3->getId(), $level3); $this->assertEquals('level4', $level3[$level4Collection]->getId()); - $level3 = static::getDatabase()->getDocument($level3Collection, 'level3'); + $level3 = $this->getDatabase()->getDocument($level3Collection, 'level3'); $this->assertEquals('level4', $level3[$level4Collection]->getId()); // Exceed fetch depth - $level1 = static::getDatabase()->getDocument($level1Collection, 'level1'); + $level1 = $this->getDatabase()->getDocument($level1Collection, 'level1'); $this->assertArrayHasKey($level2Collection, $level1); $this->assertEquals('level2', $level1[$level2Collection]->getId()); $this->assertArrayHasKey($level3Collection, $level1[$level2Collection]); @@ -11262,7 +11271,7 @@ public function testExceedMaxDepthManyToOneParent(): void public function testExceedMaxDepthOneToManyChild(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } @@ -11272,31 +11281,31 @@ public function testExceedMaxDepthOneToManyChild(): void $level3Collection = 'level3OneToManyChild'; $level4Collection = 'level4OneToManyChild'; - static::getDatabase()->createCollection($level1Collection); - static::getDatabase()->createCollection($level2Collection); - static::getDatabase()->createCollection($level3Collection); - static::getDatabase()->createCollection($level4Collection); + $this->getDatabase()->createCollection($level1Collection); + $this->getDatabase()->createCollection($level2Collection); + $this->getDatabase()->createCollection($level3Collection); + $this->getDatabase()->createCollection($level4Collection); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: $level1Collection, relatedCollection: $level2Collection, type: Database::RELATION_ONE_TO_MANY, twoWay: true, ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: $level2Collection, relatedCollection: $level3Collection, type: Database::RELATION_ONE_TO_MANY, twoWay: true, ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: $level3Collection, relatedCollection: $level4Collection, type: Database::RELATION_ONE_TO_MANY, twoWay: true, ); - $level1 = static::getDatabase()->createDocument($level1Collection, new Document([ + $level1 = $this->getDatabase()->createDocument($level1Collection, new Document([ '$id' => 'level1', '$permissions' => [ Permission::read(Role::any()), @@ -11325,23 +11334,23 @@ public function testExceedMaxDepthOneToManyChild(): void $this->assertArrayNotHasKey($level4Collection, $level1[$level2Collection][0][$level3Collection][0]); // Confirm the 4th level document does not exist - $level3 = static::getDatabase()->getDocument($level3Collection, 'level3'); + $level3 = $this->getDatabase()->getDocument($level3Collection, 'level3'); $this->assertEquals(0, count($level3[$level4Collection])); // Create level 4 document $level3->setAttribute($level4Collection, [new Document([ '$id' => 'level4', ])]); - $level3 = static::getDatabase()->updateDocument($level3Collection, $level3->getId(), $level3); + $level3 = $this->getDatabase()->updateDocument($level3Collection, $level3->getId(), $level3); $this->assertEquals('level4', $level3[$level4Collection][0]->getId()); // Verify level 4 document is set - $level3 = static::getDatabase()->getDocument($level3Collection, 'level3'); + $level3 = $this->getDatabase()->getDocument($level3Collection, 'level3'); $this->assertArrayHasKey($level4Collection, $level3); $this->assertEquals('level4', $level3[$level4Collection][0]->getId()); // Exceed fetch depth - $level4 = static::getDatabase()->getDocument($level4Collection, 'level4'); + $level4 = $this->getDatabase()->getDocument($level4Collection, 'level4'); $this->assertArrayHasKey($level3Collection, $level4); $this->assertEquals('level3', $level4[$level3Collection]->getId()); $this->assertArrayHasKey($level2Collection, $level4[$level3Collection]); @@ -11351,7 +11360,7 @@ public function testExceedMaxDepthOneToManyChild(): void public function testCreateRelationshipMissingCollection(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } @@ -11359,7 +11368,7 @@ public function testCreateRelationshipMissingCollection(): void $this->expectException(Exception::class); $this->expectExceptionMessage('Collection not found'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'missing', relatedCollection: 'missing', type: Database::RELATION_ONE_TO_MANY, @@ -11369,17 +11378,17 @@ public function testCreateRelationshipMissingCollection(): void public function testCreateRelationshipMissingRelatedCollection(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('test'); + $this->getDatabase()->createCollection('test'); $this->expectException(Exception::class); $this->expectExceptionMessage('Related collection not found'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'test', relatedCollection: 'missing', type: Database::RELATION_ONE_TO_MANY, @@ -11389,15 +11398,15 @@ public function testCreateRelationshipMissingRelatedCollection(): void public function testCreateDuplicateRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('test1'); - static::getDatabase()->createCollection('test2'); + $this->getDatabase()->createCollection('test1'); + $this->getDatabase()->createCollection('test2'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'test1', relatedCollection: 'test2', type: Database::RELATION_ONE_TO_MANY, @@ -11407,7 +11416,7 @@ public function testCreateDuplicateRelationship(): void $this->expectException(Exception::class); $this->expectExceptionMessage('Attribute already exists'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'test1', relatedCollection: 'test2', type: Database::RELATION_ONE_TO_MANY, @@ -11417,18 +11426,18 @@ public function testCreateDuplicateRelationship(): void public function testCreateInvalidRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('test3'); - static::getDatabase()->createCollection('test4'); + $this->getDatabase()->createCollection('test3'); + $this->getDatabase()->createCollection('test4'); $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid relationship type'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'test3', relatedCollection: 'test4', type: 'invalid', @@ -11438,7 +11447,7 @@ public function testCreateInvalidRelationship(): void public function testDeleteMissingRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } @@ -11446,20 +11455,20 @@ public function testDeleteMissingRelationship(): void $this->expectException(Exception::class); $this->expectExceptionMessage('Attribute not found'); - static::getDatabase()->deleteRelationship('test', 'test2'); + $this->getDatabase()->deleteRelationship('test', 'test2'); } public function testCreateInvalidIntValueRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('invalid1'); - static::getDatabase()->createCollection('invalid2'); + $this->getDatabase()->createCollection('invalid1'); + $this->getDatabase()->createCollection('invalid2'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'invalid1', relatedCollection: 'invalid2', type: Database::RELATION_ONE_TO_ONE, @@ -11469,7 +11478,7 @@ public function testCreateInvalidIntValueRelationship(): void $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid relationship value. Must be either a document, document ID, or an array of documents or document IDs.'); - static::getDatabase()->createDocument('invalid1', new Document([ + $this->getDatabase()->createDocument('invalid1', new Document([ '$id' => ID::unique(), 'invalid2' => 10, ])); @@ -11480,7 +11489,7 @@ public function testCreateInvalidIntValueRelationship(): void */ public function testCreateInvalidObjectValueRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } @@ -11488,7 +11497,7 @@ public function testCreateInvalidObjectValueRelationship(): void $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid relationship value. Must be either a document, document ID, or an array of documents or document IDs.'); - static::getDatabase()->createDocument('invalid1', new Document([ + $this->getDatabase()->createDocument('invalid1', new Document([ '$id' => ID::unique(), 'invalid2' => new \stdClass(), ])); @@ -11499,12 +11508,12 @@ public function testCreateInvalidObjectValueRelationship(): void */ public function testCreateInvalidArrayIntValueRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'invalid1', relatedCollection: 'invalid2', type: Database::RELATION_ONE_TO_MANY, @@ -11516,7 +11525,7 @@ public function testCreateInvalidArrayIntValueRelationship(): void $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid relationship value. Must be either a document, document ID, or an array of documents or document IDs.'); - static::getDatabase()->createDocument('invalid1', new Document([ + $this->getDatabase()->createDocument('invalid1', new Document([ '$id' => ID::unique(), 'invalid3' => [10], ])); @@ -11524,21 +11533,21 @@ public function testCreateInvalidArrayIntValueRelationship(): void public function testCreateEmptyValueRelationship(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('null1'); - static::getDatabase()->createCollection('null2'); + $this->getDatabase()->createCollection('null1'); + $this->getDatabase()->createCollection('null2'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'null1', relatedCollection: 'null2', type: Database::RELATION_ONE_TO_ONE, twoWay: true, ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'null1', relatedCollection: 'null2', type: Database::RELATION_ONE_TO_MANY, @@ -11546,7 +11555,7 @@ public function testCreateEmptyValueRelationship(): void id: 'null3', twoWayKey: 'null4', ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'null1', relatedCollection: 'null2', type: Database::RELATION_MANY_TO_ONE, @@ -11554,7 +11563,7 @@ public function testCreateEmptyValueRelationship(): void id: 'null4', twoWayKey: 'null5', ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'null1', relatedCollection: 'null2', type: Database::RELATION_MANY_TO_MANY, @@ -11563,21 +11572,21 @@ public function testCreateEmptyValueRelationship(): void twoWayKey: 'null7', ); - $document = static::getDatabase()->createDocument('null1', new Document([ + $document = $this->getDatabase()->createDocument('null1', new Document([ '$id' => ID::unique(), 'null2' => null, ])); $this->assertEquals(null, $document->getAttribute('null2')); - $document = static::getDatabase()->createDocument('null2', new Document([ + $document = $this->getDatabase()->createDocument('null2', new Document([ '$id' => ID::unique(), 'null1' => null, ])); $this->assertEquals(null, $document->getAttribute('null1')); - $document = static::getDatabase()->createDocument('null1', new Document([ + $document = $this->getDatabase()->createDocument('null1', new Document([ '$id' => ID::unique(), 'null3' => null, ])); @@ -11585,35 +11594,35 @@ public function testCreateEmptyValueRelationship(): void // One to many will be empty array instead of null $this->assertEquals([], $document->getAttribute('null3')); - $document = static::getDatabase()->createDocument('null2', new Document([ + $document = $this->getDatabase()->createDocument('null2', new Document([ '$id' => ID::unique(), 'null4' => null, ])); $this->assertEquals(null, $document->getAttribute('null4')); - $document = static::getDatabase()->createDocument('null1', new Document([ + $document = $this->getDatabase()->createDocument('null1', new Document([ '$id' => ID::unique(), 'null4' => null, ])); $this->assertEquals(null, $document->getAttribute('null4')); - $document = static::getDatabase()->createDocument('null2', new Document([ + $document = $this->getDatabase()->createDocument('null2', new Document([ '$id' => ID::unique(), 'null5' => null, ])); $this->assertEquals([], $document->getAttribute('null5')); - $document = static::getDatabase()->createDocument('null1', new Document([ + $document = $this->getDatabase()->createDocument('null1', new Document([ '$id' => ID::unique(), 'null6' => null, ])); $this->assertEquals([], $document->getAttribute('null6')); - $document = static::getDatabase()->createDocument('null2', new Document([ + $document = $this->getDatabase()->createDocument('null2', new Document([ '$id' => ID::unique(), 'null7' => null, ])); @@ -11623,15 +11632,15 @@ public function testCreateEmptyValueRelationship(): void public function testDeleteCollectionDeletesRelationships(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('testers'); - static::getDatabase()->createCollection('devices'); + $this->getDatabase()->createCollection('testers'); + $this->getDatabase()->createCollection('devices'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'testers', relatedCollection: 'devices', type: Database::RELATION_ONE_TO_MANY, @@ -11639,17 +11648,17 @@ public function testDeleteCollectionDeletesRelationships(): void twoWayKey: 'tester' ); - $testers = static::getDatabase()->getCollection('testers'); - $devices = static::getDatabase()->getCollection('devices'); + $testers = $this->getDatabase()->getCollection('testers'); + $devices = $this->getDatabase()->getCollection('devices'); $this->assertEquals(1, \count($testers->getAttribute('attributes'))); $this->assertEquals(1, \count($devices->getAttribute('attributes'))); $this->assertEquals(1, \count($devices->getAttribute('indexes'))); - static::getDatabase()->deleteCollection('testers'); + $this->getDatabase()->deleteCollection('testers'); - $testers = static::getDatabase()->getCollection('testers'); - $devices = static::getDatabase()->getCollection('devices'); + $testers = $this->getDatabase()->getCollection('testers'); + $devices = $this->getDatabase()->getCollection('devices'); $this->assertEquals(true, $testers->isEmpty()); $this->assertEquals(0, \count($devices->getAttribute('attributes'))); @@ -11658,15 +11667,15 @@ public function testDeleteCollectionDeletesRelationships(): void public function testDeleteTwoWayRelationshipFromChild(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('drivers'); - static::getDatabase()->createCollection('licenses'); + $this->getDatabase()->createCollection('drivers'); + $this->getDatabase()->createCollection('licenses'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'drivers', relatedCollection: 'licenses', type: Database::RELATION_ONE_TO_ONE, @@ -11675,25 +11684,25 @@ public function testDeleteTwoWayRelationshipFromChild(): void twoWayKey: 'driver' ); - $drivers = static::getDatabase()->getCollection('drivers'); - $licenses = static::getDatabase()->getCollection('licenses'); + $drivers = $this->getDatabase()->getCollection('drivers'); + $licenses = $this->getDatabase()->getCollection('licenses'); $this->assertEquals(1, \count($drivers->getAttribute('attributes'))); $this->assertEquals(1, \count($drivers->getAttribute('indexes'))); $this->assertEquals(1, \count($licenses->getAttribute('attributes'))); $this->assertEquals(1, \count($licenses->getAttribute('indexes'))); - static::getDatabase()->deleteRelationship('licenses', 'driver'); + $this->getDatabase()->deleteRelationship('licenses', 'driver'); - $drivers = static::getDatabase()->getCollection('drivers'); - $licenses = static::getDatabase()->getCollection('licenses'); + $drivers = $this->getDatabase()->getCollection('drivers'); + $licenses = $this->getDatabase()->getCollection('licenses'); $this->assertEquals(0, \count($drivers->getAttribute('attributes'))); $this->assertEquals(0, \count($drivers->getAttribute('indexes'))); $this->assertEquals(0, \count($licenses->getAttribute('attributes'))); $this->assertEquals(0, \count($licenses->getAttribute('indexes'))); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'drivers', relatedCollection: 'licenses', type: Database::RELATION_ONE_TO_MANY, @@ -11702,25 +11711,25 @@ public function testDeleteTwoWayRelationshipFromChild(): void twoWayKey: 'driver' ); - $drivers = static::getDatabase()->getCollection('drivers'); - $licenses = static::getDatabase()->getCollection('licenses'); + $drivers = $this->getDatabase()->getCollection('drivers'); + $licenses = $this->getDatabase()->getCollection('licenses'); $this->assertEquals(1, \count($drivers->getAttribute('attributes'))); $this->assertEquals(0, \count($drivers->getAttribute('indexes'))); $this->assertEquals(1, \count($licenses->getAttribute('attributes'))); $this->assertEquals(1, \count($licenses->getAttribute('indexes'))); - static::getDatabase()->deleteRelationship('licenses', 'driver'); + $this->getDatabase()->deleteRelationship('licenses', 'driver'); - $drivers = static::getDatabase()->getCollection('drivers'); - $licenses = static::getDatabase()->getCollection('licenses'); + $drivers = $this->getDatabase()->getCollection('drivers'); + $licenses = $this->getDatabase()->getCollection('licenses'); $this->assertEquals(0, \count($drivers->getAttribute('attributes'))); $this->assertEquals(0, \count($drivers->getAttribute('indexes'))); $this->assertEquals(0, \count($licenses->getAttribute('attributes'))); $this->assertEquals(0, \count($licenses->getAttribute('indexes'))); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'licenses', relatedCollection: 'drivers', type: Database::RELATION_MANY_TO_ONE, @@ -11729,25 +11738,25 @@ public function testDeleteTwoWayRelationshipFromChild(): void twoWayKey: 'licenses' ); - $drivers = static::getDatabase()->getCollection('drivers'); - $licenses = static::getDatabase()->getCollection('licenses'); + $drivers = $this->getDatabase()->getCollection('drivers'); + $licenses = $this->getDatabase()->getCollection('licenses'); $this->assertEquals(1, \count($drivers->getAttribute('attributes'))); $this->assertEquals(0, \count($drivers->getAttribute('indexes'))); $this->assertEquals(1, \count($licenses->getAttribute('attributes'))); $this->assertEquals(1, \count($licenses->getAttribute('indexes'))); - static::getDatabase()->deleteRelationship('drivers', 'licenses'); + $this->getDatabase()->deleteRelationship('drivers', 'licenses'); - $drivers = static::getDatabase()->getCollection('drivers'); - $licenses = static::getDatabase()->getCollection('licenses'); + $drivers = $this->getDatabase()->getCollection('drivers'); + $licenses = $this->getDatabase()->getCollection('licenses'); $this->assertEquals(0, \count($drivers->getAttribute('attributes'))); $this->assertEquals(0, \count($drivers->getAttribute('indexes'))); $this->assertEquals(0, \count($licenses->getAttribute('attributes'))); $this->assertEquals(0, \count($licenses->getAttribute('indexes'))); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'licenses', relatedCollection: 'drivers', type: Database::RELATION_MANY_TO_MANY, @@ -11756,9 +11765,9 @@ public function testDeleteTwoWayRelationshipFromChild(): void twoWayKey: 'licenses' ); - $drivers = static::getDatabase()->getCollection('drivers'); - $licenses = static::getDatabase()->getCollection('licenses'); - $junction = static::getDatabase()->getCollection('_' . $licenses->getInternalId() . '_' . $drivers->getInternalId()); + $drivers = $this->getDatabase()->getCollection('drivers'); + $licenses = $this->getDatabase()->getCollection('licenses'); + $junction = $this->getDatabase()->getCollection('_' . $licenses->getInternalId() . '_' . $drivers->getInternalId()); $this->assertEquals(1, \count($drivers->getAttribute('attributes'))); $this->assertEquals(0, \count($drivers->getAttribute('indexes'))); @@ -11767,11 +11776,11 @@ public function testDeleteTwoWayRelationshipFromChild(): void $this->assertEquals(2, \count($junction->getAttribute('attributes'))); $this->assertEquals(2, \count($junction->getAttribute('indexes'))); - static::getDatabase()->deleteRelationship('drivers', 'licenses'); + $this->getDatabase()->deleteRelationship('drivers', 'licenses'); - $drivers = static::getDatabase()->getCollection('drivers'); - $licenses = static::getDatabase()->getCollection('licenses'); - $junction = static::getDatabase()->getCollection('_licenses_drivers'); + $drivers = $this->getDatabase()->getCollection('drivers'); + $licenses = $this->getDatabase()->getCollection('licenses'); + $junction = $this->getDatabase()->getCollection('_licenses_drivers'); $this->assertEquals(0, \count($drivers->getAttribute('attributes'))); $this->assertEquals(0, \count($drivers->getAttribute('indexes'))); @@ -11783,20 +11792,20 @@ public function testDeleteTwoWayRelationshipFromChild(): void public function testUpdateRelationshipToExistingKey(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('ovens'); - static::getDatabase()->createCollection('cakes'); + $this->getDatabase()->createCollection('ovens'); + $this->getDatabase()->createCollection('cakes'); - static::getDatabase()->createAttribute('ovens', 'maxTemp', Database::VAR_INTEGER, 0, true); - static::getDatabase()->createAttribute('ovens', 'owner', Database::VAR_STRING, 255, true); - static::getDatabase()->createAttribute('cakes', 'height', Database::VAR_INTEGER, 0, true); - static::getDatabase()->createAttribute('cakes', 'colour', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('ovens', 'maxTemp', Database::VAR_INTEGER, 0, true); + $this->getDatabase()->createAttribute('ovens', 'owner', Database::VAR_STRING, 255, true); + $this->getDatabase()->createAttribute('cakes', 'height', Database::VAR_INTEGER, 0, true); + $this->getDatabase()->createAttribute('cakes', 'colour', Database::VAR_STRING, 255, true); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'ovens', relatedCollection: 'cakes', type: Database::RELATION_ONE_TO_MANY, @@ -11806,14 +11815,14 @@ public function testUpdateRelationshipToExistingKey(): void ); try { - static::getDatabase()->updateRelationship('ovens', 'cakes', newKey: 'owner'); + $this->getDatabase()->updateRelationship('ovens', 'cakes', newKey: 'owner'); $this->fail('Failed to throw exception'); } catch (DuplicateException $e) { $this->assertEquals('Attribute already exists', $e->getMessage()); } try { - static::getDatabase()->updateRelationship('ovens', 'cakes', newTwoWayKey: 'height'); + $this->getDatabase()->updateRelationship('ovens', 'cakes', newTwoWayKey: 'height'); $this->fail('Failed to throw exception'); } catch (DuplicateException $e) { $this->assertEquals('Related attribute already exists', $e->getMessage()); @@ -11822,29 +11831,29 @@ public function testUpdateRelationshipToExistingKey(): void public function testOneToOneRelationshipKeyWithSymbols(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('$symbols_coll.ection1'); - static::getDatabase()->createCollection('$symbols_coll.ection2'); + $this->getDatabase()->createCollection('$symbols_coll.ection1'); + $this->getDatabase()->createCollection('$symbols_coll.ection2'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: '$symbols_coll.ection1', relatedCollection: '$symbols_coll.ection2', type: Database::RELATION_ONE_TO_ONE, twoWay: true, ); - $doc1 = static::getDatabase()->createDocument('$symbols_coll.ection2', new Document([ + $doc1 = $this->getDatabase()->createDocument('$symbols_coll.ection2', new Document([ '$id' => ID::unique(), '$permissions' => [ Permission::read(Role::any()), Permission::update(Role::any()) ] ])); - $doc2 = static::getDatabase()->createDocument('$symbols_coll.ection1', new Document([ + $doc2 = $this->getDatabase()->createDocument('$symbols_coll.ection1', new Document([ '$id' => ID::unique(), '$symbols_coll.ection2' => $doc1->getId(), '$permissions' => [ @@ -11853,8 +11862,8 @@ public function testOneToOneRelationshipKeyWithSymbols(): void ] ])); - $doc1 = static::getDatabase()->getDocument('$symbols_coll.ection2', $doc1->getId()); - $doc2 = static::getDatabase()->getDocument('$symbols_coll.ection1', $doc2->getId()); + $doc1 = $this->getDatabase()->getDocument('$symbols_coll.ection2', $doc1->getId()); + $doc2 = $this->getDatabase()->getDocument('$symbols_coll.ection1', $doc2->getId()); $this->assertEquals($doc2->getId(), $doc1->getAttribute('$symbols_coll.ection1')->getId()); $this->assertEquals($doc1->getId(), $doc2->getAttribute('$symbols_coll.ection2')->getId()); @@ -11862,29 +11871,29 @@ public function testOneToOneRelationshipKeyWithSymbols(): void public function testOneToManyRelationshipKeyWithSymbols(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('$symbols_coll.ection3'); - static::getDatabase()->createCollection('$symbols_coll.ection4'); + $this->getDatabase()->createCollection('$symbols_coll.ection3'); + $this->getDatabase()->createCollection('$symbols_coll.ection4'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: '$symbols_coll.ection3', relatedCollection: '$symbols_coll.ection4', type: Database::RELATION_ONE_TO_MANY, twoWay: true, ); - $doc1 = static::getDatabase()->createDocument('$symbols_coll.ection4', new Document([ + $doc1 = $this->getDatabase()->createDocument('$symbols_coll.ection4', new Document([ '$id' => ID::unique(), '$permissions' => [ Permission::read(Role::any()), Permission::update(Role::any()) ] ])); - $doc2 = static::getDatabase()->createDocument('$symbols_coll.ection3', new Document([ + $doc2 = $this->getDatabase()->createDocument('$symbols_coll.ection3', new Document([ '$id' => ID::unique(), '$symbols_coll.ection4' => [$doc1->getId()], '$permissions' => [ @@ -11893,8 +11902,8 @@ public function testOneToManyRelationshipKeyWithSymbols(): void ] ])); - $doc1 = static::getDatabase()->getDocument('$symbols_coll.ection4', $doc1->getId()); - $doc2 = static::getDatabase()->getDocument('$symbols_coll.ection3', $doc2->getId()); + $doc1 = $this->getDatabase()->getDocument('$symbols_coll.ection4', $doc1->getId()); + $doc2 = $this->getDatabase()->getDocument('$symbols_coll.ection3', $doc2->getId()); $this->assertEquals($doc2->getId(), $doc1->getAttribute('$symbols_coll.ection3')->getId()); $this->assertEquals($doc1->getId(), $doc2->getAttribute('$symbols_coll.ection4')[0]->getId()); @@ -11902,29 +11911,29 @@ public function testOneToManyRelationshipKeyWithSymbols(): void public function testManyToOneRelationshipKeyWithSymbols(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('$symbols_coll.ection5'); - static::getDatabase()->createCollection('$symbols_coll.ection6'); + $this->getDatabase()->createCollection('$symbols_coll.ection5'); + $this->getDatabase()->createCollection('$symbols_coll.ection6'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: '$symbols_coll.ection5', relatedCollection: '$symbols_coll.ection6', type: Database::RELATION_MANY_TO_ONE, twoWay: true, ); - $doc1 = static::getDatabase()->createDocument('$symbols_coll.ection6', new Document([ + $doc1 = $this->getDatabase()->createDocument('$symbols_coll.ection6', new Document([ '$id' => ID::unique(), '$permissions' => [ Permission::read(Role::any()), Permission::update(Role::any()) ] ])); - $doc2 = static::getDatabase()->createDocument('$symbols_coll.ection5', new Document([ + $doc2 = $this->getDatabase()->createDocument('$symbols_coll.ection5', new Document([ '$id' => ID::unique(), '$symbols_coll.ection6' => $doc1->getId(), '$permissions' => [ @@ -11933,8 +11942,8 @@ public function testManyToOneRelationshipKeyWithSymbols(): void ] ])); - $doc1 = static::getDatabase()->getDocument('$symbols_coll.ection6', $doc1->getId()); - $doc2 = static::getDatabase()->getDocument('$symbols_coll.ection5', $doc2->getId()); + $doc1 = $this->getDatabase()->getDocument('$symbols_coll.ection6', $doc1->getId()); + $doc2 = $this->getDatabase()->getDocument('$symbols_coll.ection5', $doc2->getId()); $this->assertEquals($doc2->getId(), $doc1->getAttribute('$symbols_coll.ection5')[0]->getId()); $this->assertEquals($doc1->getId(), $doc2->getAttribute('$symbols_coll.ection6')->getId()); @@ -11942,29 +11951,29 @@ public function testManyToOneRelationshipKeyWithSymbols(): void public function testManyToManyRelationshipKeyWithSymbols(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('$symbols_coll.ection7'); - static::getDatabase()->createCollection('$symbols_coll.ection8'); + $this->getDatabase()->createCollection('$symbols_coll.ection7'); + $this->getDatabase()->createCollection('$symbols_coll.ection8'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: '$symbols_coll.ection7', relatedCollection: '$symbols_coll.ection8', type: Database::RELATION_MANY_TO_MANY, twoWay: true, ); - $doc1 = static::getDatabase()->createDocument('$symbols_coll.ection8', new Document([ + $doc1 = $this->getDatabase()->createDocument('$symbols_coll.ection8', new Document([ '$id' => ID::unique(), '$permissions' => [ Permission::read(Role::any()), Permission::update(Role::any()) ] ])); - $doc2 = static::getDatabase()->createDocument('$symbols_coll.ection7', new Document([ + $doc2 = $this->getDatabase()->createDocument('$symbols_coll.ection7', new Document([ '$id' => ID::unique(), '$symbols_coll.ection8' => [$doc1->getId()], '$permissions' => [ @@ -11973,8 +11982,8 @@ public function testManyToManyRelationshipKeyWithSymbols(): void ] ])); - $doc1 = static::getDatabase()->getDocument('$symbols_coll.ection8', $doc1->getId()); - $doc2 = static::getDatabase()->getDocument('$symbols_coll.ection7', $doc2->getId()); + $doc1 = $this->getDatabase()->getDocument('$symbols_coll.ection8', $doc1->getId()); + $doc2 = $this->getDatabase()->getDocument('$symbols_coll.ection7', $doc2->getId()); $this->assertEquals($doc2->getId(), $doc1->getAttribute('$symbols_coll.ection7')[0]->getId()); $this->assertEquals($doc1->getId(), $doc2->getAttribute('$symbols_coll.ection8')[0]->getId()); @@ -11982,16 +11991,16 @@ public function testManyToManyRelationshipKeyWithSymbols(): void public function testCascadeMultiDelete(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('cascadeMultiDelete1'); - static::getDatabase()->createCollection('cascadeMultiDelete2'); - static::getDatabase()->createCollection('cascadeMultiDelete3'); + $this->getDatabase()->createCollection('cascadeMultiDelete1'); + $this->getDatabase()->createCollection('cascadeMultiDelete2'); + $this->getDatabase()->createCollection('cascadeMultiDelete3'); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'cascadeMultiDelete1', relatedCollection: 'cascadeMultiDelete2', type: Database::RELATION_ONE_TO_MANY, @@ -11999,7 +12008,7 @@ public function testCascadeMultiDelete(): void onDelete: Database::RELATION_MUTATE_CASCADE ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'cascadeMultiDelete2', relatedCollection: 'cascadeMultiDelete3', type: Database::RELATION_ONE_TO_MANY, @@ -12007,7 +12016,7 @@ public function testCascadeMultiDelete(): void onDelete: Database::RELATION_MUTATE_CASCADE ); - $root = static::getDatabase()->createDocument('cascadeMultiDelete1', new Document([ + $root = $this->getDatabase()->createDocument('cascadeMultiDelete1', new Document([ '$id' => 'cascadeMultiDelete1', '$permissions' => [ Permission::read(Role::any()), @@ -12036,18 +12045,18 @@ public function testCascadeMultiDelete(): void $this->assertCount(1, $root->getAttribute('cascadeMultiDelete2')); $this->assertCount(1, $root->getAttribute('cascadeMultiDelete2')[0]->getAttribute('cascadeMultiDelete3')); - $this->assertEquals(true, static::getDatabase()->deleteDocument('cascadeMultiDelete1', $root->getId())); + $this->assertEquals(true, $this->getDatabase()->deleteDocument('cascadeMultiDelete1', $root->getId())); - $multi2 = static::getDatabase()->getDocument('cascadeMultiDelete2', 'cascadeMultiDelete2'); + $multi2 = $this->getDatabase()->getDocument('cascadeMultiDelete2', 'cascadeMultiDelete2'); $this->assertEquals(true, $multi2->isEmpty()); - $multi3 = static::getDatabase()->getDocument('cascadeMultiDelete3', 'cascadeMultiDelete3'); + $multi3 = $this->getDatabase()->getDocument('cascadeMultiDelete3', 'cascadeMultiDelete3'); $this->assertEquals(true, $multi3->isEmpty()); } public function testCollectionUpdate(): Document { - $collection = static::getDatabase()->createCollection('collectionUpdate', permissions: [ + $collection = $this->getDatabase()->createCollection('collectionUpdate', permissions: [ Permission::create(Role::users()), Permission::read(Role::users()), Permission::update(Role::users()), @@ -12056,19 +12065,19 @@ public function testCollectionUpdate(): Document $this->assertInstanceOf(Document::class, $collection); - $collection = static::getDatabase()->getCollection('collectionUpdate'); + $collection = $this->getDatabase()->getCollection('collectionUpdate'); $this->assertFalse($collection->getAttribute('documentSecurity')); $this->assertIsArray($collection->getPermissions()); $this->assertCount(4, $collection->getPermissions()); - $collection = static::getDatabase()->updateCollection('collectionUpdate', [], true); + $collection = $this->getDatabase()->updateCollection('collectionUpdate', [], true); $this->assertTrue($collection->getAttribute('documentSecurity')); $this->assertIsArray($collection->getPermissions()); $this->assertEmpty($collection->getPermissions()); - $collection = static::getDatabase()->getCollection('collectionUpdate'); + $collection = $this->getDatabase()->getCollection('collectionUpdate'); $this->assertTrue($collection->getAttribute('documentSecurity')); $this->assertIsArray($collection->getPermissions()); @@ -12083,14 +12092,14 @@ public function testCollectionUpdate(): Document public function testCollectionUpdatePermissionsThrowException(Document $collection): void { $this->expectException(DatabaseException::class); - static::getDatabase()->updateCollection($collection->getId(), permissions: [ + $this->getDatabase()->updateCollection($collection->getId(), permissions: [ 'i dont work' ], documentSecurity: false); } public function testCollectionPermissions(): Document { - $collection = static::getDatabase()->createCollection('collectionSecurity', permissions: [ + $collection = $this->getDatabase()->createCollection('collectionSecurity', permissions: [ Permission::create(Role::users()), Permission::read(Role::users()), Permission::update(Role::users()), @@ -12099,7 +12108,7 @@ public function testCollectionPermissions(): Document $this->assertInstanceOf(Document::class, $collection); - $this->assertTrue(static::getDatabase()->createAttribute( + $this->assertTrue($this->getDatabase()->createAttribute( collection: $collection->getId(), id: 'test', type: Database::VAR_STRING, @@ -12113,7 +12122,7 @@ public function testCollectionPermissions(): Document public function testCollectionPermissionsExceptions(): void { $this->expectException(DatabaseException::class); - static::getDatabase()->createCollection('collectionSecurity', permissions: [ + $this->getDatabase()->createCollection('collectionSecurity', permissions: [ 'i dont work' ]); } @@ -12124,10 +12133,10 @@ public function testCollectionPermissionsExceptions(): void */ public function testCollectionPermissionsCreateWorks(Document $collection): array { - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::users()->toString()); - $document = static::getDatabase()->createDocument($collection->getId(), new Document([ + $document = $this->getDatabase()->createDocument($collection->getId(), new Document([ '$id' => ID::unique(), '$permissions' => [ Permission::read(Role::user('random')), @@ -12147,11 +12156,11 @@ public function testCollectionPermissionsCreateWorks(Document $collection): arra */ public function testCollectionPermissionsCreateThrowsException(Document $collection): void { - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::any()->toString()); $this->expectException(AuthorizationException::class); - static::getDatabase()->createDocument($collection->getId(), new Document([ + $this->getDatabase()->createDocument($collection->getId(), new Document([ '$id' => ID::unique(), '$permissions' => [ Permission::read(Role::any()), @@ -12171,10 +12180,10 @@ public function testCollectionPermissionsGetWorks(array $data): array { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::users()->toString()); - $document = static::getDatabase()->getDocument( + $document = $this->getDatabase()->getDocument( $collection->getId(), $document->getId() ); @@ -12192,10 +12201,10 @@ public function testCollectionPermissionsGetThrowsException(array $data): void { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::any()->toString()); - $document = static::getDatabase()->getDocument( + $document = $this->getDatabase()->getDocument( $collection->getId(), $document->getId(), ); @@ -12212,17 +12221,17 @@ public function testCollectionPermissionsFindWorks(array $data): array { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::users()->toString()); - $documents = static::getDatabase()->find($collection->getId()); + $documents = $this->getDatabase()->find($collection->getId()); $this->assertNotEmpty($documents); - Authorization::cleanRoles(); - Authorization::setRole(Role::user('random')->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::user('random')->toString()); try { - static::getDatabase()->find($collection->getId()); + $this->getDatabase()->find($collection->getId()); $this->fail('Failed to throw exception'); } catch (AuthorizationException) { } @@ -12238,11 +12247,11 @@ public function testCollectionPermissionsFindThrowsException(array $data): void { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::any()->toString()); $this->expectException(AuthorizationException::class); - static::getDatabase()->find($collection->getId()); + $this->getDatabase()->find($collection->getId()); } /** @@ -12254,10 +12263,10 @@ public function testCollectionPermissionsCountWorks(array $data): array { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::users()->toString()); - $count = static::getDatabase()->count( + $count = $this->getDatabase()->count( $collection->getId() ); @@ -12274,10 +12283,10 @@ public function testCollectionPermissionsCountThrowsException(array $data): void { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::any()->toString()); - $count = static::getDatabase()->count( + $count = $this->getDatabase()->count( $collection->getId() ); $this->assertEmpty($count); @@ -12292,10 +12301,10 @@ public function testCollectionPermissionsUpdateWorks(array $data): array { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::users()->toString()); - $this->assertInstanceOf(Document::class, static::getDatabase()->updateDocument( + $this->assertInstanceOf(Document::class, $this->getDatabase()->updateDocument( $collection->getId(), $document->getId(), $document->setAttribute('test', 'ipsum') @@ -12311,11 +12320,11 @@ public function testCollectionPermissionsUpdateWorks(array $data): array public function testCollectionPermissionsUpdateThrowsException(array $data): void { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::any()->toString()); $this->expectException(AuthorizationException::class); - $document = static::getDatabase()->updateDocument( + $document = $this->getDatabase()->updateDocument( $collection->getId(), $document->getId(), $document->setAttribute('test', 'lorem') @@ -12330,11 +12339,11 @@ public function testCollectionPermissionsDeleteThrowsException(array $data): voi { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::any()->toString()); $this->expectException(AuthorizationException::class); - static::getDatabase()->deleteDocument( + $this->getDatabase()->deleteDocument( $collection->getId(), $document->getId() ); @@ -12348,10 +12357,10 @@ public function testCollectionPermissionsDeleteWorks(array $data): void { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::users()->toString()); - $this->assertTrue(static::getDatabase()->deleteDocument( + $this->assertTrue($this->getDatabase()->deleteDocument( $collection->getId(), $document->getId() )); @@ -12362,7 +12371,7 @@ public function testCollectionPermissionsDeleteWorks(array $data): void */ public function testCollectionPermissionsRelationships(): array { - $collection = static::getDatabase()->createCollection('collectionSecurity.Parent', permissions: [ + $collection = $this->getDatabase()->createCollection('collectionSecurity.Parent', permissions: [ Permission::create(Role::users()), Permission::read(Role::users()), Permission::update(Role::users()), @@ -12371,7 +12380,7 @@ public function testCollectionPermissionsRelationships(): array $this->assertInstanceOf(Document::class, $collection); - $this->assertTrue(static::getDatabase()->createAttribute( + $this->assertTrue($this->getDatabase()->createAttribute( collection: $collection->getId(), id: 'test', type: Database::VAR_STRING, @@ -12379,7 +12388,7 @@ public function testCollectionPermissionsRelationships(): array required: false )); - $collectionOneToOne = static::getDatabase()->createCollection('collectionSecurity.OneToOne', permissions: [ + $collectionOneToOne = $this->getDatabase()->createCollection('collectionSecurity.OneToOne', permissions: [ Permission::create(Role::users()), Permission::read(Role::users()), Permission::update(Role::users()), @@ -12388,7 +12397,7 @@ public function testCollectionPermissionsRelationships(): array $this->assertInstanceOf(Document::class, $collectionOneToOne); - $this->assertTrue(static::getDatabase()->createAttribute( + $this->assertTrue($this->getDatabase()->createAttribute( collection: $collectionOneToOne->getId(), id: 'test', type: Database::VAR_STRING, @@ -12396,7 +12405,7 @@ public function testCollectionPermissionsRelationships(): array required: false )); - $this->assertTrue(static::getDatabase()->createRelationship( + $this->assertTrue($this->getDatabase()->createRelationship( collection: $collection->getId(), relatedCollection: $collectionOneToOne->getId(), type: Database::RELATION_ONE_TO_ONE, @@ -12404,7 +12413,7 @@ public function testCollectionPermissionsRelationships(): array onDelete: Database::RELATION_MUTATE_CASCADE )); - $collectionOneToMany = static::getDatabase()->createCollection('collectionSecurity.OneToMany', permissions: [ + $collectionOneToMany = $this->getDatabase()->createCollection('collectionSecurity.OneToMany', permissions: [ Permission::create(Role::users()), Permission::read(Role::users()), Permission::update(Role::users()), @@ -12413,7 +12422,7 @@ public function testCollectionPermissionsRelationships(): array $this->assertInstanceOf(Document::class, $collectionOneToMany); - $this->assertTrue(static::getDatabase()->createAttribute( + $this->assertTrue($this->getDatabase()->createAttribute( collection: $collectionOneToMany->getId(), id: 'test', type: Database::VAR_STRING, @@ -12421,7 +12430,7 @@ public function testCollectionPermissionsRelationships(): array required: false )); - $this->assertTrue(static::getDatabase()->createRelationship( + $this->assertTrue($this->getDatabase()->createRelationship( collection: $collection->getId(), relatedCollection: $collectionOneToMany->getId(), type: Database::RELATION_ONE_TO_MANY, @@ -12440,10 +12449,10 @@ public function testCollectionPermissionsRelationships(): array public function testCollectionPermissionsRelationshipsCreateWorks(array $data): array { [$collection, $collectionOneToOne, $collectionOneToMany] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::users()->toString()); - $document = static::getDatabase()->createDocument($collection->getId(), new Document([ + $document = $this->getDatabase()->createDocument($collection->getId(), new Document([ '$id' => ID::unique(), '$permissions' => [ Permission::read(Role::user('random')), @@ -12493,11 +12502,11 @@ public function testCollectionPermissionsRelationshipsCreateThrowsException(arra { [$collection, $collectionOneToOne, $collectionOneToMany] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::any()->toString()); $this->expectException(AuthorizationException::class); - static::getDatabase()->createDocument($collection->getId(), new Document([ + $this->getDatabase()->createDocument($collection->getId(), new Document([ '$id' => ID::unique(), '$permissions' => [ Permission::read(Role::any()), @@ -12516,10 +12525,10 @@ public function testCollectionPermissionsRelationshipsGetWorks(array $data): arr { [$collection, $collectionOneToOne, $collectionOneToMany, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::users()->toString()); - $document = static::getDatabase()->getDocument( + $document = $this->getDatabase()->getDocument( $collection->getId(), $document->getId() ); @@ -12530,10 +12539,10 @@ public function testCollectionPermissionsRelationshipsGetWorks(array $data): arr $this->assertCount(2, $document->getAttribute(Database::RELATION_ONE_TO_MANY)); $this->assertFalse($document->isEmpty()); - Authorization::cleanRoles(); - Authorization::setRole(Role::user('random')->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::user('random')->toString()); - $document = static::getDatabase()->getDocument( + $document = $this->getDatabase()->getDocument( $collection->getId(), $document->getId() ); @@ -12555,10 +12564,10 @@ public function testCollectionPermissionsRelationshipsGetThrowsException(array $ { [$collection, $collectionOneToOne, $collectionOneToMany, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::any()->toString()); - $document = static::getDatabase()->getDocument( + $document = $this->getDatabase()->getDocument( $collection->getId(), $document->getId(), ); @@ -12574,10 +12583,10 @@ public function testCollectionPermissionsRelationshipsFindWorks(array $data): vo { [$collection, $collectionOneToOne, $collectionOneToMany, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::users()->toString()); - $documents = static::getDatabase()->find( + $documents = $this->getDatabase()->find( $collection->getId() ); @@ -12590,10 +12599,10 @@ public function testCollectionPermissionsRelationshipsFindWorks(array $data): vo $this->assertCount(2, $document->getAttribute(Database::RELATION_ONE_TO_MANY)); $this->assertFalse($document->isEmpty()); - Authorization::cleanRoles(); - Authorization::setRole(Role::user('random')->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::user('random')->toString()); - $documents = static::getDatabase()->find( + $documents = $this->getDatabase()->find( $collection->getId() ); @@ -12606,10 +12615,10 @@ public function testCollectionPermissionsRelationshipsFindWorks(array $data): vo $this->assertCount(1, $document->getAttribute(Database::RELATION_ONE_TO_MANY)); $this->assertFalse($document->isEmpty()); - Authorization::cleanRoles(); - Authorization::setRole(Role::user('unknown')->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::user('unknown')->toString()); - $documents = static::getDatabase()->find( + $documents = $this->getDatabase()->find( $collection->getId() ); @@ -12625,28 +12634,28 @@ public function testCollectionPermissionsRelationshipsCountWorks(array $data): v { [$collection, $collectionOneToOne, $collectionOneToMany, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::users()->toString()); - $documents = static::getDatabase()->count( + $documents = $this->getDatabase()->count( $collection->getId() ); $this->assertEquals(1, $documents); - Authorization::cleanRoles(); - Authorization::setRole(Role::user('random')->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::user('random')->toString()); - $documents = static::getDatabase()->count( + $documents = $this->getDatabase()->count( $collection->getId() ); $this->assertEquals(1, $documents); - Authorization::cleanRoles(); - Authorization::setRole(Role::user('unknown')->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::user('unknown')->toString()); - $documents = static::getDatabase()->count( + $documents = $this->getDatabase()->count( $collection->getId() ); @@ -12662,9 +12671,9 @@ public function testCollectionPermissionsRelationshipsUpdateWorks(array $data): { [$collection, $collectionOneToOne, $collectionOneToMany, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); - static::getDatabase()->updateDocument( + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::users()->toString()); + $this->getDatabase()->updateDocument( $collection->getId(), $document->getId(), $document @@ -12672,10 +12681,10 @@ public function testCollectionPermissionsRelationshipsUpdateWorks(array $data): $this->assertTrue(true); - Authorization::cleanRoles(); - Authorization::setRole(Role::user('random')->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::user('random')->toString()); - static::getDatabase()->updateDocument( + $this->getDatabase()->updateDocument( $collection->getId(), $document->getId(), $document->setAttribute('test', 'ipsum') @@ -12694,11 +12703,11 @@ public function testCollectionPermissionsRelationshipsUpdateThrowsException(arra { [$collection, $collectionOneToOne, $collectionOneToMany, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::any()->toString()); $this->expectException(AuthorizationException::class); - $document = static::getDatabase()->updateDocument( + $document = $this->getDatabase()->updateDocument( $collection->getId(), $document->getId(), $document->setAttribute('test', $document->getAttribute('test').'new_value') @@ -12713,11 +12722,11 @@ public function testCollectionPermissionsRelationshipsDeleteThrowsException(arra { [$collection, $collectionOneToOne, $collectionOneToMany, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::any()->toString()); $this->expectException(AuthorizationException::class); - $document = static::getDatabase()->deleteDocument( + $document = $this->getDatabase()->deleteDocument( $collection->getId(), $document->getId() ); @@ -12731,10 +12740,10 @@ public function testCollectionPermissionsRelationshipsDeleteWorks(array $data): { [$collection, $collectionOneToOne, $collectionOneToMany, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::users()->toString()); - $this->assertTrue(static::getDatabase()->deleteDocument( + $this->assertTrue($this->getDatabase()->deleteDocument( $collection->getId(), $document->getId() )); @@ -12742,28 +12751,28 @@ public function testCollectionPermissionsRelationshipsDeleteWorks(array $data): public function testCreateRelationDocumentWithoutUpdatePermission(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - Authorization::cleanRoles(); - Authorization::setRole(Role::user('a')->toString()); + self::$authorization->cleanRoles(); + self::$authorization->addRole(Role::user('a')->toString()); - static::getDatabase()->createCollection('parentRelationTest', [], [], [ + $this->getDatabase()->createCollection('parentRelationTest', [], [], [ Permission::read(Role::user('a')), Permission::create(Role::user('a')), Permission::update(Role::user('a')), Permission::delete(Role::user('a')) ]); - static::getDatabase()->createCollection('childRelationTest', [], [], [ + $this->getDatabase()->createCollection('childRelationTest', [], [], [ Permission::create(Role::user('a')), Permission::read(Role::user('a')), ]); - static::getDatabase()->createAttribute('parentRelationTest', 'name', Database::VAR_STRING, 255, false); - static::getDatabase()->createAttribute('childRelationTest', 'name', Database::VAR_STRING, 255, false); + $this->getDatabase()->createAttribute('parentRelationTest', 'name', Database::VAR_STRING, 255, false); + $this->getDatabase()->createAttribute('childRelationTest', 'name', Database::VAR_STRING, 255, false); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'parentRelationTest', relatedCollection: 'childRelationTest', type: Database::RELATION_ONE_TO_MANY, @@ -12771,7 +12780,7 @@ public function testCreateRelationDocumentWithoutUpdatePermission(): void ); // Create document with relationship with nested data - $parent = static::getDatabase()->createDocument('parentRelationTest', new Document([ + $parent = $this->getDatabase()->createDocument('parentRelationTest', new Document([ '$id' => 'parent1', 'name' => 'Parent 1', 'children' => [ @@ -12787,21 +12796,21 @@ public function testCreateRelationDocumentWithoutUpdatePermission(): void '$id' => 'child2', ], ]); - $updatedParent = static::getDatabase()->updateDocument('parentRelationTest', 'parent1', $parent); + $updatedParent = $this->getDatabase()->updateDocument('parentRelationTest', 'parent1', $parent); $this->assertEquals('child2', $updatedParent->getAttribute('children')[0]->getId()); - static::getDatabase()->deleteCollection('parentRelationTest'); - static::getDatabase()->deleteCollection('childRelationTest'); + $this->getDatabase()->deleteCollection('parentRelationTest'); + $this->getDatabase()->deleteCollection('childRelationTest'); } public function testUpdateDocumentWithRelationships(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!$this->getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection('userProfiles', [ + $this->getDatabase()->createCollection('userProfiles', [ new Document([ '$id' => ID::custom('username'), 'type' => Database::VAR_STRING, @@ -12819,7 +12828,7 @@ public function testUpdateDocumentWithRelationships(): void Permission::update(Role::any()), Permission::delete(Role::any()) ]); - static::getDatabase()->createCollection('links', [ + $this->getDatabase()->createCollection('links', [ new Document([ '$id' => ID::custom('title'), 'type' => Database::VAR_STRING, @@ -12837,7 +12846,7 @@ public function testUpdateDocumentWithRelationships(): void Permission::update(Role::any()), Permission::delete(Role::any()) ]); - static::getDatabase()->createCollection('videos', [ + $this->getDatabase()->createCollection('videos', [ new Document([ '$id' => ID::custom('title'), 'type' => Database::VAR_STRING, @@ -12855,7 +12864,7 @@ public function testUpdateDocumentWithRelationships(): void Permission::update(Role::any()), Permission::delete(Role::any()) ]); - static::getDatabase()->createCollection('products', [ + $this->getDatabase()->createCollection('products', [ new Document([ '$id' => ID::custom('title'), 'type' => Database::VAR_STRING, @@ -12873,7 +12882,7 @@ public function testUpdateDocumentWithRelationships(): void Permission::update(Role::any()), Permission::delete(Role::any()) ]); - static::getDatabase()->createCollection('settings', [ + $this->getDatabase()->createCollection('settings', [ new Document([ '$id' => ID::custom('metaTitle'), 'type' => Database::VAR_STRING, @@ -12891,7 +12900,7 @@ public function testUpdateDocumentWithRelationships(): void Permission::update(Role::any()), Permission::delete(Role::any()) ]); - static::getDatabase()->createCollection('appearance', [ + $this->getDatabase()->createCollection('appearance', [ new Document([ '$id' => ID::custom('metaTitle'), 'type' => Database::VAR_STRING, @@ -12909,7 +12918,7 @@ public function testUpdateDocumentWithRelationships(): void Permission::update(Role::any()), Permission::delete(Role::any()) ]); - static::getDatabase()->createCollection('group', [ + $this->getDatabase()->createCollection('group', [ new Document([ '$id' => ID::custom('name'), 'type' => Database::VAR_STRING, @@ -12927,7 +12936,7 @@ public function testUpdateDocumentWithRelationships(): void Permission::update(Role::any()), Permission::delete(Role::any()) ]); - static::getDatabase()->createCollection('community', [ + $this->getDatabase()->createCollection('community', [ new Document([ '$id' => ID::custom('name'), 'type' => Database::VAR_STRING, @@ -12946,21 +12955,21 @@ public function testUpdateDocumentWithRelationships(): void Permission::delete(Role::any()) ]); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'userProfiles', relatedCollection: 'links', type: Database::RELATION_ONE_TO_MANY, id: 'links' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'userProfiles', relatedCollection: 'videos', type: Database::RELATION_ONE_TO_MANY, id: 'videos' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'userProfiles', relatedCollection: 'products', type: Database::RELATION_ONE_TO_MANY, @@ -12969,35 +12978,35 @@ public function testUpdateDocumentWithRelationships(): void twoWayKey: 'userProfile', ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'userProfiles', relatedCollection: 'settings', type: Database::RELATION_ONE_TO_ONE, id: 'settings' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'userProfiles', relatedCollection: 'appearance', type: Database::RELATION_ONE_TO_ONE, id: 'appearance' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'userProfiles', relatedCollection: 'group', type: Database::RELATION_MANY_TO_ONE, id: 'group' ); - static::getDatabase()->createRelationship( + $this->getDatabase()->createRelationship( collection: 'userProfiles', relatedCollection: 'community', type: Database::RELATION_MANY_TO_ONE, id: 'community' ); - $profile = static::getDatabase()->createDocument('userProfiles', new Document([ + $profile = $this->getDatabase()->createDocument('userProfiles', new Document([ '$id' => '1', 'username' => 'user1', 'links' => [ @@ -13060,7 +13069,7 @@ public function testUpdateDocumentWithRelationships(): void 'name' => 'New Group Name', ]); - $updatedProfile = static::getDatabase()->updateDocument('userProfiles', '1', $profile); + $updatedProfile = $this->getDatabase()->updateDocument('userProfiles', '1', $profile); $this->assertEquals('New Link Value', $updatedProfile->getAttribute('links')[0]->getAttribute('title')); $this->assertEquals('New Meta Title', $updatedProfile->getAttribute('settings')->getAttribute('metaTitle')); @@ -13073,35 +13082,35 @@ public function testUpdateDocumentWithRelationships(): void $this->assertEquals('Community 1', $updatedProfile->getAttribute('community')->getAttribute('name')); // updating document using two way key in one to many relationship - $product = static::getDatabase()->getDocument('products', 'product1'); + $product = $this->getDatabase()->getDocument('products', 'product1'); $product->setAttribute('userProfile', [ '$id' => '1', 'username' => 'updated user value', ]); - $updatedProduct = static::getDatabase()->updateDocument('products', 'product1', $product); + $updatedProduct = $this->getDatabase()->updateDocument('products', 'product1', $product); $this->assertEquals('updated user value', $updatedProduct->getAttribute('userProfile')->getAttribute('username')); $this->assertEquals('Product 1', $updatedProduct->getAttribute('title')); $this->assertEquals('product1', $updatedProduct->getId()); $this->assertEquals('1', $updatedProduct->getAttribute('userProfile')->getId()); - static::getDatabase()->deleteCollection('userProfiles'); - static::getDatabase()->deleteCollection('links'); - static::getDatabase()->deleteCollection('settings'); - static::getDatabase()->deleteCollection('group'); - static::getDatabase()->deleteCollection('community'); - static::getDatabase()->deleteCollection('videos'); - static::getDatabase()->deleteCollection('products'); - static::getDatabase()->deleteCollection('appearance'); + $this->getDatabase()->deleteCollection('userProfiles'); + $this->getDatabase()->deleteCollection('links'); + $this->getDatabase()->deleteCollection('settings'); + $this->getDatabase()->deleteCollection('group'); + $this->getDatabase()->deleteCollection('community'); + $this->getDatabase()->deleteCollection('videos'); + $this->getDatabase()->deleteCollection('products'); + $this->getDatabase()->deleteCollection('appearance'); } public function testLabels(): void { - $this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createCollection( + $this->assertInstanceOf('Utopia\Database\Document', $this->getDatabase()->createCollection( 'labels_test', )); - static::getDatabase()->createAttribute('labels_test', 'attr1', Database::VAR_STRING, 10, false); + $this->getDatabase()->createAttribute('labels_test', 'attr1', Database::VAR_STRING, 10, false); - static::getDatabase()->createDocument('labels_test', new Document([ + $this->getDatabase()->createDocument('labels_test', new Document([ '$id' => 'doc1', 'attr1' => 'value1', '$permissions' => [ @@ -13109,20 +13118,20 @@ public function testLabels(): void ], ])); - $documents = static::getDatabase()->find('labels_test'); + $documents = $this->getDatabase()->find('labels_test'); $this->assertEmpty($documents); - Authorization::setRole(Role::label('reader')->toString()); + self::$authorization->addRole(Role::label('reader')->toString()); - $documents = static::getDatabase()->find('labels_test'); + $documents = $this->getDatabase()->find('labels_test'); $this->assertCount(1, $documents); } public function testEnableDisableValidation(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('validation', permissions: [ Permission::create(Role::any()), @@ -13173,21 +13182,21 @@ public function testEnableDisableValidation(): void public function testMetadata(): void { - static::getDatabase()->setMetadata('key', 'value'); + $this->getDatabase()->setMetadata('key', 'value'); - static::getDatabase()->createCollection('testers'); + $this->getDatabase()->createCollection('testers'); - $this->assertEquals(['key' => 'value'], static::getDatabase()->getMetadata()); + $this->assertEquals(['key' => 'value'], $this->getDatabase()->getMetadata()); - static::getDatabase()->resetMetadata(); + $this->getDatabase()->resetMetadata(); - $this->assertEquals([], static::getDatabase()->getMetadata()); + $this->assertEquals([], $this->getDatabase()->getMetadata()); } public function testEmptyOperatorValues(): void { try { - static::getDatabase()->findOne('documents', [ + $this->getDatabase()->findOne('documents', [ Query::equal('string', []), ]); $this->fail('Failed to throw exception'); @@ -13197,7 +13206,7 @@ public function testEmptyOperatorValues(): void } try { - static::getDatabase()->findOne('documents', [ + $this->getDatabase()->findOne('documents', [ Query::contains('string', []), ]); $this->fail('Failed to throw exception'); @@ -13221,7 +13230,7 @@ public function testIsolationModes(): void /** * Default mode already tested, we'll test 'schema' and 'table' isolation here */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->exists('schema1')) { $database->setDatabase('schema1')->delete(); @@ -13363,7 +13372,7 @@ public function testIsolationModes(): void public function testTransformations(): void { - static::getDatabase()->createCollection('docs', attributes: [ + $this->getDatabase()->createCollection('docs', attributes: [ new Document([ '$id' => 'name', 'type' => Database::VAR_STRING, @@ -13372,24 +13381,24 @@ public function testTransformations(): void ]) ]); - static::getDatabase()->createDocument('docs', new Document([ + $this->getDatabase()->createDocument('docs', new Document([ '$id' => 'doc1', 'name' => 'value1', ])); - static::getDatabase()->before(Database::EVENT_DOCUMENT_READ, 'test', function (string $query) { + $this->getDatabase()->before(Database::EVENT_DOCUMENT_READ, 'test', function (string $query) { return "SELECT 1"; }); - $result = static::getDatabase()->getDocument('docs', 'doc1'); + $result = $this->getDatabase()->getDocument('docs', 'doc1'); $this->assertTrue($result->isEmpty()); } public function testEvents(): void { - Authorization::skip(function () { - $database = static::getDatabase(); + self::$authorization->skip(function () { + $database = $this->getDatabase(); $events = [ Database::EVENT_DATABASE_CREATE, diff --git a/tests/e2e/Adapter/MariaDBTest.php b/tests/e2e/Adapter/MariaDBTest.php index e26ca69c2..5d7267ed5 100644 --- a/tests/e2e/Adapter/MariaDBTest.php +++ b/tests/e2e/Adapter/MariaDBTest.php @@ -28,7 +28,7 @@ public static function getAdapterName(): string /** * @return Database */ - public static function getDatabase(bool $fresh = false): Database + public function getDatabase(bool $fresh = false): Database { if (!is_null(self::$database) && !$fresh) { return self::$database; @@ -45,7 +45,8 @@ public static function getDatabase(bool $fresh = false): Database $redis->flushAll(); $cache = new Cache(new RedisAdapter($redis)); - $database = new Database(new MariaDB($pdo), $cache); + $database = new Database(new MariaDB($pdo), $cache, []); + $database->setAuthorization(self::$authorization); $database->setDatabase('utopiaTests'); $database->setNamespace(static::$namespace = 'myapp_' . uniqid()); diff --git a/tests/e2e/Adapter/MongoDBTest.php b/tests/e2e/Adapter/MongoDBTest.php index 887c26ec2..11db0c4bd 100644 --- a/tests/e2e/Adapter/MongoDBTest.php +++ b/tests/e2e/Adapter/MongoDBTest.php @@ -29,7 +29,7 @@ public static function getAdapterName(): string * @return Database * @throws Exception */ - public static function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -50,7 +50,8 @@ public static function getDatabase(): Database false ); - $database = new Database(new Mongo($client), $cache); + $database = new Database(new Mongo($client), $cache, []); + $database->setAuthorization(self::$authorization); $database->setDatabase($schema); $database->setNamespace(static::$namespace = 'myapp_' . uniqid()); @@ -69,10 +70,10 @@ public static function getDatabase(): Database public function testCreateExistsDelete(): void { // Mongo creates databases on the fly, so exists would always pass. So we override this test to remove the exists check. - $this->assertNotNull(static::getDatabase()->create()); - $this->assertEquals(true, static::getDatabase()->delete($this->testDatabase)); - $this->assertEquals(true, static::getDatabase()->create()); - $this->assertEquals(static::getDatabase(), static::getDatabase()->setDatabase($this->testDatabase)); + $this->assertNotNull($this->getDatabase()->create()); + $this->assertEquals(true, $this->getDatabase()->delete($this->testDatabase)); + $this->assertEquals(true, $this->getDatabase()->create()); + $this->assertEquals($this->getDatabase(), $this->getDatabase()->setDatabase($this->testDatabase)); } public function testRenameAttribute(): void diff --git a/tests/e2e/Adapter/MySQLTest.php b/tests/e2e/Adapter/MySQLTest.php index d204e8a40..7c63a43ab 100644 --- a/tests/e2e/Adapter/MySQLTest.php +++ b/tests/e2e/Adapter/MySQLTest.php @@ -28,7 +28,7 @@ public static function getAdapterName(): string /** * @return Database */ - public static function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -47,7 +47,8 @@ public static function getDatabase(): Database $cache = new Cache(new RedisAdapter($redis)); - $database = new Database(new MySQL($pdo), $cache); + $database = new Database(new MySQL($pdo), $cache, []); + $database->setAuthorization(self::$authorization); $database->setDatabase('utopiaTests'); $database->setNamespace(static::$namespace = 'myapp_' . uniqid()); diff --git a/tests/e2e/Adapter/PostgresTest.php b/tests/e2e/Adapter/PostgresTest.php index 6a12ecd8c..2f87a5863 100644 --- a/tests/e2e/Adapter/PostgresTest.php +++ b/tests/e2e/Adapter/PostgresTest.php @@ -27,7 +27,7 @@ public static function getAdapterName(): string /** * @reture Adapter */ - public static function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -44,7 +44,8 @@ public static function getDatabase(): Database $redis->flushAll(); $cache = new Cache(new RedisAdapter($redis)); - $database = new Database(new Postgres($pdo), $cache); + $database = new Database(new Postgres($pdo), $cache, []); + $database->setAuthorization(self::$authorization); $database->setDatabase('utopiaTests'); $database->setNamespace(static::$namespace = 'myapp_' . uniqid()); diff --git a/tests/e2e/Adapter/SQLiteTest.php b/tests/e2e/Adapter/SQLiteTest.php index 16c36f6fd..00d24689d 100644 --- a/tests/e2e/Adapter/SQLiteTest.php +++ b/tests/e2e/Adapter/SQLiteTest.php @@ -28,7 +28,7 @@ public static function getAdapterName(): string /** * @return Database */ - public static function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -50,7 +50,8 @@ public static function getDatabase(): Database $cache = new Cache(new RedisAdapter($redis)); - $database = new Database(new SQLite($pdo), $cache); + $database = new Database(new SQLite($pdo), $cache, []); + $database->setAuthorization(self::$authorization); $database->setDatabase('utopiaTests'); $database->setNamespace(static::$namespace = 'myapp_' . uniqid()); diff --git a/tests/unit/Validator/AuthorizationTest.php b/tests/unit/Validator/AuthorizationTest.php index 08c1e46f2..37e5a6e22 100644 --- a/tests/unit/Validator/AuthorizationTest.php +++ b/tests/unit/Validator/AuthorizationTest.php @@ -9,11 +9,15 @@ use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; use Utopia\Database\Validator\Authorization; +use Utopia\Database\Validator\Authorization\Input; class AuthorizationTest extends TestCase { + protected Authorization $authorization; + public function setUp(): void { + $this->authorization = new Authorization(); } public function tearDown(): void @@ -22,7 +26,7 @@ public function tearDown(): void public function testValues(): void { - Authorization::setRole(Role::any()->toString()); + $this->authorization->addRole(Role::any()->toString()); $document = new Document([ '$id' => ID::unique(), @@ -35,88 +39,88 @@ public function testValues(): void Permission::delete(Role::any()), ], ]); - $object = new Authorization(Database::PERMISSION_READ); - $this->assertEquals($object->isValid($document->getRead()), false); - $this->assertEquals($object->isValid(''), false); - $this->assertEquals($object->isValid([]), false); + $object = $this->authorization; + + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), false); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, [])), false); $this->assertEquals($object->getDescription(), 'No permissions provided for action \'read\''); - Authorization::setRole(Role::user('456')->toString()); - Authorization::setRole(Role::user('123')->toString()); + $this->authorization->addRole(Role::user('456')->toString()); + $this->authorization->addRole(Role::user('123')->toString()); - $this->assertEquals(Authorization::isRole(Role::user('456')->toString()), true); - $this->assertEquals(Authorization::isRole(Role::user('457')->toString()), false); - $this->assertEquals(Authorization::isRole(''), false); - $this->assertEquals(Authorization::isRole(Role::any()->toString()), true); + $this->assertEquals($this->authorization->isRole(Role::user('456')->toString()), true); + $this->assertEquals($this->authorization->isRole(Role::user('457')->toString()), false); + $this->assertEquals($this->authorization->isRole(''), false); + $this->assertEquals($this->authorization->isRole(Role::any()->toString()), true); - $this->assertEquals($object->isValid($document->getRead()), true); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), true); - Authorization::cleanRoles(); + $this->authorization->cleanRoles(); - $this->assertEquals($object->isValid($document->getRead()), false); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), false); - Authorization::setRole(Role::team('123')->toString()); + $this->authorization->addRole(Role::team('123')->toString()); - $this->assertEquals($object->isValid($document->getRead()), true); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), true); - Authorization::cleanRoles(); - Authorization::disable(); + $this->authorization->cleanRoles(); + $this->authorization->disable(); - $this->assertEquals($object->isValid($document->getRead()), true); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), true); - Authorization::reset(); + $this->authorization->reset(); - $this->assertEquals($object->isValid($document->getRead()), false); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), false); - Authorization::setDefaultStatus(false); - Authorization::disable(); + $this->authorization->setDefaultStatus(false); + $this->authorization->disable(); - $this->assertEquals($object->isValid($document->getRead()), true); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), true); - Authorization::reset(); + $this->authorization->reset(); - $this->assertEquals($object->isValid($document->getRead()), true); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), true); - Authorization::enable(); + $this->authorization->enable(); - $this->assertEquals($object->isValid($document->getRead()), false); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), false); - Authorization::setRole('textX'); + $this->authorization->addRole('textX'); - $this->assertContains('textX', Authorization::getRoles()); + $this->assertContains('textX', $this->authorization->getRoles()); - Authorization::unsetRole('textX'); + $this->authorization->removeRole('textX'); - $this->assertNotContains('textX', Authorization::getRoles()); + $this->assertNotContains('textX', $this->authorization->getRoles()); // Test skip method - $this->assertEquals($object->isValid($document->getRead()), false); - $this->assertEquals(Authorization::skip(function () use ($object, $document) { - return $object->isValid($document->getRead()); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), false); + $this->assertEquals($this->authorization->skip(function () use ($object, $document) { + return $object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())); }), true); } public function testNestedSkips(): void { - $this->assertEquals(true, Authorization::$status); + $this->assertEquals(true, $this->authorization->getStatus()); - Authorization::skip(function () { - $this->assertEquals(false, Authorization::$status); + $this->authorization->skip(function () { + $this->assertEquals(false, $this->authorization->getStatus()); - Authorization::skip(function () { - $this->assertEquals(false, Authorization::$status); + $this->authorization->skip(function () { + $this->assertEquals(false, $this->authorization->getStatus()); - Authorization::skip(function () { - $this->assertEquals(false, Authorization::$status); + $this->authorization->skip(function () { + $this->assertEquals(false, $this->authorization->getStatus()); }); - $this->assertEquals(false, Authorization::$status); + $this->assertEquals(false, $this->authorization->getStatus()); }); - $this->assertEquals(false, Authorization::$status); + $this->assertEquals(false, $this->authorization->getStatus()); }); - $this->assertEquals(true, Authorization::$status); + $this->assertEquals(true, $this->authorization->getStatus()); } }