Files
ppeasy_vg12/tests/Feature/Api/Profile/DetailsControllerTest.php
T
2026-10-09 08:21:06 +02:00

98 lines
2.5 KiB
PHP

<?php
use App\Http\Resources\UserResource;
use Carbon\Carbon;
test('get user profile unauthenticated', function () {
$this->getJson('/api/me')->assertStatus(401);
});
test('get user profile', function () {
$user = $this->login();
$this->getJson('/api/me')
->assertOk()
->assertJson(['data' => (new UserResource($user))->resolve()]);
});
test('update user profile unauthenticated', function () {
$this->patchJson('/api/me/details')->assertStatus(401);
});
test('update user profile', function () {
$user = $this->login();
$data = getData();
$response = $this->patchJson('/api/me/details', $data);
$transformed = (new UserResource($user->fresh()))->resolve();
$response->assertJsonFragment($transformed);
$this->assertDatabaseHas('users', array_merge($data, ['id' => $user->id]));
});
test('partially update user details', function () {
$user = $this->login();
$data = [
'first_name' => 'John',
'last_name' => 'Doe',
];
$response = $this->patchJson('/api/me/details', $data);
$transformed = (new UserResource($user->fresh()))->resolve();
$response->assertJsonFragment($transformed);
$this->assertDatabaseHas('users', array_merge($data, [
'id' => $user->id,
'birthday' => $user->birthday->format('Y-m-d'),
'phone' => $user->phone,
'address' => $user->address,
'country_id' => $user->country_id,
]));
});
test('update without country id', function () {
$user = $this->login();
$data = getData();
unset($data['country_id']);
$response = $this->patchJson('/api/me/details', $data);
$transformed = (new UserResource($user->fresh()))->resolve();
$response->assertJsonFragment($transformed);
$this->assertDatabaseHas('users', array_merge($data, ['id' => $user->id]));
});
test('update with invalid date format', function () {
$this->login();
$this->patchJson('/api/me/details', ['birthday' => 'foo'])
->assertStatus(422)
->assertJsonFragment([
'birthday' => [
trans('validation.date', ['attribute' => 'birthday']),
],
]);
});
function getData(array $attrs = []): array
{
return array_merge([
'first_name' => 'John',
'last_name' => 'Doe',
'birthday' => Carbon::now()->subYears(25)->format('Y-m-d'),
'phone' => '(123) 456 789',
'address' => 'some address 1',
'country_id' => 688,
], $attrs);
}