2017-05-30 68 views
0

我是一名学生,我对PHP很陌生。对于我的实践教育,我需要在一个大项目中使用PHPUnit编写功能测试。最近3天,我试图在PHPUnit测试期间使用数据库。例如,我已经为用户进行了测试,以注册到网站和登录。但问题是当用户注册时,我必须激活它们。我设法使用爬虫来激活,但如果我可以访问数据库,进行查询而不是进行测试,那将更容易。即使排除这个,我想创建一个模拟(我相信这就是它的调用方式),只需在数据库中运行测试,这样我就不必使用爬虫和漫长的方式作为管理员和手动删除用户登录。PHPUnit:在使用dbunit或其他任何功能测试期间修改数据库条目

我不够熟练,不知道如何解决这个任务。我尝试安装dbUnit并阅读教程。但是当我尝试使用它时,我没有设法完成任何事情。使用SetUp()和TearDown()也不起作用。

这里是我的计算器发现类似地雷问题的链接: Delete test entity after testing with phpunit symfony - 这家伙有几乎相同的使用履带式的,因为我做的,但我不明白的解决方案。 Phpunit testing with database - 这里的答案很好,那就是我决定获得dbunit的地方。然而,由于我缺乏理解,我无法设法做任何事情。

无论如何,这里是我的代码(剥离出来,因为我有500行爬虫在网站上爬行)我正在一个类中运行所有测试,就好像它们正在一个接一个地运行我可以遵循注册,在和删除。如果我能够知道如何在生活之间使用数据库互动,那么要容易得多。

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; 
use Doctrine\ORM\EntityRepository; 
use PHPUnit_Extensions_Database_TestCase_Trait; 
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; 
use Doctrine\Common\Persistence\ObjectManager; 
use PDO; 
use Doctrine\ORM\Mapping as ORM; 
class LoginRegistroTest extends WebTestCase 
{ 
+public function testUserRegisterFail()   

+public function testUserRegister() //check if new user can be registered and this also writes the user to the database 
+public function testAdminLogin() //this uses the website admin panel and crawler to login and find the registered user and change Active from 0 to 1. 
+public function testLoginFail() 
+public function testUserLogin() //check if new user can login 
+public function testUserDelete() //delete the user created for testing - again using the website admin panel and admin account. 
} 

编辑:这里是一个代码,我想工作的例子,但我不知道如何执行查询。例如,在结尾处注明我想更改用户的名称。

/** 
     * @var \Doctrine\ORM\EntityManager 
     */ 
     private $em; 

    /** 
    * {@inheritDoc} 
    */ 
    protected function setUp() 
    { 
     self::bootKernel(); 

     $this->em = static::$kernel->getContainer() 
      ->get('doctrine') 
      ->getManager(); 
    } 



    public function testUserRegister() //check if new artista can be registered 
    { 


     $client = static::createClient(); 
     $crawler=$client->request('GET', "/"); 
     // Create a new client to browse the application 
     $link = $crawler->filter('a:contains("Regístrate")')->eq(0)->link(); 
     $crawler = $client->click($link); 
     // var_dump($crawler->html()); 
     $this->assertEquals(1, $crawler->filter('h1:contains("REGISTRO")')->count()); 


     $buttonCrawlerNode = $crawler->selectButton("Entra"); 
     $form = $buttonCrawlerNode->form(); 
     $crawler->filter('#registro_category option:contains("Usuario")'); 

     $form['frontendbundle_artista[nombre]'] = 'Test404'; 
     $form['frontendbundle_artista[apellidos]'] = 'Test404'; 
     $form['frontendbundle_artista[email]'] = '[email protected]'; 
     $form['frontendbundle_artista[password][first]'] = 'Test404'; 
     $form['frontendbundle_artista[password][second]'] = 'Test404'; 
     $form['frontendbundle_artista[condiciones]'] ->tick(); 

     // submit the form 
     $client->followRedirects(); 
     $crawler = $client->submit($form); 

     $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET "); 
     $this->assertTrue($crawler->filter('body:contains("[email protected]")')->count() >= 1); //if user was created this will be true 

     $this->em 
      ->CreateQuery('UPDATE usuario SET nombre = \'Alfred Schmidt\' WHERE UsuarioID = 2106;') 
     ; 

     $client->getResponse()->getContent(); 
    } 
+0

那类框架没有太大的帮助。请记住,我们不了解您正在使用的系统。你能否显示错误消息和相关的代码片段(也许有不必要的部分注释掉)?恐怕在目前的状态下,我将不得不投票结束这个问题,因为它太宽泛了。 – dbrumann

+0

基本上,有一件事可以帮助我解决所有问题,那就是如何在使用PHPUnit进行功能测试期间将DQL查询发送到数据库。 – demandingproblem

回答

0

这是什么我能解决我的所有问题:

$em = $client->getContainer()->get('doctrine.orm.entity_manager'); 
     $nombre = 'Test405'; 
     $usuario =$em->getRepository('AppBundle:usuario')->findOneByNombre($nombre); 
     $em->remove($usuario); 
     $em->flush(); 
相关问题