Skip to content

Commit 7c12a23

Browse files
committed
Add tests
1 parent f9641c2 commit 7c12a23

File tree

7 files changed

+167
-23
lines changed

7 files changed

+167
-23
lines changed

app/Jobs/LikeThread.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public function __construct(Thread $thread, User $user)
3939
*/
4040
public function handle()
4141
{
42-
try {
43-
$this->thread->likedBy($this->user);
44-
} catch (QueryException $exception) {
42+
if ($this->thread->isLikedBy($this->user)) {
4543
throw new CannotLikeThreadMultipleTimes();
4644
}
45+
46+
$this->thread->likedBy($this->user);
4747
}
4848
}

database/factories/LikeFactory.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use App\Models\Like;
4+
use App\Models\Reply;
5+
use App\Models\Thread;
6+
use App\User;
7+
8+
$factory->define(Like::class, function (Faker\Generator $faker, array $attributes = []) {
9+
return [
10+
'user_id' => function () {
11+
return factory(User::class)->create()->id;
12+
},
13+
];
14+
});
15+
16+
$factory->state(Like::class, 'reply', [
17+
'likeable_id' => function () {
18+
return factory(Reply::class)->create()->id;
19+
},
20+
'likeable_type' => 'replies',
21+
]);
22+
23+
$factory->state(Like::class, 'thread', [
24+
'likeable_id' => function () {
25+
return factory(Thread::class)->create()->id;
26+
},
27+
'likeable_type' => 'threads',
28+
]);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Tests\Components\Jobs;
4+
5+
use App\Exceptions\CannotLikeThreadMultipleTimes;
6+
use App\Jobs\LikeThread;
7+
use App\Models\Thread;
8+
use App\User;
9+
use Illuminate\Foundation\Testing\DatabaseMigrations;
10+
use Tests\TestCase;
11+
12+
class LikeThreadTest extends TestCase
13+
{
14+
use DatabaseMigrations;
15+
16+
/** @test */
17+
public function we_can_like_a_thread()
18+
{
19+
$user = factory(User::class)->create();
20+
$thread = factory(Thread::class)->create();
21+
22+
$this->dispatch(new LikeThread($thread, $user));
23+
24+
$this->assertTrue($thread->fresh()->isLikedBy($user));
25+
}
26+
27+
/** @test */
28+
public function we_cannot_like_a_thread_twice()
29+
{
30+
$user = factory(User::class)->create();
31+
$thread = factory(Thread::class)->create();
32+
33+
$this->dispatch(new LikeThread($thread, $user));
34+
35+
$this->assertTrue($thread->fresh()->isLikedBy($user));
36+
37+
$this->expectException(CannotLikeThreadMultipleTimes::class);
38+
39+
$this->dispatch(new LikeThread($thread, $user));
40+
}
41+
}

tests/Components/Jobs/UnlikeReplyTest.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class UnlikeReplyTest extends TestCase
1313
use DatabaseMigrations;
1414

1515
/** @test */
16-
public function we_can_dislike_a_reply()
16+
public function we_can_unlike_a_reply()
1717
{
1818
$user = factory(User::class)->create();
1919
$reply = factory(Reply::class)->create();
@@ -25,20 +25,4 @@ public function we_can_dislike_a_reply()
2525

2626
$this->assertFalse($reply->fresh()->isLikedBy($user));
2727
}
28-
29-
/** @test */
30-
public function we_cannot_dislike_a_reply()
31-
{
32-
$user = factory(User::class)->create();
33-
$reply = factory(Reply::class)->create();
34-
35-
$reply->likedBy($user);
36-
$this->assertTrue($reply->fresh()->isLikedBy($user));
37-
38-
$this->dispatch(new UnlikeReply($reply, $user));
39-
40-
$this->dispatch(new UnlikeReply($reply, $user));
41-
42-
//$this->assertFalse($reply->fresh()->isLikedBy($user));
43-
}
4428
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Tests\Components\Jobs;
4+
5+
use App\Jobs\UnlikeThread;
6+
use App\Models\Thread;
7+
use App\User;
8+
use Illuminate\Foundation\Testing\DatabaseMigrations;
9+
use Tests\TestCase;
10+
11+
class UnlikeThreadTest extends TestCase
12+
{
13+
use DatabaseMigrations;
14+
15+
/** @test */
16+
public function we_can_unlike_a_thread()
17+
{
18+
$user = factory(User::class)->create();
19+
$thread = factory(Thread::class)->create();
20+
21+
$thread->likedBy($user);
22+
$this->assertTrue($thread->fresh()->isLikedBy($user));
23+
24+
$this->dispatch(new UnlikeThread($thread, $user));
25+
26+
$this->assertFalse($thread->fresh()->isLikedBy($user));
27+
}
28+
}

