a tiny mvc framework for php using php-activerecord
1<?php
2require_once 'DatabaseLoader.php';
3
4class DatabaseTest extends SnakeCase_PHPUnit_Framework_TestCase
5{
6 protected $conn;
7 public static $log = false;
8
9 public function set_up($connection_name=null)
10 {
11 ActiveRecord\Table::clear_cache();
12
13 $config = ActiveRecord\Config::instance();
14 $this->original_default_connection = $config->get_default_connection();
15
16 if ($connection_name)
17 $config->set_default_connection($connection_name);
18
19 if ($connection_name == 'sqlite' || $config->get_default_connection() == 'sqlite')
20 {
21 // need to create the db. the adapter specifically does not create it for us.
22 $this->db = substr(ActiveRecord\Config::instance()->get_connection('sqlite'),9);
23 new SQLite3($this->db);
24 }
25
26 $this->connection_name = $connection_name;
27 $this->conn = ActiveRecord\ConnectionManager::get_connection($connection_name);
28
29 $GLOBALS['ACTIVERECORD_LOG'] = false;
30
31 $loader = new DatabaseLoader($this->conn);
32 $loader->reset_table_data();
33
34 if (self::$log)
35 $GLOBALS['ACTIVERECORD_LOG'] = true;
36 }
37
38 public function tear_down()
39 {
40 if ($this->original_default_connection)
41 ActiveRecord\Config::instance()->set_default_connection($this->original_default_connection);
42 }
43
44 public function assert_exception_message_contains($contains, $closure)
45 {
46 $message = "";
47
48 try {
49 $closure();
50 } catch (ActiveRecord\UndefinedPropertyException $e) {
51 $message = $e->getMessage();
52 }
53
54 $this->assert_true(strpos($message,$contains) !== false);
55 }
56
57 /**
58 * Returns true if $regex matches $actual.
59 *
60 * Takes database specific quotes into account by removing them. So, this won't
61 * work if you have actual quotes in your strings.
62 */
63 public function assert_sql_has($needle, $haystack)
64 {
65 $needle = str_replace(array('"','`'),'',$needle);
66 $haystack = str_replace(array('"','`'),'',$haystack);
67 return $this->assert_true(strpos($haystack,$needle) !== false);
68 }
69
70 public function assert_sql_doesnt_has($needle, $haystack)
71 {
72 $needle = str_replace(array('"','`'),'',$needle);
73 $haystack = str_replace(array('"','`'),'',$haystack);
74 return $this->assert_false(strpos($haystack,$needle) !== false);
75 }
76}
77?>