blob: 671336cc4fd02b39d190ff26240b0f20d0316132 [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 Aditya5b421662012-04-05 01:24:50 +070091 }
92
93 /**
94 * Create the dummy datas
95 *
96 * @return void
97 */
Taufan Aditya55f622f2012-04-05 19:01:34 +070098 public static function create_data()
Taufan Aditya5b421662012-04-05 01:24:50 +070099 {
100 // Job Data
101 $data = array(
Taufan Aditya17f799e2012-05-03 15:15:40 +0700102 'user' => array(
Taufan Aditya1afd4792012-05-04 20:34:30 +0700103 array('id' => 1, 'name' => 'Derek Jones', 'email' => 'derek@world.com', 'country' => 'US'),
104 array('id' => 2, 'name' => 'Ahmadinejad', 'email' => 'ahmadinejad@world.com', 'country' => 'Iran'),
105 array('id' => 3, 'name' => 'Richard A Causey', 'email' => 'richard@world.com', 'country' => 'US'),
106 array('id' => 4, 'name' => 'Chris Martin', 'email' => 'chris@world.com', 'country' => 'UK'),
Taufan Adityad6b41bb2012-05-04 15:05:22 +0700107 ),
Taufan Aditya5b421662012-04-05 01:24:50 +0700108 'job' => array(
109 array('id' => 1, 'name' => 'Developer', 'description' => 'Awesome job, but sometimes makes you bored'),
110 array('id' => 2, 'name' => 'Politician', 'description' => 'This is not really a job'),
111 array('id' => 3, 'name' => 'Accountant', 'description' => 'Boring job, but you will get free snack at lunch'),
112 array('id' => 4, 'name' => 'Musician', 'description' => 'Only Coldplay can actually called Musician'),
113 ),
114 );
115
116 foreach ($data as $table => $dummy_data)
117 {
Taufan Aditya55f622f2012-04-05 19:01:34 +0700118 static::$db->truncate($table);
Taufan Aditya024098d2012-04-05 01:51:57 +0700119
120 foreach ($dummy_data as $single_dummy_data)
Taufan Aditya5b421662012-04-05 01:24:50 +0700121 {
Taufan Aditya55f622f2012-04-05 19:01:34 +0700122 static::$db->insert($table, $single_dummy_data);
Taufan Aditya5b421662012-04-05 01:24:50 +0700123 }
124 }
125 }
126}