Skip to content

Commit 902826b

Browse files
committed
(fix): update tests to use enum types and new hook API after refactoring
1 parent 03f2ba5 commit 902826b

9 files changed

Lines changed: 64 additions & 62 deletions

File tree

tests/e2e/Adapter/Base.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Tests\E2E\Adapter\Scopes\SpatialTests;
1919
use Tests\E2E\Adapter\Scopes\VectorTests;
2020
use Utopia\Database\Database;
21-
use Utopia\Database\Hook\RelationshipHandler;
21+
use Utopia\Database\Hook\Relationships;
2222
use Utopia\Database\Validator\Authorization;
2323

2424
\ini_set('memory_limit', '2048M');
@@ -62,7 +62,7 @@ protected function setUp(): void
6262

6363
$db = $this->getDatabase();
6464
if ($db->getRelationshipHook() === null) {
65-
$db->setRelationshipHook(new RelationshipHandler($db));
65+
$db->addHook(new Relationships($db));
6666
}
6767
}
6868

tests/e2e/Adapter/Scopes/CollectionTests.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Utopia\Database\Helpers\Permission;
2020
use Utopia\Database\Helpers\Role;
2121
use Utopia\Database\Hook\Lifecycle;
22-
use Utopia\Database\Hook\QueryTransform;
22+
use Utopia\Database\Hook\Transform;
2323
use Utopia\Database\Index;
2424
use Utopia\Database\Query;
2525
use Utopia\Database\Relationship;
@@ -1081,7 +1081,7 @@ public function testEvents(): void
10811081
];
10821082

