Skip to content

Commit

Permalink
#469 enum p15
Browse files Browse the repository at this point in the history
  • Loading branch information
eventhorizonpl committed Jan 27, 2022
1 parent e5b2204 commit 94b898b
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 37 deletions.
10 changes: 6 additions & 4 deletions src/Entity/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,9 @@ class Profile
private bool $showMotivationalMessages;

#[Assert\Choice(callback: 'getThemeValidationChoices')]
#[Assert\Length(max: 8)]
#[Assert\Type('string')]
#[Assert\Type(ProfileThemeEnum::class)]
#[Groups(['gdpr'])]
#[ORM\Column(length: 8, nullable: true, type: Types::STRING)]
#[ORM\Column(enumType: ProfileThemeEnum::class, length: 8, nullable: true, type: Types::STRING)]
private ?ProfileThemeEnum $theme;

#[Assert\Length(max: 36)]
Expand Down Expand Up @@ -306,7 +305,10 @@ public static function getThemeFormChoices(): array

public static function getThemeValidationChoices(): array
{
return array_keys(self::getThemeFormChoices());
return [
ProfileThemeEnum::DARK,
ProfileThemeEnum::LIGHT,
];
}

public function setTheme(?ProfileThemeEnum $theme): self
Expand Down
12 changes: 8 additions & 4 deletions src/Entity/Routine.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,10 @@ class Routine
private ?string $name;

#[Assert\Choice(callback: 'getTypeValidationChoices', groups: ['form', 'system'])]
#[Assert\Length(groups: ['form', 'system'], max: 16)]
#[Assert\NotBlank(groups: ['form', 'system'])]
#[Assert\Type('string', groups: ['form', 'system'])]
#[Assert\Type(RoutineTypeEnum::class, groups: ['form', 'system'])]
#[Groups(['gdpr'])]
#[ORM\Column(length: 16, type: Types::STRING)]
#[ORM\Column(enumType: RoutineTypeEnum::class, length: 16, type: Types::STRING)]
private RoutineTypeEnum $type;

public function __construct()
Expand Down Expand Up @@ -468,7 +467,12 @@ public static function getTypeFormChoices(): array

public static function getTypeValidationChoices(): array
{
return array_keys(self::getTypeFormChoices());
return [
RoutineTypeEnum::HOBBY,
RoutineTypeEnum::LEARNING,
RoutineTypeEnum::SPORT,
RoutineTypeEnum::WORK,
];
}

public function setType(RoutineTypeEnum $type): self
Expand Down
10 changes: 6 additions & 4 deletions src/Entity/SavedEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ class SavedEmail
private string $email;

#[Assert\Choice(callback: 'getTypeValidationChoices', groups: ['system'])]
#[Assert\Length(groups: ['system'], max: 16)]
#[Assert\NotBlank(groups: ['system'])]
#[Assert\Type('string', groups: ['system'])]
#[Assert\Type(SavedEmailTypeEnum::class, groups: ['system'])]
#[Groups(['gdpr'])]
#[ORM\Column(length: 16, type: Types::STRING)]
#[ORM\Column(enumType: SavedEmailTypeEnum::class, length: 16, type: Types::STRING)]
private SavedEmailTypeEnum $type;

public function __construct()
Expand Down Expand Up @@ -81,7 +80,10 @@ public static function getTypeFormChoices(): array

public static function getTypeValidationChoices(): array
{
return array_keys(self::getTypeFormChoices());
return [
SavedEmailTypeEnum::INVITATION,
SavedEmailTypeEnum::MOTIVATIONAL,
];
}

public function setType(SavedEmailTypeEnum $type): self
Expand Down
12 changes: 8 additions & 4 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ class User implements UserInterface, TwoFactorInterface
private array $roles = [];

#[Assert\Choice(callback: 'getTypeValidationChoices', groups: ['system'])]
#[Assert\Length(groups: ['system'], max: 8)]
#[Assert\NotBlank(groups: ['system'])]
#[Assert\Type('string', groups: ['system'])]
#[ORM\Column(length: 8, type: Types::STRING)]
#[Assert\Type(UserTypeEnum::class, groups: ['system'])]
#[ORM\Column(enumType: UserTypeEnum::class, length: 8, type: Types::STRING)]
private UserTypeEnum $type;

public function __construct()
Expand Down Expand Up @@ -750,7 +749,12 @@ public static function getTypeFormChoices(): array

public static function getTypeValidationChoices(): array
{
return array_keys(self::getTypeFormChoices());
return [
UserTypeEnum::CUSTOMER,
UserTypeEnum::LEAD,
UserTypeEnum::PROSPECT,
UserTypeEnum::STAFF,
];
}

