blob: 05499f82fd94d3deca3acce02d99c71599de4ec9 [file] [log] [blame]
Taufan Aditya5b421662012-04-05 01:24:50 +07001<?php
2
3class Mock_Database_Schema_Skeleton {
Taufan Aditya55f622f2012-04-05 19:01:34 +07004
5 /**
6 * @var object Database Holder
7 */
8 public static $db;
9
10 /**
11 * @var object Forge Holder
12 */
13 public static $forge;
14
15 /**
16 * @var object Driver Holder
17 */
18 public static $driver;
19
20 /**
21 * Initialize both database and forge components
22 */
23 public static function init($driver)
24 {
25 if (empty(static::$db) && empty(static::$forge))
26 {
27 $config = Mock_Database_DB::config($driver);
28 $connection = new Mock_Database_DB($config);
29 $db = Mock_Database_DB::DB($connection->set_dsn($driver), TRUE);
30
31 CI_TestCase::instance()->ci_instance_var('db', $db);
32
33 $loader = new Mock_Core_Loader();
34 $loader->dbforge();
35 $forge = CI_TestCase::instance()->ci_instance_var('dbforge');
36
37 static::$db = $db;
38 static::$forge = $forge;
39 static::$driver = $driver;
40 }
41
42 return static::$db;
43 }
44
Taufan Aditya5b421662012-04-05 01:24:50 +070045
46 /**
47 * Create the dummy tables
48 *
49 * @return void
50 */
Taufan Aditya55f622f2012-04-05 19:01:34 +070051 public static function create_tables()
Taufan Aditya5b421662012-04-05 01:24:50 +070052 {
Taufan Aditya17f799e2012-05-03 15:15:40 +070053 // User Table
54 static::$forge->add_field(array(
55 'id' => array(
56 'type' => 'INTEGER',
57 'constraint' => 3,
58 ),
59 'name' => array(
60 'type' => 'VARCHAR',
61 'constraint' => 40,
62 ),
63 'email' => array(
64 'type' => 'VARCHAR',
65 'constraint' => 100,
66 ),
Taufan Aditya1afd4792012-05-04 20:34:30 +070067 'country' => array(
68 'type' => 'VARCHAR',
69 'constraint' => 40,
70 ),
Taufan Aditya17f799e2012-05-03 15:15:40 +070071 ));
72 static::$forge->add_key('id', TRUE);
73 static::$forge->create_table('user', (strpos(static::$driver, 'pgsql') === FALSE));
74
Taufan Aditya5b421662012-04-05 01:24:50 +070075 // Job Table
Taufan Aditya55f622f2012-04-05 19:01:34 +070076 static::$forge->add_field(array(
Taufan Aditya5b421662012-04-05 01:24:50 +070077 'id' => array(
Taufan Aditya1bfc0d92012-04-05 02:25:25 +070078 'type' => 'INTEGER',
Taufan Aditya5b421662012-04-05 01:24:50 +070079 'constraint' => 3,
80 ),
81 'name' => array(
82 'type' => 'VARCHAR',
83 'constraint' => 40,
84 ),
85 'description' => array(
86 'type' => 'TEXT',
Taufan Aditya5b421662012-04-05 01:24:50 +070087 ),
88 ));
Taufan Aditya55f622f2012-04-05 19:01:34 +070089 static::$forge->add_key('id', TRUE);
90 static::$forge->create_table('job', (strpos(static::$driver, 'pgsql') === FALSE));
Taufan Aditya2d574452012-05-25 04:03:56 +070091
92 // Misc Table
93 static::$forge->add_field(array(
94 'id' => array(
95 'type' => 'INTEGER',
96 'constraint' => 3,
97 ),
98 'key' => array(
99 'type' => 'VARCHAR',
100 'constraint' => 40,
101 ),
102 'value' => array(
103 'type' => 'TEXT',
104 ),
105 ));
106 static::$forge->add_key('id', TRUE);
107 static::$forge->create_table('misc', (strpos(static::$driver, 'pgsql') === FALSE));
Taufan Aditya5b421662012-04-05 01:24:50 +0700108 }
109
110 /**
111 * Create the dummy datas
112 *
113 * @return void
114 */
Taufan Aditya55f622f2012-04-05 19:01:34 +0700115 public static function create_data()
Taufan Aditya5b421662012-04-05 01:24:50 +0700116 {
117 // Job Data
118 $data = array(
Taufan Aditya17f799e2012-05-03 15:15:40 +0700119 'user' => array(
Taufan Aditya1afd4792012-05-04 20:34:30 +0700120 array('id' => 1, 'name' => 'Derek Jones', 'email' => 'derek@world.com', 'country' => 'US'),
121 array('id' => 2, 'name' => 'Ahmadinejad', 'email' => 'ahmadinejad@world.com', 'country' => 'Iran'),
122 array('id' => 3, 'name' => 'Richard A Causey', 'email' => 'richard@world.com', 'country' => 'US'),
123 array('id' => 4, 'name' => 'Chris Martin', 'email' => 'chris@world.com', 'country' => 'UK'),
Taufan Adityad6b41bb2012-05-04 15:05:22 +0700124 ),
Taufan Aditya5b421662012-04-05 01:24:50 +0700125 'job' => array(
126 array('id' => 1, 'name' => 'Developer', 'description' => 'Awesome job, but sometimes makes you bored'),
127 array('id' => 2, 'name' => 'Politician', 'description' => 'This is not really a job'),
128 array('id' => 3, 'name' => 'Accountant', 'description' => 'Boring job, but you will get free snack at lunch'),
129 array('id' => 4, 'name' => 'Musician', 'description' => 'Only Coldplay can actually called Musician'),
130 ),
Taufan Aditya2d574452012-05-25 04:03:56 +0700131 'misc' => array(
132 array('id' => 1, 'key' => '\\xxxfoo456', 'value' => 'Entry with \\xxx'),
133 array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%'),
134 ),
Taufan Aditya5b421662012-04-05 01:24:50 +0700135 );
136
137 foreach ($data as $table => $dummy_data)
138 {
Taufan Aditya55f622f2012-04-05 19:01:34 +0700139 static::$db->truncate($table);
Taufan Aditya024098d2012-04-05 01:51:57 +0700140
141 foreach ($dummy_data as $single_dummy_data)
Taufan Aditya5b421662012-04-05 01:24:50 +0700142 {
Taufan Aditya55f622f2012-04-05 19:01:34 +0700143 static::$db->insert($table, $single_dummy_data);
Taufan Aditya5b421662012-04-05 01:24:50 +0700144 }
145 }
146 }
147}