Files
git-course/tests/Feature/CategoryCrudTest.php
2026-05-12 10:41:18 +08:00

97 lines
2.5 KiB
PHP

<?php
use App\Models\Category;
use App\Models\User;
test('category pages require authentication', function () {
$category = Category::factory()->create();
$this->get('/category')->assertRedirect('/login');
$this->get("/category/{$category->id}/edit")->assertRedirect('/login');
});
test('authenticated users can view categories', function () {
Category::factory()->create([
'name' => 'Design',
'slug' => 'design',
'description' => 'Visual work',
'color' => '#ec4899',
]);
$response = $this
->actingAs(User::factory()->create())
->get('/category');
$response
->assertSuccessful()
->assertSee('Categories')
->assertSee('Design')
->assertSee('design')
->assertSee('Visual work');
});
test('authenticated users can create a category', function () {
$response = $this
->actingAs(User::factory()->create())
->post('/category', [
'name' => 'Operations',
'slug' => '',
'description' => 'Internal workflows',
'color' => '#10b981',
]);
$response->assertRedirect('/category');
$this->assertDatabaseHas('categories', [
'name' => 'Operations',
'slug' => 'operations',
'description' => 'Internal workflows',
'color' => '#10b981',
]);
});
test('authenticated users can update a category', function () {
$category = Category::factory()->create([
'name' => 'Legacy',
'slug' => 'legacy',
'color' => '#4f46e5',
]);
$response = $this
->actingAs(User::factory()->create())
->patch("/category/{$category->id}", [
'name' => 'Marketing',
'slug' => 'marketing',
'description' => 'Campaign planning',
'color' => '#f59e0b',
]);
$response->assertRedirect("/category/{$category->id}/edit");
$this->assertDatabaseHas('categories', [
'id' => $category->id,
'name' => 'Marketing',
'slug' => 'marketing',
'description' => 'Campaign planning',
'color' => '#f59e0b',
]);
});
test('authenticated users can delete a category', function () {
$category = Category::factory()->create([
'name' => 'Archive',
'slug' => 'archive',
'color' => '#64748b',
]);
$response = $this
->actingAs(User::factory()->create())
->delete("/category/{$category->id}");
$response->assertRedirect('/category');
$this->assertDatabaseMissing('categories', [
'id' => $category->id,
]);
});