Mysql PHPUnit:在功能测试期间使用dbunit或任何其他工具修改数据库条目

Mysql PHPUnit:在功能测试期间使用dbunit或任何其他工具修改数据库条目,mysql,symfony,phpunit,functional-testing,dbunit,Mysql,Symfony,Phpunit,Functional Testing,Dbunit,我是一名学生,对php很陌生。对于我的实践教育,我被要求在一个大项目上使用PHPUnit编写功能测试。在过去的3天里,我一直在PHPUnit测试期间尝试使用数据库。例如,我做了一些测试,让用户注册到网站并登录。但问题是,当用户注册时,我必须激活他们。我设法使用crawler来激活它,但是如果我可以访问数据库,进行查询,而不是继续进行测试,那就容易多了。即使排除了这一点,我还是想创建一个mock(我相信这就是它的名称)并在数据库中运行测试,这样我就不必使用crawler和long-way以管理员身

我是一名学生,对php很陌生。对于我的实践教育,我被要求在一个大项目上使用PHPUnit编写功能测试。在过去的3天里,我一直在PHPUnit测试期间尝试使用数据库。例如,我做了一些测试,让用户注册到网站并登录。但问题是,当用户注册时,我必须激活他们。我设法使用crawler来激活它,但是如果我可以访问数据库,进行查询,而不是继续进行测试,那就容易多了。即使排除了这一点,我还是想创建一个mock(我相信这就是它的名称)并在数据库中运行测试,这样我就不必使用crawler和long-way以管理员身份登录并手动删除用户

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

以下是我在stackoverflow上找到的与我的问题类似的链接: -这家伙几乎和我一样使用爬虫,但我不明白解决方案。 -这里的答案很好,这就是我决定使用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]'] = 'Test404@gmail.com';
        $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("Test404@gmail.com")')->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();
    }

这就是我能够解决所有问题的原因:

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

那门课帮不了什么忙。请记住,我们不了解您正在使用的系统。能否显示错误消息和相关代码段(可能注释掉了不必要的部分)?我担心在目前的状态下,我将不得不投票结束这个问题,因为它太宽泛了。基本上,有一件事可以帮助我解决所有问题,那就是如何在使用PHPUnit进行功能测试期间将DQL查询发送到数据库。