Skip to content

Commit 8007d6f

Browse files
committed
Update PHPUnit configuration to stop on failure and refactor authorization handling in Database class to use instance methods. Adjust tests to reflect changes in method signatures.
1 parent de79b67 commit 8007d6f

File tree

17 files changed

+98
-100
lines changed

17 files changed

+98
-100
lines changed

src/Database/Database.php

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4333,9 +4333,8 @@ public function createDocuments(
43334333
$batchSize = \min(Database::INSERT_BATCH_SIZE, \max(1, $batchSize));
43344334
$collection = $this->silent(fn () => $this->getCollection($collection));
43354335
if ($collection->getId() !== self::METADATA) {
4336-
$authorization = new Authorization(self::PERMISSION_CREATE);
4337-
if (!$authorization->isValid($collection->getCreate())) {
4338-
throw new AuthorizationException($authorization->getDescription());
4336+
if (!$this->authorization->isValid(new Input(self::PERMISSION_CREATE, $collection->getCreate()))) {
4337+
throw new AuthorizationException($this->authorization->getDescription());
43394338
}
43404339
}
43414340

@@ -4886,26 +4885,31 @@ public function updateDocument(string $collection, string $id, Document $documen
48864885
break;
48874886
}
48884887
}
4889-
}
4890-
4891-
if ($shouldUpdate) {
48924888

4893-
$isUpdateValid = $this->authorization->isValid(new Input(self::PERMISSION_UPDATE, [
4894-
...$collection->getUpdate(),
4895-
...($documentSecurity ? $old->getUpdate() : [])
4896-
]));
4889+
$updatePermissions = array_merge(
4890+
$collection->getUpdate(),
4891+
$documentSecurity ? $old->getUpdate() : []
4892+
);
48974893

4898-
$isReadValid = $this->authorization->isValid(new Input(self::PERMISSION_READ, [
4899-
...$collection->getRead(),
4900-
...($documentSecurity ? $old->getRead() : [])
4901-
]));
4894+
$readPermissions = array_merge(
4895+
$collection->getRead(),
4896+
$documentSecurity ? $old->getRead() : []
4897+
);
49024898

4903-
if (!$isReadValid || !$isUpdateValid) {
4904-
throw new AuthorizationException($this->authorization->getDescription());
4899+
if ($shouldUpdate) {
4900+
if(!$this->authorization->isValid(new Input(self::PERMISSION_UPDATE, $updatePermissions))) {
4901+
throw new AuthorizationException($this->authorization->getDescription());
4902+
}
4903+
} else {
4904+
if(!$this->authorization->isValid(new Input(self::PERMISSION_READ, $readPermissions))){
4905+
throw new AuthorizationException($this->authorization->getDescription());
4906+
}
49054907
}
4906-
4907-
$document->setAttribute('$updatedAt', ($newUpdatedAt === null || !$this->preserveDates) ? $time : $newUpdatedAt);
49084908
}
4909+
4910+
if ($shouldUpdate) {
4911+
$document->setAttribute('$updatedAt', ($newUpdatedAt === null || !$this->preserveDates) ? $time : $newUpdatedAt);
4912+
}
49094913

49104914
// Check if document was updated after the request timestamp
49114915
$oldUpdatedAt = new \DateTime($old->getUpdatedAt());
@@ -4991,11 +4995,10 @@ public function updateDocuments(
49914995
}
49924996

49934997
$documentSecurity = $collection->getAttribute('documentSecurity', false);
4994-
$authorization = new Authorization(self::PERMISSION_UPDATE);
4995-
$skipAuth = $authorization->isValid($collection->getUpdate());
4998+
$skipAuth = $this->authorization->isValid(new Input(self::PERMISSION_UPDATE, $collection->getUpdate()));
49964999

49975000
if (!$skipAuth && !$documentSecurity && $collection->getId() !== self::METADATA) {
4998-
throw new AuthorizationException($authorization->getDescription());
5001+
throw new AuthorizationException($this->authorization->getDescription());
49995002
}
50005003

50015004
$attributes = $collection->getAttribute('attributes', []);
@@ -6070,7 +6073,7 @@ public function deleteDocument(string $collection, string $id): bool
60706073
if ($collection->getId() !== self::METADATA) {
60716074
$documentSecurity = $collection->getAttribute('documentSecurity', false);
60726075

6073-
if (!$this->authorization->isValid(new Input([
6076+
if (!$this->authorization->isValid(new Input(self::PERMISSION_DELETE, [
60746077
...$collection->getDelete(),
60756078
...($documentSecurity ? $document->getDelete() : [])
60766079
]))) {
@@ -6522,11 +6525,10 @@ public function deleteDocuments(
65226525
}
65236526

65246527
$documentSecurity = $collection->getAttribute('documentSecurity', false);
6525-
$authorization = new Authorization(self::PERMISSION_DELETE);
6526-
$skipAuth = $authorization->isValid($collection->getDelete());
6528+
$skipAuth = $this->authorization->isValid(new Input(self::PERMISSION_DELETE, $collection->getDelete()));
65276529

65286530
if (!$skipAuth && !$documentSecurity && $collection->getId() !== self::METADATA) {
6529-
throw new AuthorizationException($authorization->getDescription());
6531+
throw new AuthorizationException($this->authorization->getDescription());
65306532
}
65316533

65326534
$attributes = $collection->getAttribute('attributes', []);
@@ -6745,12 +6747,12 @@ public function find(string $collection, array $queries = [], string $forPermiss
67456747
}
67466748
}
67476749

6748-
$authorization = new Authorization($forPermission);
67496750
$documentSecurity = $collection->getAttribute('documentSecurity', false);
6750-
$skipAuth = $authorization->isValid($collection->getPermissionsByType($forPermission));
6751+
$skipAuth = $this->authorization->isValid(new Input($forPermission, $collection->getPermissionsByType($forPermission)));
6752+
67516753

67526754
if (!$skipAuth && !$documentSecurity && $collection->getId() !== self::METADATA) {
6753-
throw new AuthorizationException($authorization->getDescription());
6755+
throw new AuthorizationException($this->authorization->getDescription());
67546756
}
67556757

67566758
$relationships = \array_filter(
@@ -6825,7 +6827,7 @@ public function find(string $collection, array $queries = [], string $forPermiss
68256827
$cursorDirection,
68266828
$forPermission
68276829
);
6828-
6830+
68296831
$results = $skipAuth ? $this->authorization->skip($getResults) : $getResults();
68306832
}
68316833

@@ -6968,11 +6970,7 @@ public function count(string $collection, array $queries = [], ?int $max = null)
69686970
}
69696971
}
69706972

6971-
$authorization = new Authorization(self::PERMISSION_READ);
6972-
if ($authorization->isValid($collection->getRead())) {
6973-
$skipAuth = true;
6974-
}
6975-
6973+
$skipAuth = $this->authorization->isValid(new Input(self::PERMISSION_READ, $collection->getRead()));
69766974
$relationships = \array_filter(
69776975
$collection->getAttribute('attributes', []),
69786976
fn (Document $attribute) => $attribute->getAttribute('type') === self::VAR_RELATIONSHIP

src/Database/Mirror.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,8 +1083,8 @@ protected function getUpgradeStatus(string $collection): ?Document
10831083
if ($collection === 'upgrades' || $collection === Database::METADATA) {
10841084
return new Document();
10851085
}
1086-
1087-
return $this->getAuthorization()->skip(function () use ($collection) {
1086+
1087+
return $this->getSource()->getAuthorization()->skip(function () use ($collection) {
10881088
try {
10891089
return $this->source->getDocument('upgrades', $collection);
10901090
} catch (\Throwable) {

tests/e2e/Adapter/Base.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ abstract protected function getDatabase(): Database;
4545
*
4646
* @return bool
4747
*/
48-
abstract protected static function deleteColumn(string $collection, string $column): bool;
48+
abstract protected function deleteColumn(string $collection, string $column): bool;
4949

5050
/**
5151
* @param string $collection
5252
* @param string $index
5353
*
5454
* @return bool
5555
*/
56-
abstract protected static function deleteIndex(string $collection, string $index): bool;
56+
abstract protected function deleteIndex(string $collection, string $index): bool;
5757

5858
public function setUp(): void
5959
{

tests/e2e/Adapter/MariaDBTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MariaDBTest extends Base
1818
/**
1919
* @return Database
2020
*/
21-
public function getDatabase(bool $fresh = false): Database
21+
public function getDatabase(bool $fresh = false): Database
2222
{
2323
if (!is_null(self::$database) && !$fresh) {
2424
return self::$database;
@@ -52,19 +52,19 @@ public function getDatabase(bool $fresh = false): Database
5252
return self::$database = $database;
5353
}
5454

55-
protected static function deleteColumn(string $collection, string $column): bool
55+
protected function deleteColumn(string $collection, string $column): bool
5656
{
57-
$sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`";
57+
$sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`";
5858
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";
5959

6060
self::$pdo->exec($sql);
6161

6262
return true;
6363
}
6464

65-
protected static function deleteIndex(string $collection, string $index): bool
65+
protected function deleteIndex(string $collection, string $index): bool
6666
{
67-
$sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`";
67+
$sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`";
6868
$sql = "DROP INDEX `{$index}` ON {$sqlTable}";
6969

7070
self::$pdo->exec($sql);

tests/e2e/Adapter/MirrorTest.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class MirrorTest extends Base
3333
* @throws \RedisException
3434
* @throws Exception
3535
*/
36-
protected function getDatabase(bool $fresh = false): Mirror
36+
protected function getDatabase(bool $fresh = false): Mirror
3737
{
3838
if (!is_null(self::$database) && !$fresh) {
3939
return self::$database;
@@ -95,6 +95,7 @@ protected function getDatabase(bool $fresh = false): Mirror
9595

9696
$database
9797
->setDatabase('utopiaTests')
98+
->setAuthorization(self::$authorization)
9899
->setNamespace(static::$namespace = 'myapp_' . uniqid());
99100

100101
$database->create();
@@ -108,7 +109,7 @@ protected function getDatabase(bool $fresh = false): Mirror
108109
*/
109110
public function testGetMirrorSource(): void
110111
{
111-
$database = self::getDatabase();
112+
$database = $this->getDatabase();
112113
$source = $database->getSource();
113114
$this->assertInstanceOf(Database::class, $source);
114115
$this->assertEquals(self::$source, $source);
@@ -120,7 +121,7 @@ public function testGetMirrorSource(): void
120121
*/
121122
public function testGetMirrorDestination(): void
122123
{
123-
$database = self::getDatabase();
124+
$database = $this->getDatabase();
124125
$destination = $database->getDestination();
125126
$this->assertInstanceOf(Database::class, $destination);
126127
$this->assertEquals(self::$destination, $destination);
@@ -134,7 +135,7 @@ public function testGetMirrorDestination(): void
134135
*/
135136
public function testCreateMirroredCollection(): void
136137
{
137-
$database = self::getDatabase();
138+
$database = $this->getDatabase();
138139

139140
$database->createCollection('testCreateMirroredCollection');
140141

@@ -152,7 +153,7 @@ public function testCreateMirroredCollection(): void
152153
*/
153154
public function testUpdateMirroredCollection(): void
154155
{
155-
$database = self::getDatabase();
156+
$database = $this->getDatabase();
156157

157158
$database->createCollection('testUpdateMirroredCollection', permissions: [
158159
Permission::read(Role::any()),
@@ -182,7 +183,7 @@ public function testUpdateMirroredCollection(): void
182183

183184
public function testDeleteMirroredCollection(): void
184185
{
185-
$database = self::getDatabase();
186+
$database = $this->getDatabase();
186187

187188
$database->createCollection('testDeleteMirroredCollection');
188189

@@ -203,7 +204,7 @@ public function testDeleteMirroredCollection(): void
203204
*/
204205
public function testCreateMirroredDocument(): void
205206
{
206-
$database = self::getDatabase();
207+
$database = $this->getDatabase();
207208

208209
$database->createCollection('testCreateMirroredDocument', attributes: [
209210
new Document([
@@ -245,7 +246,7 @@ public function testCreateMirroredDocument(): void
245246
*/
246247
public function testUpdateMirroredDocument(): void
247248
{
248-
$database = self::getDatabase();
249+
$database = $this->getDatabase();
249250

250251
$database->createCollection('testUpdateMirroredDocument', attributes: [
251252
new Document([
@@ -285,7 +286,7 @@ public function testUpdateMirroredDocument(): void
285286

286287
public function testDeleteMirroredDocument(): void
287288
{
288-
$database = self::getDatabase();
289+
$database = $this->getDatabase();
289290

290291
$database->createCollection('testDeleteMirroredDocument', attributes: [
291292
new Document([
@@ -312,7 +313,7 @@ public function testDeleteMirroredDocument(): void
312313
$this->assertTrue($database->getDestination()->getDocument('testDeleteMirroredDocument', $document->getId())->isEmpty());
313314
}
314315

315-
protected static function deleteColumn(string $collection, string $column): bool
316+
protected function deleteColumn(string $collection, string $column): bool
316317
{
317318
$sqlTable = "`" . self::$source->getDatabase() . "`.`" . self::$source->getNamespace() . "_" . $collection . "`";
318319
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";
@@ -327,7 +328,7 @@ protected static function deleteColumn(string $collection, string $column): bool
327328
return true;
328329
}
329330

330-
protected static function deleteIndex(string $collection, string $index): bool
331+
protected function deleteIndex(string $collection, string $index): bool
331332
{
332333
$sqlTable = "`" . self::$source->getDatabase() . "`.`" . self::$source->getNamespace() . "_" . $collection . "`";
333334
$sql = "DROP INDEX `{$index}` ON {$sqlTable}";

tests/e2e/Adapter/MySQLTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MySQLTest extends Base
2424
* @throws Exception
2525
* @throws Limit
2626
*/
27-
public function getDatabase(): Database
27+
public function getDatabase(): Database
2828
{
2929
if (!is_null(self::$database)) {
3030
return self::$database;
@@ -58,19 +58,19 @@ public function getDatabase(): Database
5858
return self::$database = $database;
5959
}
6060

61-
protected static function deleteColumn(string $collection, string $column): bool
61+
protected function deleteColumn(string $collection, string $column): bool
6262
{
63-
$sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`";
63+
$sqlTable = "`" . $this->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`";
6464
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";
6565

6666
self::$pdo->exec($sql);
6767

6868
return true;
6969
}
7070

71-
protected static function deleteIndex(string $collection, string $index): bool
71+
protected function deleteIndex(string $collection, string $index): bool
7272
{
73-
$sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`";
73+
$sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`";
7474
$sql = "DROP INDEX `{$index}` ON {$sqlTable}";
7575

7676
self::$pdo->exec($sql);

tests/e2e/Adapter/PoolTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PoolTest extends Base
3232
* @throws Duplicate
3333
* @throws Limit
3434
*/
35-
public function getDatabase(): Database
35+
public function getDatabase(): Database
3636
{
3737
if (!is_null(self::$database)) {
3838
return self::$database;
@@ -75,9 +75,9 @@ public function getDatabase(): Database
7575
return self::$database = $database;
7676
}
7777

78-
protected static function deleteColumn(string $collection, string $column): bool
78+
protected function deleteColumn(string $collection, string $column): bool
7979
{
80-
$sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`";
80+
$sqlTable = "`" . $this->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`";
8181
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";
8282

8383
self::$pool->use(function (Adapter $adapter) use ($sql) {
@@ -92,9 +92,9 @@ protected static function deleteColumn(string $collection, string $column): bool
9292
return true;
9393
}
9494

95-
protected static function deleteIndex(string $collection, string $index): bool
95+
protected function deleteIndex(string $collection, string $index): bool
9696
{
97-
$sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`";
97+
$sqlTable = "`" . $this->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`";
9898
$sql = "DROP INDEX `{$index}` ON {$sqlTable}";
9999

100100
self::$pool->use(function (Adapter $adapter) use ($sql) {

0 commit comments

Comments
 (0)