Refactor ShiftForm to streamline field definitions and implement unique code generation for shifts based on their names. Update ShiftFactory to utilize the new code generation method.
This commit is contained in:
44
tests/Feature/Models/ShiftTest.php
Normal file
44
tests/Feature/Models/ShiftTest.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\Shift;
|
||||
|
||||
it('generates an uppercase slug code from name', function (): void {
|
||||
expect(Shift::generateUniqueCodeFromName('Morning Shift'))
|
||||
->toBe('MORNING-SHIFT');
|
||||
});
|
||||
|
||||
it('generates code on save when code is blank', function (): void {
|
||||
$shift = Shift::factory()->create([
|
||||
'name' => 'Morning Shift',
|
||||
'code' => '',
|
||||
]);
|
||||
|
||||
expect($shift->code)->toBe('MORNING-SHIFT');
|
||||
});
|
||||
|
||||
it('does not overwrite an existing code on update', function (): void {
|
||||
$shift = Shift::factory()->create([
|
||||
'name' => 'Morning Shift',
|
||||
'code' => 'MS',
|
||||
]);
|
||||
|
||||
$shift->update(['name' => 'Early Shift']);
|
||||
|
||||
expect($shift->fresh()->code)->toBe('MS');
|
||||
});
|
||||
|
||||
it('appends a suffix when the generated code already exists', function (): void {
|
||||
Shift::factory()->create([
|
||||
'name' => 'Morning Team',
|
||||
'code' => 'MORNING',
|
||||
]);
|
||||
|
||||
$shift = Shift::factory()->create([
|
||||
'name' => 'Morning',
|
||||
'code' => '',
|
||||
]);
|
||||
|
||||
expect($shift->code)->toBe('MORNING-1');
|
||||
});
|
||||
Reference in New Issue
Block a user