Skip to content

Commit c6f7216

Browse files
committed
feat: add existsByLabel method to RolePersistence for label existence checks
1 parent a4622d3 commit c6f7216

File tree

3 files changed

+99
-29
lines changed

3 files changed

+99
-29
lines changed

contexts/Authorization/Domain/Repositories/RoleRepository.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ public function paginate(int $currentPage = 1, int $perPage = 10, array $criteri
2424
public function delete(Role $role): void;
2525

2626
public function getByLabels(array $labels): Collection;
27+
28+
public function existsByLabel(string $label): bool;
2729
}

contexts/Authorization/Infrastructure/Persistence/RolePersistence.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,9 @@ public function getByLabels(array $labels): Collection
9595
return $record->toDomain();
9696
});
9797
}
98+
99+
public function existsByLabel(string $label): bool
100+
{
101+
return RoleRecord::where('label', $label)->exists();
102+
}
98103
}

contexts/Authorization/Tests/Feature/Infrastructure/Persistence/RolePersistenceTest.php

Lines changed: 92 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@
88
use Contexts\Authorization\Domain\Role\Models\RoleStatus;
99
use Contexts\Authorization\Infrastructure\Persistence\RolePersistence;
1010
use Contexts\Authorization\Infrastructure\Records\RoleRecord;
11+
use Contexts\Authorization\Domain\Factories\RoleFactory;
12+
use Contexts\Authorization\Domain\Services\RoleLabelUniquenessService;
13+
14+
beforeEach(function () {
15+
$this->roleLabelUniquenessService = mock(RoleLabelUniquenessService::class);
16+
$this->roleLabelUniquenessService->shouldReceive('ensureUnique')->andReturn(true);
17+
$this->roleFactory = new RoleFactory($this->roleLabelUniquenessService);
18+
});
1119

