|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit; |
| 4 | + |
| 5 | +use App\Models\Category; |
| 6 | +use App\Models\Post; |
| 7 | +use App\Models\Tag; |
| 8 | +use App\Models\User; |
| 9 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 10 | +use PHPUnit\Framework\Attributes\Test; |
| 11 | +use Tests\TestCase; |
| 12 | + |
| 13 | +class PostModelTest extends TestCase |
| 14 | +{ |
| 15 | + use RefreshDatabase; |
| 16 | + |
| 17 | + #[Test] |
| 18 | + public function post_can_generate_correct_slug_from_title() |
| 19 | + { |
| 20 | + $category = Category::factory()->create(); |
| 21 | + $user = User::factory()->create(); |
| 22 | + |
| 23 | + $this->actingAs($user); |
| 24 | + |
| 25 | + $post = Post::add([ |
| 26 | + 'title' => 'Laravel Best Practices & Tips', |
| 27 | + 'content' => 'Test content about Laravel development', |
| 28 | + 'category_id' => $category->id |
| 29 | + ]); |
| 30 | + |
| 31 | + $this->assertEquals('laravel-best-practices-tips', $post->slug); |
| 32 | + } |
| 33 | + |
| 34 | + #[Test] |
| 35 | + public function post_can_toggle_status_from_draft_to_public() |
| 36 | + { |
| 37 | + $post = Post::factory()->create(['status' => Post::IS_DRAFT]); |
| 38 | + |
| 39 | + $this->assertEquals(Post::IS_DRAFT, $post->status); |
| 40 | + |
| 41 | + $post->toggleStatus(1); |
| 42 | + $this->assertEquals(Post::IS_PUBLIC, $post->status); |
| 43 | + |
| 44 | + $post->toggleStatus(0); |
| 45 | + $this->assertEquals(Post::IS_DRAFT, $post->status); |
| 46 | + } |
| 47 | + |
| 48 | + #[Test] |
| 49 | + public function post_can_toggle_featured_status() |
| 50 | + { |
| 51 | + $post = Post::factory()->create(['is_featured' => 0]); |
| 52 | + |
| 53 | + $this->assertEquals(0, $post->is_featured); |
| 54 | + |
| 55 | + $post->toggleFeatured(1); |
| 56 | + $this->assertEquals(1, $post->is_featured); |
| 57 | + |
| 58 | + $post->toggleFeatured(0); |
| 59 | + $this->assertEquals(0, $post->is_featured); |
| 60 | + } |
| 61 | + |
| 62 | + #[Test] |
| 63 | + public function post_returns_correct_category_title() |
| 64 | + { |
| 65 | + $category = Category::factory()->create(['title' => 'PHP Development']); |
| 66 | + $post = Post::factory()->create(['category_id' => $category->id]); |
| 67 | + |
| 68 | + $this->assertEquals('PHP Development', $post->getCategoryTitle()); |
| 69 | + } |
| 70 | + |
| 71 | + #[Test] |
| 72 | + public function post_returns_fallback_when_no_category() |
| 73 | + { |
| 74 | + $post = Post::factory()->create(['category_id' => null]); |
| 75 | + |
| 76 | + $this->assertEquals('No category', $post->getCategoryTitle()); |
| 77 | + } |
| 78 | + |
| 79 | + #[Test] |
| 80 | + public function post_returns_correct_tags_titles() |
| 81 | + { |
| 82 | + $post = Post::factory()->create(); |
| 83 | + $tag1 = Tag::factory()->create(['title' => 'Laravel']); |
| 84 | + $tag2 = Tag::factory()->create(['title' => 'PHP']); |
| 85 | + |
| 86 | + $post->tags()->attach([$tag1->id, $tag2->id]); |
| 87 | + |
| 88 | + $this->assertEquals('Laravel, PHP', $post->getTagsTitles()); |
| 89 | + } |
| 90 | + |
| 91 | + #[Test] |
| 92 | + public function post_returns_fallback_when_no_tags() |
| 93 | + { |
| 94 | + $post = Post::factory()->create(); |
| 95 | + |
| 96 | + $this->assertEquals('No tags', $post->getTagsTitles()); |
| 97 | + } |
| 98 | + |
| 99 | + #[Test] |
| 100 | + public function post_returns_default_image_when_no_image_uploaded() |
| 101 | + { |
| 102 | + $post = Post::factory()->create(['image' => null]); |
| 103 | + |
| 104 | + $this->assertEquals('/storage/blog_images/no-image.png', $post->getImage()); |
| 105 | + } |
| 106 | + |
| 107 | + #[Test] |
| 108 | + public function post_returns_uploaded_image_path_when_image_exists() |
| 109 | + { |
| 110 | + $post = Post::factory()->create(['image' => 'test-image.jpg']); |
| 111 | + |
| 112 | + $this->assertEquals('/storage/uploads/test-image.jpg', $post->getImage()); |
| 113 | + } |
| 114 | + |
| 115 | + #[Test] |
| 116 | + public function post_returns_correct_views_count() |
| 117 | + { |
| 118 | + $post = Post::factory()->create(['views_count' => 0]); |
| 119 | + $this->assertEquals(1, $post->getViewsCount()); |
| 120 | + |
| 121 | + $post = Post::factory()->create(['views_count' => 42]); |
| 122 | + $this->assertEquals(42, $post->getViewsCount()); |
| 123 | + } |
| 124 | + |
| 125 | + #[Test] |
| 126 | + public function post_can_increment_views_count() |
| 127 | + { |
| 128 | + $post = Post::factory()->create(['views_count' => 5]); |
| 129 | + |
| 130 | + $post->updateViewsCount(); |
| 131 | + |
| 132 | + $this->assertEquals(6, $post->views_count); |
| 133 | + } |
| 134 | + |
| 135 | + #[Test] |
| 136 | + public function post_returns_stripped_description() |
| 137 | + { |
| 138 | + $post = Post::factory()->create([ |
| 139 | + 'description' => '<p>This is <strong>HTML</strong> content</p>' |
| 140 | + ]); |
| 141 | + |
| 142 | + $this->assertEquals('This is HTML content', $post->getDescription()); |
| 143 | + } |
| 144 | + |
| 145 | + #[Test] |
| 146 | + public function post_returns_default_description_when_empty() |
| 147 | + { |
| 148 | + $post = Post::factory()->create(['description' => '']); |
| 149 | + |
| 150 | + $this->assertEquals( |
| 151 | + 'Programming reference for PHP, JS topics', |
| 152 | + $post->getDescription() |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + #[Test] |
| 157 | + public function post_has_category_returns_correct_boolean() |
| 158 | + { |
| 159 | + $category = Category::factory()->create(); |
| 160 | + $postWithCategory = Post::factory()->create(['category_id' => $category->id]); |
| 161 | + $postWithoutCategory = Post::factory()->create(['category_id' => null]); |
| 162 | + |
| 163 | + $this->assertTrue($postWithCategory->hasCategory()); |
| 164 | + $this->assertFalse($postWithoutCategory->hasCategory()); |
| 165 | + } |
| 166 | + |
| 167 | + #[Test] |
| 168 | + public function post_can_find_related_posts_by_category() |
| 169 | + { |
| 170 | + $category1 = Category::factory()->create(); |
| 171 | + $category2 = Category::factory()->create(); |
| 172 | + |
| 173 | + $post1 = Post::factory()->create(['category_id' => $category1->id]); |
| 174 | + $post2 = Post::factory()->create(['category_id' => $category1->id]); |
| 175 | + $post3 = Post::factory()->create(['category_id' => $category1->id]); |
| 176 | + |
| 177 | + Post::factory()->create(['category_id' => $category2->id]); |
| 178 | + |
| 179 | + $relatedPosts = $post1->related(); |
| 180 | + |
| 181 | + $this->assertLessThanOrEqual(5, $relatedPosts->count()); |
| 182 | + $this->assertFalse($relatedPosts->contains($post1)); |
| 183 | + $this->assertTrue($relatedPosts->contains($post2)); |
| 184 | + $this->assertTrue($relatedPosts->contains($post3)); |
| 185 | + } |
| 186 | +} |
0 commit comments