I'm studying Laravel 11 and I have my first problem using PHPStorm (I don't know if it's my test or not).
I have this controller:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class LoginController extends Controller
{
public function login(Request $request)
{
$request->validate([
'email' => 'email|string',
'password' => 'required|min:8',
]);
$credentials = request(['email', 'password']);
if (!$token = auth()->attempt($credentials)) {
return jsonResponse(status: 401, message: 'Unauthorized');
}
return jsonResponse(data: [
'token' => $token,
'expires_in' => auth()->factory()->getTTL() * 60
]);
}
}
My test code:
public function test_email_must_be_required(): void
{
$credentials = ['password' => 'password'];
$response = $this->postJson("{$this->apiBase}/login", $credentials);
$response->assertStatus(422);
$response->assertJsonStructure(['message', 'data', 'status', 'errors' => ['email']]);
$response->assertJsonFragment(['errors' => ['email' => ['The email field is required.']]]);
}
But when I run the test, I have the following response:
However, it should not return 401 because the email in the test credentials is not being sent. It should return response code 422 instead.
[EDIT] When I change my validate method:
...
'email' => 'required|email|string',
...
My code response is 200:
[EDIT 2]
This is my routes/api.php code:


