|
8 | 8 | use Contexts\Authorization\Domain\Role\Models\RoleStatus; |
9 | 9 | use Contexts\Authorization\Infrastructure\Persistence\RolePersistence; |
10 | 10 | 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 | +}); |
11 | 19 |
|
12 | 20 | 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(); |
15 | 23 |
|
16 | 24 | $rolePersistence->create($role); |
17 | 25 |
|
|
23 | 31 |
|
24 | 32 | it('can retrieve an role by ID', function () { |
25 | 33 | // 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(); |
28 | 36 | $savedRole = $rolePersistence->create($createdRole); |
29 | 37 |
|
30 | 38 | // Retrieve the role using getById |
|
38 | 46 |
|
39 | 47 | it('can retrieve multiple roles by IDs', function () { |
40 | 48 | // Create multiple test roles in the database |
41 | | - $rolePersistence = new RolePersistence; |
| 49 | + $rolePersistence = new RolePersistence(); |
42 | 50 |
|
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'); |
46 | 54 |
|
47 | 55 | $role1 = $rolePersistence->create($role1); |
48 | 56 | $role2 = $rolePersistence->create($role2); |
|
63 | 71 | }); |
64 | 72 |
|
65 | 73 | it('throws an exception when retrieving a non-existent role', function () { |
66 | | - $rolePersistence = new RolePersistence; |
| 74 | + $rolePersistence = new RolePersistence(); |
67 | 75 |
|
68 | 76 | // Attempt to retrieve a non-existent role |
69 | 77 | $rolePersistence->getById(RoleId::fromInt(999)); |
70 | 78 | })->throws(RoleNotFoundException::class); |
71 | 79 |
|
72 | 80 | it('can update an role', function () { |
73 | 81 | // 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(); |
76 | 84 | $savedRole = $rolePersistence->create($createdRole); |
77 | 85 |
|
78 | 86 | // Create an updated version of the role |
79 | | - $updatedRole = Role::create( |
| 87 | + $updatedRole = $this->roleFactory->create( |
80 | 88 | $savedRole->id, |
81 | 89 | 'Updated Label', |
82 | 90 | ); |
|
97 | 105 | }); |
98 | 106 |
|
99 | 107 | it('throws an exception when updating a non-existent role', function () { |
100 | | - $rolePersistence = new RolePersistence; |
| 108 | + $rolePersistence = new RolePersistence(); |
101 | 109 |
|
102 | 110 | // 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')); |
104 | 112 | })->throws(RoleNotFoundException::class); |
105 | 113 |
|
106 | 114 | it('can paginate roles', function () { |
107 | 115 | // Create multiple test roles |
108 | | - $rolePersistence = new RolePersistence; |
| 116 | + $rolePersistence = new RolePersistence(); |
109 | 117 |
|
110 | 118 | // Create 5 roles |
111 | 119 | for ($i = 1; $i <= 5; $i++) { |
112 | | - $role = Role::create( |
| 120 | + $role = $this->roleFactory->create( |
113 | 121 | RoleId::null(), |
114 | 122 | "Role $i", |
115 | | - new CarbonImmutable |
| 123 | + new CarbonImmutable() |
116 | 124 | ); |
117 | 125 | $rolePersistence->create($role); |
118 | 126 | } |
|
138 | 146 | }); |
139 | 147 |
|
140 | 148 | it('can filter roles with search criteria', function () { |
141 | | - $rolePersistence = new RolePersistence; |
| 149 | + $rolePersistence = new RolePersistence(); |
142 | 150 |
|
143 | 151 | // Create roles with specific labels |
144 | | - $role1 = Role::create( |
| 152 | + $role1 = $this->roleFactory->create( |
145 | 153 | RoleId::null(), |
146 | 154 | 'Laravel Role', |
147 | | - new CarbonImmutable |
| 155 | + new CarbonImmutable() |
148 | 156 | ); |
149 | 157 | $rolePersistence->create($role1); |
150 | 158 |
|
151 | | - $role2 = Role::create( |
| 159 | + $role2 = $this->roleFactory->create( |
152 | 160 | RoleId::null(), |
153 | 161 | 'PHP Tutorial', |
154 | | - new CarbonImmutable |
| 162 | + new CarbonImmutable() |
155 | 163 | ); |
156 | 164 | $role2->subspend(); |
157 | 165 | $rolePersistence->create($role2); |
158 | 166 |
|
159 | | - $role3 = Role::create( |
| 167 | + $role3 = $this->roleFactory->create( |
160 | 168 | RoleId::null(), |
161 | 169 | 'Laravel Tips', |
162 | | - new CarbonImmutable |
| 170 | + new CarbonImmutable() |
163 | 171 | ); |
164 | 172 | $role3->subspend(); |
165 | 173 | $rolePersistence->create($role3); |
|
180 | 188 | expect($result->total())->toBe(0); |
181 | 189 |
|
182 | 190 | // Test search by created_at_range criteria |
183 | | - $role4 = Role::create( |
| 191 | + $role4 = $this->roleFactory->create( |
184 | 192 | RoleId::null(), |
185 | 193 | 'Past Role', |
186 | 194 | new CarbonImmutable('2021-01-01') |
|
196 | 204 |
|
197 | 205 | it('can delete an role', function () { |
198 | 206 | // 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(); |
201 | 209 | $savedRole = $rolePersistence->create($createdRole); |
202 | 210 |
|
203 | 211 | // Delete the role |
|
210 | 218 | }); |
211 | 219 |
|
212 | 220 | it('throws an exception when deleting a non-existent role', function () { |
213 | | - $rolePersistence = new RolePersistence; |
| 221 | + $rolePersistence = new RolePersistence(); |
214 | 222 |
|
215 | 223 | // 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')); |
217 | 225 | })->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