Skip to content

Commit b048d7e

Browse files
committed
test: feature test UserEnrolledCoursesController
1 parent 0956284 commit b048d7e

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

tests/Feature/App/Courses/Http/Controllers/ShowControllerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function test_it_returns_unauthorized_response_when_user_is_not_authentic
6868
$response->assertUnauthorized();
6969
}
7070

71-
public function test_it_returns_unauthorized_response_when_user_is_not_enrolled_to_course(): void
71+
public function test_it_returns_forbidden_response_when_user_is_not_enrolled_to_course(): void
7272
{
7373
// Given
7474
$user = User::factory()->create();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Feature\App\Courses\Http\Controllers;
6+
7+
use Domain\Courses\Models\Course;
8+
use Domain\Users\Models\User;
9+
use Illuminate\Contracts\Routing\UrlGenerator;
10+
use Illuminate\Foundation\Testing\RefreshDatabase;
11+
use Tests\TestCase;
12+
13+
class UserEnrolledCoursesControllerTest extends TestCase
14+
{
15+
use RefreshDatabase;
16+
17+
private UrlGenerator $urlGenerator;
18+
19+
protected function setUp(): void
20+
{
21+
parent::setUp();
22+
23+
$this->urlGenerator = $this->app->get(UrlGenerator::class);
24+
}
25+
26+
public function test_it_can_list_the_enrolled_courses_of_a_user(): void
27+
{
28+
// Given
29+
$user = User::factory()->create();
30+
$courses = Course::factory(2)->create();
31+
$user->enrolledCourses()->sync($courses->pluck('id'));
32+
33+
$firstCourse = $courses->get(0);
34+
$secondCourse = $courses->get(1);
35+
36+
$endpoint = $this->urlGenerator->route('api.v1.users.show.enrolled_courses.index', [
37+
'user' => $user,
38+
]);
39+
40+
// When
41+
$response = $this
42+
->actingAs($user, 'api')
43+
->getJson($endpoint);
44+
45+
// Then
46+
$response->assertOk();
47+
$response->assertJson([
48+
'data' => [
49+
[
50+
'uuid' => $firstCourse->uuid,
51+
'title' => $firstCourse->title,
52+
'content' => $firstCourse->content,
53+
'created_at' => $firstCourse->created_at->toISOString(),
54+
'updated_at' => $firstCourse->updated_at->toISOString(),
55+
],
56+
[
57+
'uuid' => $secondCourse->uuid,
58+
'title' => $secondCourse->title,
59+
'content' => $secondCourse->content,
60+
'created_at' => $secondCourse->created_at->toISOString(),
61+
'updated_at' => $secondCourse->updated_at->toISOString(),
62+
],
63+
],
64+
]);
65+
}
66+
67+
public function test_it_returns_unauthorized_response_when_user_is_not_authenticated(): void
68+
{
69+
// Given
70+
$user = User::factory()->create();
71+
72+
$endpoint = $this->urlGenerator->route('api.v1.users.show.enrolled_courses.index', [
73+
'user' => $user,
74+
]);
75+
76+
// When
77+
$response = $this->getJson($endpoint);
78+
79+
// Then
80+
$response->assertUnauthorized();
81+
}
82+
83+
public function test_it_returns_forbidden_response_when_user_is_not_allowed_to_view_the_user(): void
84+
{
85+
// Given
86+
$user = User::factory()->create();
87+
$userToView = User::factory()->create();
88+
89+
$endpoint = $this->urlGenerator->route('api.v1.users.show.enrolled_courses.index', [
90+
'user' => $userToView,
91+
]);
92+
93+
// When
94+
$response = $this
95+
->actingAs($user, 'api')
96+
->getJson($endpoint);
97+
98+
// Then
99+
$response->assertForbidden();
100+
}
101+
}

0 commit comments

Comments
 (0)