1220
it('can persist role data correctly', function () {
13-
$role = Role::create(RoleId::null(), 'My Role');
14-
$rolePersistence = new RolePersistence;
21+
$role = $this->roleFactory->create(RoleId::null(), 'My Role');
22+
$rolePersistence = new RolePersistence();
1523

1624
$rolePersistence->create($role);
1725

@@ -23,8 +31,8 @@
2331

2432
it('can retrieve an role by ID', function () {
2533
// Create a test role in the database
26-
$createdRole = Role::create(RoleId::null(), 'Test Role');
27-
$rolePersistence = new RolePersistence;
34+
$createdRole = $this->roleFactory->create(RoleId::null(), 'Test Role');
35+
$rolePersistence = new RolePersistence();
2836
$savedRole = $rolePersistence->create($createdRole);
2937

3038
// Retrieve the role using getById
@@ -38,11 +46,11 @@
3846

3947
it('can retrieve multiple roles by IDs', function () {
4048
// Create multiple test roles in the database
41-
$rolePersistence = new RolePersistence;
49+
$rolePersistence = new RolePersistence();
4250

43-
$role1 = Role::create(RoleId::null(), 'Role 1');
44-
$role2 = Role::create(RoleId::null(), 'Role 2');
45-
$role3 = Role::create(RoleId::null(), 'Role 3');
51+
$role1 = $this->roleFactory->create(RoleId::null(), 'Role 1');
52+
$role2 = $this->roleFactory->create(RoleId::null(), 'Role 2');
53+
$role3 = $this->roleFactory->create(RoleId::null(), 'Role 3');
4654

4755
$role1 = $rolePersistence->create($role1);
4856
$role2 = $rolePersistence->create($role2);
@@ -63,20 +71,20 @@
6371
});
6472

6573
it('throws an exception when retrieving a non-existent role', function () {
66-
$rolePersistence = new RolePersistence;
74+
$rolePersistence = new RolePersistence();
6775

6876
// Attempt to retrieve a non-existent role
6977
$rolePersistence->getById(RoleId::fromInt(999));
7078
})->throws(RoleNotFoundException::class);
7179

7280
it('can update an role', function () {
7381
// Create a test role in the database
74-
$createdRole = Role::create(RoleId::null(), 'Original Label');
75-
$rolePersistence = new RolePersistence;
82+
$createdRole = $this->roleFactory->create(RoleId::null(), 'Original Label');
83+
$rolePersistence = new RolePersistence();
7684
$savedRole = $rolePersistence->create($createdRole);
7785

7886
// Create an updated version of the role
79-
$updatedRole = Role::create(
87+
$updatedRole = $this->roleFactory->create(
8088
$savedRole->id,
8189
'Updated Label',
8290
);
@@ -97,22 +105,22 @@
97105
});
98106

99107
it('throws an exception when updating a non-existent role', function () {
100-
$rolePersistence = new RolePersistence;
108+
$rolePersistence = new RolePersistence();
101109

102110
// Attempt to update a non-existent role
103-
$rolePersistence->update(Role::create(RoleId::fromInt(999), 'Updated Label'));
111+
$rolePersistence->update($this->roleFactory->create(RoleId::fromInt(999), 'Updated Label'));
104112
})->throws(RoleNotFoundException::class);
105113

106114
it('can paginate roles', function () {
107115
// Create multiple test roles
108-
$rolePersistence = new RolePersistence;
116+
$rolePersistence = new RolePersistence();
109117

110118
// Create 5 roles
111119
for ($i = 1; $i <= 5; $i++) {
112-
$role = Role::create(
120+
$role = $this->roleFactory->create(
113121
RoleId::null(),
114122
"Role $i",
115-
new CarbonImmutable
123+
new CarbonImmutable()
116124
);
117125
$rolePersistence->create($role);
118126
}
@@ -138,28 +146,28 @@
138146
});
139147

140148
it('can filter roles with search criteria', function () {
141-
$rolePersistence = new RolePersistence;
149+
$rolePersistence = new RolePersistence();
142150

143151
// Create roles with specific labels
144-
$role1 = Role::create(
152+
$role1 = $this->roleFactory->create(
145153
RoleId::null(),
146154
'Laravel Role',
147-
new CarbonImmutable
155+
new CarbonImmutable()
148156
);
149157
$rolePersistence->create($role1);
150158

151-
$role2 = Role::create(
159+
$role2 = $this->roleFactory->create(
152160
RoleId::null(),
153161
'PHP Tutorial',
154-
new CarbonImmutable
162+
new CarbonImmutable()
155163
);
156164
$role2->subspend();
157165
$rolePersistence->create($role2);
158166

159-
$role3 = Role::create(
167+
$role3 = $this->roleFactory->create(
160168
RoleId::null(),
161169
'Laravel Tips',
162-
new CarbonImmutable
170+
new CarbonImmutable()
163171
);
164172
$role3->subspend();
165173
$rolePersistence->create($role3);
@@ -180,7 +188,7 @@
180188
expect($result->total())->toBe(0);
181189

182190
// Test search by created_at_range criteria
183-
$role4 = Role::create(
191+
$role4 = $this->roleFactory->create(
184192
RoleId::null(),
185193
'Past Role',
186194
new CarbonImmutable('2021-01-01')
@@ -196,8 +204,8 @@
196204

197205
it('can delete an role', function () {
198206
// Create a test role in the database
199-
$createdRole = Role::create(RoleId::null(), 'Test Role');
200-
$rolePersistence = new RolePersistence;
207+
$createdRole = $this->roleFactory->create(RoleId::null(), 'Test Role');
208+
$rolePersistence = new RolePersistence();
201209
$savedRole = $rolePersistence->create($createdRole);
202210

203211
// Delete the role
@@ -210,8 +218,63 @@
210218
});
211219

212220
it('throws an exception when deleting a non-existent role', function () {
213-
$rolePersistence = new RolePersistence;
221+
$rolePersistence = new RolePersistence();
214222

215223
// Attempt to delete a non-existent role
216-
$rolePersistence->delete(Role::create(RoleId::fromInt(999), 'Test Role'));
224+
$rolePersistence->delete($this->roleFactory->create(RoleId::fromInt(999), 'Test Role'));
217225
})->throws(RoleNotFoundException::class);
226+
227+
228+
it('returns true when role exists with given label', function () {
229+
// Create a test role in the database with a specific label
230+
$createdRole = $this->roleFactory->create(RoleId::null(), 'Unique Label');
231+
$rolePersistence = new RolePersistence();
232+
$rolePersistence->create($createdRole);
233+
234+
// Check if the method correctly detects the existing label
235+
$result = $rolePersistence->existsByLabel('Unique Label');
236+
237+
expect($result)->toBeTrue();
238+
});
239+
240+
it('returns false when role does not exist with given label', function () {
241+
$rolePersistence = new RolePersistence();
242+
243+
// Test with a label that doesn't exist in the database
244+
$result = $rolePersistence->existsByLabel('Non-Existent Label');
245+
246+
expect($result)->toBeFalse();
247+
});
248+
249+
it('can retrieve roles by labels', function () {
250+
// Create roles with specific labels
251+
$rolePersistence = new RolePersistence();
252+
253+
$role1 = $this->roleFactory->create(RoleId::null(), 'Admin Role');
254+
$role2 = $this->roleFactory->create(RoleId::null(), 'Editor Role');
255+
$role3 = $this->roleFactory->create(RoleId::null(), 'Viewer Role');
256+
257+
$rolePersistence->create($role1);
258+
$rolePersistence->create($role2);
259+
$rolePersistence->create($role3);
260+
261+
// Retrieve the roles by labels
262+
$retrievedRoles = $rolePersistence->getByLabels(['Admin Role', 'Editor Role']);
263+
// Assert correct records were retrieved
264+
$roleLabelArray = $retrievedRoles->map(function ($role) {
265+
return $role->getLabel();
266+
})->toArray();
267+
expect($roleLabelArray)->toHaveCount(2);
268+
expect($roleLabelArray)->toContain('Admin Role', 'Editor Role');
269+
expect($roleLabelArray)->not->toContain('Viewer Role');
270+
});
271+
272+
it('returns empty collection when no roles match the given labels', function () {
273+
$rolePersistence = new RolePersistence();
274+
275+
// Retrieve with non-existent labels
276+
$retrievedRoles = $rolePersistence->getByLabels(['Non-Existent Label']);
277+
278+
expect($retrievedRoles)->toBeInstanceOf(\Illuminate\Support\Collection::class);
279+
expect($retrievedRoles)->toBeEmpty();
280+
});

0 commit comments

Comments
 (0)