tests/Feature/ForumTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Tests\Feature;
44

5+
use App\Models\Like;
56
use App\Models\Reply;
67
use App\Models\Tag;
78
use App\Models\Thread;
9+
use App\User;
810
use Illuminate\Foundation\Testing\DatabaseMigrations;
911
use Tests\BrowserKitTestCase;
1012

@@ -170,4 +172,39 @@ public function users_cannot_edit_a_thread_with_a_subject_that_is_too_long()
170172
$response->assertSessionHas('error', 'Something went wrong. Please review the fields below.');
171173
$response->assertSessionHasErrors(['subject' => 'The subject may not be greater than 60 characters.']);
172174
}
175+
176+
/** @test */
177+
public function users_can_like_a_thread()
178+
{
179+
$user = factory(User::class)->create();
180+
$thread = factory(Thread::class)->create(['author_id' => $user->id(), 'slug' => 'the-first-thread']);
181+
182+
$this->loginAs($user);
183+
$this->put("/forum/{$thread->slug}/like")
184+
->assertRedirectedTo("/forum/the-first-thread");
185+
186+
$this->seeInDatabase('likes', [
187+
'user_id' => $user->id,
188+
'likeable_id' => $thread->id,
189+
'likeable_type' => 'threads'
190+
]);
191+
}
192+
193+
/** @test */
194+
public function users_can_unlike_a_thread()
195+
{
196+
$user = factory(User::class)->create();
197+
$thread = factory(Thread::class)->create(['author_id' => $user->id(), 'slug' => 'the-first-thread']);
198+
factory(Like::class)->states('thread')->create(['user_id' => $user->id, 'likeable_id' => $thread->id]);
199+
200+
$this->loginAs($user);
201+
$this->delete("/forum/{$thread->slug}/unlike")
202+
->assertRedirectedTo("/forum/the-first-thread");
203+
204+
$this->notSeeInDatabase('likes', [
205+
'user_id' => $user->id,
206+
'likeable_id' => $thread->id,
207+
'likeable_type' => 'threads'
208+
]);
209+
}
173210
}

tests/Feature/ReplyTest.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Feature;
44

5+
use App\Models\Like;
56
use App\Models\Reply;
67
use App\Models\Thread;
78
use App\User;
@@ -118,16 +119,41 @@ public function unconfirmed_users_cannot_see_the_reply_input()
118119
}
119120

120121
/** @test */
121-
public function user_can_like_a_reply()
122+
public function users_can_like_a_reply()
122123
{
123124
$user = factory(User::class)->create();
124125

125126
$thread = factory(Thread::class)->create(['author_id' => $user->id(), 'slug' => 'the-first-thread']);
126127
$reply = factory(Reply::class)->create(['replyable_id' => $thread->id()]);
127128

128-
$this->login();
129-
$this->visit("/forum/{$thread->slug}");
129+
$this->loginAs($user);
130130
$this->put("/replies/{$reply->id()}/like")
131131
->assertRedirectedTo("/forum/the-first-thread#{$reply->id}");
132+
133+
$this->seeInDatabase('likes', [
134+
'user_id' => $user->id,
135+
'likeable_id' => $reply->id,
136+
'likeable_type' => 'replies'
137+
]);
138+
}
139+
140+
/** @test */
141+
public function users_can_unlike_a_reply()
142+
{
143+
$user = factory(User::class)->create();
144+
145+
$thread = factory(Thread::class)->create(['author_id' => $user->id(), 'slug' => 'the-first-thread']);
146+
$reply = factory(Reply::class)->create(['replyable_id' => $thread->id()]);
147+
factory(Like::class)->states('reply')->create(['user_id' => $user->id, 'likeable_id' => $reply->id]);
148+
149+
$this->loginAs($user);
150+
$this->delete("/replies/{$reply->id()}/unlike")
151+
->assertRedirectedTo("/forum/the-first-thread#{$reply->id}");
152+
153+
$this->notSeeInDatabase('likes', [
154+
'user_id' => $user->id,
155+
'likeable_id' => $reply->id,
156+
'likeable_type' => 'replies'
157+
]);
132158
}
133159
}

0 commit comments

Comments
 (0)