Skip to content

Commit 8322644

Browse files
committed
Dynamic profile setup
1 parent 32bde9d commit 8322644

File tree

4 files changed

+115
-12
lines changed

4 files changed

+115
-12
lines changed

src/Http/Controllers/ProfileController.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Binaryk\LaravelRestify\Http\Requests\ProfileAvatarRequest;
66
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
77
use Binaryk\LaravelRestify\Repositories\Repository;
8+
use Binaryk\LaravelRestify\Repositories\UserProfile;
89
use Binaryk\LaravelRestify\Services\Search\RepositorySearchService;
910

1011
class ProfileController extends RepositoryController
@@ -28,7 +29,7 @@ public function __invoke(RestifyRequest $request)
2829
$meta = [];
2930

3031
if (method_exists($user, 'profile')) {
31-
$meta = (array) call_user_func([$user, 'profile'], $request);
32+
$meta = (array)call_user_func([$user, 'profile'], $request);
3233
}
3334

3435
return $this->response()
@@ -40,12 +41,12 @@ public function guessRepository(RestifyRequest $request): ?Repository
4041
{
4142
$repository = $request->repository('users');
4243

43-
if (! $repository) {
44+
if (!$repository) {
4445
return null;
4546
}
4647

4748
if (method_exists($repository, 'canUseForProfile')) {
48-
if (! call_user_func($repository, 'canUseForProfile', $request)) {
49+
if (!call_user_func([$repository, 'canUseForProfile'], $request)) {
4950
return null;
5051
}
5152
}

src/Repositories/UserProfile.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Repositories;
4+
5+
use Illuminate\Http\Request;
6+
7+
trait UserProfile
8+
{
9+
public static $canUseForProfile = false;
10+
11+
public static $metaProfile = [];
12+
13+
public static function canUseForProfile(Request $request)
14+
{
15+
return is_callable(static::$canUseForProfile)
16+
? forward_static_call(static::$canUseForProfile, $request)
17+
: static::$canUseForProfile;
18+
}
19+
20+
public static function metaProfile(Request $request): array
21+
{
22+
return static::$metaProfile;
23+
}
24+
25+
public function resolveShowMeta($request)
26+
{
27+
return [
28+
'authorizedToShow' => $this->authorizedToShow($request),
29+
'authorizedToStore' => $this->authorizedToStore($request),
30+
'authorizedToUpdate' => $this->authorizedToUpdate($request),
31+
'authorizedToDelete' => $this->authorizedToDelete($request),
32+
] + static::metaProfile($request);
33+
}
34+
}

tests/Controllers/ProfileControllerTest.php

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Binaryk\LaravelRestify\Tests\Controllers;
44

55
use Binaryk\LaravelRestify\Tests\Fixtures\User\User;
6+
use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository;
67
use Binaryk\LaravelRestify\Tests\IntegrationTest;
78
use Illuminate\Http\UploadedFile;
89
use Illuminate\Support\Facades\Hash;
@@ -17,14 +18,18 @@ protected function setUp(): void
1718
'name' => 'Eduard Lupacescu',
1819
'email' => '[email protected]',
1920
]));
21+
22+
$this->mockPosts(
23+
$this->authenticatedAs->id
24+
);
2025
}
2126

2227
public function test_profile_returns_authenticated_user()
2328
{
2429
$response = $this->getJson('/restify-api/profile')
2530
->assertStatus(200)
2631
->assertJsonStructure([
27-
'attributes',
32+
'data',
2833
]);
2934

3035
$response->assertJsonFragment([
@@ -34,24 +39,28 @@ public function test_profile_returns_authenticated_user()
3439

3540
public function test_profile_returns_authenticated_user_with_related_posts()
3641
{
37-
$response = $this->getJson('/restify-api/profile?related=posts')
42+
$this->getJson('/restify-api/profile?related=posts')
3843
->assertStatus(200)
3944
->assertJsonStructure([
40-
'attributes',
45+
'data' => [
46+
'posts' => [
47+
[
48+
'title'
49+
]
50+
]
51+
]
4152
]);
42-
43-
$response->assertJsonFragment([
44-
'email' => $this->authenticatedAs->email,
45-
'posts' => [],
46-
]);
4753
}
4854

4955
public function test_profile_returns_authenticated_user_with_meta_profile_data()
5056
{
5157
$this->getJson('/restify-api/profile')
5258
->assertStatus(200)
5359
->assertJsonStructure([
54-
'meta',
60+
'data',
61+
'meta' => [
62+
'roles',
63+
],
5564
]);
5665
}
5766

@@ -120,4 +129,60 @@ public function test_profile_validation_from_repository()
120129
],
121130
]);
122131
}
132+
133+
public function test_get_profile_can_use_repository()
134+
{
135+
UserRepository::$canUseForProfile = true;
136+
137+
$response = $this->getJson('/restify-api/profile')
138+
->assertStatus(200)
139+
->assertJsonStructure([
140+
'attributes',
141+
'meta'
142+
]);
143+
144+
$response->assertJsonFragment([
145+
'email' => $this->authenticatedAs->email,
146+
]);
147+
}
148+
149+
public function test_profile_returns_authenticated_user_with_related_posts_via_repository()
150+
{
151+
UserRepository::$canUseForProfile = true;
152+
153+
$response = $this->getJson('/restify-api/profile?related=posts')
154+
->assertStatus(200)
155+
->assertJsonStructure([
156+
'attributes',
157+
'relationships' => [
158+
'posts' => [
159+
[
160+
'attributes'
161+
]
162+
]
163+
]
164+
]);
165+
166+
$response->assertJsonFragment([
167+
'email' => $this->authenticatedAs->email,
168+
]);
169+
}
170+
171+
public function test_profile_returns_authenticated_user_with_meta_profile_data_via_repository()
172+
{
173+
UserRepository::$canUseForProfile = true;
174+
175+
UserRepository::$metaProfile = [
176+
'roles' => '',
177+
];
178+
179+
$this->getJson('/restify-api/profile')
180+
->assertStatus(200)
181+
->assertJsonStructure([
182+
'attributes',
183+
'meta' => [
184+
'roles',
185+
],
186+
]);
187+
}
123188
}

tests/Fixtures/User/UserRepository.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
use Binaryk\LaravelRestify\Fields\Field;
77
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
88
use Binaryk\LaravelRestify\Repositories\Repository;
9+
use Binaryk\LaravelRestify\Repositories\UserProfile;
910

1011
/**
1112
* @author Eduard Lupacescu <[email protected]>
1213
*/
1314
class UserRepository extends Repository
1415
{
16+
use UserProfile;
17+
1518
public static $model = User::class;
1619

1720
public static $search = [

0 commit comments

Comments
 (0)