Skip to content

Commit ce3a2e1

Browse files
committed
update profile
1 parent e6232f8 commit ce3a2e1

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
class UserController extends Controller
8+
{
9+
public function __construct() {
10+
$this->middleware('auth:api');
11+
}
12+
13+
public function updateProfile() {
14+
$attributes = request()->validate(['name' => 'nullable|string']);
15+
16+
auth()->user()->update($attributes);
17+
18+
return $this->respondWithMessage("User successfully updated");
19+
}
20+
}

routes/api.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@
2727
Route::post('register', 'RegistrationController@register');
2828
Route::get('email/verify/{id}', 'VerificationController@verify')->name('verification.verify');
2929
Route::get('email/resend', 'VerificationController@resend')->name('verification.resend');
30+
31+
Route::patch('user/profile', 'UserController@updateProfile');
3032
});

tests/Feature/UsersTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use App\User;
6+
use Tests\TestCase;
7+
use JWTAuth;
8+
9+
class UsersTest extends TestCase
10+
{
11+
/** @test */
12+
public function a_user_can_edit_his_profile() {
13+
$user = User::first();
14+
15+
$token = JWTAuth::fromUser($user);
16+
17+
$attributes = ['name' => $this->faker->name];
18+
19+
$this->patchJson('api/user/profile', $attributes, ['Authorization' => "bearer $token"])
20+
->assertStatus(200);
21+
22+
$this->assertDatabaseHas($user->getTable(), array_merge($attributes, [
23+
'id' => $user->id
24+
]));
25+
}
26+
}

tests/TestCase.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@
33
namespace Tests;
44

55
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
6+
use Illuminate\Foundation\Testing\WithFaker;
67

78
abstract class TestCase extends BaseTestCase
89
{
9-
use CreatesApplication;
10+
use CreatesApplication, WithFaker;
11+
12+
protected function setUp() : void {
13+
parent::setUp();
14+
15+
$this->artisan('migrate');
16+
$this->artisan('db:seed');
17+
18+
$this->withoutExceptionHandling();
19+
}
1020
}

0 commit comments

Comments
 (0)