2015-07-13 10 views
5

Jestem nowy w testowaniu. Chcę przetestować moją funkcję. Z powodzeniem zainstalowałem phpUnit. Sprawdzam wiele tutoriali w Internecie. Nie mogłem jednak uzyskać odpowiednich informacji dotyczących testowania. Oto mój kod funkcji:Testowanie funkcjonalne w Symfony

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'); 
    } 

jak przetestować tę funkcję? Proszę o pomoc

+3

http://symfony.com/doc/current/book/testing.html –

+0

najpierw zadaj sobie pytanie, co chcesz przetestować. Czy chcesz wydrukować test? Ten kod byłby dość trudny do testowania jednostkowego, ale naprawdę powinien go zmienić. – Robert

Odpowiedz

1

Nie wiem, co chcesz przetestować, ale tutaj jest exemple, co można zrobić, aby przetestować fonctionnalities użytkowników:

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()); 
} 

Mam nadzieję, że pomoże to zrobisz undestand jak testować.

Powiązane problemy