Functional Testing in Symfony

I am new to testing. I want to test my function. I have successfully installed phpUnit. I check many online tutorials. But I could not get the correct testing information. Here is my function code:

public function loginAction(Request $request) { $session = $this->getRequest()->getSession(); if( $session->get('userId') && $session->get('userId') != '' && $session->get('type') == '2') { //if user is login then it will be redirect to login page return $this->redirect($this->generateUrl('registrarGeneral_dashboard')); } $em = $this->getDoctrine()->getEntityManager(); $repository = $em->getRepository('DRPAdminBundle:User'); if ($request->getMethod() == 'POST') { $session->clear(); $userName = $request->get('username'); $password = md5($request->get('password')); //find email, password type and status of User $user = $repository->findOneBy(array('username' => $userName, 'password' => $password,'type'=>2,'status'=>1 )); $userEmail = $repository->findOneBy(array('email' => $userName, 'password' => $password,'type'=>2,'status'=>1 )); if ($user) { //set session of User login $session->set('userId', $user->getId()); $session->set('type', 2); $session->set('nameRegistrar', $user->getFirstName()); $session->set('pictureRegistrar', $user->getPicture()); //echo "<pre>";print_r($session->get('picture'));die; return $this->redirect($this->generateUrl('registrarGeneral_dashboard')); } if ($userEmail) { $session->set('type', 2); $session->set('userId', $userEmail->getId()); $session->set('nameRegistrar', $userEmail->getFirstName()); $session->set('pictureRegistrar', $userEmail->getPicture()); //echo "<pre>";print_r($session->get('picture'));die; return $this->redirect($this->generateUrl('registrarGeneral_dashboard')); } else { return $this->render('DRPRegistrarGeneralBundle:Pages:login.html.twig', array('name' => 'Invalid Email/Password')); } } return $this->render('DRPRegistrarGeneralBundle:Pages:login.html.twig'); } 

How to check this feature? Please, help

+5
source share
1 answer

I don’t know what you want to check, but here is an example of what you can do to check your user settings:

 public function testUserPageDown() { $client = static::createClient(); $client->request('GET', '/user/login'); $this->assertTrue($client->getResponse()->isSuccessful()); $client->request('GET', '/user/register'); $this->assertTrue($client->getResponse()->isSuccessful()); } public function testUserFirewall() { $client = static::createClient(); //Trying go to user routes without being logged $client->request('GET', '/user/profile'); $this->assertTrue($client->getResponse()->isRedirect()); $client->request('GET', '/user/profile/edit'); $this->assertTrue($client->getResponse()->isRedirect()); $client->request('GET', '/user/profile/editpassword'); $this->assertTrue($client->getResponse()->isRedirect()); } public function testUserFormRegister() { $client = static::createClient(); $crawler = $client->request('GET', '/user/register'); $buttonCrawlerNode = $crawler->selectButton('submit_user_register'); $form = $buttonCrawlerNode->form(); $testForm = array( 'wineot_databundle_user[username]' => 'test', 'wineot_databundle_user[firstname]' => 'test', 'wineot_databundle_user[lastname]' => 'test', 'wineot_databundle_user[mail]' => ' test@mail.fr ', 'wineot_databundle_user[plain_password][first]' => 'blabla321', 'wineot_databundle_user[plain_password][second]' => 'blabla321' ); $response = $client->getResponse(); $client->submit($form, $testForm); //If the submit is true that mean that the register is ok $this->assertTrue($response->isSuccessful()); } 

I hope this helps you figure out how to test.

+1
source

All Articles