Symfony2 phpunit:如何在运行测试之前删除和复制数据库


symfony2 phpunit : how to drop and copy database before running tests?

在我的symfony2应用程序中,我使用phpunit来验证来自每个路由的响应代码是否为200。

在运行测试之前,我想删除测试数据库,并以test的名称复制生产数据库(即,我想重新启动测试数据库)。

我了解了public static function setUpBeforeClass(),但我不知道如何删除和复制数据库。

我该怎么做呢?

我的课到目前为止:

<?php
namespace AppBundle'Tests'Controller;
use AppBundle'FoodMeUpParameters;
use Symfony'Bundle'FrameworkBundle'Test'WebTestCase;
require_once __DIR__.'/../../../../app/AppKernel.php';

class ApplicationAvailabilityFunctionalTest extends WebTestCase
{
    public static function setUpBeforeClass()
    {
    }
    /**
     * @dataProvider routeProvider
     * @param $route
     */
    public function testAllRoutesAreLoaded($route)
    {
        $listedRoutes = $this->getListedRoutes();
        $this->assertArrayHasKey($route, $listedRoutes);
    }
}

这是一些用于备份MySQL数据库的PHP代码。——例程包括视图、函数、存储过程等。

<?php
$schemaFile = "schema.sql";
$mysqlArgs  = "-u {$dbUser} -h {$dbHost} {$dbName}";
//if you don't have a pass and still pass in arg -p it will interrupt and prompt
if (isset($dbPassword) && strlen(trim($dbPassword)) > 0) { 
    $mysqlArgs .= " -p{$dbPassword}";
}
$mysqlDumpCommand = "mysqldump {$mysqlArgs} --routines";
$schemaDump       = "{$mysqlDumpCommand} > {$schemaFile}";
system($schemaDump);

然后代码导入它假设相同的$mysqlArgs代码如上。

$mysqlImportCommand = "mysql {$mysqlArgs}";
$import             = "{$mysqlImportCommand} < {$schemaFile}";
system($import);

我的5c。通常您不需要使用setUpBeforeClass,因为它将填充整个套件的DB,因此在单元测试必须独立时创建测试依赖的风险。使用setUptearDown方法代替。现在说重点:你说你只想测试响应是代码200。当您可以简单地模拟模型的预期响应时,为什么要首先填充整个DB呢?