/** * Sign Up action * * @return array|\Zend\Http\Response|ViewModel * @throws \Exception */ public function indexAction() { $form = new Form\SignupForm('create-user', ['serviceLocator' => $this->getServiceLocator()]); if ($this->getRequest()->isPost()) { $form->setData($this->getRequest()->getPost()); if ($form->isValid()) { $userService = new Service\User($this->getServiceLocator()); try { $user = $userService->create($form); $this->flashMessenger()->addSuccessMessage('You must confirm your email address to complete registration'); return $this->redirect()->toRoute('home'); } catch (\Exception $exception) { throw $exception; } } } return new ViewModel(['form' => $form, 'serviceLocator' => $this->getServiceLocator()]); }
/** * @param SignupForm $form * @return Entity\User * @throws \Exception */ public function create(SignupForm $form) { $objectManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $data = $form->getData(); $user = new Entity\User(); $objectManager->getConnection()->beginTransaction(); try { $hydrator = new DoctrineHydrator($objectManager); $hydrator->hydrate($form->getData(), $user); $user->setDisplayName($user->getEmail()); $user->setRole($user::ROLE_USER); $user->setConfirm($user->generateConfirm()); $user->setStatus($user::STATUS_UNCONFIRMED); $objectManager->persist($user); $objectManager->flush(); /** * @var $authService \User\Service\Auth */ $authService = $this->getServiceLocator()->get('User\\Service\\Auth'); $authService->generateEquals($user, $data['password']); //use module mail for user registration if ($this->useModuleMail()) { /** @var \Mail\Service\Mail $mailService */ $mailService = $this->getServiceLocator()->get('Mail\\Service\\Mail'); $mailService->signUpMail($user); } else { $html = $this->getServiceLocator()->get('ControllerPluginManager')->get('forward')->dispatch('User\\Controller\\Mail', array('action' => 'signup', 'user' => $user)); $this->signupMail($user, $html); } $objectManager->getConnection()->commit(); } catch (\Exception $exception) { $objectManager->getConnection()->rollback(); throw $exception; } return $user; }
/** * signup index action test */ public function testIndex() { $mailTransportMock = $this->getMockBuilder('Zend\\Mail\\Transport\\Smtp')->disableOriginalConstructor()->getMock(); $mailTransportMock->expects($this->once())->method('send')->will($this->returnValue(null)); $mailMessageMock = $this->getMockBuilder('Zend\\Mail\\Message')->disableOriginalConstructor()->getMock(); $mailMessageMock->expects($this->once())->method('addTo')->will($this->returnSelf()); $mailMessageMock->expects($this->once())->method('setSubject')->will($this->returnSelf()); $mailMessageMock->expects($this->once())->method('setBody')->will($this->returnSelf()); $this->getApplicationServiceLocator()->setAllowOverride(true); $this->getApplicationServiceLocator()->setService('mail.transport', $mailTransportMock); $this->getApplicationServiceLocator()->setService('mail.message', $mailMessageMock); $form = new SignupForm('create-user', ['serviceLocator' => $this->getApplicationServiceLocator()]); $data = ['security' => $form->get('security')->getValue(), 'email' => '*****@*****.**', 'password' => '123456', 'repeat-password' => '123456']; $this->dispatch('/user/signup/index', 'POST', $data); $this->assertResponseStatusCode(302); $this->assertRedirectTo('/'); }