public function setType(UserTypeEnum $type): self
Expand Down
7 changes: 3 additions & 4 deletions src/Faker/RoutineFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Faker;

use App\Entity\Routine;
use App\Enum\RoutineTypeEnum;
use App\Factory\RoutineFactory;
use Faker\{Factory, Generator};

Expand All @@ -22,7 +23,7 @@ public function createRoutine(
?string $description = null,
?bool $isEnabled = null,
?string $name = null,
?string $type = null
?RoutineTypeEnum $type = null
): Routine {
if (null === $description) {
$description = (string) $this->faker->text();
Expand All @@ -37,9 +38,7 @@ public function createRoutine(
}

if (null === $type) {
$type = (string) $this->faker->randomElement(
Routine::getTypeFormChoices()
);
$type = RoutineTypeEnum::HOBBY;
}

$routine = $this->routineFactory->createRoutineWithRequired(
Expand Down
7 changes: 3 additions & 4 deletions src/Faker/SavedEmailFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Faker;

use App\Entity\SavedEmail;
use App\Enum\SavedEmailTypeEnum;
use App\Factory\SavedEmailFactory;
use Faker\{Factory, Generator};

Expand All @@ -20,16 +21,14 @@ public function __construct(

public function createSavedEmail(
?string $email = null,
?string $type = null
?SavedEmailTypeEnum $type = null
): SavedEmail {
if (null === $email) {
$email = (string) $this->faker->safeEmail();
}

if (null === $type) {
$type = (string) $this->faker->randomElement(
SavedEmail::getTypeFormChoices()
);
$type = SavedEmailTypeEnum::INVITATION;
}

return $this->savedEmailFactory->createSavedEmailWithRequired(
Expand Down
20 changes: 9 additions & 11 deletions src/Faker/UserFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function createUser(
?bool $isEnabled = null,
?string $password = null,
?array $roles = null,
?string $type = null
?UserTypeEnum $type = null
): User {
if (null === $email) {
$email = (string) $this->faker->safeEmail();
Expand All @@ -70,13 +70,11 @@ public function createUser(
}

if (null === $roles) {
$roles = [UserRoleEnum::ROLE_USER];
$roles = [UserRoleEnum::ROLE_USER->value];
}

if (null === $type) {
$type = (string) $this->faker->randomElement(
User::getTypeFormChoices()
);
$type = UserTypeEnum::STAFF;
}

$user = $this->userFactory->createUserWithRequired(
Expand All @@ -94,7 +92,7 @@ public function createUserPersisted(
?bool $isEnabled = null,
?string $password = null,
?array $roles = null,
?string $type = null
?UserTypeEnum $type = null
): User {
$user = $this->createUser(
$email,
Expand Down Expand Up @@ -125,9 +123,9 @@ public function createAdminUserPersisted(
true,
$password,
[
UserRoleEnum::ROLE_ADMIN,
UserRoleEnum::ROLE_SUPER_ADMIN,
UserRoleEnum::ROLE_USER,
UserRoleEnum::ROLE_ADMIN->value,
UserRoleEnum::ROLE_SUPER_ADMIN->value,
UserRoleEnum::ROLE_USER->value,
],
UserTypeEnum::STAFF
);
Expand All @@ -149,7 +147,7 @@ public function createCustomerUserPersisted(
$email,
true,
$password,
[UserRoleEnum::ROLE_USER],
[UserRoleEnum::ROLE_USER->value],
UserTypeEnum::CUSTOMER
);
}
Expand All @@ -170,7 +168,7 @@ public function createRichUserPersisted(
$email,
true,
$password,
[UserRoleEnum::ROLE_USER],
[UserRoleEnum::ROLE_USER->value],
UserTypeEnum::CUSTOMER
);

Expand Down
4 changes: 2 additions & 2 deletions tests/Faker/UserFakerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function testCreateUser(): void
$email = 'test@example.org';
$isEnabled = true;
$password = 'test password';
$roles = [UserRoleEnum::ROLE_USER];
$roles = [UserRoleEnum::ROLE_USER->value];
$type = UserTypeEnum::STAFF;
$user = $this->userFaker->createUser(
$email,
Expand All @@ -219,7 +219,7 @@ public function testCreateUserPersisted(): void
$email = 'test@example.org';
$isEnabled = true;
$password = 'test password';
$roles = [UserRoleEnum::ROLE_USER];
$roles = [UserRoleEnum::ROLE_USER->value];
$type = UserTypeEnum::STAFF;
$user = $this->userFaker->createUserPersisted(
$email,
Expand Down

0 comments on commit 94b898b

Please sign in to comment.