[英]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代碼。 --routines包括你的視圖,函數,存儲過程等。
<?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,因此會產生測試依賴性的風險,而單元測試必須是獨立的。 請改用setUp
和tearDown
方法。 現在到了這一點:你說你只想測試那個響應是代碼200.當你只是模擬模型的預期響應時,你為什么要首先填充整個數據庫?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.