Files
zemailnator/tests/Unit/Models/BlogTest.php
idevakk 3706072ce5 fix: resolve PSR-4 autoloading and test failures
- Add proper Tests\ namespace to all test classes in tests/Feature and tests/Unit
  - Split RemainingModelsTest.php into separate files (PSR-4 compliance)
  - Create missing factories: MetaFactory, RemoteEmailFactory
  - Add HasFactory trait to RemoteEmail model
  - Add missing ReflectionClass imports to test files
  - Fix mass assignment issues in Meta and RemoteEmail models
  - Override database connection for RemoteEmail in testing environment
  - Fix DateTime comparison precision issues in tests
2025-11-13 09:49:21 -08:00

156 lines
4.5 KiB
PHP

<?php
namespace Tests\Unit\Models;
use App\Models\Blog;
use App\Models\Category;
use App\Models\User;
use Tests\TestCase;
class BlogTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->create();
$this->category = Category::factory()->create();
}
/** @test */
public function it_can_create_a_blog_with_factory()
{
$blog = Blog::factory()->create();
$this->assertInstanceOf(Blog::class, $blog);
$this->assertIsString($blog->post);
$this->assertIsString($blog->slug);
}
/** @test */
public function it_has_correct_fillable_attributes()
{
$blogData = [
'post' => 'Test Blog Post',
'slug' => 'test-blog-post',
'content' => 'This is the blog content.',
'meta' => json_encode(['description' => 'Meta description', 'keywords' => 'keyword1,keyword2']),
'custom_header' => 'Blog excerpt',
'post_image' => 'blog-image.jpg',
'is_published' => true,
'category_id' => $this->category->id,
];
$blog = Blog::create($blogData);
foreach ($blogData as $key => $value) {
$this->assertEquals($value, $blog->$key);
}
}
/** @test */
public function it_belongs_to_a_category()
{
$blog = Blog::factory()->create(['category_id' => $this->category->id]);
$this->assertInstanceOf(Category::class, $blog->category);
$this->assertEquals($this->category->id, $blog->category->id);
}
/** @test */
public function it_generates_unique_slugs()
{
$blog1 = Blog::factory()->create(['post' => 'Same Title']);
$blog2 = Blog::factory()->create(['post' => 'Same Title']);
$this->assertNotEquals($blog1->slug, $blog2->slug);
}
/** @test */
public function it_can_query_published_blogs()
{
$publishedBlog = Blog::factory()->create(['is_published' => true]);
$draftBlog = Blog::factory()->create(['is_published' => false]);
$publishedBlogs = Blog::where('is_published', true)->get();
$draftBlogs = Blog::where('is_published', false)->get();
$this->assertCount(1, $publishedBlogs);
$this->assertCount(1, $draftBlogs);
}
/** @test */
public function it_can_create_blogs_with_custom_headers()
{
$blog = Blog::factory()->create(['custom_header' => 'Custom Header Text']);
$this->assertEquals('Custom Header Text', $blog->custom_header);
}
/** @test */
public function it_orders_blogs_by_creation_date()
{
$oldBlog = Blog::factory()->create(['created_at' => now()->subDays(2)]);
$newBlog = Blog::factory()->create(['created_at' => now()]);
$blogs = Blog::orderBy('created_at', 'desc')->get();
$this->assertEquals($newBlog->id, $blogs->first()->id);
$this->assertEquals($oldBlog->id, $blogs->last()->id);
}
/** @test */
public function it_handles_long_content()
{
$longContent = str_repeat('This is a very long blog content. ', 100);
$blog = Blog::factory()->create(['content' => $longContent]);
$this->assertEquals($longContent, $blog->content);
$this->assertGreaterThan(1000, strlen($blog->content));
}
/** @test */
public function it_can_update_blog_status()
{
$blog = Blog::factory()->create(['is_published' => false]);
$blog->update(['is_published' => true]);
$blog->refresh();
$this->assertEquals(true, $blog->is_published);
}
/** @test */
public function it_scopes_blogs_by_category()
{
$category1 = Category::factory()->create();
$category2 = Category::factory()->create();
$blog1 = Blog::factory()->create(['category_id' => $category1->id]);
$blog2 = Blog::factory()->create(['category_id' => $category1->id]);
$blog3 = Blog::factory()->create(['category_id' => $category2->id]);
$category1Blogs = Blog::where('category_id', $category1->id)->get();
$category2Blogs = Blog::where('category_id', $category2->id)->get();
$this->assertCount(2, $category1Blogs);
$this->assertCount(1, $category2Blogs);
}
/** @test */
public function it_uses_correct_table_name()
{
$blog = new Blog;
$this->assertEquals('blogs', $blog->getTable());
}
/** @test */
public function it_extends_model_class()
{
$blog = new Blog;
$this->assertInstanceOf(\Illuminate\Database\Eloquent\Model::class, $blog);
}
}