10831083
$test = $this;
1084-
$database->addLifecycleHook(new class ($events, $test) implements Lifecycle {
1084+
$database->addHook(new class ($events, $test) implements Lifecycle {
10851085
/** @param array<Event> $events */
10861086
public function __construct(private array &$events, private $test)
10871087
{
@@ -1222,18 +1222,19 @@ public function testTransformations(): void
12221222
'name' => 'value1',
12231223
]));
12241224

1225-
$database->addQueryTransform('test', new class () implements QueryTransform {
1225+
$hook = new class () implements Transform {
12261226
public function transform(Event $event, string $query): string
12271227
{
12281228
return 'SELECT 1';
12291229
}
1230-
});
1230+
};
1231+
$database->addHook($hook);
12311232

12321233
$result = $database->getDocument('docs', 'doc1');
12331234

12341235
$this->assertTrue($result->isEmpty());
12351236

1236-
$database->removeQueryTransform('test');
1237+
$database->removeTransform($hook::class);
12371238
}
12381239

12391240
public function testSetGlobalCollection(): void

tests/unit/Authorization/AuthorizationTest.php

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Unit\Authorization;
44

55
use PHPUnit\Framework\TestCase;
6+
use Utopia\Database\PermissionType;
67
use Utopia\Database\Validator\Authorization;
78
use Utopia\Database\Validator\Authorization\Input;
89

@@ -25,26 +26,26 @@ public function testDefaultRolesContainAny(): void
2526
public function testIsValidWithMatchingRole(): void
2627
{
2728
$this->auth->addRole('user:123');
28-
$input = new Input('read', ['user:123']);
29+
$input = new Input(PermissionType::Read, ['user:123']);
2930
$this->assertTrue($this->auth->isValid($input));
3031
}
3132

3233
public function testIsValidWithNonMatchingRole(): void
3334
{
3435
$this->auth->addRole('user:123');
35-
$input = new Input('read', ['user:456']);
36+
$input = new Input(PermissionType::Read, ['user:456']);
3637
$this->assertFalse($this->auth->isValid($input));
3738
}
3839

3940
public function testIsValidWithAnyRoleMatchesAllPermissions(): void
4041
{
41-
$input = new Input('read', ['any']);
42+
$input = new Input(PermissionType::Read, ['any']);
4243
$this->assertTrue($this->auth->isValid($input));
4344
}
4445

4546
public function testIsValidReturnsFalseWithEmptyPermissions(): void
4647
{
47-
$input = new Input('read', []);
48+
$input = new Input(PermissionType::Read, []);
4849
$this->assertFalse($this->auth->isValid($input));
4950
$this->assertStringContainsString('No permissions provided', $this->auth->getDescription());
5051
}
@@ -89,7 +90,7 @@ public function testSkipBypassesAuthorization(): void
8990
{
9091
$this->auth->cleanRoles();
9192

92-
$input = new Input('read', ['user:999']);
93+
$input = new Input(PermissionType::Read, ['user:999']);
9394
$this->assertFalse($this->auth->isValid($input));
9495

9596
$result = $this->auth->skip(function () use ($input) {
@@ -129,63 +130,63 @@ public function testIsValidWithMultipleRoles(): void
129130
$this->auth->addRole('user:123');
130131
$this->auth->addRole('team:456');
131132

132-
$input = new Input('read', ['team:456']);
133+
$input = new Input(PermissionType::Read, ['team:456']);
133134
$this->assertTrue($this->auth->isValid($input));
134135
}
135136

136137
public function testIsValidWithMultiplePermissionsMatchesFirst(): void
137138
{
138139
$this->auth->addRole('user:123');
139140

140-
$input = new Input('read', ['user:123', 'team:456']);
141+
$input = new Input(PermissionType::Read, ['user:123', 'team:456']);
141142
$this->assertTrue($this->auth->isValid($input));
142143
}
143144

144145
public function testIsValidWithMultiplePermissionsMatchesLast(): void
145146
{
146147
$this->auth->addRole('team:456');
147148

148-
$input = new Input('read', ['user:123', 'team:456']);
149+
$input = new Input(PermissionType::Read, ['user:123', 'team:456']);
149150
$this->assertTrue($this->auth->isValid($input));
150151
}
151152

152153
public function testIsValidWithGuestsRole(): void
153154
{
154155
$this->auth->addRole('guests');
155156

156-
$input = new Input('read', ['guests']);
157+
$input = new Input(PermissionType::Read, ['guests']);
157158
$this->assertTrue($this->auth->isValid($input));
158159
}
159160

160161
public function testIsValidWithUsersRole(): void
161162
{
162163
$this->auth->addRole('users');
163164

164-
$input = new Input('read', ['users']);
165+
$input = new Input(PermissionType::Read, ['users']);
165166
$this->assertTrue($this->auth->isValid($input));
166167
}
167168

168169
public function testIsValidWithDimensionalRole(): void
169170
{
170171
$this->auth->addRole('user:123/admin');
171172

172-
$input = new Input('read', ['user:123/admin']);
173+
$input = new Input(PermissionType::Read, ['user:123/admin']);
173174
$this->assertTrue($this->auth->isValid($input));
174175
}
175176

176177
public function testDimensionalRoleDoesNotMatchWithoutDimension(): void
177178
{
178179
$this->auth->addRole('user:123/admin');
179180

180-
$input = new Input('read', ['user:123']);
181+
$input = new Input(PermissionType::Read, ['user:123']);
181182
$this->assertFalse($this->auth->isValid($input));
182183
}
183184

184185
public function testNonDimensionalRoleDoesNotMatchWithDimension(): void
185186
{
186187
$this->auth->addRole('user:123');
187188

188-
$input = new Input('read', ['user:123/admin']);
189+
$input = new Input(PermissionType::Read, ['user:123/admin']);
189190
$this->assertFalse($this->auth->isValid($input));
190191
}
191192

@@ -194,7 +195,7 @@ public function testGetDescriptionOnFailure(): void
194195
$this->auth->cleanRoles();
195196
$this->auth->addRole('user:123');
196197

197-
$input = new Input('read', ['team:456']);
198+
$input = new Input(PermissionType::Read, ['team:456']);
198199
$this->assertFalse($this->auth->isValid($input));
199200

200201
$description = $this->auth->getDescription();
@@ -204,7 +205,7 @@ public function testGetDescriptionOnFailure(): void
204205

205206
public function testGetDescriptionOnEmptyPermissions(): void
206207
{
207-
$input = new Input('write', []);
208+
$input = new Input(PermissionType::Write, []);
208209
$this->assertFalse($this->auth->isValid($input));
209210
$this->assertStringContainsString("No permissions provided for action 'write'", $this->auth->getDescription());
210211
}
@@ -236,7 +237,7 @@ public function testDisabledAuthorizationBypassesAllChecks(): void
236237
$this->auth->disable();
237238
$this->auth->cleanRoles();
238239

239-
$input = new Input('read', ['user:999']);
240+
$input = new Input(PermissionType::Read, ['user:999']);
240241
$this->assertTrue($this->auth->isValid($input));
241242
}
242243

@@ -263,39 +264,39 @@ public function testPermissionTypeMatchingRead(): void
263264
{
264265
$this->auth->addRole('user:123');
265266

266-
$input = new Input('read', ['user:123']);
267+
$input = new Input(PermissionType::Read, ['user:123']);
267268
$this->assertTrue($this->auth->isValid($input));
268269
}
269270

270271
public function testPermissionTypeMatchingCreate(): void
271272
{
272273
$this->auth->addRole('user:123');
273274

274-
$input = new Input('create', ['user:123']);
275+
$input = new Input(PermissionType::Create, ['user:123']);
275276
$this->assertTrue($this->auth->isValid($input));
276277
}
277278

278279
public function testPermissionTypeMatchingUpdate(): void
279280
{
280281
$this->auth->addRole('user:123');
281282

282-
$input = new Input('update', ['user:123']);
283+
$input = new Input(PermissionType::Update, ['user:123']);
283284
$this->assertTrue($this->auth->isValid($input));
284285
}
285286

286287
public function testPermissionTypeMatchingDelete(): void
287288
{
288289
$this->auth->addRole('user:123');
289290

290-
$input = new Input('delete', ['user:123']);
291+
$input = new Input(PermissionType::Delete, ['user:123']);
291292
$this->assertTrue($this->auth->isValid($input));
292293
}
293294

294295
public function testPermissionTypeMatchingWrite(): void
295296
{
296297
$this->auth->addRole('user:123');
297298

298-
$input = new Input('write', ['user:123']);
299+
$input = new Input(PermissionType::Write, ['user:123']);
299300
$this->assertTrue($this->auth->isValid($input));
300301
}
301302

@@ -320,11 +321,11 @@ public function testGetType(): void
320321

321322
public function testInputSettersAndGetters(): void
322323
{
323-
$input = new Input('read', ['user:123']);
324+
$input = new Input(PermissionType::Read, ['user:123']);
324325
$this->assertEquals('read', $input->getAction());
325326
$this->assertEquals(['user:123'], $input->getPermissions());
326327

327-
$input->setAction('write');
328+
$input->setAction(PermissionType::Write);
328329
$this->assertEquals('write', $input->getAction());
329330

330331
$input->setPermissions(['team:456']);
@@ -335,10 +336,10 @@ public function testIsValidWithTeamDimensionRole(): void
335336
{
336337
$this->auth->addRole('team:abc/owner');
337338

338-
$input = new Input('read', ['team:abc/owner']);
339+
$input = new Input(PermissionType::Read, ['team:abc/owner']);
339340
$this->assertTrue($this->auth->isValid($input));
340341

341-
$input = new Input('read', ['team:abc/member']);
342+
$input = new Input(PermissionType::Read, ['team:abc/member']);
342343
$this->assertFalse($this->auth->isValid($input));
343344
}
344345

@@ -361,21 +362,21 @@ public function testLabelRole(): void
361362
{
362363
$this->auth->addRole('label:vip');
363364

364-
$input = new Input('read', ['label:vip']);
365+
$input = new Input(PermissionType::Read, ['label:vip']);
365366
$this->assertTrue($this->auth->isValid($input));
366367

367-
$input = new Input('read', ['label:premium']);
368+
$input = new Input(PermissionType::Read, ['label:premium']);
368369
$this->assertFalse($this->auth->isValid($input));
369370
}
370371

371372
public function testMemberRole(): void
372373
{
373374
$this->auth->addRole('member:abc123');
374375

375-
$input = new Input('read', ['member:abc123']);
376+
$input = new Input(PermissionType::Read, ['member:abc123']);
376377
$this->assertTrue($this->auth->isValid($input));
377378

378-
$input = new Input('read', ['member:def456']);
379+
$input = new Input(PermissionType::Read, ['member:def456']);
379380
$this->assertFalse($this->auth->isValid($input));
380381
}
381382
}

tests/unit/DocumentTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,17 @@ public function test_get_delete(): void
113113

114114
public function test_get_permission_by_type(): void
115115
{
116-
$this->assertEquals(['any', 'user:creator'], $this->document->getPermissionsByType(PermissionType::Create->value));
117-
$this->assertEquals([], $this->empty->getPermissionsByType(PermissionType::Create->value));
116+
$this->assertEquals(['any', 'user:creator'], $this->document->getPermissionsByType(PermissionType::Create));
117+
$this->assertEquals([], $this->empty->getPermissionsByType(PermissionType::Create));
118118

119-
$this->assertEquals(['user:123', 'team:123'], $this->document->getPermissionsByType(PermissionType::Read->value));
120-
$this->assertEquals([], $this->empty->getPermissionsByType(PermissionType::Read->value));
119+
$this->assertEquals(['user:123', 'team:123'], $this->document->getPermissionsByType(PermissionType::Read));
120+
$this->assertEquals([], $this->empty->getPermissionsByType(PermissionType::Read));
121121

122-
$this->assertEquals(['any', 'user:updater'], $this->document->getPermissionsByType(PermissionType::Update->value));
123-
$this->assertEquals([], $this->empty->getPermissionsByType(PermissionType::Update->value));
122+
$this->assertEquals(['any', 'user:updater'], $this->document->getPermissionsByType(PermissionType::Update));
123+
$this->assertEquals([], $this->empty->getPermissionsByType(PermissionType::Update));
124124

125-
$this->assertEquals(['any', 'user:deleter'], $this->document->getPermissionsByType(PermissionType::Delete->value));
126-
$this->assertEquals([], $this->empty->getPermissionsByType(PermissionType::Delete->value));
125+
$this->assertEquals(['any', 'user:deleter'], $this->document->getPermissionsByType(PermissionType::Delete));
126+
$this->assertEquals([], $this->empty->getPermissionsByType(PermissionType::Delete));
127127
}
128128

129129
public function test_get_permissions(): void

tests/unit/PermissionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public function test_aggregation(): void
298298
$parsed = Permission::aggregate($permissions);
299299
$this->assertEquals(['create("any")', 'update("any")', 'delete("any")'], $parsed);
300300

301-
$parsed = Permission::aggregate($permissions, [PermissionType::Update->value, PermissionType::Delete->value]);
301+
$parsed = Permission::aggregate($permissions, [PermissionType::Update, PermissionType::Delete]);
302302
$this->assertEquals(['update("any")', 'delete("any")'], $parsed);
303303

304304
$permissions = [
@@ -310,7 +310,7 @@ public function test_aggregation(): void
310310
'delete("user:123")',
311311
];
312312

313-
$parsed = Permission::aggregate($permissions, [PermissionType::Create->value, PermissionType::Read->value, PermissionType::Update->value, PermissionType::Delete->value]);
313+
$parsed = Permission::aggregate($permissions, [PermissionType::Create, PermissionType::Read, PermissionType::Update, PermissionType::Delete]);
314314
$this->assertEquals([
315315
'read("any")',
316316
'read("user:123")',

tests/unit/Relationships/RelationshipValidationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Utopia\Database\Helpers\ID;
2020
use Utopia\Database\Helpers\Permission;
2121
use Utopia\Database\Helpers\Role;
22-
use Utopia\Database\Hook\RelationshipHandler;
22+
use Utopia\Database\Hook\Relationships;
2323
use Utopia\Database\Operator;
2424
use Utopia\Database\Relationship;
2525
use Utopia\Database\RelationType;
@@ -161,7 +161,7 @@ function (Document $col, string $docId) use ($meta, $colMap, $documents) {
161161
$database->getAuthorization()->addRole(Role::any()->toString());
162162

163163
if ($withRelationshipHook) {
164-
$database->setRelationshipHook(new RelationshipHandler($database));
164+
$database->addHook(new Relationships($database));
165165
}
166166

167167
return $database;

tests/unit/Validator/AttributeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1845,7 +1845,7 @@ public function test_invalid_format_for_type(): void
18451845
{
18461846
Structure::addFormat('testformat', function (mixed $attribute) {
18471847
return new \Utopia\Validator\Text(100);
1848-
}, ColumnType::Integer->value);
1848+
}, ColumnType::Integer);
18491849

18501850
$validator = new Attribute(
18511851
attributes: [],

0 commit comments

Comments
 (0)