Skip to content

Commit 007ca8a

Browse files
committed
added unit tests and ci fix
1 parent b6b0cb6 commit 007ca8a

File tree

3 files changed

+191
-5
lines changed

3 files changed

+191
-5
lines changed

app-laravel/api-laravel/app/Models/Post.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,14 @@ public function toggleFeatured($value): void
197197

198198
public function getCategoryTitle(): string
199199
{
200-
return $this->category->title ?? 'Нет категории';
200+
return $this->category->title ?? 'No category';
201201
}
202202

203203
public function getTagsTitles(): string
204204
{
205205
return (!$this->tags->isEmpty())
206206
? implode(', ', $this->tags->pluck('title')->all())
207-
: 'Нет тегов';
207+
: 'No tags';
208208
}
209209

210210
public function getCategoryID()
@@ -266,7 +266,7 @@ public function updateViewsCount(): void
266266
public function getDescription(): string
267267
{
268268
return empty($this->description)
269-
? 'Справочник по тематике программирования на языках PHP, JS'
269+
? 'Programming reference for Golang, PHP and JS topics'
270270
: strip_tags((string)$this->description);
271271
}
272272

@@ -277,7 +277,7 @@ public function getTitle(): string
277277
}
278278

279279
if (empty($this->title)) {
280-
return 'Лучшие практики PHP, JS, SQL, блог программиста.';
280+
return 'Best practices Golang, PHP, JS and SQL blog.';
281281
}
282282

283283
return $this->title;

app-laravel/api-laravel/resources/views/layout.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ class="w-20 h-20 object-cover rounded"></a>
202202
<div class="text-center text-gray-400">
203203
&copy; 2019-@php echo date('Y'); @endphp <a href="#" class="hover:text-white">Web3 blog, </a> Designed
204204
with
205-
<i class="fas fa-heart text-red-500"></i> and <a href="#" class="hover:text-white">Laravel 11</a>
205+
<i class="fas fa-heart text-red-500"></i> and <a href="#" class="hover:text-white">Laravel 12</a>
206206
</div>
207207
</div>
208208
</div>
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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

Comments
 (0)