Reuse the existing UUID trait instead of a second copy of the logic

Mailbox hand-rolled the same creating-time UUID assignment that
App\Models\Concerns\HasUuid already provides to 17 other models under
R11 (URLs address records by UUID, not integer PK), and skipped the
route-key binding that comes with it. Swap onto the trait, and cover
both the assignment and the route key with a test matching the
convention already used for Host/Order.

Also match MailboxFactory's @extends annotation to how the other
factories in the repo write it (Pint's fully_qualified_strict_types
rule turns a fully-qualified docblock reference into an import plus a
short name anyway, so this is what it would end up as either way).
feat/mailboxes
nexxo 2026-07-27 21:31:39 +02:00
parent c642090d97
commit aaddffc096
3 changed files with 11 additions and 9 deletions

View File

@ -2,10 +2,10 @@
namespace App\Models;
use App\Models\Concerns\HasUuid;
use App\Services\Secrets\SecretCipher;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
/**
* One sending address.
@ -16,7 +16,7 @@ use Illuminate\Support\Str;
*/
class Mailbox extends Model
{
use HasFactory;
use HasFactory, HasUuid;
protected $fillable = [
'key', 'address', 'display_name', 'username',
@ -32,13 +32,6 @@ class Mailbox extends Model
];
}
protected static function booted(): void
{
static::creating(function (self $box) {
$box->uuid ??= (string) Str::uuid();
});
}
public static function findByKey(string $key): ?self
{
return static::query()->where('key', $key)->first();

View File

@ -2,8 +2,10 @@
namespace Database\Factories;
use App\Models\Mailbox;
use Illuminate\Database\Eloquent\Factories\Factory;
/** @extends Factory<Mailbox> */
class MailboxFactory extends Factory
{
public function definition(): array

View File

@ -39,3 +39,10 @@ it('finds a mailbox by its key', function () {
expect(Mailbox::findByKey('billing'))->not->toBeNull()
->and(Mailbox::findByKey('gibtsnicht'))->toBeNull();
});
it('auto-assigns a uuid and routes by it', function () {
$box = Mailbox::factory()->create();
expect($box->uuid)->not->toBeNull()
->and($box->getRouteKeyName())->toBe('uuid');
});