user4869699
user4869699

Reputation:

Functional Testing in Symfony

I am new in testing.I want to test my function.I have successfully installed phpUnit. I check many tutorials on internet.But I could not get the proper information regarding testing. Here is the 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 test this function? Please help

Upvotes: 5

Views: 134

Answers (1)

Floran
Floran

Reputation: 136

I don't know what you want to test but here is an exemple of what you can do to test user fonctionnalities :

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]'  => '[email protected]',
        '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 that will help you do undestand how to test.

Upvotes: 1

Related Questions