symfony - Symfony 中的功能测试

标签 symfony

我是测试新手。我想测试我的功能。我已经成功安装了 phpUnit。我在互联网上查看了许多教程。但我无法获得有关测试的正确信息。这是我的功能代码:

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

如何测试这个功能?请帮忙

最佳答案

我不知道您想测试什么,但这里有一个示例,说明您可以做什么来测试用户功能:

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

我希望这会帮助你理解如何测试。

关于symfony - Symfony 中的功能测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31382287/

相关文章:

symfony - Symfony 2 是否通过 header 接受 CSRF?

symfony 路由 : annotation vs file

php - Symfony2 , 提高查询性能

php - 在 Symfony 2.0.x 中自动加载非 PSR0 库

mysql - Symfony 原则 orderby 和 group by(不同)

security - 如何在 Symfony 中生成 pin 码

Symfony 升级到 4.1 Assets 错误

php - 如何在 Symfony 中使用 FOSUserBundle 创建多个登录表单

Symfony 应用程序在获取 EntityManager 时卡住了

php - Symfony2 类的使用