95 lines
2.6 KiB
PHP
95 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Models;
|
|
|
|
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()
|
|
{
|
|
$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()
|
|
{
|
|
$categoryData = [
|
|
'name' => 'Technology',
|
|
'slug' => 'technology',
|
|
'is_active' => true,
|
|
];
|
|
|
|
$category = Category::create($categoryData);
|
|
|
|
foreach ($categoryData as $key => $value) {
|
|
$this->assertEquals($value, $category->$key);
|
|
}
|
|
}
|
|
|
|
/** @test */
|
|
public function it_has_many_blogs()
|
|
{
|
|
$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()
|
|
{
|
|
$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()
|
|
{
|
|
$activeCategory = Category::factory()->create(['is_active' => true]);
|
|
$inactiveCategory = Category::factory()->create(['is_active' => false]);
|
|
|
|
$activeCategories = Category::where('is_active', true)->get();
|
|
$inactiveCategories = Category::where('is_active', false)->get();
|
|
|
|
$this->assertCount(1, $activeCategories);
|
|
$this->assertCount(1, $inactiveCategories);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_stores_is_active_status_correctly()
|
|
{
|
|
$category = Category::factory()->create(['is_active' => false]);
|
|
|
|
$this->assertFalse($category->is_active);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_uses_correct_table_name()
|
|
{
|
|
$category = new Category;
|
|
|
|
$this->assertEquals('categories', $category->getTable());
|
|
}
|
|
|
|
/** @test */
|
|
public function it_extends_model_class()
|
|
{
|
|
$category = new Category;
|
|
|
|
$this->assertInstanceOf(\Illuminate\Database\Eloquent\Model::class, $category);
|
|
}
|
|
}
|