Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d9ddf27

Browse files
Get started with TDD | Update User Profile API
1 parent f8799ab commit d9ddf27

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
@@ -29,4 +29,6 @@
2929
Route::get('email/resend', 'VerificationController@resend')->name('verification.resend');
3030
Route::post('password/email', 'ForgotPasswordController@forgot');
3131
Route::post('password/reset', 'ForgotPasswordController@reset');
32+
33+
Route::patch('user/profile', 'UserController@updateProfile');
3234
});

‎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)

AltStyle によって変換されたページ (->オリジナル) /