summaryrefslogtreecommitdiff
path: root/demos/time-tracker/tests/unit/BaseTestCase.php
blob: d72cbc5c3fc99e07282eeed0aeb251c5ba8c9338 (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
60
61
62
63
64
<?php


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

class BaseTestCase extends UnitTestCase
{
	protected $sqlmap;

	function setup()
	{
		$app = Prado::getApplication();
		$this->sqlmap = $app->getModule('daos')->getClient();
	}


	function flushDatabase()
	{
		$conn = $this->sqlmap->getDbConnection();
		$find = 'sqlite:protected';
		if(is_int(strpos($conn->getConnectionString(),$find)))
			$conn->ConnectionString = str_replace($find, 'sqlite:../protected', $conn->ConnectionString);
		$conn->setActive(false);
		$conn->setActive(true);
		switch(strtolower($conn->getDriverName()))
		{
			case 'mysql':
			return $this->flushMySQLDatabase();
			case 'sqlite':
			return $this->flushSQLiteDatabase();
		}
	}

	function flushSQLiteDatabase()
	{
		$conn = $this->sqlmap->getDbConnection();
		$file = str_replace('sqlite:','',$conn->getConnectionString());
		$backup = $file.'.bak';
		copy($backup, $file);
	}

	function flushMySQLDatabase()
	{
		$conn = $this->sqlmap->getDbConnection();
		$file = Prado::getPathOfNamespace('Application.App_Data.MySQL4.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->createCommand($line)->execute();
		}
	}
}
?>