gpt4 book ai didi

symfony - Symfony 中的功能测试

转载 作者:行者123 更新时间:2023-12-04 16:42:01 25 4
gpt4 key购买 nike

我是测试新手。我想测试我的功能。我已经成功安装了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com