Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Controller
- class SecurityController extends AbstractFOSRestController
- {
- /**
- * @Rest\Post("/register", name="user_register")
- *
- * @param Request $request
- * @param Register $register
- * @param FormErrorsConverter $converter
- *
- * @return Response
- */
- public function register(
- Request $request,
- Register $register,
- FormErrorsConverter $converter
- ): Response {
- $form = $this->createForm(UserType::class, null);
- $form->submit($request->request->all());
- if ($form->isValid()) {
- $data = $form->getData();
- $registerAction = $register->registerUser($data);
- return $this->json([
- 'success' => $registerAction
- ], Response::HTTP_OK);
- }
- return $this->json([
- 'error' => "Form is not valid",
- 'errors_fields' => $converter->convertErrorsFromFrom($form),
- ], Response::HTTP_BAD_REQUEST);
- }
- }
- // Service
- class Register
- {
- /** @var EntityManagerInterface */
- private $entityManager;
- /** @var UserPasswordEncoderInterface */
- private $passwordEncoder;
- /**
- * @param EntityManagerInterface $entityManager
- * @param UserPasswordEncoderInterface $encoder
- */
- public function __construct(
- EntityManagerInterface $entityManager,
- UserPasswordEncoderInterface $encoder
- ) {
- $this->entityManager = $entityManager;
- $this->passwordEncoder = $encoder;
- }
- /**
- * @param User $user
- * @return int|null
- */
- public function registerUser(User $user)
- {
- $user->setPassword(
- $this->passwordEncoder->encodePassword($user, $user->getPlainPassword())
- );
- $this->entityManager->persist($user);
- $this->entityManager->flush();
- return $user->getId();
- }
- }
- // Entity
- class User implements UserInterface, \Serializable
- {
- /**
- * @ORM\Id()
- * @ORM\GeneratedValue()
- * @ORM\Column(type="integer")
- */
- private $id;
- /**
- * @ORM\Column(type="string", length=50, unique=true)
- */
- private $username;
- /**
- * @ORM\Column(type="string", length=60)
- */
- private $password;
- /**
- * @ORM\Column(type="string", length=100, unique=true)
- */
- private $email;
- /**
- * @ORM\Column(type="boolean")
- */
- private $isActive;
- /**
- * @ORM\Column(type="datetime")
- */
- private $createdAt;
- /**
- * Field used to encode the password
- *
- * @var string
- */
- private $plainPassword;
- public function __construct()
- {
- $this->isActive = true;
- }
- public function getId(): ?int
- {
- return $this->id;
- }
- public function getUsername(): ?string
- {
- return $this->username;
- }
- public function setUsername(string $username): self
- {
- $this->username = $username;
- return $this;
- }
- public function getPassword(): ?string
- {
- return $this->password;
- }
- public function setPassword(string $password): self
- {
- $this->password = $password;
- return $this;
- }
- public function getEmail(): ?string
- {
- return $this->email;
- }
- public function setEmail(string $email): self
- {
- $this->email = $email;
- return $this;
- }
- public function getIsActive(): ?bool
- {
- return $this->isActive;
- }
- public function setIsActive(bool $isActive): self
- {
- $this->isActive = $isActive;
- return $this;
- }
- public function getSalt()
- {
- return null;
- }
- public function getRoles()
- {
- return array('ROLE_USER');
- }
- /**
- * @ORM\PrePersist()
- */
- public function setCreatedAt()
- {
- $this->createdAt = new \DateTime();
- }
- public function getPlainPassword(): ?string
- {
- return $this->plainPassword;
- }
- public function setPlainPassword(?string $plainPassword): self
- {
- $this->plainPassword = $plainPassword;
- return $this;
- }
- public function eraseCredentials()
- {
- $this->plainPassword = null;
- }
- /** @see \Serializable::serialize() */
- public function serialize()
- {
- return serialize(array(
- $this->id,
- $this->username,
- $this->password,
- ));
- }
- /** @see \Serializable::unserialize()
- * @param $serialized
- */
- public function unserialize($serialized)
- {
- list (
- $this->id,
- $this->username,
- $this->password,
- ) = unserialize($serialized, array('allowed_classes' => false));
- }
- }
- // FormType
- class UserType extends AbstractType
- {
- /**
- * @param FormBuilderInterface $builder
- * @param array $options
- */
- public function buildForm(FormBuilderInterface $builder, array $options)
- {
- $builder
- ->add('username', TextType::class)
- ->add('plainPassword', PasswordType::class)
- ->add('email', EmailType::class)
- ;
- }
- /**
- * @param OptionsResolver $resolver
- */
- public function configureOptions(OptionsResolver $resolver)
- {
- $resolver->setDefaults([
- 'data_class' => User::class,
- ]);
- }
- }
- // Testy
- class RegisterTest extends TestCase
- {
- /** @var EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
- private $entityManagerInterface;
- /** @var UserPasswordEncoderInterface */
- private $passwordEncoder;
- /** @var Register */
- private $registerService;
- protected function setUp()
- {
- $this->entityManagerInterface = $this->getMockBuilder(EntityManagerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->passwordEncoder = $this->createMock(UserPasswordEncoderInterface::class);
- $this->registerService = new Register($this->entityManagerInterface, $this->passwordEncoder);
- }
- protected function tearDown()
- {
- $this->entityManagerInterface = null;
- $this->passwordEncoder = null;
- }
- public function testRegisterUser()
- {
- $user = new User();
- $user->setUsername("ExampleUsername");
- $user->setPlainPassword("ExamplePlainPassword");
- $user->setPassword("ExamplePassword");
- $user->setEmail("Example@Email.com");
- $this->entityManagerInterface
- ->expects($this->once())
- ->method('persist')
- ->with($user);
- $this->entityManagerInterface
- ->expects($this->once())
- ->method('flush');
- $result = $this->registerService->registerUser($user);
- $this->assertEquals(1, $result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement