PromptVault/tests/Unit/ModelsTest.php

75 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2026-10-26 20:12:16 +00:00
<?php
namespace Tests\Unit;
use App\Models\Prompt;
use App\Models\Tag;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\TestCase;
class ModelsTest extends TestCase
{
use RefreshDatabase;
public function test_prompt_casts(): void
{
$prompt = Prompt::factory()->create([
'is_favorite' => true,
'usage_count' => 7,
'last_used_at' => '2026-10-01 12:00:00',
]);
$this->assertTrue($prompt->fresh()->is_favorite);
$this->assertSame(7, $prompt->fresh()->usage_count);
$this->assertInstanceOf(Carbon::class, $prompt->fresh()->last_used_at);
}
public function test_prompt_tags_relation_is_ordered_alphabetically(): void
{
$zeta = Tag::query()->create(['name' => 'zeta']);
$alpha = Tag::query()->create(['name' => 'alpha']);
$mid = Tag::query()->create(['name' => 'mid']);
$prompt = Prompt::factory()->create();
$prompt->tags()->attach([$zeta->id, $mid->id, $alpha->id]);
$this->assertSame(
['alpha', 'mid', 'zeta'],
$prompt->fresh()->tags->pluck('name')->all()
);
}
public function test_tag_prompts_relation(): void
{
$tag = Tag::query()->create(['name' => 'testing']);
$first = Prompt::factory()->create();
$second = Prompt::factory()->create();
$tag->prompts()->attach([$first->id, $second->id]);
$this->assertSame([$first->id, $second->id], $tag->fresh()->prompts->pluck('id')->all());
}
public function test_prompt_factory_states(): void
{
$favorite = Prompt::factory()->favorite()->create();
$this->assertTrue($favorite->is_favorite);
$custom = Prompt::factory()->body('Say {{greeting}}')->create();
$this->assertSame('Say {{greeting}}', $custom->body);
}
public function test_user_smoke(): void
{
$user = User::factory()->create(['password' => 'secret-password']);
$this->assertNotEmpty($user->id);
$this->assertNotEmpty($user->name);
$this->assertNotEmpty($user->email);
$this->assertNotSame('secret-password', $user->getRawOriginal('password'));
$this->assertArrayNotHasKey('password', $user->toArray());
}
}