Optimized Entities, Controllers and tests

This commit is contained in:
Kevin Frantz 2018-09-06 20:20:29 +02:00
parent 69ea47facd
commit cff5afe018
6 changed files with 13 additions and 7 deletions

View File

@ -22,7 +22,10 @@ class SecurityController extends AbstractController
$error = $authenticationUtils->getLastAuthenticationError(); $error = $authenticationUtils->getLastAuthenticationError();
if ($error) { if ($error) {
$this->addFlash('danger', $error->getMessage()); $this->addFlash('danger', $error->getMessage());
}else{
$this->addFlash('success', "User loged in.");
} }
$this->addFlash('info', $authenticationUtils->getLastUsername());
return $this->render("user/login.html.twig",[ return $this->render("user/login.html.twig",[
'last_username'=>$authenticationUtils->getLastUsername(), 'last_username'=>$authenticationUtils->getLastUsername(),
]); ]);

View File

@ -10,6 +10,7 @@ use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
/** /**
* *
@ -32,7 +33,7 @@ class UserController extends AbstractController implements UserControllerInterfa
* *
* @Route("/user/register", name="user_register") * @Route("/user/register", name="user_register")
*/ */
public function register(Request $request): Response public function register(Request $request,UserPasswordEncoderInterface $encoder): Response
{ {
$user = new User(); $user = new User();
$form = $this->createFormBuilder($user) $form = $this->createFormBuilder($user)
@ -44,9 +45,11 @@ class UserController extends AbstractController implements UserControllerInterfa
->getForm(); ->getForm();
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$task = $form->getData(); $user = $form->getData();
$encoded = $encoder->encodePassword($user, $request->get('password'));
$user->setPassword($encoded);
$entityManager = $this->getDoctrine()->getManager(); $entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($task); $entityManager->persist($user);
try { try {
$entityManager->flush(); $entityManager->flush();
$this->addFlash('success', 'User created!'); $this->addFlash('success', 'User created!');

View File

@ -14,5 +14,5 @@ interface UserControllerInterface
{ {
public function logout():Response; public function logout():Response;
public function register(Request $request):Response; #public function register(Request $request):Response;
} }

View File

@ -19,7 +19,7 @@ trait UsernameAttribut{
} }
public function setUsername(string $username):void{ public function setUsername(string $username):void{
$this->username = $username; $this->username = \trim($username);
} }
} }

View File

@ -32,7 +32,7 @@ class UserControllerTest extends WebTestCase
public function testLogin(): void public function testLogin(): void
{ {
$client = static::createClient(); $client = static::createClient();
$client->request('GET', '/user/login'); $client->request('GET', '/login');
$this->assertEquals(200, $client->getResponse()->getStatusCode()); $this->assertEquals(200, $client->getResponse()->getStatusCode());
} }

View File

@ -24,7 +24,7 @@ class UserTest extends TestCase
public function setUp():void{ public function setUp():void{
$this->user = new User(); $this->user = new User();
$this->user->setPassword(self::PASSWORD); $this->user->setPassword(self::PASSWORD);
$this->user->setUsername(self::USERNAME); $this->user->setUsername(' '.self::USERNAME.' ');
} }
public function testUsername():void{ public function testUsername():void{