Files
zemailnator/tests/Unit/Models/CategoryTest.php
2025-11-14 02:01:01 -08:00

96 lines
2.7 KiB
PHP

<?php
namespace Tests\Unit\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Blog;
use App\Models\Category;
use Tests\TestCase;
class CategoryTest extends TestCase
{
/** @test */
public function it_can_create_a_category_with_factory(): void
{
$category = Category::factory()->create();
$this->assertInstanceOf(Category::class, $category);
$this->assertIsString($category->name);
$this->assertIsString($category->slug);
}
/** @test */
public function it_has_correct_fillable_attributes(): void
{
$categoryData = [
'name' => 'Technology',
'slug' => 'technology',
'is_active' => true,
];
$category = Category::query()->create($categoryData);
foreach ($categoryData as $key => $value) {
$this->assertEquals($value, $category->$key);
}
}
/** @test */
public function it_has_many_blogs(): void
{
$category = Category::factory()->create();
$blog1 = Blog::factory()->create(['category_id' => $category->id]);
$blog2 = Blog::factory()->create(['category_id' => $category->id]);
$this->assertCount(2, $category->blogs);
$this->assertContains($blog1->id, $category->blogs->pluck('id'));
$this->assertContains($blog2->id, $category->blogs->pluck('id'));
}
/** @test */
public function it_generates_unique_slugs(): void
{
$category1 = Category::factory()->create(['name' => 'Same Name']);
$category2 = Category::factory()->create(['name' => 'Same Name']);
$this->assertNotEquals($category1->slug, $category2->slug);
}
/** @test */
public function it_can_query_active_categories(): void
{
Category::factory()->create(['is_active' => true]);
Category::factory()->create(['is_active' => false]);
$activeCategories = Category::query()->where('is_active', true)->get();
$inactiveCategories = Category::query()->where('is_active', false)->get();
$this->assertCount(1, $activeCategories);
$this->assertCount(1, $inactiveCategories);
}
/** @test */
public function it_stores_is_active_status_correctly(): void
{
$category = Category::factory()->create(['is_active' => false]);
$this->assertFalse($category->is_active);
}
/** @test */
public function it_uses_correct_table_name(): void
{
$category = new Category;
$this->assertEquals('categories', $category->getTable());
}
/** @test */
public function it_extends_model_class(): void
{
$category = new Category;
$this->assertInstanceOf(Model::class, $category);
}
}