summaryrefslogtreecommitdiff
path: root/demos/time-tracker/tests/unit/BaseTestCase.php
blob: 549229e3ac7c437b6181590480cad4e95523048e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php


Prado::using('Application.App_Code.Dao.*');

class BaseTestCase extends UnitTestCase
{
	protected $sqlmap;
	
	function setup()
	{
		$app = Prado::getApplication();
		$this->sqlmap = $app->getModule('daos')->getConnection();
	}
	
	
	function flushDatabase()
	{
		$conn = $this->sqlmap->openConnection();
		switch(strtolower($conn->getProvider()->getDriver()))
		{
			case 'mysql':
			return $this->flushMySQLDatabase();
			case 'sqlite':
			return $this->flushSQLiteDatabase(); 
		}		
	}
	
	function flushSQLiteDatabase()
	{
		$conn = $this->sqlmap->openConnection();
		$file = $conn->getProvider()->getHost();
		$backup = $file.'.bak';
		copy($backup, $file);
	}
	
	function flushMySQLDatabase()
	{
		$conn = $this->sqlmap->openConnection();
		$file = Prado::getPathOfNamespace('Application.App_Data.mysql-reset','.sql');
		if(is_file($file))
			$this->runScript($conn, $file);
		else
			throw new Exception('unable to find script file '.$file);
	}
	
	protected function runScript($connection, $script)
	{
		$sql = file_get_contents($script);
		$lines = explode(';', $sql);
		foreach($lines as $line)
		{
			$line = trim($line);
			if(strlen($line) > 0)
				$connection->execute($line);
		}
	}
}
?>