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:
Mekan1206
2026-08-02 20:17:16 +05:00
parent e3ec83686c
commit 28cbef8eb5
4 changed files with 99 additions and 23 deletions

View 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');
});