Skip to content

[make:user] add class name for UniqueConstraint #1722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Maker/MakeUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen

$manipulator->setIo($io);

$this->userClassBuilder->addUserInterfaceImplementation($manipulator, $userClassConfiguration);
$this->userClassBuilder->addUserInterfaceImplementation($manipulator, $userClassConfiguration, $userClassNameDetails->getShortName());

$generator->dumpFile($classPath, $manipulator->getSourceCode());

Expand Down
8 changes: 4 additions & 4 deletions src/Security/UserClassBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
*/
final class UserClassBuilder
{
public function addUserInterfaceImplementation(ClassSourceManipulator $manipulator, UserClassConfiguration $userClassConfig): void
public function addUserInterfaceImplementation(ClassSourceManipulator $manipulator, UserClassConfiguration $userClassConfig, string $className): void
{
$manipulator->addInterface(UserInterface::class);

$this->addUniqueConstraint($manipulator, $userClassConfig);
$this->addUniqueConstraint($manipulator, $userClassConfig, $className);

$this->addGetUsername($manipulator, $userClassConfig);

Expand Down Expand Up @@ -332,7 +332,7 @@ private function addSerialize(ClassSourceManipulator $manipulator): void
$manipulator->addMethodBuilder($builder);
}

private function addUniqueConstraint(ClassSourceManipulator $manipulator, UserClassConfiguration $userClassConfig): void
private function addUniqueConstraint(ClassSourceManipulator $manipulator, UserClassConfiguration $userClassConfig, string $className): void
{
if (!$userClassConfig->isEntity()) {
return;
Expand All @@ -341,7 +341,7 @@ private function addUniqueConstraint(ClassSourceManipulator $manipulator, UserCl
$manipulator->addAttributeToClass(
'ORM\\UniqueConstraint',
[
'name' => 'UNIQ_IDENTIFIER_'.strtoupper(Str::asSnakeCase($userClassConfig->getIdentityPropertyName())),
'name' => strtoupper(Str::asSnakeCase($className)).'_UNIQ_IDENTIFIER_'.strtoupper(Str::asSnakeCase($userClassConfig->getIdentityPropertyName())),
'fields' => [$userClassConfig->getIdentityPropertyName()],
]
);
Expand Down
11 changes: 9 additions & 2 deletions tests/Security/UserClassBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class UserClassBuilderTest extends TestCase
/**
* @dataProvider getUserInterfaceTests
*/
public function testAddUserInterfaceImplementation(UserClassConfiguration $userClassConfig, string $expectedFilename): void
public function testAddUserInterfaceImplementation(UserClassConfiguration $userClassConfig, string $expectedFilename, string $className): void
{
$manipulator = $this->getClassSourceManipulator($userClassConfig);

$classBuilder = new UserClassBuilder();
$classBuilder->addUserInterfaceImplementation($manipulator, $userClassConfig);
$classBuilder->addUserInterfaceImplementation($manipulator, $userClassConfig, $className);

$expectedPath = $this->getExpectedPath($expectedFilename, null);
$expectedSource = file_get_contents($expectedPath);
Expand All @@ -49,36 +49,43 @@ public function getUserInterfaceTests(): \Generator
yield 'entity_with_email_as_identifier' => [
new UserClassConfiguration(true, 'email', true),
'UserEntityWithEmailAsIdentifier.php',
'User',
];

yield 'entity_with_password' => [
new UserClassConfiguration(true, 'userIdentifier', true),
'UserEntityWithPassword.php',
'User',
];

yield 'entity_with_user_identifier_as_identifier' => [
new UserClassConfiguration(true, 'user_identifier', true),
'UserEntityWithUser_IdentifierAsIdentifier.php',
'User',
];

yield 'entity_without_password' => [
new UserClassConfiguration(true, 'userIdentifier', false),
'UserEntityWithoutPassword.php',
'User',
];

yield 'model_with_email_as_identifier' => [
new UserClassConfiguration(false, 'email', true),
'UserModelWithEmailAsIdentifier.php',
'User',
];

yield 'model_with_password' => [
new UserClassConfiguration(false, 'userIdentifier', true),
'UserModelWithPassword.php',
'User',
];

yield 'model_without_password' => [
new UserClassConfiguration(false, 'userIdentifier', false),
'UserModelWithoutPassword.php',
'User',
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symfony\Component\Security\Core\User\UserInterface;

#[ORM\Entity]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])]
#[ORM\UniqueConstraint(name: 'USER_UNIQ_IDENTIFIER_EMAIL', fields: ['email'])]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symfony\Component\Security\Core\User\UserInterface;

#[ORM\Entity]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USER_IDENTIFIER', fields: ['userIdentifier'])]
#[ORM\UniqueConstraint(name: 'USER_UNIQ_IDENTIFIER_USER_IDENTIFIER', fields: ['userIdentifier'])]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symfony\Component\Security\Core\User\UserInterface;

#[ORM\Entity]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USER_IDENTIFIER', fields: ['user_identifier'])]
#[ORM\UniqueConstraint(name: 'USER_UNIQ_IDENTIFIER_USER_IDENTIFIER', fields: ['user_identifier'])]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Symfony\Component\Security\Core\User\UserInterface;

#[ORM\Entity]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USER_IDENTIFIER', fields: ['userIdentifier'])]
#[ORM\UniqueConstraint(name: 'USER_UNIQ_IDENTIFIER_USER_IDENTIFIER', fields: ['userIdentifier'])]
class User implements UserInterface
{
#[ORM\Id]
Expand Down
Loading