Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
 
4
Prado::using('Application.App_Code.Dao.*');
5
 
6
class BaseTestCase extends UnitTestCase
7
{
8
	protected $sqlmap;
9
 
10
	function setup()
11
	{
12
		$app = Prado::getApplication();
13
		$this->sqlmap = $app->getModule('daos')->getClient();
14
	}
15
 
16
 
17
	function flushDatabase()
18
	{
19
		$conn = $this->sqlmap->getDbConnection();
20
		$find = 'sqlite:protected';
21
		if(is_int(strpos($conn->getConnectionString(),$find)))
22
			$conn->ConnectionString = str_replace($find, 'sqlite:../protected', $conn->ConnectionString);
23
		$conn->setActive(false);
24
		$conn->setActive(true);
25
		switch(strtolower($conn->getDriverName()))
26
		{
27
			case 'mysql':
28
			return $this->flushMySQLDatabase();
29
			case 'sqlite':
30
			return $this->flushSQLiteDatabase();
31
		}
32
	}
33
 
34
	function flushSQLiteDatabase()
35
	{
36
		$conn = $this->sqlmap->getDbConnection();
37
		$file = str_replace('sqlite:','',$conn->getConnectionString());
38
		$backup = $file.'.bak';
39
		copy($backup, $file);
40
	}
41
 
42
	function flushMySQLDatabase()
43
	{
44
		$conn = $this->sqlmap->getDbConnection();
45
		$file = Prado::getPathOfNamespace('Application.App_Data.MySQL4.mysql-reset','.sql');
46
		if(is_file($file))
47
			$this->runScript($conn, $file);
48
		else
49
			throw new Exception('unable to find script file '.$file);
50
	}
51
 
52
	protected function runScript($connection, $script)
53
	{
54
		$sql = file_get_contents($script);
55
		$lines = explode(';', $sql);
56
		foreach($lines as $line)
57
		{
58
			$line = trim($line);
59
			if(strlen($line) > 0)
60
				$connection->createCommand($line)->execute();
61
		}
62
	}
63
}
64
?>