Skip to content

Commit 9140473

Browse files
committed
test: add TestimonialTest for API endpoint CRUD operations
1 parent 84c4839 commit 9140473

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

app/Http/Controllers/Api/V1/Testimonial/TestimonialController.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,14 @@ public function store(StoreTestimonialRequest $request)
4242
if (!$user) {
4343
return response()->json($this->errorResponse('Unauthorized. Please log in.', Response::HTTP_UNAUTHORIZED));
4444
}
45-
45+
4646
try {
47-
// Check if user has a name, if not use a fallback
48-
$userName = $user->name ?? $user->username ?? 'Anonymous User';
49-
5047
$testimonial = Testimonial::create([
5148
'user_id' => $user->id,
52-
'name' => $userName,
49+
'name' => $request->get('name') ?? 'Anonymous User', // Use request name, fallback to 'Anonymous User'
5350
'content' => $request->get('content'),
5451
]);
55-
52+
5653
return response()->json($this->successResponse('Testimonial created successfully', $testimonial->toArray()), Response::HTTP_CREATED);
5754
} catch (\Exception $e) {
5855
return response()->json($this->errorResponse('Internal Server Error. Please try again later.', Response::HTTP_INTERNAL_SERVER_ERROR, ['error' => $e->getMessage()]));

tests/Feature/TestimonialTest.php

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,39 @@ public function testUnauthenticatedUserCannotCreateTestimonial()
2525
]);
2626
}
2727

28-
public function testAuthenticatedUserCanCreateTestimonial()
29-
{
30-
// Create a user with a known password
31-
$user = User::factory()->create(['password' => bcrypt('password')]);
32-
33-
// Get a JWT token
34-
$token = JWTAuth::attempt(['email' => $user->email, 'password' => 'password']);
35-
36-
// Make an authenticated request
37-
$response = $this->postJson('/api/v1/testimonials', [
38-
'content' => 'This is a testimonial.',
39-
], [
40-
'Authorization' => 'Bearer ' . $token,
41-
]);
42-
43-
$response->assertStatus(201);
44-
$response->assertJson([
45-
'status' => 'success',
46-
'message' => 'Testimonial created successfully',
47-
'data' => [
48-
'name' => $user->name,
49-
'content' => 'This is a testimonial.',
50-
],
51-
]);
52-
}
28+
public function testAuthenticatedUserCanCreateTestimonialWithAnonymousName()
29+
{
30+
// Create a user with a known password
31+
$user = User::factory()->create(['password' => bcrypt('password')]);
32+
33+
// Get a JWT token
34+
$token = JWTAuth::attempt(['email' => $user->email, 'password' => 'password']);
35+
36+
// Make an authenticated request without a name
37+
$response = $this->postJson('/api/v1/testimonials', [
38+
'content' => 'This is a testimonial without a name.',
39+
], [
40+
'Authorization' => 'Bearer ' . $token,
41+
]);
42+
43+
$response->assertStatus(201);
44+
$response->assertJson([
45+
'status' => 'success',
46+
'message' => 'Testimonial created successfully',
47+
'data' => [
48+
'name' => 'Anonymous User', // Expecting the fallback
49+
'content' => 'This is a testimonial without a name.',
50+
'user_id' => $user->id,
51+
],
52+
]);
53+
54+
// Verify the testimonial exists in the database
55+
$this->assertDatabaseHas('testimonials', [
56+
'user_id' => $user->id,
57+
'name' => 'Anonymous User',
58+
'content' => 'This is a testimonial without a name.',
59+
]);
60+
}
5361

5462
public function testValidationErrorsAreReturnedForMissingData()
5563
{

0 commit comments

Comments
 (0)