diff --git a/docs/configuration.rst b/docs/configuration.rst index 3bcbe34b..e4a130ca 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -9,14 +9,9 @@ All available configuration options are listed below with their default values. use_listener: true use_flash_notifications: true use_authentication_listener: true - profile: - form: - model: Nucleos\ProfileBundle\Form\Model\Profile registration: confirmation: from_email: ~ # Required enabled: false # change to true for required email confirmation - form: - model: Nucleos\ProfileBundle\Form\Model\Registration service: mailer: nucleos_profile.mailer.default diff --git a/docs/customize_profile.rst b/docs/customize_profile.rst index 87e16ddf..b1125451 100644 --- a/docs/customize_profile.rst +++ b/docs/customize_profile.rst @@ -1,15 +1,56 @@ Customize Profile ================= -.. note:: +If the user is allowed to change some date, you can modify the profile form. It is possible to add, remove or modify form fields provided by NucleosProfileBundle. - Work in progress! -Add more fields ---------------- +How to customize a Form +----------------------- -- Extend ``Nucleos\ProfileBundle\Form\Model\Profile`` -- Add custom form model to configuration -- Use symfony FormExtensions +If you want to modify the Profile form in your project there are a few steps that you should take. For example if you would like to add checkbox for accepting terms and conditions you will have to follow these steps: +1. Extend ``Nucleos\UserBundle\Model\User\User`` +.. code-block:: php-annotations + + namespace App\Model; + + use Nucleos\UserBundle\Model\User as BaseUser; + + class User extends BaseUser + { + protected ?bool $termsAccepted = false; + + public function gettermsAccepted(): ?bool + { + return $this->termsAccepted; + } + + public function setTermsAccepted(?bool $termsAccepted): void + { + $this->termsAccepted = $termsAccepted; + } + } + +2. Use Symfony Form Extensions to add fields. You can use builder to remove or modify fields as well. + +.. code-block:: php-annotations + + namespace App\Form\Type; + + use Symfony\Component\Form\AbstractTypeExtension; + use Symfony\Component\Form\Extension\Core\Type\CheckboxType; + use Symfony\Component\Form\FormBuilderInterface; + + class ProfileFormType extends AbstractTypeExtension + { + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder->add('termsAccepted', CheckboxType::class); + } + + public static function getExtendedTypes(): iterable + { + return ['Nucleos\ProfileBundle\Form\Type\ProfileFormType']; + } + } diff --git a/docs/customize_registration.rst b/docs/customize_registration.rst index 996ad7b3..ce67a6fe 100644 --- a/docs/customize_registration.rst +++ b/docs/customize_registration.rst @@ -1,11 +1,6 @@ Customize Registration ====================== -.. note:: - - Work in progress! - - Your business needs might require changes in registration form. It is possible to add, remove or modify form fields provided by NucleosProfileBundle. How to customize a Form @@ -13,48 +8,30 @@ How to customize a Form If you want to modify the Registration form in your project there are a few steps that you should take. For example if you would like to add checkbox for accepting terms and conditions you will have to follow these steps: -1. Extend ``Nucleos\ProfileBundle\Form\Model\Registration`` +1. Extend ``Nucleos\UserBundle\Model\User\User`` .. code-block:: php-annotations - namespace App\Form\Model; + namespace App\Model; - use Nucleos\ProfileBundle\Form\Model\Registration as BaseRegistration; + use Nucleos\UserBundle\Model\User as BaseUser; - class Registration extends BaseRegistration + class User extends BaseUser { - /** - * @var bool|null - */ - protected $termsAccepted; - - /** - * @return bool|null - */ + protected ?bool $termsAccepted = false; + public function gettermsAccepted(): ?bool { return $this->termsAccepted; } - /** - * @param Boolean|null $termsAccepted - */ public function setTermsAccepted(?bool $termsAccepted): void { $this->termsAccepted = $termsAccepted; } } -2. Add custom form model to configuration - -.. code-block:: yaml - - nucleos_profile: - registration: - form: - model: App\Form\Model\Registration - -3. Use Symfony Form Extensions to add fields. You can use builder to remove or modify fields as well. +2. Use Symfony Form Extensions to add fields. You can use builder to remove or modify fields as well. .. code-block:: php-annotations @@ -68,7 +45,6 @@ If you want to modify the Registration form in your project there are a few step { public function buildForm(FormBuilderInterface $builder, array $options) { - // Adding new fields works just like in the parent form type. $builder->add('termsAccepted', CheckboxType::class); } diff --git a/infection.json b/infection.json index 4e81d814..6603b168 100644 --- a/infection.json +++ b/infection.json @@ -9,6 +9,6 @@ "text": "build/infection/infection-log.txt" }, "ignoreMsiWithNoMutations": true, - "minMsi": 15, - "minCoveredMsi": 40 + "minMsi": 10, + "minCoveredMsi": 35 } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index afcf802c..364905f7 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,7 +1,2 @@ parameters: ignoreErrors: - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: src/Form/Model/Profile.php - diff --git a/src/Action/EditProfileAction.php b/src/Action/EditProfileAction.php index 096f4242..317e6fe1 100644 --- a/src/Action/EditProfileAction.php +++ b/src/Action/EditProfileAction.php @@ -11,9 +11,7 @@ namespace Nucleos\ProfileBundle\Action; -use LogicException; use Nucleos\ProfileBundle\Event\UserFormEvent; -use Nucleos\ProfileBundle\Form\Model\Profile; use Nucleos\ProfileBundle\Form\Type\ProfileFormType; use Nucleos\ProfileBundle\NucleosProfileEvents; use Nucleos\UserBundle\Event\FilterUserResponseEvent; @@ -22,6 +20,7 @@ use Nucleos\UserBundle\Model\UserManagerInterface; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\FormFactoryInterface; +use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -45,22 +44,13 @@ final class EditProfileAction private Security $security; - /** - * @phpstan-var class-string - */ - private string $formModel; - - /** - * @phpstan-param class-string $formModel - */ public function __construct( EventDispatcherInterface $eventDispatcher, FormFactoryInterface $formFactory, UserManagerInterface $userManager, Environment $twig, RouterInterface $router, - Security $security, - string $formModel + Security $security ) { $this->eventDispatcher = $eventDispatcher; $this->formFactory = $formFactory; @@ -68,7 +58,6 @@ public function __construct( $this->twig = $twig; $this->router = $router; $this->security = $security; - $this->formModel = $formModel; } public function __invoke(Request $request): Response @@ -86,10 +75,8 @@ public function __invoke(Request $request): Response return $event->getResponse(); } - $formModel = $this->createFormModel($user); - - $form = $this->formFactory - ->create(ProfileFormType::class, $formModel, [ + $form = $this->formFactory + ->create(ProfileFormType::class, $user, [ 'validation_groups' => ['Profile', 'Default'], ]) ->add('save', SubmitType::class, [ @@ -100,39 +87,30 @@ public function __invoke(Request $request): Response $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $formModel->updateUser($user); - - $event = new UserFormEvent($user, $form, $request); - $this->eventDispatcher->dispatch($event, NucleosProfileEvents::PROFILE_EDIT_SUCCESS); + return $this->updateUser($request, $form, $user); + } - $this->userManager->updateUser($user); + return new Response($this->twig->render('@NucleosProfile/Profile/edit.html.twig', [ + 'form' => $form->createView(), + ])); + } - if (null === $response = $event->getResponse()) { - $url = $this->router->generate('nucleos_profile_profile_show'); - $response = new RedirectResponse($url); - } + private function updateUser(Request $request, FormInterface $form, UserInterface $user): Response + { + $event = new UserFormEvent($user, $form, $request); + $this->eventDispatcher->dispatch($event, NucleosProfileEvents::PROFILE_EDIT_SUCCESS); - $this->eventDispatcher->dispatch( - new FilterUserResponseEvent($user, $request, $response), - NucleosProfileEvents::PROFILE_EDIT_COMPLETED - ); + $this->userManager->updateUser($user); - return $response; + if (null === $response = $event->getResponse()) { + $response = new RedirectResponse($this->router->generate('nucleos_profile_profile_show')); } - return new Response( - $this->twig->render('@NucleosProfile/Profile/edit.html.twig', [ - 'form' => $form->createView(), - ]) + $this->eventDispatcher->dispatch( + new FilterUserResponseEvent($user, $request, $response), + NucleosProfileEvents::PROFILE_EDIT_COMPLETED ); - } - - private function createFormModel(UserInterface $user): Profile - { - if (!is_a($this->formModel, Profile::class, true)) { - throw new LogicException(sprintf('The "%s" is not a valid "%s" class', $this->formModel, Profile::class)); - } - return ($this->formModel)::fromUser($user); + return $response; } } diff --git a/src/Action/RegistrationAction.php b/src/Action/RegistrationAction.php index 7bd2835d..78c78475 100644 --- a/src/Action/RegistrationAction.php +++ b/src/Action/RegistrationAction.php @@ -13,11 +13,11 @@ use Nucleos\ProfileBundle\Event\GetResponseRegistrationEvent; use Nucleos\ProfileBundle\Event\UserFormEvent; -use Nucleos\ProfileBundle\Form\Model\Registration; use Nucleos\ProfileBundle\Form\Type\RegistrationFormType; use Nucleos\ProfileBundle\NucleosProfileEvents; use Nucleos\UserBundle\Event\FilterUserResponseEvent; use Nucleos\UserBundle\Event\FormEvent; +use Nucleos\UserBundle\Model\UserInterface; use Nucleos\UserBundle\Model\UserManagerInterface; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\FormFactoryInterface; @@ -41,43 +41,33 @@ final class RegistrationAction private RouterInterface $router; - /** - * @phpstan-var class-string - */ - private string $formModel; - - /** - * @phpstan-param class-string $formModel - */ public function __construct( EventDispatcherInterface $eventDispatcher, FormFactoryInterface $formFactory, UserManagerInterface $userManager, RouterInterface $router, - Environment $twig, - string $formModel + Environment $twig ) { $this->eventDispatcher = $eventDispatcher; $this->formFactory = $formFactory; $this->userManager = $userManager; $this->router = $router; $this->twig = $twig; - $this->formModel = $formModel; } public function __invoke(Request $request): Response { - $formModel = $this->createFormModel(); + $user = $this->userManager->createUser(); - $event = new GetResponseRegistrationEvent($formModel, $request); + $event = new GetResponseRegistrationEvent($user, $request); $this->eventDispatcher->dispatch($event, NucleosProfileEvents::REGISTRATION_INITIALIZE); if (null !== $event->getResponse()) { return $event->getResponse(); } - $form = $this->formFactory - ->create(RegistrationFormType::class, $formModel, [ + $form = $this->formFactory + ->create(RegistrationFormType::class, $user, [ 'validation_groups' => ['Registration', 'Default'], ]) ->add('save', SubmitType::class, [ @@ -89,7 +79,7 @@ public function __invoke(Request $request): Response if ($form->isSubmitted()) { if ($form->isValid()) { - return $this->updateUser($request, $formModel, $form); + return $this->updateUser($request, $user, $form); } $event = new FormEvent($form, $request); @@ -105,23 +95,15 @@ public function __invoke(Request $request): Response ])); } - private function createFormModel(): Registration + private function updateUser(Request $request, UserInterface $user, FormInterface $form): Response { - return new $this->formModel(); - } - - private function updateUser(Request $request, Registration $formModel, FormInterface $form): Response - { - $user = $formModel->toUser($this->userManager); - $event = new UserFormEvent($user, $form, $request); $this->eventDispatcher->dispatch($event, NucleosProfileEvents::REGISTRATION_SUCCESS); $this->userManager->updateUser($user); if (null === $response = $event->getResponse()) { - $url = $this->router->generate('nucleos_profile_registration_confirmed'); - $response = new RedirectResponse($url); + $response = new RedirectResponse($this->router->generate('nucleos_profile_registration_confirmed')); } $this->eventDispatcher->dispatch( diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 5996a3ba..9ce5075a 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -11,8 +11,6 @@ namespace Nucleos\ProfileBundle\DependencyInjection; -use Nucleos\ProfileBundle\Form\Model\Profile; -use Nucleos\ProfileBundle\Form\Model\Registration; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; @@ -40,7 +38,6 @@ public function getConfigTreeBuilder(): TreeBuilder ; $this->addRegistrationSection($rootNode); - $this->addProfileSection($rootNode); $this->addServiceSection($rootNode); return $treeBuilder; @@ -53,12 +50,6 @@ private function addRegistrationSection(ArrayNodeDefinition $node): void ->arrayNode('registration') ->addDefaultsIfNotSet() ->children() - ->arrayNode('form') - ->addDefaultsIfNotSet() - ->children() - ->scalarNode('model')->defaultValue(Registration::class)->cannotBeEmpty()->end() - ->end() - ->end() ->arrayNode('confirmation') ->addDefaultsIfNotSet() ->children() @@ -72,25 +63,6 @@ private function addRegistrationSection(ArrayNodeDefinition $node): void ; } - private function addProfileSection(ArrayNodeDefinition $node): void - { - $node - ->children() - ->arrayNode('profile') - ->addDefaultsIfNotSet() - ->children() - ->arrayNode('form') - ->addDefaultsIfNotSet() - ->children() - ->scalarNode('model')->defaultValue(Profile::class)->cannotBeEmpty()->end() - ->end() - ->end() - ->end() - ->end() - ->end() - ; - } - private function addServiceSection(ArrayNodeDefinition $node): void { $node diff --git a/src/DependencyInjection/NucleosProfileExtension.php b/src/DependencyInjection/NucleosProfileExtension.php index 9e648e12..30170907 100644 --- a/src/DependencyInjection/NucleosProfileExtension.php +++ b/src/DependencyInjection/NucleosProfileExtension.php @@ -45,7 +45,7 @@ public function load(array $configs, ContainerBuilder $container): void } $this->loadRegistration($config['registration'], $container, $loader); - $this->loadProfile($config['profile'], $container, $loader); + $this->loadProfile($loader); $container->setAlias('nucleos_profile.mailer', $config['service']['mailer']); } @@ -61,17 +61,11 @@ private function loadRegistration(array $config, ContainerBuilder $container, Fi $loader->load('email_confirmation.php'); } - $container->setParameter('nucleos_profile.registration.form.model', $config['form']['model']); $container->setParameter('nucleos_profile.registration.confirmation.from_email', $config['confirmation']['from_email']); } - /** - * @param array $config - */ - private function loadProfile(array $config, ContainerBuilder $container, FileLoader $loader): void + private function loadProfile(FileLoader $loader): void { $loader->load('profile.php'); - - $container->setParameter('nucleos_profile.profile.form.model', $config['form']['model']); } } diff --git a/src/Event/GetResponseRegistrationEvent.php b/src/Event/GetResponseRegistrationEvent.php index b287123e..42764236 100644 --- a/src/Event/GetResponseRegistrationEvent.php +++ b/src/Event/GetResponseRegistrationEvent.php @@ -11,7 +11,7 @@ namespace Nucleos\ProfileBundle\Event; -use Nucleos\ProfileBundle\Form\Model\Registration; +use Nucleos\UserBundle\Model\UserInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Contracts\EventDispatcher\Event; @@ -20,14 +20,14 @@ class GetResponseRegistrationEvent extends Event { protected ?Request $request; - protected Registration $registration; + protected UserInterface $user; private ?Response $response = null; - public function __construct(Registration $user, Request $request = null) + public function __construct(UserInterface $user, Request $request = null) { - $this->registration = $user; - $this->request = $request; + $this->user = $user; + $this->request = $request; } public function setResponse(?Response $response): void @@ -40,9 +40,9 @@ public function getResponse(): ?Response return $this->response; } - public function getRegistration(): Registration + public function getUser(): UserInterface { - return $this->registration; + return $this->user; } public function getRequest(): ?Request diff --git a/src/Form/Model/Profile.php b/src/Form/Model/Profile.php deleted file mode 100644 index a5d7d12f..00000000 --- a/src/Form/Model/Profile.php +++ /dev/null @@ -1,62 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Nucleos\ProfileBundle\Form\Model; - -use Nucleos\UserBundle\Model\LocaleAwareInterface; -use Nucleos\UserBundle\Model\UserInterface; - -class Profile -{ - protected ?string $locale = null; - - protected ?string $timezone = null; - - public static function fromUser(UserInterface $user): self - { - $profile = new static(); - - if ($user instanceof LocaleAwareInterface) { - $profile->setLocale((string) $user->getLocale()); - $profile->setTimezone((string) $user->getTimezone()); - } - - return $profile; - } - - public function setLocale(?string $locale): void - { - $this->locale = $locale; - } - - public function getLocale(): ?string - { - return $this->locale; - } - - public function setTimezone(?string $timezone): void - { - $this->timezone = $timezone; - } - - public function getTimezone(): ?string - { - return $this->timezone; - } - - public function updateUser(UserInterface $user): void - { - if ($user instanceof LocaleAwareInterface) { - $user->setLocale((string) $this->getLocale()); - $user->setTimezone((string) $this->getTimezone()); - } - } -} diff --git a/src/Form/Model/Registration.php b/src/Form/Model/Registration.php deleted file mode 100644 index 2eb564ed..00000000 --- a/src/Form/Model/Registration.php +++ /dev/null @@ -1,65 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Nucleos\ProfileBundle\Form\Model; - -use Nucleos\UserBundle\Model\UserInterface; -use Nucleos\UserBundle\Model\UserManagerInterface; - -class Registration -{ - protected ?string $username = null; - - protected ?string $email = null; - - protected ?string $plainPassword = null; - - public function getUsername(): ?string - { - return $this->username; - } - - public function setUsername(?string $username): void - { - $this->username = $username; - } - - public function getEmail(): ?string - { - return $this->email; - } - - public function setEmail(?string $email): void - { - $this->email = $email; - } - - public function getPlainPassword(): ?string - { - return $this->plainPassword; - } - - public function setPlainPassword(?string $plainPassword): void - { - $this->plainPassword = $plainPassword; - } - - public function toUser(UserManagerInterface $manager): UserInterface - { - $user = $manager->createUser(); - $user->setEnabled(true); - $user->setUsername((string) $this->getUsername()); - $user->setEmail((string) $this->getEmail()); - $user->setPlainPassword((string) $this->getPlainPassword()); - - return $user; - } -} diff --git a/src/Form/Type/ProfileFormType.php b/src/Form/Type/ProfileFormType.php index d46bc5fd..872de974 100644 --- a/src/Form/Type/ProfileFormType.php +++ b/src/Form/Type/ProfileFormType.php @@ -11,7 +11,7 @@ namespace Nucleos\ProfileBundle\Form\Type; -use Nucleos\ProfileBundle\Form\Model\Profile; +use Nucleos\UserBundle\Model\UserInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\LocaleType; use Symfony\Component\Form\Extension\Core\Type\TimezoneType; @@ -20,19 +20,6 @@ final class ProfileFormType extends AbstractType { - /** - * @phpstan-var class-string - */ - private string $class; - - /** - * @phpstan-param class-string $class The User class name - */ - public function __construct(string $class) - { - $this->class = $class; - } - /** * @param array $options */ @@ -53,7 +40,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ - 'data_class' => $this->class, + 'data_class' => UserInterface::class, 'csrf_token_id' => 'profile', 'translation_domain' => 'NucleosProfileBundle', ]); diff --git a/src/Form/Type/RegistrationFormType.php b/src/Form/Type/RegistrationFormType.php index 3828652c..4361888b 100644 --- a/src/Form/Type/RegistrationFormType.php +++ b/src/Form/Type/RegistrationFormType.php @@ -11,58 +11,70 @@ namespace Nucleos\ProfileBundle\Form\Type; -use Nucleos\ProfileBundle\Form\Model\Registration; -use Nucleos\UserBundle\Model\UserManagerInterface; +use Exception; +use Nucleos\UserBundle\Model\UserInterface; use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\Event\PostSubmitEvent; +use Symfony\Component\Form\Exception\UnexpectedTypeException; use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\Extension\Core\Type\PasswordType; use Symfony\Component\Form\Extension\Core\Type\RepeatedType; use Symfony\Component\Form\Extension\Core\Type\TextType; -use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\Form\FormEvents; +use Symfony\Component\Form\FormInterface; use Symfony\Component\OptionsResolver\OptionsResolver; -use Symfony\Component\Validator\ConstraintViolation; -use Symfony\Component\Validator\ConstraintViolationInterface; -use Symfony\Component\Validator\Validator\ValidatorInterface; -use Throwable; final class RegistrationFormType extends AbstractType { - /** - * @phpstan-var class-string - */ - private string $class; - - private UserManagerInterface $userManager; - - private ValidatorInterface $validator; - - private ViolationMapper $violationMapper; - - /** - * @phpstan-param class-string $class The User class name - */ - public function __construct(string $class, UserManagerInterface $userManager, ValidatorInterface $validator) - { - $this->class = $class; - $this->validator = $validator; - $this->userManager = $userManager; - $this->violationMapper = new ViolationMapper(); - } - /** * @param array $options + * + * @SuppressWarnings(PHPMD.NPathComplexity) + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function buildForm(FormBuilderInterface $builder, array $options): void { $builder ->add('email', EmailType::class, [ 'label' => 'form.email', + 'getter' => static function (UserInterface $user, FormInterface $form): string { + try { + return $user->getEmail(); + } catch (Exception $exception) { + return ''; + } + }, + 'setter' => static function (UserInterface &$user, ?string $value, FormInterface $form): void { + if (null === $value) { + return; + } + + try { + $user->setEmail($value); + } catch (Exception $exception) { + throw new UnexpectedTypeException($value, 'string'); + } + }, ]) ->add('username', TextType::class, [ 'label' => 'form.username', + 'getter' => static function (UserInterface $user, FormInterface $form): string { + try { + return $user->getUsername(); + } catch (Exception $exception) { + return ''; + } + }, + 'setter' => static function (UserInterface &$user, ?string $value, FormInterface $form): void { + if (null === $value) { + return; + } + + try { + $user->setUsername($value); + } catch (Exception $exception) { + throw new UnexpectedTypeException($value, 'string'); + } + }, ]) ->add('plainPassword', RepeatedType::class, [ 'type' => PasswordType::class, @@ -75,46 +87,16 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'second_options' => ['label' => 'form.password_confirmation'], 'invalid_message' => 'nucleos_user.password.mismatch', ]) - ->addEventListener(FormEvents::POST_SUBMIT, function (PostSubmitEvent $event) use ($options): void { - $errors = $this->getUserErrors($event->getData(), (array) $options['validation_groups']); - - foreach ($errors as $error) { - \assert($error instanceof ConstraintViolation); - - $this->violationMapper->mapViolation($error, $event->getForm()); - } - }) ; } public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ - 'data_class' => $this->class, + 'data_class' => UserInterface::class, 'csrf_token_id' => 'registration', 'translation_domain' => 'NucleosProfileBundle', 'validation_groups' => [], ]); } - - /** - * @param mixed $registration - * @param string[] $validationGroups - * - * @return ConstraintViolationInterface[] - */ - private function getUserErrors($registration, array $validationGroups): array - { - if (!$registration instanceof Registration) { - return []; - } - - try { - $user = $registration->toUser($this->userManager); - } catch (Throwable $exception) { - return []; - } - - return iterator_to_array($this->validator->validate($user, null, $validationGroups)); - } } diff --git a/src/Resources/config/profile.php b/src/Resources/config/profile.php index 61c0795c..7f5659e3 100644 --- a/src/Resources/config/profile.php +++ b/src/Resources/config/profile.php @@ -14,7 +14,6 @@ use Nucleos\ProfileBundle\Action\EditProfileAction; use Nucleos\ProfileBundle\Action\ShowProfileAction; use Nucleos\ProfileBundle\Form\Type\ProfileFormType; -use Symfony\Component\DependencyInjection\Parameter; use Symfony\Component\DependencyInjection\Reference; return static function (ContainerConfigurator $container): void { @@ -22,9 +21,6 @@ ->set(ProfileFormType::class) ->tag('form.type') - ->args([ - new Parameter('nucleos_profile.profile.form.model'), - ]) ->set(EditProfileAction::class) ->public() @@ -35,7 +31,6 @@ new Reference('twig'), new Reference('router'), new Reference('security.helper'), - new Parameter('nucleos_profile.profile.form.model'), ]) ->set(ShowProfileAction::class) diff --git a/src/Resources/config/registration.php b/src/Resources/config/registration.php index 6fd734f8..4f7490ac 100644 --- a/src/Resources/config/registration.php +++ b/src/Resources/config/registration.php @@ -16,20 +16,13 @@ use Nucleos\ProfileBundle\Action\RegistrationAction; use Nucleos\ProfileBundle\Action\RegistrationConfirmedAction; use Nucleos\ProfileBundle\Form\Type\RegistrationFormType; -use Symfony\Component\DependencyInjection\Parameter; use Symfony\Component\DependencyInjection\Reference; -use Symfony\Component\Validator\Validator\ValidatorInterface; return static function (ContainerConfigurator $container): void { $container->services() ->set(RegistrationFormType::class) ->tag('form.type') - ->args([ - new Parameter('nucleos_profile.registration.form.model'), - new Reference('nucleos_user.user_manager'), - new Reference(ValidatorInterface::class), - ]) ->set(RegistrationAction::class) ->public() @@ -39,7 +32,6 @@ new Reference('nucleos_user.user_manager'), new Reference('router'), new Reference('twig'), - new Parameter('nucleos_profile.registration.form.model'), ]) ->set(CheckRegistrationMailAction::class) diff --git a/src/Resources/config/validation.xml b/src/Resources/config/validation.xml deleted file mode 100644 index 0274152c..00000000 --- a/src/Resources/config/validation.xml +++ /dev/null @@ -1,55 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tests/App/config/config.yaml b/tests/App/config/config.yaml index 791b3c8e..8dff82e6 100644 --- a/tests/App/config/config.yaml +++ b/tests/App/config/config.yaml @@ -33,10 +33,10 @@ nucleos_user: from_email: 'no-reply@localhost' - user_class: 'Nucleos\ProfileBundle\Tests\App\Entity\User' + user_class: 'Nucleos\ProfileBundle\Tests\App\Entity\TestUser' group: - group_class: 'Nucleos\ProfileBundle\Tests\App\Entity\Group' + group_class: 'Nucleos\ProfileBundle\Tests\App\Entity\TestGroup' nucleos_profile: diff --git a/tests/DependencyInjection/NucleosProfileExtensionTest.php b/tests/DependencyInjection/NucleosProfileExtensionTest.php index e893a8e5..401465ad 100644 --- a/tests/DependencyInjection/NucleosProfileExtensionTest.php +++ b/tests/DependencyInjection/NucleosProfileExtensionTest.php @@ -13,8 +13,6 @@ use Nucleos\ProfileBundle\DependencyInjection\NucleosProfileExtension; use Nucleos\ProfileBundle\EventListener\FlashListener; -use Nucleos\ProfileBundle\Form\Model\Profile; -use Nucleos\ProfileBundle\Form\Model\Registration; use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Yaml\Parser; @@ -51,22 +49,6 @@ public function testUserLoadFlashesCanBeDisabled(): void $this->assertNotHasDefinition(FlashListener::class); } - public function testUserLoadFormModelsWithDefaults(): void - { - $this->createEmptyConfiguration(); - - $this->assertParameter(Profile::class, 'nucleos_profile.profile.form.model'); - $this->assertParameter(Registration::class, 'nucleos_profile.registration.form.model'); - } - - public function testUserLoadFormModels(): void - { - $this->createFullConfiguration(); - - $this->assertParameter('App\Form\Profile', 'nucleos_profile.profile.form.model'); - $this->assertParameter('App\Form\Registration', 'nucleos_profile.registration.form.model'); - } - private function createEmptyConfiguration(): void { $this->configuration = new ContainerBuilder(); @@ -92,12 +74,7 @@ private function getFullConfig(): array $yaml = <<<'EOF' use_listener: true use_flash_notifications: false -profile: - form: - model: App\Form\Profile registration: - form: - model: App\Form\Registration confirmation: from_email: register@acme.org enabled: true @@ -114,14 +91,6 @@ private function assertAlias(string $value, string $key): void static::assertSame($value, (string) $this->configuration->getAlias($key), sprintf('%s alias is correct', $key)); } - /** - * @param mixed $value - */ - private function assertParameter($value, string $key): void - { - static::assertSame($value, $this->configuration->getParameter($key), sprintf('%s parameter is correct', $key)); - } - private function assertHasDefinition(string $id): void { static::assertTrue(($this->configuration->hasDefinition($id) ? true : $this->configuration->hasAlias($id))); diff --git a/tests/Form/Type/ProfileFormTypeTest.php b/tests/Form/Type/ProfileFormTypeTest.php index 1762193b..adb8dacb 100644 --- a/tests/Form/Type/ProfileFormTypeTest.php +++ b/tests/Form/Type/ProfileFormTypeTest.php @@ -11,16 +11,16 @@ namespace Nucleos\ProfileBundle\Tests\Form\Type; -use Nucleos\ProfileBundle\Form\Model\Profile; use Nucleos\ProfileBundle\Form\Type\ProfileFormType; +use Nucleos\ProfileBundle\Tests\App\Entity\TestUser; final class ProfileFormTypeTest extends ValidatorExtensionTypeTestCase { public function testSubmit(): void { - $profile = new Profile(); + $user = new TestUser(); - $form = $this->factory->create(ProfileFormType::class, $profile); + $form = $this->factory->create(ProfileFormType::class, $user); $formData = [ 'timezone' => 'Europe/Berlin', 'locale' => 'de_DE', @@ -28,9 +28,9 @@ public function testSubmit(): void $form->submit($formData); static::assertTrue($form->isSynchronized()); - static::assertSame($profile, $form->getData()); - static::assertSame('Europe/Berlin', $profile->getTimezone()); - static::assertSame('de_DE', $profile->getLocale()); + static::assertSame($user, $form->getData()); + static::assertSame('Europe/Berlin', $user->getTimezone()); + static::assertSame('de_DE', $user->getLocale()); } /** @@ -38,11 +38,8 @@ public function testSubmit(): void */ protected function getTypes(): array { - return array_merge( - parent::getTypes(), - [ - new ProfileFormType(Profile::class), - ] - ); + return array_merge(parent::getTypes(), [ + new ProfileFormType(), + ]); } } diff --git a/tests/Form/Type/RegistrationFormTypeTest.php b/tests/Form/Type/RegistrationFormTypeTest.php index af4a7118..63f18684 100644 --- a/tests/Form/Type/RegistrationFormTypeTest.php +++ b/tests/Form/Type/RegistrationFormTypeTest.php @@ -11,54 +11,16 @@ namespace Nucleos\ProfileBundle\Tests\Form\Type; -use Nucleos\ProfileBundle\Form\Model\Registration; use Nucleos\ProfileBundle\Form\Type\RegistrationFormType; -use Nucleos\UserBundle\Model\UserInterface; -use Nucleos\UserBundle\Model\UserManagerInterface; -use PHPUnit\Framework\MockObject\MockObject; -use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper; -use Symfony\Component\Validator\ConstraintViolationList; -use Symfony\Component\Validator\Validator\ValidatorInterface; +use Nucleos\ProfileBundle\Tests\App\Entity\TestUser; final class RegistrationFormTypeTest extends ValidatorExtensionTypeTestCase { - /** - * @var MockObject&UserManagerInterface - */ - private $userManager; - - /** - * @var MockObject&ValidatorInterface - */ - private $validator; - - /** - * @var MockObject&ViolationMapper - */ - private $violationMapper; - - protected function setUp(): void - { - $this->userManager = $this->createMock(UserManagerInterface::class); - $this->validator = $this->createMock(ValidatorInterface::class); - $this->violationMapper = $this->createMock(ViolationMapper::class); - - parent::setUp(); - } - public function testSubmit(): void { - $registration = new Registration(); - - $user = $this->createMock(UserInterface::class); - - $this->userManager->method('createUser')->willReturn($user); - - $this->validator->method('validate')->with($user, null, []) - ->willReturn(new ConstraintViolationList()) - ; + $user = new TestUser(); - $form = $this->factory->create(RegistrationFormType::class, $registration); + $form = $this->factory->create(RegistrationFormType::class, $user); $formData = [ 'username' => 'bar', 'email' => 'john@doe.com', @@ -70,10 +32,10 @@ public function testSubmit(): void $form->submit($formData); static::assertTrue($form->isSynchronized()); - static::assertSame($registration, $form->getData()); - static::assertSame('bar', $registration->getUsername()); - static::assertSame('john@doe.com', $registration->getEmail()); - static::assertSame('test', $registration->getPlainPassword()); + static::assertSame($user, $form->getData()); + static::assertSame('bar', $user->getUsername()); + static::assertSame('john@doe.com', $user->getEmail()); + static::assertSame('test', $user->getPlainPassword()); } /** @@ -81,15 +43,8 @@ public function testSubmit(): void */ protected function getTypes(): array { - return array_merge( - parent::getTypes(), - [ - new RegistrationFormType( - Registration::class, - $this->userManager, - $this->validator - ), - ] - ); + return array_merge(parent::getTypes(), [ + new RegistrationFormType(), + ]); } }