Skip to content

Commit c43faba

Browse files
author
Julien Neuhart
committed
Minor refactore of update profile picture use case
1 parent c6d8f2c commit c43faba

File tree

6 files changed

+42
-42
lines changed

6 files changed

+42
-42
lines changed

src/api/src/UseCase/User/CreateUser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function create(
8686

8787
if ($profilePicture === null) {
8888
$this->userDao->save($user);
89+
$this->resetPassword->resetPassword($email);
8990

9091
return $user;
9192
}
@@ -97,7 +98,6 @@ public function create(
9798
$user,
9899
$profilePicture
99100
);
100-
101101
$this->resetPassword->resetPassword($email);
102102

103103
return $user;

src/api/src/UseCase/User/UpdateProfilePicture.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,9 @@ public function __construct(
3838
*/
3939
public function updateProfilePicture(
4040
User $user,
41-
?UploadedFileInterface $profilePicture = null
41+
UploadedFileInterface $profilePicture
4242
): User {
43-
$storable = null;
44-
if ($profilePicture !== null) {
45-
$storable = ProfilePicture::createFromUploadedFile($profilePicture);
46-
}
43+
$storable = ProfilePicture::createFromUploadedFile($profilePicture);
4744

4845
return $this->update($user, $storable);
4946
}
@@ -54,15 +51,8 @@ public function updateProfilePicture(
5451
*/
5552
public function update(
5653
User $user,
57-
?ProfilePicture $profilePicture = null
54+
ProfilePicture $profilePicture
5855
): User {
59-
if ($profilePicture === null) {
60-
$user->setProfilePicture(null);
61-
$this->userDao->save($user);
62-
63-
return $user;
64-
}
65-
6656
// We validate the new profile picture before
6757
// doing anything else.
6858
$this->profilePictureStorage->validate($profilePicture);

src/api/tests/UseCase/User/CreateUserTest.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use App\Domain\Enum\Locale;
66
use App\Domain\Enum\Role;
7+
use App\Domain\Model\Storable\ProfilePicture;
78
use App\Domain\Throwable\InvalidModel;
89
use App\Tests\UseCase\DummyValues;
910
use App\UseCase\User\CreateUser;
1011

1112
use function PHPUnit\Framework\assertEquals;
13+
use function PHPUnit\Framework\assertNotNull;
1214
use function PHPUnit\Framework\assertNull;
1315

1416
it(
@@ -18,17 +20,26 @@ function (
1820
string $lastName,
1921
string $email,
2022
Locale $locale,
21-
Role $role
23+
Role $role,
24+
?string $filename
2225
): void {
2326
$createUser = self::$container->get(CreateUser::class);
2427
assert($createUser instanceof CreateUser);
2528

26-
$user = $createUser->createUser(
29+
$storable = null;
30+
if ($filename !== null) {
31+
$storable = ProfilePicture::createFromPath(
32+
dirname(__FILE__) . '/' . $filename
33+
);
34+
}
35+
36+
$user = $createUser->create(
2737
$firstName,
2838
$lastName,
2939
$email,
3040
$locale,
31-
$role
41+
$role,
42+
$storable
3243
);
3344

3445
assertEquals($firstName, $user->getFirstName());
@@ -37,11 +48,17 @@ function (
3748
assertNull($user->getPassword());
3849
assertEquals($locale, $user->getLocale());
3950
assertEquals($role, $user->getRole());
51+
52+
if ($filename !== null) {
53+
assertNotNull($user->getProfilePicture());
54+
} else {
55+
assertNull($user->getProfilePicture());
56+
}
4057
}
4158
)
4259
->with([
43-
['foo', 'bar', 'foo.bar@baz.com', Locale::EN(), Role::ADMINISTRATOR()],
44-
['foo', 'bar', 'foo.bar@baz.com', Locale::EN(), Role::USER()],
60+
['foo', 'bar', 'foo.bar@baz.com', Locale::EN(), Role::ADMINISTRATOR(), null],
61+
['foo', 'bar', 'foo.bar@baz.com', Locale::EN(), Role::USER(), 'foo.png'],
4562
])
4663
->group('user');
4764

src/api/tests/UseCase/User/UpdateProfilePictureTest.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use function PHPUnit\Framework\assertFalse;
1515
use function PHPUnit\Framework\assertNotNull;
16-
use function PHPUnit\Framework\assertNull;
1716
use function PHPUnit\Framework\assertTrue;
1817

1918
beforeEach(function (): void {
@@ -32,28 +31,22 @@
3231
});
3332

3433
it('updates a profile picture', function (?string $filename): void {
35-
$userDao = self::$container->get(UserDao::class);
36-
assert($userDao instanceof UserDao);
37-
$updateProfilePicture = self::$container->get(UpdateProfilePicture::class);
38-
assert($updateProfilePicture instanceof UpdateProfilePicture);
39-
$profilePictureStorage = self::$container->get(ProfilePictureStorage::class);
40-
assert($profilePictureStorage instanceof ProfilePictureStorage);
41-
42-
$user = $userDao->getById('1');
34+
$userDao = self::$container->get(UserDao::class);
35+
assert($userDao instanceof UserDao);
36+
$updateProfilePicture = self::$container->get(UpdateProfilePicture::class);
37+
assert($updateProfilePicture instanceof UpdateProfilePicture);
38+
$profilePictureStorage = self::$container->get(ProfilePictureStorage::class);
39+
assert($profilePictureStorage instanceof ProfilePictureStorage);
40+
41+
$user = $userDao->getById('1');
42+
$storable = ProfilePicture::createFromPath(
43+
dirname(__FILE__) . '/' . $filename
44+
);
45+
$user = $updateProfilePicture->update($user, $storable);
4346

44-
if ($filename !== null) {
45-
$storable = ProfilePicture::createFromPath(
46-
dirname(__FILE__) . '/' . $filename
47-
);
48-
$user = $updateProfilePicture->update($user, $storable);
49-
assertTrue($profilePictureStorage->fileExists($user->getProfilePicture()));
50-
} else {
51-
$user = $updateProfilePicture->update($user, null);
52-
assertNull($user->getProfilePicture());
53-
}
47+
assertTrue($profilePictureStorage->fileExists($user->getProfilePicture()));
5448
})
5549
->with([
56-
null,
5750
'foo.png',
5851
'foo.jpg',
5952
])

src/api/tests/UseCase/User/UpdateUserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function (
7171
assertEquals($role, $user->getRole());
7272

7373
if ($filename !== null) {
74-
assertNotNull($filename);
74+
assertNotNull($user->getProfilePicture());
7575
} else {
7676
assertNull($user->getProfilePicture());
7777
}
@@ -110,7 +110,7 @@ function (
110110
Role::USER()
111111
);
112112

113-
$updateUser->update(
113+
$updateUser->updateUser(
114114
$userDao->getById('1'),
115115
$firstName,
116116
$lastName,

src/webapp/graphql/users/update_profile_picture.mutation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { gql } from 'graphql-request'
22

33
export const UpdateProfilePictureMutation = gql`
4-
mutation updateProfilePicture($profilePicture: Upload) {
4+
mutation updateProfilePicture($profilePicture: Upload!) {
55
updateProfilePicture(profilePicture: $profilePicture) {
66
profilePicture
77
}

0 commit comments

Comments
 (0)