chore: code refactor via rector
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Tests\Unit\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Models\Blog;
|
||||
use App\Models\Category;
|
||||
use App\Models\User;
|
||||
@@ -9,6 +11,8 @@ use Tests\TestCase;
|
||||
|
||||
class BlogTest extends TestCase
|
||||
{
|
||||
private User|Collection $user;
|
||||
public $category;
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
@@ -18,7 +22,7 @@ class BlogTest extends TestCase
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_create_a_blog_with_factory()
|
||||
public function it_can_create_a_blog_with_factory(): void
|
||||
{
|
||||
$blog = Blog::factory()->create();
|
||||
|
||||
@@ -28,7 +32,7 @@ class BlogTest extends TestCase
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_has_correct_fillable_attributes()
|
||||
public function it_has_correct_fillable_attributes(): void
|
||||
{
|
||||
$blogData = [
|
||||
'post' => 'Test Blog Post',
|
||||
@@ -41,7 +45,7 @@ class BlogTest extends TestCase
|
||||
'category_id' => $this->category->id,
|
||||
];
|
||||
|
||||
$blog = Blog::create($blogData);
|
||||
$blog = Blog::query()->create($blogData);
|
||||
|
||||
foreach ($blogData as $key => $value) {
|
||||
$this->assertEquals($value, $blog->$key);
|
||||
@@ -49,7 +53,7 @@ class BlogTest extends TestCase
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_belongs_to_a_category()
|
||||
public function it_belongs_to_a_category(): void
|
||||
{
|
||||
$blog = Blog::factory()->create(['category_id' => $this->category->id]);
|
||||
|
||||
@@ -58,7 +62,7 @@ class BlogTest extends TestCase
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_generates_unique_slugs()
|
||||
public function it_generates_unique_slugs(): void
|
||||
{
|
||||
$blog1 = Blog::factory()->create(['post' => 'Same Title']);
|
||||
$blog2 = Blog::factory()->create(['post' => 'Same Title']);
|
||||
@@ -67,20 +71,20 @@ class BlogTest extends TestCase
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_query_published_blogs()
|
||||
public function it_can_query_published_blogs(): void
|
||||
{
|
||||
$publishedBlog = Blog::factory()->create(['is_published' => true]);
|
||||
$draftBlog = Blog::factory()->create(['is_published' => false]);
|
||||
Blog::factory()->create(['is_published' => true]);
|
||||
Blog::factory()->create(['is_published' => false]);
|
||||
|
||||
$publishedBlogs = Blog::where('is_published', true)->get();
|
||||
$draftBlogs = Blog::where('is_published', false)->get();
|
||||
$publishedBlogs = Blog::query()->where('is_published', true)->get();
|
||||
$draftBlogs = Blog::query()->where('is_published', false)->get();
|
||||
|
||||
$this->assertCount(1, $publishedBlogs);
|
||||
$this->assertCount(1, $draftBlogs);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_create_blogs_with_custom_headers()
|
||||
public function it_can_create_blogs_with_custom_headers(): void
|
||||
{
|
||||
$blog = Blog::factory()->create(['custom_header' => 'Custom Header Text']);
|
||||
|
||||
@@ -88,30 +92,30 @@ class BlogTest extends TestCase
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_orders_blogs_by_creation_date()
|
||||
public function it_orders_blogs_by_creation_date(): void
|
||||
{
|
||||
$oldBlog = Blog::factory()->create(['created_at' => now()->subDays(2)]);
|
||||
$newBlog = Blog::factory()->create(['created_at' => now()]);
|
||||
|
||||
$blogs = Blog::orderBy('created_at', 'desc')->get();
|
||||
$blogs = Blog::query()->latest()->get();
|
||||
|
||||
$this->assertEquals($newBlog->id, $blogs->first()->id);
|
||||
$this->assertEquals($oldBlog->id, $blogs->last()->id);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_handles_long_content()
|
||||
public function it_handles_long_content(): void
|
||||
{
|
||||
$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));
|
||||
$this->assertGreaterThan(1000, strlen((string) $blog->content));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_update_blog_status()
|
||||
public function it_can_update_blog_status(): void
|
||||
{
|
||||
$blog = Blog::factory()->create(['is_published' => false]);
|
||||
|
||||
@@ -122,24 +126,24 @@ class BlogTest extends TestCase
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_scopes_blogs_by_category()
|
||||
public function it_scopes_blogs_by_category(): void
|
||||
{
|
||||
$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]);
|
||||
Blog::factory()->create(['category_id' => $category1->id]);
|
||||
Blog::factory()->create(['category_id' => $category1->id]);
|
||||
Blog::factory()->create(['category_id' => $category2->id]);
|
||||
|
||||
$category1Blogs = Blog::where('category_id', $category1->id)->get();
|
||||
$category2Blogs = Blog::where('category_id', $category2->id)->get();
|
||||
$category1Blogs = Blog::query()->where('category_id', $category1->id)->get();
|
||||
$category2Blogs = Blog::query()->where('category_id', $category2->id)->get();
|
||||
|
||||
$this->assertCount(2, $category1Blogs);
|
||||
$this->assertCount(1, $category2Blogs);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_uses_correct_table_name()
|
||||
public function it_uses_correct_table_name(): void
|
||||
{
|
||||
$blog = new Blog;
|
||||
|
||||
@@ -147,10 +151,10 @@ class BlogTest extends TestCase
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_extends_model_class()
|
||||
public function it_extends_model_class(): void
|
||||
{
|
||||
$blog = new Blog;
|
||||
|
||||
$this->assertInstanceOf(\Illuminate\Database\Eloquent\Model::class, $blog);
|
||||
$this->assertInstanceOf(Model::class, $blog);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user