Skip to content

Commit 65e0435

Browse files
committed
Profile endpoints
1 parent 4e7dbf2 commit 65e0435

File tree

14 files changed

+205
-5
lines changed

14 files changed

+205
-5
lines changed

routes/api.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22

33
use Binaryk\LaravelRestify\Http\Controllers\GlobalSearchController;
4+
use Binaryk\LaravelRestify\Http\Controllers\ProfileAvatarController;
5+
use Binaryk\LaravelRestify\Http\Controllers\ProfileController;
6+
use Binaryk\LaravelRestify\Http\Controllers\ProfileUpdateController;
47
use Binaryk\LaravelRestify\Http\Controllers\RepositoryAttachController;
58
use Binaryk\LaravelRestify\Http\Controllers\RepositoryDestroyController;
69
use Binaryk\LaravelRestify\Http\Controllers\RepositoryDetachController;
@@ -14,6 +17,10 @@
1417
// Global Search...
1518
Route::get('/search', '\\'.GlobalSearchController::class);
1619

20+
Route::get('/profile', '\\' . ProfileController::class);
21+
Route::put('/profile', '\\' . ProfileUpdateController::class);
22+
Route::post('/profile/avatar', '\\' . ProfileAvatarController::class);
23+
1724
// Filters
1825
Route::get('/{repository}/filters', '\\'.RepositoryFilterController::class);
1926

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Http\Controllers;
4+
5+
use Binaryk\LaravelRestify\Http\Requests\ProfileAvatarRequest;
6+
7+
class ProfileAvatarController extends RepositoryController
8+
{
9+
public function __invoke(ProfileAvatarRequest $request)
10+
{
11+
$user = $request->user();
12+
13+
$request->validate([
14+
'avatar' => 'required|image',
15+
]);
16+
17+
ProfileAvatarRequest::$path = "avatars/{$user->getKey()}";
18+
19+
$path = is_callable(ProfileAvatarRequest::$pathCallback) ? call_user_func(ProfileAvatarRequest::$pathCallback, $request) : $request::$path;
20+
21+
$path = $request->file('avatar')->store($path);
22+
23+
$user->{$request::$userAvatarAttribute} = $path;
24+
$user->save();
25+
26+
return $this->response()->data($user->fresh());
27+
}
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Http\Controllers;
4+
5+
use Binaryk\LaravelRestify\Http\Requests\ProfileAvatarRequest;
6+
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
7+
8+
class ProfileController extends RepositoryController
9+
{
10+
public function __invoke(RestifyRequest $request)
11+
{
12+
$user = $request->user();
13+
14+
if (isset($user->{ProfileAvatarRequest::$userAvatarAttribute})) {
15+
$user->{ProfileAvatarRequest::$userAvatarAttribute} = url($user->{ProfileAvatarRequest::$userAvatarAttribute});
16+
}
17+
18+
return $this->response()->data($request->user());
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Http\Controllers;
4+
5+
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
6+
7+
class ProfileUpdateController extends RepositoryController
8+
{
9+
public function __invoke(RestifyRequest $request)
10+
{
11+
$user = $request->user();
12+
13+
$request->validate([
14+
'email' => 'sometimes|required|unique:users,email,except,'.$user->id,
15+
]);
16+
17+
$user->update($request->only($user->getFillable()));
18+
19+
return $this->response()->data($user->fresh());
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Http\Requests;
4+
5+
use Closure;
6+
7+
class ProfileAvatarRequest extends RestifyRequest
8+
{
9+
/**
10+
* @var Closure
11+
*/
12+
public static $pathCallback;
13+
14+
/**
15+
* @var string
16+
*/
17+
public static string $path = 'avatars';
18+
19+
/**
20+
* @var string
21+
*/
22+
public static string $userAvatarAttribute = 'avatar';
23+
24+
public static function usingPath(callable $pathCallback)
25+
{
26+
static::$pathCallback = $pathCallback;
27+
}
28+
29+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Tests\Controllers;
4+
5+
use Binaryk\LaravelRestify\Tests\Fixtures\User\User;
6+
use Binaryk\LaravelRestify\Tests\IntegrationTest;
7+
use Illuminate\Http\UploadedFile;
8+
use Illuminate\Support\Facades\Storage;
9+
10+
class ProfileControllerTest extends IntegrationTest
11+
{
12+
protected function setUp(): void
13+
{
14+
parent::setUp();
15+
16+
$this->authenticate(factory(User::class)->create([
17+
'name' => 'Eduard Lupacescu',
18+
'email' => '[email protected]',
19+
]));
20+
}
21+
22+
public function test_profile_returns_authenticated_user()
23+
{
24+
$response = $this->getJson('/restify-api/profile')
25+
->assertStatus(200)
26+
->assertJsonStructure([
27+
'data'
28+
]);
29+
30+
$response->assertJsonFragment([
31+
'email' => $this->authenticatedAs->email,
32+
]);
33+
}
34+
35+
public function test_profile_update()
36+
{
37+
$response = $this->putJson('restify-api/profile', [
38+
'email' => '[email protected]',
39+
'name' => 'Eduard',
40+
])
41+
->assertStatus(200);
42+
43+
$response->assertJsonFragment([
44+
'email' => '[email protected]',
45+
'name' => 'Eduard',
46+
]);
47+
}
48+
49+
public function test_profile_update_unique_email()
50+
{
51+
factory(User::class)->create([
52+
'email' => '[email protected]',
53+
]);
54+
55+
$this->putJson('restify-api/profile', [
56+
'email' => '[email protected]',
57+
'name' => 'Eduard',
58+
])->assertStatus(400);
59+
}
60+
61+
public function test_profile_upload_avatar()
62+
{
63+
$file = UploadedFile::fake()->image($this->getTestJpg())->size(100);
64+
65+
$this->postJson('restify-api/profile/avatar', [
66+
'avatar' => $file
67+
])
68+
->assertStatus(200);
69+
}
70+
}

tests/Fixtures/User/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class User extends Authenticatable implements Sanctumable, MustVerifyEmail, Rest
3636
* @var array
3737
*/
3838
protected $fillable = [
39-
'name', 'email', 'password', 'email_verified_at',
39+
'name', 'email', 'password', 'email_verified_at', 'avatar'
4040
];
4141

4242
/**

tests/IntegrationTest.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,17 @@ public function loadRepositories()
191191

192192
/**
193193
* Authenticate as an anonymous user.
194+
* @param Authenticatable|null $user
195+
* @return IntegrationTest
194196
*/
195-
protected function authenticate()
197+
protected function authenticate(Authenticatable $user = null)
196198
{
197-
$this->actingAs($this->authenticatedAs = Mockery::mock(Authenticatable::class));
199+
$this->actingAs($this->authenticatedAs = $user ?? Mockery::mock(Authenticatable::class));
198200

199-
$this->authenticatedAs->shouldReceive('getAuthIdentifier')->andReturn(1);
200-
$this->authenticatedAs->shouldReceive('getKey')->andReturn(1);
201+
if (is_null($user)) {
202+
$this->authenticatedAs->shouldReceive('getAuthIdentifier')->andReturn(1);
203+
$this->authenticatedAs->shouldReceive('getKey')->andReturn(1);
204+
}
201205

202206
return $this;
203207
}
@@ -243,4 +247,24 @@ public function mockPosts($userId, $count = 1)
243247

244248
return $users->shuffle(); // randomly shuffles the items in the collection
245249
}
250+
251+
public function getTempDirectory($suffix = ''): string
252+
{
253+
return __DIR__.'/TestSupport/temp'.($suffix == '' ? '' : '/'.$suffix);
254+
}
255+
256+
public function getMediaDirectory($suffix = ''): string
257+
{
258+
return $this->getTempDirectory().'/media'.($suffix == '' ? '' : '/'.$suffix);
259+
}
260+
261+
public function getTestFilesDirectory($suffix = ''): string
262+
{
263+
return $this->getTempDirectory().'/testfiles'.($suffix == '' ? '' : '/'.$suffix);
264+
}
265+
266+
public function getTestJpg(): string
267+
{
268+
return $this->getTestFilesDirectory('test.jpg');
269+
}
246270
}

tests/Migrations/2017_10_10_000000_create_users_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function up()
1616
Schema::create('users', function (Blueprint $table) {
1717
$table->increments('id');
1818
$table->string('name');
19+
$table->string('avatar')->nullable();
1920
$table->string('email')->unique();
2021
$table->string('password');
2122
$table->timestamp('email_verified_at')->nullable();
11.7 KB
Loading

0 commit comments

Comments
 (0)